package config import ( "encoding/json" "io/ioutil" ) // DNSConfig DNS配置 type DNSConfig struct { Port int `json:"port"` UpstreamDNS []string `json:"upstreamDNS"` Timeout int `json:"timeout"` StatsFile string `json:"statsFile"` // 统计数据持久化文件 SaveInterval int `json:"saveInterval"` // 数据保存间隔(秒) CacheTTL int `json:"cacheTTL"` // DNS缓存过期时间(分钟) } // HTTPConfig HTTP控制台配置 type HTTPConfig struct { Port int `json:"port"` Host string `json:"host"` EnableAPI bool `json:"enableAPI"` Username string `json:"username"` // 登录用户名 Password string `json:"password"` // 登录密码 } // BlacklistEntry 黑名单条目 type BlacklistEntry struct { Name string `json:"name"` URL string `json:"url"` Enabled bool `json:"enabled"` RuleCount int `json:"ruleCount,omitempty"` LastUpdateTime string `json:"lastUpdateTime,omitempty"` } // ShieldConfig 屏蔽规则配置 type ShieldConfig struct { LocalRulesFile string `json:"localRulesFile"` Blacklists []BlacklistEntry `json:"blacklists"` UpdateInterval int `json:"updateInterval"` HostsFile string `json:"hostsFile"` BlockMethod string `json:"blockMethod"` // 屏蔽方法: "NXDOMAIN", "refused", "emptyIP", "customIP" CustomBlockIP string `json:"customBlockIP"` // 自定义屏蔽IP,当BlockMethod为"customIP"时使用 StatsFile string `json:"statsFile"` // 计数数据持久化文件 StatsSaveInterval int `json:"statsSaveInterval"` // 计数数据保存间隔(秒) RemoteRulesCacheDir string `json:"remoteRulesCacheDir"` // 远程规则缓存目录 } // 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{"223.5.5.5:53", "223.6.6.6:53"} } if config.DNS.StatsFile == "" { config.DNS.StatsFile = "./data/stats.json" // 默认统计数据文件路径 } if config.DNS.SaveInterval == 0 { config.DNS.SaveInterval = 300 // 默认5分钟保存一次 } // 默认DNS缓存TTL为30分钟 if config.DNS.CacheTTL == 0 { config.DNS.CacheTTL = 30 // 默认30分钟 } if config.HTTP.Port == 0 { config.HTTP.Port = 8080 } if config.HTTP.Host == "" { config.HTTP.Host = "0.0.0.0" } // 默认用户名和密码,如果未配置则使用admin/admin if config.HTTP.Username == "" { config.HTTP.Username = "admin" } if config.HTTP.Password == "" { config.HTTP.Password = "admin" } if config.Shield.UpdateInterval == 0 { config.Shield.UpdateInterval = 3600 } if config.Shield.BlockMethod == "" { config.Shield.BlockMethod = "NXDOMAIN" // 默认屏蔽方法为NXDOMAIN } if config.Shield.StatsFile == "" { config.Shield.StatsFile = "./data/shield_stats.json" // 默认Shield统计数据文件路径 } if config.Shield.StatsSaveInterval == 0 { config.Shield.StatsSaveInterval = 300 // 默认5分钟保存一次 } if config.Shield.RemoteRulesCacheDir == "" { config.Shield.RemoteRulesCacheDir = "./data/remote_rules" // 默认远程规则缓存目录 } // 如果黑名单列表为空,添加一些默认的黑名单 if len(config.Shield.Blacklists) == 0 { config.Shield.Blacklists = []BlacklistEntry{ {Name: "AdGuard DNS filter", URL: "https://gitea.amazehome.xyz/AMAZEHOME/hosts-and-filters/raw/branch/main/filter.txt", Enabled: true}, {Name: "Adaway Default Blocklist", URL: "https://gitea.amazehome.xyz/AMAZEHOME/hosts-and-Filters/raw/branch/main/hosts/adaway.txt", Enabled: true}, {Name: "CHN-anti-AD", URL: "https://gitea.amazehome.xyz/AMAZEHOME/hosts-and-Filters/raw/branch/main/list/easylist.txt", Enabled: true}, {Name: "My GitHub Rules", URL: "https://gitea.amazehome.xyz/AMAZEHOME/hosts-and-filters/raw/branch/main/rules/costomize.txt", Enabled: true}, } } if config.Log.Level == "" { config.Log.Level = "info" } return &config, nil }