| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- package config
- import (
- "encoding/json"
- "fmt"
- "network-topology-discovery/pkg/models"
- "os"
- )
- // Config 应用配置
- type Config struct {
- ScanRanges []string `json:"scan_ranges"`
- Devices []DeviceConfig `json:"devices"`
- SSH SSHConfig `json:"ssh"`
- Web WebConfig `json:"web"`
- Scanner ScannerConfig `json:"scanner"`
- Auth AuthConfig `json:"auth"`
- }
- // DeviceConfig 设备配置
- type DeviceConfig struct {
- IP string `json:"ip"`
- Type models.DeviceType `json:"type"`
- Username string `json:"username"`
- Password string `json:"password"`
- KeyFile string `json:"key_file"`
- Port int `json:"port"`
- }
- // SSHConfig SSH配置
- type SSHConfig struct {
- Timeout int `json:"timeout"`
- MaxRetries int `json:"max_retries"`
- Port int `json:"port"`
- }
- // WebConfig Web服务配置
- type WebConfig struct {
- Port int `json:"port"`
- Host string `json:"host"`
- }
- // AuthConfig 认证配置
- type AuthConfig struct {
- Enabled bool `json:"enabled"`
- Username string `json:"username"`
- Password string `json:"password"`
- }
- // ScannerConfig 扫描器配置
- type ScannerConfig struct {
- Concurrency int `json:"concurrency"`
- Timeout int `json:"timeout"`
- }
- // LoadConfig 从文件加载配置
- func LoadConfig(filename string) (*Config, error) {
- data, err := os.ReadFile(filename)
- if err != nil {
- return nil, fmt.Errorf("failed to read config file: %w", err)
- }
- var config Config
- if err := json.Unmarshal(data, &config); err != nil {
- return nil, fmt.Errorf("failed to parse config file: %w", err)
- }
- // 设置默认值
- config.setDefaults()
- return &config, nil
- }
- // SaveConfig 保存配置到文件
- func SaveConfig(filename string, config *Config) error {
- data, err := json.MarshalIndent(config, "", " ")
- if err != nil {
- return fmt.Errorf("failed to marshal config: %w", err)
- }
- if err := os.WriteFile(filename, data, 0644); err != nil {
- return fmt.Errorf("failed to write config file: %w", err)
- }
- return nil
- }
- // DefaultConfig 返回默认配置
- func DefaultConfig() *Config {
- config := &Config{
- ScanRanges: []string{},
- Devices: []DeviceConfig{},
- SSH: SSHConfig{
- Timeout: 10,
- MaxRetries: 3,
- Port: 22,
- },
- Web: WebConfig{
- Port: 8080,
- Host: "0.0.0.0",
- },
- Scanner: ScannerConfig{
- Concurrency: 10,
- Timeout: 2,
- },
- }
- return config
- }
- // setDefaults 设置默认值
- func (c *Config) setDefaults() {
- if c.SSH.Timeout == 0 {
- c.SSH.Timeout = 10
- }
- if c.SSH.MaxRetries == 0 {
- c.SSH.MaxRetries = 3
- }
- if c.SSH.Port == 0 {
- c.SSH.Port = 22
- }
- if c.Web.Port == 0 {
- c.Web.Port = 8080
- }
- if c.Web.Host == "" {
- c.Web.Host = "0.0.0.0"
- }
- if c.Scanner.Concurrency == 0 {
- c.Scanner.Concurrency = 10
- }
- if c.Scanner.Timeout == 0 {
- c.Scanner.Timeout = 2
- }
- if c.Auth.Enabled && c.Auth.Username == "" {
- c.Auth.Username = "admin"
- }
- if c.Auth.Enabled && c.Auth.Password == "" {
- c.Auth.Password = "admin"
- }
- }
|