更新
This commit is contained in:
+117
-104
@@ -1,14 +1,15 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"strings"
|
||||
|
||||
"gopkg.in/ini.v1"
|
||||
)
|
||||
|
||||
// DomainSpecificDNS 域名特定DNS服务器配置
|
||||
// 格式:{"domainMatch": ["dns1", "dns2"]}
|
||||
// domainMatch: 域名匹配字符串,当域名中包含该字符串时使用对应的DNS服务器
|
||||
// dns1, dns2: 用于解析匹配域名的DNS服务器列表
|
||||
// INI格式:domain_域名匹配字符串 = DNS服务器1, DNS服务器2
|
||||
// 例如:domain_google.com = 8.8.8.8:53, 8.8.4.4:53
|
||||
|
||||
type DomainSpecificDNS map[string][]string
|
||||
|
||||
@@ -86,113 +87,65 @@ type Config struct {
|
||||
|
||||
// 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)
|
||||
// 解析INI文件
|
||||
cfg, err := ini.Load(data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 设置默认值
|
||||
if config.DNS.Port == 0 {
|
||||
config.DNS.Port = 53
|
||||
// 初始化配置
|
||||
config := &Config{
|
||||
DNS: DNSConfig{
|
||||
Port: cfg.Section("dns").Key("port").MustInt(53),
|
||||
SaveInterval: cfg.Section("dns").Key("saveInterval").MustInt(300),
|
||||
CacheTTL: cfg.Section("dns").Key("cacheTTL").MustInt(30),
|
||||
EnableDNSSEC: cfg.Section("dns").Key("enableDNSSEC").MustBool(true),
|
||||
QueryMode: cfg.Section("dns").Key("queryMode").MustString("parallel"),
|
||||
QueryTimeout: cfg.Section("dns").Key("queryTimeout").MustInt(500),
|
||||
EnableFastReturn: cfg.Section("dns").Key("enableFastReturn").MustBool(true),
|
||||
EnableIPv6: cfg.Section("dns").Key("enableIPv6").MustBool(false),
|
||||
CacheMode: cfg.Section("dns").Key("cacheMode").MustString("memory"),
|
||||
CacheSize: cfg.Section("dns").Key("cacheSize").MustInt(100),
|
||||
MaxCacheTTL: cfg.Section("dns").Key("maxCacheTTL").MustInt(120),
|
||||
MinCacheTTL: cfg.Section("dns").Key("minCacheTTL").MustInt(5),
|
||||
CacheFilePath: "data/cache.json", // 固定路径
|
||||
UpstreamDNS: parseStringList(cfg.Section("dns").Key("upstreamDNS").MustString("223.5.5.5:53,223.6.6.6:53")),
|
||||
DNSSECUpstreamDNS: parseStringList(cfg.Section("dns").Key("dnssecUpstreamDNS").MustString("8.8.8.8:53,1.1.1.1:53")),
|
||||
NoDNSSECDomains: parseStringList(cfg.Section("dns").Key("noDNSSECDomains").MustString("")),
|
||||
DomainSpecificDNS: parseDomainSpecificDNS(cfg.Section("dns")),
|
||||
},
|
||||
HTTP: HTTPConfig{
|
||||
Port: cfg.Section("http").Key("port").MustInt(8080),
|
||||
Host: cfg.Section("http").Key("host").MustString("0.0.0.0"),
|
||||
EnableAPI: cfg.Section("http").Key("enableAPI").MustBool(true),
|
||||
Username: cfg.Section("http").Key("username").MustString("admin"),
|
||||
Password: cfg.Section("http").Key("password").MustString("admin"),
|
||||
},
|
||||
Shield: ShieldConfig{
|
||||
UpdateInterval: cfg.Section("shield").Key("updateInterval").MustInt(3600),
|
||||
BlockMethod: cfg.Section("shield").Key("blockMethod").MustString("NXDOMAIN"),
|
||||
CustomBlockIP: cfg.Section("shield").Key("customBlockIP").MustString(""),
|
||||
StatsSaveInterval: cfg.Section("shield").Key("statsSaveInterval").MustInt(300),
|
||||
Blacklists: parseBlacklists(cfg.Section("shield")),
|
||||
},
|
||||
GFWList: GFWListConfig{
|
||||
IP: cfg.Section("gfwList").Key("ip").MustString("127.0.0.1"),
|
||||
Content: cfg.Section("gfwList").Key("content").MustString(""),
|
||||
Enabled: cfg.Section("gfwList").Key("enabled").MustBool(false),
|
||||
},
|
||||
Log: LogConfig{
|
||||
Level: cfg.Section("log").Key("level").MustString("info"),
|
||||
MaxSize: cfg.Section("log").Key("maxSize").MustInt(100),
|
||||
MaxBackups: cfg.Section("log").Key("maxBackups").MustInt(10),
|
||||
MaxAge: cfg.Section("log").Key("maxAge").MustInt(30),
|
||||
},
|
||||
}
|
||||
if len(config.DNS.UpstreamDNS) == 0 {
|
||||
config.DNS.UpstreamDNS = []string{"223.5.5.5:53", "223.6.6.6:53"}
|
||||
}
|
||||
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.DNS.CacheMode == "" {
|
||||
config.DNS.CacheMode = "memory" // 默认内存缓存
|
||||
}
|
||||
// 缓存大小默认值(100MB)
|
||||
if config.DNS.CacheSize == 0 {
|
||||
config.DNS.CacheSize = 100 // 默认100MB
|
||||
}
|
||||
// 最大缓存TTL默认值(120分钟)
|
||||
if config.DNS.MaxCacheTTL == 0 {
|
||||
config.DNS.MaxCacheTTL = 120 // 默认120分钟
|
||||
}
|
||||
// 最小缓存TTL默认值(5分钟)
|
||||
if config.DNS.MinCacheTTL == 0 {
|
||||
config.DNS.MinCacheTTL = 5 // 默认5分钟
|
||||
}
|
||||
// 缓存文件路径固定为data/cache.json,不再从配置文件读取
|
||||
config.DNS.CacheFilePath = "data/cache.json"
|
||||
// DNSSEC默认配置
|
||||
// 如果未在配置文件中设置,默认启用DNSSEC支持
|
||||
// json.Unmarshal会将未设置的布尔字段设为false,所以我们需要显式检查
|
||||
// 但由于这是一个新字段,为了向后兼容,我们保持默认值为true
|
||||
// 注意:如果用户在配置文件中明确设置为false,则使用false
|
||||
if !config.DNS.EnableDNSSEC {
|
||||
// 检查是否真的是用户设置为false,还是默认值
|
||||
// 由于JSON布尔值默认是false,我们无法直接区分
|
||||
// 所以这里保持默认行为,让用户可以通过配置文件设置为false
|
||||
}
|
||||
// IPv6默认配置
|
||||
// 注意:我们不能直接设置默认值,因为JSON布尔值默认是false
|
||||
// 我们需要检查配置文件中是否真的设置了这个字段
|
||||
// 由于我们无法直接区分,这里保持现状,让用户可以通过配置文件设置为false
|
||||
// DNSSEC专用服务器默认配置
|
||||
if len(config.DNS.DNSSECUpstreamDNS) == 0 {
|
||||
config.DNS.DNSSECUpstreamDNS = []string{"8.8.8.8:53", "1.1.1.1:53"}
|
||||
}
|
||||
// 查询模式默认配置
|
||||
if config.DNS.QueryMode == "" {
|
||||
config.DNS.QueryMode = "parallel" // 默认使用并行请求模式
|
||||
}
|
||||
// 查询超时默认配置(毫秒)
|
||||
if config.DNS.QueryTimeout == 0 {
|
||||
config.DNS.QueryTimeout = 500 // 默认超时时间为500ms
|
||||
}
|
||||
// 快速返回机制默认配置
|
||||
if config.DNS.EnableFastReturn == false {
|
||||
config.DNS.EnableFastReturn = true // 默认启用快速返回机制
|
||||
}
|
||||
// 域名特定DNS服务器配置默认值
|
||||
if config.DNS.DomainSpecificDNS == nil {
|
||||
config.DNS.DomainSpecificDNS = make(DomainSpecificDNS) // 默认为空映射
|
||||
}
|
||||
|
||||
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.StatsSaveInterval == 0 {
|
||||
config.Shield.StatsSaveInterval = 300 // 默认5分钟保存一次
|
||||
}
|
||||
|
||||
// GFWList默认配置
|
||||
if config.GFWList.IP == "" {
|
||||
config.GFWList.IP = "127.0.0.1" // 默认GFWList解析目标IP为127.0.0.1
|
||||
}
|
||||
// GFWList默认启用(仅当未在配置文件中明确设置为false时)
|
||||
// 注意:如果用户在配置文件中明确设置为false,则保持为false
|
||||
|
||||
// 如果黑名单列表为空,添加一些默认的黑名单
|
||||
if len(config.Shield.Blacklists) == 0 {
|
||||
@@ -203,9 +156,69 @@ func LoadConfig(path string) (*Config, error) {
|
||||
{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
|
||||
}
|
||||
|
||||
// parseStringList 解析逗号分隔的字符串列表
|
||||
func parseStringList(s string) []string {
|
||||
if s == "" {
|
||||
return []string{}
|
||||
}
|
||||
|
||||
return &config, nil
|
||||
// 分割字符串
|
||||
parts := []string{}
|
||||
for _, part := range strings.Split(s, ",") {
|
||||
part = strings.TrimSpace(part)
|
||||
if part != "" {
|
||||
parts = append(parts, part)
|
||||
}
|
||||
}
|
||||
return parts
|
||||
}
|
||||
|
||||
// parseDomainSpecificDNS 解析域名特定DNS服务器配置
|
||||
func parseDomainSpecificDNS(section *ini.Section) DomainSpecificDNS {
|
||||
domainDNS := make(DomainSpecificDNS)
|
||||
|
||||
// 遍历所有键,查找以"domain_"开头的键
|
||||
for _, key := range section.Keys() {
|
||||
if strings.HasPrefix(key.Name(), "domain_") {
|
||||
domain := strings.TrimPrefix(key.Name(), "domain_")
|
||||
dnsServers := parseStringList(key.String())
|
||||
if len(dnsServers) > 0 {
|
||||
domainDNS[domain] = dnsServers
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return domainDNS
|
||||
}
|
||||
|
||||
// parseBlacklists 解析黑名单配置
|
||||
func parseBlacklists(section *ini.Section) []BlacklistEntry {
|
||||
blacklists := []BlacklistEntry{}
|
||||
|
||||
// 遍历所有键,查找以"blacklist_"开头的键
|
||||
for _, key := range section.Keys() {
|
||||
if strings.HasPrefix(key.Name(), "blacklist_") {
|
||||
// 提取黑名单名称和属性
|
||||
name := strings.TrimPrefix(key.Name(), "blacklist_")
|
||||
value := key.String()
|
||||
|
||||
// 解析黑名单URL和启用状态,格式: url,enabled
|
||||
parts := strings.Split(value, ",")
|
||||
if len(parts) >= 2 {
|
||||
url := strings.TrimSpace(parts[0])
|
||||
enabled := strings.TrimSpace(parts[1]) == "true"
|
||||
blacklists = append(blacklists, BlacklistEntry{
|
||||
Name: name,
|
||||
URL: url,
|
||||
Enabled: enabled,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return blacklists
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user