更新版本到2.0
This commit is contained in:
@@ -12,6 +12,7 @@ import (
|
||||
|
||||
"dns-server/config"
|
||||
"dns-server/dns"
|
||||
"dns-server/gfw"
|
||||
"dns-server/logger"
|
||||
"dns-server/shield"
|
||||
|
||||
@@ -24,6 +25,7 @@ type Server struct {
|
||||
config *config.HTTPConfig
|
||||
dnsServer *dns.Server
|
||||
shieldManager *shield.ShieldManager
|
||||
gfwManager *gfw.GFWListManager
|
||||
server *http.Server
|
||||
|
||||
// 会话管理相关字段
|
||||
@@ -39,12 +41,13 @@ type Server struct {
|
||||
}
|
||||
|
||||
// NewServer 创建HTTP服务器实例
|
||||
func NewServer(globalConfig *config.Config, dnsServer *dns.Server, shieldManager *shield.ShieldManager) *Server {
|
||||
func NewServer(globalConfig *config.Config, dnsServer *dns.Server, shieldManager *shield.ShieldManager, gfwManager *gfw.GFWListManager) *Server {
|
||||
server := &Server{
|
||||
globalConfig: globalConfig,
|
||||
config: &globalConfig.HTTP,
|
||||
dnsServer: dnsServer,
|
||||
shieldManager: shieldManager,
|
||||
gfwManager: gfwManager,
|
||||
upgrader: websocket.Upgrader{
|
||||
ReadBufferSize: 1024,
|
||||
WriteBufferSize: 1024,
|
||||
@@ -430,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
|
||||
}
|
||||
}
|
||||
@@ -533,7 +537,8 @@ func (s *Server) handleTopBlockedDomains(w http.ResponseWriter, r *http.Request)
|
||||
return
|
||||
}
|
||||
|
||||
domains := s.dnsServer.GetTopBlockedDomains(10)
|
||||
// 返回最近30天的所有域名,设置合理上限50
|
||||
domains := s.dnsServer.GetTopBlockedDomains(50)
|
||||
|
||||
// 转换为前端需要的格式
|
||||
result := make([]map[string]interface{}, len(domains))
|
||||
@@ -584,7 +589,7 @@ func (s *Server) handleRecentBlockedDomains(w http.ResponseWriter, r *http.Reque
|
||||
for i, domain := range domains {
|
||||
result[i] = map[string]interface{}{
|
||||
"domain": domain.Domain,
|
||||
"time": domain.LastSeen.Format("15:04:05"),
|
||||
"time": time.Unix(domain.LastSeen, 0).Format("15:04:05"),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -743,10 +748,10 @@ func (s *Server) handleTopDomains(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
// 获取TOP被屏蔽域名
|
||||
blockedDomains := s.dnsServer.GetTopBlockedDomains(10)
|
||||
// 获取TOP已解析域名
|
||||
resolvedDomains := s.dnsServer.GetTopResolvedDomains(10)
|
||||
// 获取TOP被屏蔽域名,返回最近30天的数据,设置合理上限50
|
||||
blockedDomains := s.dnsServer.GetTopBlockedDomains(50)
|
||||
// 获取TOP已解析域名,返回最近30天的数据,设置合理上限50
|
||||
resolvedDomains := s.dnsServer.GetTopResolvedDomains(50)
|
||||
|
||||
// 合并并去重域名统计
|
||||
domainMap := make(map[string]int64)
|
||||
@@ -777,9 +782,9 @@ func (s *Server) handleTopDomains(w http.ResponseWriter, r *http.Request) {
|
||||
return domainList[i]["count"].(int64) > domainList[j]["count"].(int64)
|
||||
})
|
||||
|
||||
// 返回限制数量
|
||||
if len(domainList) > 10 {
|
||||
domainList = domainList[:10]
|
||||
// 返回所有合并后的域名,设置合理上限50
|
||||
if len(domainList) > 50 {
|
||||
domainList = domainList[:50]
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
@@ -1239,13 +1244,21 @@ func (s *Server) handleConfig(w http.ResponseWriter, r *http.Request) {
|
||||
"blacklists": s.globalConfig.Shield.Blacklists,
|
||||
"updateInterval": s.globalConfig.Shield.UpdateInterval,
|
||||
},
|
||||
"GFWList": map[string]interface{}{
|
||||
"ip": s.globalConfig.GFWList.IP,
|
||||
"content": s.globalConfig.GFWList.Content,
|
||||
},
|
||||
"DNSServer": map[string]interface{}{
|
||||
"port": s.globalConfig.DNS.Port,
|
||||
"QueryMode": s.globalConfig.DNS.QueryMode,
|
||||
"UpstreamServers": s.globalConfig.DNS.UpstreamDNS,
|
||||
"DNSSECUpstreamServers": s.globalConfig.DNS.DNSSECUpstreamDNS,
|
||||
"timeout": s.globalConfig.DNS.Timeout,
|
||||
"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,
|
||||
@@ -1258,11 +1271,16 @@ func (s *Server) handleConfig(w http.ResponseWriter, r *http.Request) {
|
||||
var req struct {
|
||||
DNSServer struct {
|
||||
Port int `json:"port"`
|
||||
QueryMode string `json:"queryMode"`
|
||||
UpstreamServers []string `json:"upstreamServers"`
|
||||
DnssecUpstreamServers []string `json:"dnssecUpstreamServers"`
|
||||
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"`
|
||||
@@ -1273,6 +1291,10 @@ func (s *Server) handleConfig(w http.ResponseWriter, r *http.Request) {
|
||||
Blacklists []config.BlacklistEntry `json:"blacklists"`
|
||||
UpdateInterval int `json:"updateInterval"`
|
||||
} `json:"shield"`
|
||||
GFWList struct {
|
||||
IP string `json:"ip"`
|
||||
Content string `json:"content"`
|
||||
} `json:"gfwList"`
|
||||
}
|
||||
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
@@ -1290,13 +1312,27 @@ func (s *Server) handleConfig(w http.ResponseWriter, r *http.Request) {
|
||||
if len(req.DNSServer.DnssecUpstreamServers) > 0 {
|
||||
s.globalConfig.DNS.DNSSECUpstreamDNS = req.DNSServer.DnssecUpstreamServers
|
||||
}
|
||||
if req.DNSServer.Timeout > 0 {
|
||||
s.globalConfig.DNS.Timeout = req.DNSServer.Timeout
|
||||
}
|
||||
if req.DNSServer.SaveInterval > 0 {
|
||||
s.globalConfig.DNS.SaveInterval = req.DNSServer.SaveInterval
|
||||
}
|
||||
s.globalConfig.DNS.EnableIPv6 = req.DNSServer.EnableIPv6
|
||||
// 更新查询模式
|
||||
if req.DNSServer.QueryMode != "" {
|
||||
s.globalConfig.DNS.QueryMode = req.DNSServer.QueryMode
|
||||
}
|
||||
// 更新缓存配置
|
||||
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 {
|
||||
@@ -1338,6 +1374,17 @@ func (s *Server) handleConfig(w http.ResponseWriter, r *http.Request) {
|
||||
s.globalConfig.Shield.CustomBlockIP = req.Shield.CustomBlockIP
|
||||
}
|
||||
|
||||
// 更新GFWList配置
|
||||
s.globalConfig.GFWList.IP = req.GFWList.IP
|
||||
s.globalConfig.GFWList.Content = req.GFWList.Content
|
||||
|
||||
// 重新加载GFWList规则
|
||||
if s.gfwManager != nil {
|
||||
if err := s.gfwManager.LoadRules(); err != nil {
|
||||
logger.Error("重新加载GFWList规则失败", "error", err)
|
||||
}
|
||||
}
|
||||
|
||||
// 更新黑名单配置
|
||||
if req.Shield.Blacklists != nil {
|
||||
// 验证黑名单配置
|
||||
@@ -1367,6 +1414,19 @@ func (s *Server) handleConfig(w http.ResponseWriter, r *http.Request) {
|
||||
s.shieldManager.StartAutoUpdate()
|
||||
}
|
||||
|
||||
// 更新现有的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
|
||||
|
||||
// 更新缓存配置
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user