优化修复

This commit is contained in:
Alex Yang
2026-01-17 01:18:03 +08:00
parent cdac4fcf43
commit ac96c7c10b
57 changed files with 895 additions and 1252132 deletions

View File

@@ -433,7 +433,8 @@ func (s *Server) areStatsEqual(stats1, stats2 map[string]interface{}) bool {
if dns1["Queries"] != dns2["Queries"] ||
dns1["Blocked"] != dns2["Blocked"] ||
dns1["Allowed"] != dns2["Allowed"] ||
dns1["Errors"] != dns2["Errors"] {
dns1["Errors"] != dns2["Errors"] ||
dns1["AvgResponseTime"] != dns2["AvgResponseTime"] {
return false
}
}
@@ -1253,6 +1254,10 @@ func (s *Server) handleConfig(w http.ResponseWriter, r *http.Request) {
"DNSSECUpstreamServers": s.globalConfig.DNS.DNSSECUpstreamDNS,
"saveInterval": s.globalConfig.DNS.SaveInterval,
"enableIPv6": s.globalConfig.DNS.EnableIPv6,
"CacheMode": s.globalConfig.DNS.CacheMode,
"CacheSize": s.globalConfig.DNS.CacheSize,
"MaxCacheTTL": s.globalConfig.DNS.MaxCacheTTL,
"MinCacheTTL": s.globalConfig.DNS.MinCacheTTL,
},
"HTTPServer": map[string]interface{}{
"port": s.globalConfig.HTTP.Port,
@@ -1270,6 +1275,10 @@ func (s *Server) handleConfig(w http.ResponseWriter, r *http.Request) {
Timeout int `json:"timeout"`
SaveInterval int `json:"saveInterval"`
EnableIPv6 bool `json:"enableIPv6"`
CacheMode string `json:"cacheMode"`
CacheSize int `json:"cacheSize"`
MaxCacheTTL int `json:"maxCacheTTL"`
MinCacheTTL int `json:"minCacheTTL"`
} `json:"dnsserver"`
HTTPServer struct {
Port int `json:"port"`
@@ -1305,6 +1314,19 @@ func (s *Server) handleConfig(w http.ResponseWriter, r *http.Request) {
s.globalConfig.DNS.SaveInterval = req.DNSServer.SaveInterval
}
s.globalConfig.DNS.EnableIPv6 = req.DNSServer.EnableIPv6
// 更新缓存配置
if req.DNSServer.CacheMode != "" {
s.globalConfig.DNS.CacheMode = req.DNSServer.CacheMode
}
if req.DNSServer.CacheSize > 0 {
s.globalConfig.DNS.CacheSize = req.DNSServer.CacheSize
}
if req.DNSServer.MaxCacheTTL > 0 {
s.globalConfig.DNS.MaxCacheTTL = req.DNSServer.MaxCacheTTL
}
if req.DNSServer.MinCacheTTL > 0 {
s.globalConfig.DNS.MinCacheTTL = req.DNSServer.MinCacheTTL
}
// 更新HTTP配置
if req.HTTPServer.Port > 0 {
@@ -1386,17 +1408,30 @@ func (s *Server) handleConfig(w http.ResponseWriter, r *http.Request) {
s.shieldManager.StartAutoUpdate()
}
// 保存配置到文件
if err := saveConfigToFile(s.globalConfig, "./config.json"); err != nil {
logger.Error("保存配置到文件失败", "error", err)
// 不返回错误,只记录日志,因为配置已经在内存中更新成功
}
// 更新现有的DNSCache实例配置
// 最大和最小TTL
maxCacheTTL := time.Duration(s.globalConfig.DNS.MaxCacheTTL) * time.Second
minCacheTTL := time.Duration(s.globalConfig.DNS.MinCacheTTL) * time.Second
// 最大缓存大小(字节)
maxCacheSize := int64(s.globalConfig.DNS.CacheSize) * 1024 * 1024
// 返回成功响应
json.NewEncoder(w).Encode(map[string]interface{}{
"success": true,
"message": "配置已更新",
})
// 更新缓存配置
s.dnsServer.DnsCache.SetMaxCacheTTL(maxCacheTTL)
s.dnsServer.DnsCache.SetMinCacheTTL(minCacheTTL)
s.dnsServer.DnsCache.SetCacheMode(s.globalConfig.DNS.CacheMode)
s.dnsServer.DnsCache.SetMaxCacheSize(maxCacheSize)
// 保存配置到文件
if err := saveConfigToFile(s.globalConfig, "./config.json"); err != nil {
logger.Error("保存配置到文件失败", "error", err)
// 不返回错误,只记录日志,因为配置已经在内存中更新成功
}
// 返回成功响应
json.NewEncoder(w).Encode(map[string]interface{}{
"success": true,
"message": "配置已更新",
})
default:
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)