添加重启endpoint

This commit is contained in:
Alex Yang
2025-11-27 18:31:32 +08:00
parent 82f17ad875
commit 2541996b18
11 changed files with 35 additions and 203010 deletions

View File

@@ -6,7 +6,7 @@
"223.6.6.6:53"
],
"timeout": 5000,
"statsFile": "./data/stats.json",
"statsFile": "data/stats.json",
"saveInterval": 300
},
"http": {

View File

@@ -1,5 +0,0 @@
{
"blockedDomainsCount": {},
"resolvedDomainsCount": {},
"lastSaved": "2025-11-27T18:23:48.562056138+08:00"
}

File diff suppressed because it is too large Load Diff

Binary file not shown.

View File

@@ -65,13 +65,13 @@ type Server struct {
resolvedDomainsMutex sync.RWMutex
resolvedDomains map[string]*BlockedDomain // 用于记录解析的域名
clientStatsMutex sync.RWMutex
clientStats map[string]*ClientStats // 用于记录客户端统计
clientStats map[string]*ClientStats // 用于记录客户端统计
hourlyStatsMutex sync.RWMutex
hourlyStats map[string]int64 // 按小时统计屏蔽数量
hourlyStats map[string]int64 // 按小时统计屏蔽数量
dailyStatsMutex sync.RWMutex
dailyStats map[string]int64 // 按天统计屏蔽数量
dailyStats map[string]int64 // 按天统计屏蔽数量
monthlyStatsMutex sync.RWMutex
monthlyStats map[string]int64 // 按月统计屏蔽数量
monthlyStats map[string]int64 // 按月统计屏蔽数量
saveTicker *time.Ticker // 用于定时保存数据
startTime time.Time // 服务器启动时间
saveDone chan struct{} // 用于通知保存协程停止
@@ -104,9 +104,9 @@ func NewServer(config *config.DNSConfig, shieldConfig *config.ShieldConfig, shie
Net: "udp",
Timeout: time.Duration(config.Timeout) * time.Millisecond,
},
ctx: ctx,
cancel: cancel,
startTime: time.Now(), // 记录服务器启动时间
ctx: ctx,
cancel: cancel,
startTime: time.Now(), // 记录服务器启动时间
stats: &Stats{
Queries: 0,
Blocked: 0,
@@ -137,6 +137,22 @@ func NewServer(config *config.DNSConfig, shieldConfig *config.ShieldConfig, shie
// Start 启动DNS服务器
func (s *Server) Start() error {
// 重新初始化上下文和取消函数
ctx, cancel := context.WithCancel(context.Background())
s.ctx = ctx
s.cancel = cancel
// 重新初始化saveDone通道
s.saveDone = make(chan struct{})
// 重置stopped标志
s.stoppedMutex.Lock()
s.stopped = false
s.stoppedMutex.Unlock()
// 更新服务器启动时间
s.startTime = time.Now()
s.server = &dns.Server{
Addr: fmt.Sprintf(":%d", s.config.Port),
Net: "udp",
@@ -178,6 +194,16 @@ func (s *Server) Start() error {
// Stop 停止DNS服务器
func (s *Server) Stop() {
// 检查服务器是否已经停止
s.stoppedMutex.Lock()
if s.stopped {
s.stoppedMutex.Unlock()
return // 服务器已经停止,直接返回
}
// 标记服务器为已停止状态
s.stopped = true
s.stoppedMutex.Unlock()
// 发送停止信号给保存协程
close(s.saveDone)

View File

@@ -1,6 +0,0 @@
# DNS Server Hosts File
# Generated by DNS Server
::1 localhost
ad.qq.com 127.0.0.1
ad.qq.com 0.0.0.0

File diff suppressed because it is too large Load Diff