config.go 2.6 KB

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