From 628c09fdde65fe3f9f4fdee162a347bfef962eeb Mon Sep 17 00:00:00 2001 From: coward Date: Mon, 9 Dec 2024 09:06:51 +0800 Subject: [PATCH] =?UTF-8?q?:art:=E6=96=B0=E5=A2=9E=E5=AE=A2=E6=88=B7?= =?UTF-8?q?=E7=AB=AF=E7=A6=BB=E7=BA=BF=E5=90=8E=E4=B8=8A=E7=BA=BF=E9=80=9A?= =?UTF-8?q?=E7=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cron/task/client.go | 39 +++++++++++++++++++++++++++--------- global/constant/redis_key.go | 5 +++-- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/cron/task/client.go b/cron/task/client.go index c4796cc..e816a9a 100644 --- a/cron/task/client.go +++ b/cron/task/client.go @@ -1,6 +1,7 @@ package task import ( + "context" "fmt" "gitee.ltd/lxh/logger/log" jsoniter "github.com/json-iterator/go" @@ -9,6 +10,7 @@ import ( "time" "wireguard-ui/component" "wireguard-ui/global/client" + "wireguard-ui/global/constant" "wireguard-ui/model" "wireguard-ui/service" "wireguard-ui/utils" @@ -69,17 +71,17 @@ func (c networkClient) ClientOfflineNotify() { online := time.Since(peer.LastHandshakeTime).Minutes() < 3 log.Debugf("客户端[%v]在线状态: %v,离线时间: %v", clientName, online, time.Since(peer.LastHandshakeTime).Minutes()) + var ipAllocation string + for _, iaip := range peer.AllowedIPs { + ipAllocation += iaip.String() + "," + } + // 去除一下最右边的逗号 + if len(ipAllocation) > 0 { + ipAllocation = strings.TrimRight(ipAllocation, ",") + } + // 如果存在,判断离线时间 if !online { - var ipAllocation string - for _, iaip := range peer.AllowedIPs { - ipAllocation += iaip.String() + "," - } - // 去除一下最右边的逗号 - if len(ipAllocation) > 0 { - ipAllocation = strings.TrimRight(ipAllocation, ",") - } - // 已经离线,发送通知 msg := fmt.Sprintf(`[离线通知] 客户端名称 : %v @@ -90,6 +92,25 @@ func (c networkClient) ClientOfflineNotify() { log.Errorf("微信消息[%v]通知失败: %v", msg, err.Error()) continue } + + // 离线了,设置离线标识 + client.Redis.Set(context.Background(), fmt.Sprintf("%s%s", constant.ClientOffline, utils.Hash().MD5(ipAllocation)), true, 0) + } else { + // 判断是否存在缓存 + if client.Redis.Exists(context.Background(), fmt.Sprintf("%s%s", constant.ClientOffline, utils.Hash().MD5(ipAllocation))).Val() > 0 { + // 存在,删除离线标识 + client.Redis.Del(context.Background(), fmt.Sprintf("%s%s", constant.ClientOffline, utils.Hash().MD5(ipAllocation))) + // 微信通知该客户端已经上线 + msg := fmt.Sprintf(`[上线通知] +客户端名称 : %v +客户端IP : %v +最后在线时间 : %v`, clientName, ipAllocation, peer.LastHandshakeTime.Format("2006-01-02 15:04:05")) + err := utils.WechatNotify(code).SendTextMessage(msg) + if err != nil { + log.Errorf("微信消息[%v]通知失败: %v", msg, err.Error()) + continue + } + } } } diff --git a/global/constant/redis_key.go b/global/constant/redis_key.go index 5f64905..35c6fa9 100644 --- a/global/constant/redis_key.go +++ b/global/constant/redis_key.go @@ -1,6 +1,7 @@ package constant const ( - Captcha = "captcha" - UserToken = "token" + Captcha = "captcha" + UserToken = "token" + ClientOffline = "client:offline:" )