config.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package config
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "network-topology-discovery/pkg/models"
  6. "os"
  7. )
  8. // Config 应用配置
  9. type Config struct {
  10. ScanRanges []string `json:"scan_ranges"`
  11. Devices []DeviceConfig `json:"devices"`
  12. SSH SSHConfig `json:"ssh"`
  13. Web WebConfig `json:"web"`
  14. Scanner ScannerConfig `json:"scanner"`
  15. Auth AuthConfig `json:"auth"`
  16. }
  17. // DeviceConfig 设备配置
  18. type DeviceConfig struct {
  19. IP string `json:"ip"`
  20. Type models.DeviceType `json:"type"`
  21. Username string `json:"username"`
  22. Password string `json:"password"`
  23. KeyFile string `json:"key_file"`
  24. Port int `json:"port"`
  25. }
  26. // SSHConfig SSH配置
  27. type SSHConfig struct {
  28. Timeout int `json:"timeout"`
  29. MaxRetries int `json:"max_retries"`
  30. Port int `json:"port"`
  31. }
  32. // WebConfig Web服务配置
  33. type WebConfig struct {
  34. Port int `json:"port"`
  35. Host string `json:"host"`
  36. }
  37. // AuthConfig 认证配置
  38. type AuthConfig struct {
  39. Enabled bool `json:"enabled"`
  40. Username string `json:"username"`
  41. Password string `json:"password"`
  42. }
  43. // ScannerConfig 扫描器配置
  44. type ScannerConfig struct {
  45. Concurrency int `json:"concurrency"`
  46. Timeout int `json:"timeout"`
  47. }
  48. // LoadConfig 从文件加载配置
  49. func LoadConfig(filename string) (*Config, error) {
  50. data, err := os.ReadFile(filename)
  51. if err != nil {
  52. return nil, fmt.Errorf("failed to read config file: %w", err)
  53. }
  54. var config Config
  55. if err := json.Unmarshal(data, &config); err != nil {
  56. return nil, fmt.Errorf("failed to parse config file: %w", err)
  57. }
  58. // 设置默认值
  59. config.setDefaults()
  60. return &config, nil
  61. }
  62. // SaveConfig 保存配置到文件
  63. func SaveConfig(filename string, config *Config) error {
  64. data, err := json.MarshalIndent(config, "", " ")
  65. if err != nil {
  66. return fmt.Errorf("failed to marshal config: %w", err)
  67. }
  68. if err := os.WriteFile(filename, data, 0644); err != nil {
  69. return fmt.Errorf("failed to write config file: %w", err)
  70. }
  71. return nil
  72. }
  73. // DefaultConfig 返回默认配置
  74. func DefaultConfig() *Config {
  75. config := &Config{
  76. ScanRanges: []string{},
  77. Devices: []DeviceConfig{},
  78. SSH: SSHConfig{
  79. Timeout: 10,
  80. MaxRetries: 3,
  81. Port: 22,
  82. },
  83. Web: WebConfig{
  84. Port: 8080,
  85. Host: "0.0.0.0",
  86. },
  87. Scanner: ScannerConfig{
  88. Concurrency: 10,
  89. Timeout: 2,
  90. },
  91. }
  92. return config
  93. }
  94. // setDefaults 设置默认值
  95. func (c *Config) setDefaults() {
  96. if c.SSH.Timeout == 0 {
  97. c.SSH.Timeout = 10
  98. }
  99. if c.SSH.MaxRetries == 0 {
  100. c.SSH.MaxRetries = 3
  101. }
  102. if c.SSH.Port == 0 {
  103. c.SSH.Port = 22
  104. }
  105. if c.Web.Port == 0 {
  106. c.Web.Port = 8080
  107. }
  108. if c.Web.Host == "" {
  109. c.Web.Host = "0.0.0.0"
  110. }
  111. if c.Scanner.Concurrency == 0 {
  112. c.Scanner.Concurrency = 10
  113. }
  114. if c.Scanner.Timeout == 0 {
  115. c.Scanner.Timeout = 2
  116. }
  117. if c.Auth.Enabled && c.Auth.Username == "" {
  118. c.Auth.Username = "admin"
  119. }
  120. if c.Auth.Enabled && c.Auth.Password == "" {
  121. c.Auth.Password = "admin"
  122. }
  123. }