2024-12-05 09:54:55 +08:00
|
|
|
|
package task
|
|
|
|
|
|
|
|
|
|
import (
|
2024-12-09 09:06:51 +08:00
|
|
|
|
"context"
|
2024-12-05 09:54:55 +08:00
|
|
|
|
"fmt"
|
|
|
|
|
"gitee.ltd/lxh/logger/log"
|
|
|
|
|
jsoniter "github.com/json-iterator/go"
|
|
|
|
|
"golang.org/x/exp/slices"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
|
|
|
|
"wireguard-ui/component"
|
|
|
|
|
"wireguard-ui/global/client"
|
2024-12-09 09:06:51 +08:00
|
|
|
|
"wireguard-ui/global/constant"
|
2024-12-05 09:54:55 +08:00
|
|
|
|
"wireguard-ui/model"
|
|
|
|
|
"wireguard-ui/service"
|
|
|
|
|
"wireguard-ui/utils"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type NetworkClientImpl interface {
|
2024-12-05 11:49:29 +08:00
|
|
|
|
ClientOfflineNotify() // 客户端离线通知
|
2024-12-05 09:54:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type networkClient struct{}
|
|
|
|
|
|
|
|
|
|
func NetworkClient() NetworkClientImpl {
|
|
|
|
|
return networkClient{}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ClientOfflineNotify
|
|
|
|
|
// @description: 客户端离线通知
|
|
|
|
|
// @receiver c
|
|
|
|
|
// @return error
|
2024-12-05 11:49:29 +08:00
|
|
|
|
func (c networkClient) ClientOfflineNotify() {
|
|
|
|
|
log.Debugf("开始执行离线通知任务")
|
2024-12-05 09:54:55 +08:00
|
|
|
|
// 开始扫描已经链接过的客户端
|
|
|
|
|
connectedPeers, err := component.Wireguard().GetClients()
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Errorf("获取已连接客户端失败: %v", err.Error())
|
2024-12-05 11:49:29 +08:00
|
|
|
|
return
|
2024-12-05 09:54:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 查询一下通知配置
|
|
|
|
|
code, err := service.Setting().GetByCode("WECHAT_NOTIFY")
|
|
|
|
|
if err != nil {
|
2024-12-05 11:06:21 +08:00
|
|
|
|
log.Errorf("获取微信通知配置失败: %v", err.Error())
|
2024-12-05 11:49:29 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 查询出所有配置了离线通知的客户端
|
|
|
|
|
var clients []model.Client
|
|
|
|
|
if err := client.DB.Where("offline_monitoring = ?", 1).Find(&clients).Error; err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(clients) <= 0 {
|
|
|
|
|
return
|
2024-12-05 09:54:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, peer := range connectedPeers {
|
|
|
|
|
var clientName string
|
|
|
|
|
if !slices.ContainsFunc(clients, func(cli model.Client) bool {
|
2024-12-05 14:33:38 +08:00
|
|
|
|
isExist := peer.PublicKey.String() == jsoniter.Get([]byte(cli.Keys), "publicKey").ToString()
|
2024-12-05 09:54:55 +08:00
|
|
|
|
if isExist {
|
|
|
|
|
clientName = cli.Name
|
|
|
|
|
}
|
|
|
|
|
return isExist
|
|
|
|
|
}) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-05 14:19:54 +08:00
|
|
|
|
online := time.Since(peer.LastHandshakeTime).Minutes() < 3
|
|
|
|
|
log.Debugf("客户端[%v]在线状态: %v,离线时间: %v", clientName, online, time.Since(peer.LastHandshakeTime).Minutes())
|
|
|
|
|
|
2024-12-09 09:06:51 +08:00
|
|
|
|
var ipAllocation string
|
|
|
|
|
for _, iaip := range peer.AllowedIPs {
|
|
|
|
|
ipAllocation += iaip.String() + ","
|
|
|
|
|
}
|
|
|
|
|
// 去除一下最右边的逗号
|
|
|
|
|
if len(ipAllocation) > 0 {
|
|
|
|
|
ipAllocation = strings.TrimRight(ipAllocation, ",")
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-05 09:54:55 +08:00
|
|
|
|
// 如果存在,判断离线时间
|
2024-12-05 14:19:54 +08:00
|
|
|
|
if !online {
|
2024-12-05 09:54:55 +08:00
|
|
|
|
// 已经离线,发送通知
|
2024-12-05 15:09:52 +08:00
|
|
|
|
msg := fmt.Sprintf(`[离线通知]
|
|
|
|
|
客户端名称 : %v
|
|
|
|
|
客户端IP : %v
|
|
|
|
|
最后在线时间 : %v`, clientName, ipAllocation, peer.LastHandshakeTime.Format("2006-01-02 15:04:05"))
|
2024-12-05 09:54:55 +08:00
|
|
|
|
err := utils.WechatNotify(code).SendTextMessage(msg)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Errorf("微信消息[%v]通知失败: %v", msg, err.Error())
|
|
|
|
|
continue
|
|
|
|
|
}
|
2024-12-09 09:06:51 +08:00
|
|
|
|
|
|
|
|
|
// 离线了,设置离线标识
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-12-05 09:54:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-05 11:49:29 +08:00
|
|
|
|
return
|
2024-12-05 09:54:55 +08:00
|
|
|
|
}
|