d0927cbad5
- 支持Cisco、华为、H3C、ASA、Linux、Windows设备 - SSH远程采集设备信息 - 自动发现网络拓扑(LLDP/CDP) - Web可视化界面 - 支持旧版SSH加密算法兼容
126 rivejä
2.6 KiB
Go
126 rivejä
2.6 KiB
Go
package config
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"network-topology-discovery/pkg/models"
|
|
)
|
|
|
|
// 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"`
|
|
}
|
|
|
|
// 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"`
|
|
}
|
|
|
|
// 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
|
|
}
|
|
}
|