package config import ( "encoding/json" "io/ioutil" ) // DNSConfig DNS服务器配置 type DNSConfig struct { Port int `json:"port"` UpstreamDNS []string `json:"upstreamDNS"` Timeout int `json:"timeout"` } // HTTPConfig HTTP控制台配置 type HTTPConfig struct { Port int `json:"port"` Host string `json:"host"` EnableAPI bool `json:"enableAPI"` } // ShieldConfig 屏蔽规则配置 type ShieldConfig struct { LocalRulesFile string `json:"localRulesFile"` RemoteRules []string `json:"remoteRules"` UpdateInterval int `json:"updateInterval"` HostsFile string `json:"hostsFile"` BlockMethod string `json:"blockMethod"` // 屏蔽方法: "NXDOMAIN", "refused", "emptyIP", "customIP" CustomBlockIP string `json:"customBlockIP"` // 自定义屏蔽IP,当BlockMethod为"customIP"时使用 } // LogConfig 日志配置 type LogConfig struct { File string `json:"file"` Level string `json:"level"` MaxSize int `json:"maxSize"` MaxBackups int `json:"maxBackups"` MaxAge int `json:"maxAge"` } // Config 整体配置 type Config struct { DNS DNSConfig `json:"dns"` HTTP HTTPConfig `json:"http"` Shield ShieldConfig `json:"shield"` Log LogConfig `json:"log"` } // LoadConfig 加载配置文件 func LoadConfig(path string) (*Config, error) { data, err := ioutil.ReadFile(path) if err != nil { return nil, err } var config Config err = json.Unmarshal(data, &config) if err != nil { return nil, err } // 设置默认值 if config.DNS.Port == 0 { config.DNS.Port = 53 } if len(config.DNS.UpstreamDNS) == 0 { config.DNS.UpstreamDNS = []string{"8.8.8.8:53", "1.1.1.1:53"} } if config.HTTP.Port == 0 { config.HTTP.Port = 8080 } if config.HTTP.Host == "" { config.HTTP.Host = "0.0.0.0" } if config.Shield.UpdateInterval == 0 { config.Shield.UpdateInterval = 3600 } if config.Shield.BlockMethod == "" { config.Shield.BlockMethod = "NXDOMAIN" // 默认屏蔽方法为NXDOMAIN } if config.Log.Level == "" { config.Log.Level = "info" } return &config, nil }