Files
dns-server/log/query_logger.go
T
Alex Yang efebce3c39 whois
2026-04-01 12:22:55 +08:00

88 lines
3.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package log
import (
"time"
)
// QueryLog DNS 查询日志结构
type QueryLog struct {
ID int64 `json:"id"`
Timestamp time.Time `json:"timestamp"` // 查询时间
ClientIP string `json:"clientIP"` // 客户端 IP
Domain string `json:"domain"` // 查询域名
QueryType string `json:"queryType"` // 查询类型
ResponseTime int64 `json:"responseTime"` // 响应时间 (ms)
Result string `json:"result"` // 查询结果(allowed, blocked, error
BlockRule string `json:"blockRule"` // 屏蔽规则(如果被屏蔽)
BlockType string `json:"blockType"` // 屏蔽类型(如果被屏蔽)
FromCache bool `json:"fromCache"` // 是否来自缓存
DNSSEC bool `json:"dnssec"` // 是否使用了 DNSSEC
EDNS bool `json:"edns"` // 是否使用了 EDNS
DNSServer string `json:"dnsServer"` // 使用的 DNS 服务器
DNSSECServer string `json:"dnssecServer"` // 使用的 DNSSEC 专用服务器
Answers string `json:"answers"` // 解析记录(JSON 格式)
ResponseCode int `json:"responseCode"` // DNS 响应代码
}
// LogFilter 日志过滤条件
type LogFilter struct {
Result string // 结果过滤(allowed, blocked, error
SearchTerm string // 搜索关键词(域名或 IP
QueryType string // 查询类型过滤
StartTime time.Time // 开始时间
EndTime time.Time // 结束时间
}
// PageParams 分页参数
type PageParams struct {
Limit int // 每页数量
Offset int // 偏移量
SortField string // 排序字段
SortDirection string // 排序方向(asc, desc
}
// LogStats 日志统计信息
type LogStats struct {
TotalQueries int64 `json:"totalQueries"` // 总查询数
BlockedQueries int64 `json:"blockedQueries"` // 被屏蔽查询数
AllowedQueries int64 `json:"allowedQueries"` // 允许查询数
ErrorQueries int64 `json:"errorQueries"` // 错误查询数
AvgResponseTime float64 `json:"avgResponseTime"` // 平均响应时间
QueryTypes map[string]int64 `json:"queryTypes"` // 查询类型分布
TopDomains []DomainCount `json:"topDomains"` // TOP 域名
TopClients []ClientCount `json:"topClients"` // TOP 客户端
}
// DomainCount 域名统计
type DomainCount struct {
Domain string `json:"domain"`
Count int64 `json:"count"`
}
// ClientCount 客户端统计
type ClientCount struct {
IP string `json:"ip"`
Count int64 `json:"count"`
}
// QueryLogger 日志记录器接口
type QueryLogger interface {
// Log 记录日志(异步、非阻塞)
Log(log QueryLog) error
// QueryLogs 查询日志(支持分页、过滤、排序)
QueryLogs(filter LogFilter, page PageParams) ([]QueryLog, int64, error)
// GetStats 获取统计信息
GetStats(timeRange TimeRange) (*LogStats, error)
// Close 关闭日志记录器
Close() error
}
// TimeRange 时间范围
type TimeRange struct {
StartTime time.Time
EndTime time.Time
}