增加数据持久化功能

This commit is contained in:
Alex Yang
2025-11-23 19:07:59 +08:00
parent 63a95f7463
commit f5911af449
8 changed files with 519 additions and 16 deletions

View File

@@ -5,11 +5,13 @@ import (
"io/ioutil"
)
// DNSConfig DNS服务器配置
// DNSConfig DNS配置
type DNSConfig struct {
Port int `json:"port"`
UpstreamDNS []string `json:"upstreamDNS"`
Timeout int `json:"timeout"`
Port int `json:"port"`
UpstreamDNS []string `json:"upstreamDNS"`
Timeout int `json:"timeout"`
StatsFile string `json:"statsFile"` // 统计数据持久化文件
SaveInterval int `json:"saveInterval"` // 数据保存间隔(秒)
}
// HTTPConfig HTTP控制台配置
@@ -21,12 +23,14 @@ type HTTPConfig struct {
// 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"时使用
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"时使用
StatsFile string `json:"statsFile"` // 计数数据持久化文件
StatsSaveInterval int `json:"statsSaveInterval"` // 计数数据保存间隔(秒)
}
// LogConfig 日志配置
@@ -64,7 +68,13 @@ func LoadConfig(path string) (*Config, error) {
config.DNS.Port = 53
}
if len(config.DNS.UpstreamDNS) == 0 {
config.DNS.UpstreamDNS = []string{"8.8.8.8:53", "1.1.1.1:53"}
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分钟保存一次
}
if config.HTTP.Port == 0 {
config.HTTP.Port = 8080
@@ -78,6 +88,12 @@ func LoadConfig(path string) (*Config, error) {
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.Log.Level == "" {
config.Log.Level = "info"
}