From 8dab976cbaaf58a9a01cafd20e2ce554a0cd805f Mon Sep 17 00:00:00 2001 From: coward Date: Fri, 14 Jun 2024 08:45:16 +0800 Subject: [PATCH] =?UTF-8?q?:bug:=E4=BF=AE=E5=A4=8DCPU=E8=B4=9F=E8=BD=BD?= =?UTF-8?q?=E8=BF=87=E9=AB=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cron_task/offline_monitoring.go | 88 ++++++++++++++++----------------- main.go | 5 +- 2 files changed, 45 insertions(+), 48 deletions(-) diff --git a/cron_task/offline_monitoring.go b/cron_task/offline_monitoring.go index 2f93a04..d9bb0de 100644 --- a/cron_task/offline_monitoring.go +++ b/cron_task/offline_monitoring.go @@ -13,53 +13,51 @@ import ( // offlineMonitoring // @description: 离线监听任务 func offlineMonitoring() { - for { - devices, err := client.WireguardClient.Devices() - if err != nil { - time.Sleep(5 * time.Minute) // 休眠五分钟再执行 - offlineMonitoring() - continue - } - - // 遍历客户端数据,并渲染数据信息 - for _, d := range devices { - for _, p := range d.Peers { - clientInfo, err := repository.Client().GetByPublicKey(p.PublicKey.String()) - if err != nil { - continue - } - - // 没有启用离线监听时,即使客户端已经离线则也不执行 - if clientInfo.OfflineMonitoring == nil || *clientInfo.OfflineMonitoring != 1 || clientInfo.Email == "" { - continue - } - - var ipAllocation string - for _, iaip := range p.AllowedIPs { - ipAllocation += iaip.String() + "," - } - ipAllocation = strings.TrimRight(ipAllocation, ",") - isOnline := time.Since(p.LastHandshakeTime).Minutes() < 3 - - // 未离线 - if !isOnline { - continue - } - - content := fmt.Sprintf("客户端:%s\r\n", clientInfo.Name) - content += fmt.Sprintf("客户端IP:%s\r\n", ipAllocation) - content += fmt.Sprintf("端点IP:%s", p.Endpoint.String()) - content += fmt.Sprintf("最后握手时间:%s\r\n", p.LastHandshakeTime.Format("2006-01-02 15:04:05")) - content += fmt.Sprintf("离线时间:%s\r\n", time.Now().Format("2006-01-02 15:04:05")) - - // 离线并且配置了邮箱,准备发送邮件 - err = utils.Mail().SendMail(clientInfo.Email, fmt.Sprintf("客户端离线通知"), content, "") - if err != nil { - log.Errorf("发送离线通知邮件失败: %v", err.Error()) - continue - } + devices, err := client.WireguardClient.Devices() + if err != nil { + time.Sleep(5 * time.Minute) // 休眠五分钟再执行 + offlineMonitoring() + return + } + // 遍历客户端数据,并渲染数据信息 + for _, d := range devices { + for _, p := range d.Peers { + clientInfo, err := repository.Client().GetByPublicKey(p.PublicKey.String()) + if err != nil { + continue } + + // 没有启用离线监听时,即使客户端已经离线则也不执行 + if clientInfo.OfflineMonitoring == nil || *clientInfo.OfflineMonitoring != 1 || clientInfo.Email == "" { + continue + } + + var ipAllocation string + for _, iaip := range p.AllowedIPs { + ipAllocation += iaip.String() + "," + } + ipAllocation = strings.TrimRight(ipAllocation, ",") + isOnline := time.Since(p.LastHandshakeTime).Minutes() < 3 + + // 未离线 + if !isOnline { + continue + } + + content := fmt.Sprintf("客户端:%s\r\n", clientInfo.Name) + content += fmt.Sprintf("客户端IP:%s\r\n", ipAllocation) + content += fmt.Sprintf("端点IP:%s\r\n", p.Endpoint.String()) + content += fmt.Sprintf("最后握手时间:%s\r\n", p.LastHandshakeTime.Format("2006-01-02 15:04:05")) + + // 离线并且配置了邮箱,准备发送邮件 + err = utils.Mail().SendMail(clientInfo.Email, fmt.Sprintf("客户端离线通知"), content, "") + if err != nil { + log.Errorf("发送离线通知邮件失败: %v", err.Error()) + continue + } + } } + } diff --git a/main.go b/main.go index a283a8a..c170718 100644 --- a/main.go +++ b/main.go @@ -7,7 +7,6 @@ import ( "net/http" "time" "wireguard-dashboard/config" - "wireguard-dashboard/cron_task" "wireguard-dashboard/initialize" "wireguard-dashboard/queues" "wireguard-dashboard/route" @@ -19,8 +18,8 @@ func init() { if err := script.NewScript().Do(); err != nil { log.Errorf("执行脚本失败: %v", err.Error()) } - go queues.StartConsumer() // 启动队列 - go cron_task.StartCronTask() // 启动定时任务 + go queues.StartConsumer() // 启动队列 + //go cron_task.StartCronTask() // 启动定时任务 } func main() {