Optimize the [Device Restriction] function

This commit is contained in:
心隨緣動 2025-09-12 16:44:08 +08:00 committed by GitHub
parent 74fdd3bedc
commit fdd2a5437b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

23
main.go
View File

@ -85,8 +85,27 @@ func runWebServer() {
ticker := time.NewTicker(10 * time.Second)
defer ticker.Stop()
// 中文注释: 创建我们的任务实例, 并传入 xrayService
checkJob := job.NewCheckDeviceLimitJob(&xrayService)
// 〔中文注释〕: 步骤一:在循环外部,只声明一次 tgBotService 变量。
// 我们将其声明为接口类型,初始值为 nil。
var tgBotService service.TelegramService
// 〔中文注释〕: 步骤二:检查 Telegram Bot 是否在面板设置中启用。
settingService := service.SettingService{}
tgEnable, err := settingService.GetTgbotEnabled()
if err != nil {
logger.Warningf("无法获取 Telegram Bot 设置: %v, 设备限制通知功能可能无法使用", err)
}
// 〔中文注释〕: 步骤三:如果 Bot 已启用,则初始化实例并赋值给上面声明的变量。
// 注意这里使用的是 `=` 而不是 `:=`,因为我们是给已存在的变量赋值。
if tgEnable {
tgBotService = new(service.Tgbot)
}
// 〔中文注释〕:步骤四:创建任务实例时,将 xrayService 和 可能为 nil 的 tgBotService 一同传入。
// 这样做是安全的,因为 check_client_ip_job.go 内部的 SendMessage 调用前,会先判断服务实例是否可用。
checkJob := job.NewCheckDeviceLimitJob(&xrayService, tgBotService)
// 中文注释: 使用一个无限循环,每次定时器触发,就执行一次任务的 Run() 函数
for {