This commit is contained in:
Alex Yang
2025-12-16 00:11:42 +08:00
parent ba26e2b647
commit 11d39d6b76
35 changed files with 12378 additions and 1157 deletions

View File

@@ -1067,36 +1067,13 @@ func (s *Server) handleConfig(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
// 返回当前配置(包括所有配置
// 返回当前配置(包括黑名单配置)
config := map[string]interface{}{
"dns": map[string]interface{}{
"port": s.globalConfig.DNS.Port,
"upstreamDNS": s.globalConfig.DNS.UpstreamDNS,
"timeout": s.globalConfig.DNS.Timeout,
"statsFile": s.globalConfig.DNS.StatsFile,
"saveInterval": s.globalConfig.DNS.SaveInterval,
},
"http": map[string]interface{}{
"port": s.globalConfig.HTTP.Port,
"host": s.globalConfig.HTTP.Host,
"enableAPI": s.globalConfig.HTTP.EnableAPI,
},
"shield": map[string]interface{}{
"blockMethod": s.globalConfig.Shield.BlockMethod,
"customBlockIP": s.globalConfig.Shield.CustomBlockIP,
"blacklists": s.globalConfig.Shield.Blacklists,
"updateInterval": s.globalConfig.Shield.UpdateInterval,
"localRulesFile": s.globalConfig.Shield.LocalRulesFile,
"hostsFile": s.globalConfig.Shield.HostsFile,
"statsFile": s.globalConfig.Shield.StatsFile,
"statsSaveInterval": s.globalConfig.Shield.StatsSaveInterval,
},
"log": map[string]interface{}{
"file": s.globalConfig.Log.File,
"level": s.globalConfig.Log.Level,
"maxSize": s.globalConfig.Log.MaxSize,
"maxBackups": s.globalConfig.Log.MaxBackups,
"maxAge": s.globalConfig.Log.MaxAge,
"blockMethod": s.globalConfig.Shield.BlockMethod,
"customBlockIP": s.globalConfig.Shield.CustomBlockIP,
"blacklists": s.globalConfig.Shield.Blacklists,
"updateInterval": s.globalConfig.Shield.UpdateInterval,
},
}
json.NewEncoder(w).Encode(config)
@@ -1104,36 +1081,12 @@ func (s *Server) handleConfig(w http.ResponseWriter, r *http.Request) {
case http.MethodPost:
// 更新配置
var req struct {
DNS struct {
Port int `json:"port"`
UpstreamDNS []string `json:"upstreamDNS"`
Timeout int `json:"timeout"`
StatsFile string `json:"statsFile"`
SaveInterval int `json:"saveInterval"`
} `json:"dns"`
HTTP struct {
Port int `json:"port"`
Host string `json:"host"`
EnableAPI bool `json:"enableAPI"`
} `json:"http"`
Shield struct {
LocalRulesFile string `json:"localRulesFile"`
BlockMethod string `json:"blockMethod"`
CustomBlockIP string `json:"customBlockIP"`
Blacklists []config.BlacklistEntry `json:"blacklists"`
UpdateInterval int `json:"updateInterval"`
HostsFile string `json:"hostsFile"`
StatsFile string `json:"statsFile"`
StatsSaveInterval int `json:"statsSaveInterval"`
RemoteRulesCacheDir string `json:"remoteRulesCacheDir"`
BlockMethod string `json:"blockMethod"`
CustomBlockIP string `json:"customBlockIP"`
Blacklists []config.BlacklistEntry `json:"blacklists"`
UpdateInterval int `json:"updateInterval"`
} `json:"shield"`
Log struct {
File string `json:"file"`
Level string `json:"level"`
MaxSize int `json:"maxSize"`
MaxBackups int `json:"maxBackups"`
MaxAge int `json:"maxAge"`
} `json:"log"`
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
@@ -1141,33 +1094,6 @@ func (s *Server) handleConfig(w http.ResponseWriter, r *http.Request) {
return
}
// 更新DNS配置
if req.DNS.Port > 0 {
s.globalConfig.DNS.Port = req.DNS.Port
}
if len(req.DNS.UpstreamDNS) > 0 {
s.globalConfig.DNS.UpstreamDNS = req.DNS.UpstreamDNS
}
if req.DNS.Timeout > 0 {
s.globalConfig.DNS.Timeout = req.DNS.Timeout
}
if req.DNS.StatsFile != "" {
s.globalConfig.DNS.StatsFile = req.DNS.StatsFile
}
if req.DNS.SaveInterval > 0 {
s.globalConfig.DNS.SaveInterval = req.DNS.SaveInterval
}
// 更新HTTP配置
if req.HTTP.Port > 0 {
s.globalConfig.HTTP.Port = req.HTTP.Port
}
if req.HTTP.Host != "" {
s.globalConfig.HTTP.Host = req.HTTP.Host
}
// EnableAPI可以设置为false所以需要单独处理
s.globalConfig.HTTP.EnableAPI = req.HTTP.EnableAPI
// 更新屏蔽配置
if req.Shield.BlockMethod != "" {
// 验证屏蔽方法是否有效
@@ -1203,26 +1129,6 @@ func (s *Server) handleConfig(w http.ResponseWriter, r *http.Request) {
s.globalConfig.Shield.CustomBlockIP = req.Shield.CustomBlockIP
}
if req.Shield.LocalRulesFile != "" {
s.globalConfig.Shield.LocalRulesFile = req.Shield.LocalRulesFile
}
if req.Shield.HostsFile != "" {
s.globalConfig.Shield.HostsFile = req.Shield.HostsFile
}
if req.Shield.StatsFile != "" {
s.globalConfig.Shield.StatsFile = req.Shield.StatsFile
}
if req.Shield.StatsSaveInterval > 0 {
s.globalConfig.Shield.StatsSaveInterval = req.Shield.StatsSaveInterval
}
if req.Shield.RemoteRulesCacheDir != "" {
s.globalConfig.Shield.RemoteRulesCacheDir = req.Shield.RemoteRulesCacheDir
}
// 更新黑名单配置
if req.Shield.Blacklists != nil {
// 验证黑名单配置
@@ -1252,61 +1158,16 @@ func (s *Server) handleConfig(w http.ResponseWriter, r *http.Request) {
s.shieldManager.StartAutoUpdate()
}
// 更新Log配置
if req.Log.File != "" {
s.globalConfig.Log.File = req.Log.File
}
if req.Log.Level != "" {
s.globalConfig.Log.Level = req.Log.Level
}
if req.Log.MaxSize > 0 {
s.globalConfig.Log.MaxSize = req.Log.MaxSize
}
if req.Log.MaxBackups > 0 {
s.globalConfig.Log.MaxBackups = req.Log.MaxBackups
}
if req.Log.MaxAge > 0 {
s.globalConfig.Log.MaxAge = req.Log.MaxAge
}
// 保存配置到文件
if err := saveConfigToFile(s.globalConfig, "./config.json"); err != nil {
logger.Error("保存配置到文件失败", "error", err)
http.Error(w, "保存配置到文件失败", http.StatusInternalServerError)
return
// 不返回错误,只记录日志,因为配置已经在内存中更新成功
}
// 重启服务
go func() {
logger.Info("配置已更新,正在重启服务...")
// 停止当前服务
s.dnsServer.Stop()
s.Stop()
s.shieldManager.StopAutoUpdate()
// 重新启动服务(这里需要注意,实际实现可能需要重新创建服务器实例)
// 由于当前架构限制我们只重启ShieldManager和DNS服务器的核心功能
// 重新加载屏蔽规则
if err := s.shieldManager.LoadRules(); err != nil {
logger.Error("重新加载屏蔽规则失败", "error", err)
}
// 重新启动DNS服务器
if err := s.dnsServer.Start(); err != nil {
logger.Error("DNS服务器重启失败", "error", err)
}
// 重新启动定时更新任务
s.shieldManager.StartAutoUpdate()
logger.Info("服务已重启,配置已生效")
}()
// 返回成功响应
json.NewEncoder(w).Encode(map[string]interface{}{
"success": true,
"message": "配置已更新并保存,服务正在重启以应用新配置",
"message": "配置已更新",
})
default: