From 4d53b13220d6471470782be6d4aa472c5753543b Mon Sep 17 00:00:00 2001 From: Alex Yang Date: Tue, 25 Nov 2025 18:14:53 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86=E6=A8=A1=E6=8B=9F=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=B8=BA=E7=9C=9F=E5=AE=9E=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E5=99=A8=E7=BB=9F=E8=AE=A1=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dns/server.go | 27 ++++++++++++++-------- http/server.go | 63 +++++++++++++++++++++++++++++++++++--------------- 2 files changed, 62 insertions(+), 28 deletions(-) diff --git a/dns/server.go b/dns/server.go index b5d9e55..730828d 100644 --- a/dns/server.go +++ b/dns/server.go @@ -62,6 +62,7 @@ type Server struct { monthlyStatsMutex sync.RWMutex monthlyStats map[string]int64 // 按月统计屏蔽数量 saveTicker *time.Ticker // 用于定时保存数据 + startTime time.Time // 服务器启动时间 saveDone chan struct{} // 用于通知保存协程停止 } @@ -90,18 +91,19 @@ func NewServer(config *config.DNSConfig, shieldConfig *config.ShieldConfig, shie Net: "udp", Timeout: time.Duration(config.Timeout) * time.Millisecond, }, - ctx: ctx, - cancel: cancel, + ctx: ctx, + cancel: cancel, + startTime: time.Now(), // 记录服务器启动时间 stats: &Stats{ - Queries: 0, - Blocked: 0, - Allowed: 0, - Errors: 0, - AvgResponseTime: 0, + Queries: 0, + Blocked: 0, + Allowed: 0, + Errors: 0, + AvgResponseTime: 0, TotalResponseTime: 0, - QueryTypes: make(map[string]int64), - SourceIPs: make(map[string]bool), - CpuUsage: 0, + QueryTypes: make(map[string]int64), + SourceIPs: make(map[string]bool), + CpuUsage: 0, }, blockedDomains: make(map[string]*BlockedDomain), resolvedDomains: make(map[string]*BlockedDomain), @@ -474,6 +476,11 @@ func (s *Server) updateStats(update func(*Stats)) { update(s.stats) } +// GetStartTime 获取服务器启动时间 +func (s *Server) GetStartTime() time.Time { + return s.startTime +} + // GetStats 获取DNS服务器统计信息 func (s *Server) GetStats() *Stats { s.statsMutex.Lock() diff --git a/http/server.go b/http/server.go index 8a58192..9ef6f37 100644 --- a/http/server.go +++ b/http/server.go @@ -87,27 +87,41 @@ func (s *Server) handleStats(w http.ResponseWriter, r *http.Request) { dnsStats := s.dnsServer.GetStats() shieldStats := s.shieldManager.GetStats() - // 获取最常用查询类型 - topQueryType := "A" + // 获取最常用查询类型(如果有) + topQueryType := "-" maxCount := int64(0) - for queryType, count := range dnsStats.QueryTypes { - if count > maxCount { - maxCount = count - topQueryType = queryType + if len(dnsStats.QueryTypes) > 0 { + for queryType, count := range dnsStats.QueryTypes { + if count > maxCount { + maxCount = count + topQueryType = queryType + } } } // 获取活跃来源IP数量 activeIPCount := len(dnsStats.SourceIPs) + // 构建响应数据,确保所有字段都反映服务器的真实状态 stats := map[string]interface{}{ - "dns": dnsStats, - "shield": shieldStats, - "topQueryType": topQueryType, - "activeIPs": activeIPCount, - "avgResponseTime": dnsStats.AvgResponseTime, - "cpuUsage": dnsStats.CpuUsage, - "time": time.Now(), + "dns": map[string]interface{}{ + "Queries": dnsStats.Queries, + "Blocked": dnsStats.Blocked, + "Allowed": dnsStats.Allowed, + "Errors": dnsStats.Errors, + "LastQuery": dnsStats.LastQuery, + "AvgResponseTime": dnsStats.AvgResponseTime, + "TotalResponseTime": dnsStats.TotalResponseTime, + "QueryTypes": dnsStats.QueryTypes, + "SourceIPs": dnsStats.SourceIPs, + "CpuUsage": dnsStats.CpuUsage, + }, + "shield": shieldStats, + "topQueryType": topQueryType, + "activeIPs": activeIPCount, + "avgResponseTime": dnsStats.AvgResponseTime, + "cpuUsage": dnsStats.CpuUsage, + "time": time.Now(), } w.Header().Set("Content-Type", "application/json") @@ -580,12 +594,25 @@ func (s *Server) handleStatus(w http.ResponseWriter, r *http.Request) { } stats := s.dnsServer.GetStats() + + // 使用服务器的实际启动时间计算准确的运行时间 + serverStartTime := s.dnsServer.GetStartTime() + uptime := time.Since(serverStartTime) + + // 构建包含所有真实服务器统计数据的响应 status := map[string]interface{}{ - "status": "running", - "queries": stats.Queries, - "lastQuery": stats.LastQuery, - "uptime": time.Since(stats.LastQuery), - "timestamp": time.Now(), + "status": "running", + "queries": stats.Queries, + "blocked": stats.Blocked, + "allowed": stats.Allowed, + "errors": stats.Errors, + "lastQuery": stats.LastQuery, + "avgResponseTime": stats.AvgResponseTime, + "activeIPs": len(stats.SourceIPs), + "startTime": serverStartTime, + "uptime": uptime, + "cpuUsage": stats.CpuUsage, + "timestamp": time.Now(), } w.Header().Set("Content-Type", "application/json")