将模拟数据修改为真实服务器统计数据

This commit is contained in:
Alex Yang
2025-11-25 18:14:53 +08:00
parent 397181429e
commit 4d53b13220
2 changed files with 62 additions and 28 deletions

View File

@@ -62,6 +62,7 @@ type Server struct {
monthlyStatsMutex sync.RWMutex monthlyStatsMutex sync.RWMutex
monthlyStats map[string]int64 // 按月统计屏蔽数量 monthlyStats map[string]int64 // 按月统计屏蔽数量
saveTicker *time.Ticker // 用于定时保存数据 saveTicker *time.Ticker // 用于定时保存数据
startTime time.Time // 服务器启动时间
saveDone chan struct{} // 用于通知保存协程停止 saveDone chan struct{} // 用于通知保存协程停止
} }
@@ -90,18 +91,19 @@ func NewServer(config *config.DNSConfig, shieldConfig *config.ShieldConfig, shie
Net: "udp", Net: "udp",
Timeout: time.Duration(config.Timeout) * time.Millisecond, Timeout: time.Duration(config.Timeout) * time.Millisecond,
}, },
ctx: ctx, ctx: ctx,
cancel: cancel, cancel: cancel,
startTime: time.Now(), // 记录服务器启动时间
stats: &Stats{ stats: &Stats{
Queries: 0, Queries: 0,
Blocked: 0, Blocked: 0,
Allowed: 0, Allowed: 0,
Errors: 0, Errors: 0,
AvgResponseTime: 0, AvgResponseTime: 0,
TotalResponseTime: 0, TotalResponseTime: 0,
QueryTypes: make(map[string]int64), QueryTypes: make(map[string]int64),
SourceIPs: make(map[string]bool), SourceIPs: make(map[string]bool),
CpuUsage: 0, CpuUsage: 0,
}, },
blockedDomains: make(map[string]*BlockedDomain), blockedDomains: make(map[string]*BlockedDomain),
resolvedDomains: make(map[string]*BlockedDomain), resolvedDomains: make(map[string]*BlockedDomain),
@@ -474,6 +476,11 @@ func (s *Server) updateStats(update func(*Stats)) {
update(s.stats) update(s.stats)
} }
// GetStartTime 获取服务器启动时间
func (s *Server) GetStartTime() time.Time {
return s.startTime
}
// GetStats 获取DNS服务器统计信息 // GetStats 获取DNS服务器统计信息
func (s *Server) GetStats() *Stats { func (s *Server) GetStats() *Stats {
s.statsMutex.Lock() s.statsMutex.Lock()

View File

@@ -87,27 +87,41 @@ func (s *Server) handleStats(w http.ResponseWriter, r *http.Request) {
dnsStats := s.dnsServer.GetStats() dnsStats := s.dnsServer.GetStats()
shieldStats := s.shieldManager.GetStats() shieldStats := s.shieldManager.GetStats()
// 获取最常用查询类型 // 获取最常用查询类型(如果有)
topQueryType := "A" topQueryType := "-"
maxCount := int64(0) maxCount := int64(0)
for queryType, count := range dnsStats.QueryTypes { if len(dnsStats.QueryTypes) > 0 {
if count > maxCount { for queryType, count := range dnsStats.QueryTypes {
maxCount = count if count > maxCount {
topQueryType = queryType maxCount = count
topQueryType = queryType
}
} }
} }
// 获取活跃来源IP数量 // 获取活跃来源IP数量
activeIPCount := len(dnsStats.SourceIPs) activeIPCount := len(dnsStats.SourceIPs)
// 构建响应数据,确保所有字段都反映服务器的真实状态
stats := map[string]interface{}{ stats := map[string]interface{}{
"dns": dnsStats, "dns": map[string]interface{}{
"shield": shieldStats, "Queries": dnsStats.Queries,
"topQueryType": topQueryType, "Blocked": dnsStats.Blocked,
"activeIPs": activeIPCount, "Allowed": dnsStats.Allowed,
"avgResponseTime": dnsStats.AvgResponseTime, "Errors": dnsStats.Errors,
"cpuUsage": dnsStats.CpuUsage, "LastQuery": dnsStats.LastQuery,
"time": time.Now(), "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") 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() stats := s.dnsServer.GetStats()
// 使用服务器的实际启动时间计算准确的运行时间
serverStartTime := s.dnsServer.GetStartTime()
uptime := time.Since(serverStartTime)
// 构建包含所有真实服务器统计数据的响应
status := map[string]interface{}{ status := map[string]interface{}{
"status": "running", "status": "running",
"queries": stats.Queries, "queries": stats.Queries,
"lastQuery": stats.LastQuery, "blocked": stats.Blocked,
"uptime": time.Since(stats.LastQuery), "allowed": stats.Allowed,
"timestamp": time.Now(), "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") w.Header().Set("Content-Type", "application/json")