Compare commits

..

5 Commits

Author SHA1 Message Date
93911536d9 🎨调整定时任务执行时间,以及启动时间 2024-12-05 11:52:17 +08:00
f09d0d2994 🎨调整定时任务执行时间,以及启动时间 2024-12-05 11:49:29 +08:00
ca42c72e0f 🐛修复微信通知bug 2024-12-05 11:25:21 +08:00
683e7b2cc3 🐛修复定时任务bug 2024-12-05 11:06:21 +08:00
accb060e27 🐛修复定时任务bug 2024-12-05 10:43:40 +08:00
4 changed files with 33 additions and 23 deletions

View File

@@ -14,7 +14,6 @@ func Task() {
return return
} }
_, _ = sch.NewJob(gocron.DurationJob(3*time.Minute), gocron.NewTask(task.NetworkClient().ClientOfflineNotify())) // 每分钟检测离线的客户端并且发送消息 _, _ = sch.NewJob(gocron.DurationJob(1*time.Minute), gocron.NewTask(task.NetworkClient().ClientOfflineNotify)) // 每分钟执行一次
sch.Start() sch.Start()
} }

View File

@@ -15,7 +15,7 @@ import (
) )
type NetworkClientImpl interface { type NetworkClientImpl interface {
ClientOfflineNotify() error // 客户端离线通知 ClientOfflineNotify() // 客户端离线通知
} }
type networkClient struct{} type networkClient struct{}
@@ -28,28 +28,30 @@ func NetworkClient() NetworkClientImpl {
// @description: 客户端离线通知 // @description: 客户端离线通知
// @receiver c // @receiver c
// @return error // @return error
func (c networkClient) ClientOfflineNotify() error { func (c networkClient) ClientOfflineNotify() {
// 查询出所有配置了离线通知的客户端 log.Debugf("开始执行离线通知任务")
var clients []model.Client
if err := client.DB.Where("offline_monitoring = ?", 1).Find(&clients).Error; err != nil {
return err
}
if len(clients) <= 0 {
return nil
}
// 开始扫描已经链接过的客户端 // 开始扫描已经链接过的客户端
connectedPeers, err := component.Wireguard().GetClients() connectedPeers, err := component.Wireguard().GetClients()
if err != nil { if err != nil {
log.Errorf("获取已连接客户端失败: %v", err.Error()) log.Errorf("获取已连接客户端失败: %v", err.Error())
return err return
} }
// 查询一下通知配置 // 查询一下通知配置
code, err := service.Setting().GetByCode("WECHAT_NOTIFY") code, err := service.Setting().GetByCode("WECHAT_NOTIFY")
if err != nil { if err != nil {
return err log.Errorf("获取微信通知配置失败: %v", err.Error())
return
}
// 查询出所有配置了离线通知的客户端
var clients []model.Client
if err := client.DB.Where("offline_monitoring = ?", 1).Find(&clients).Error; err != nil {
return
}
if len(clients) <= 0 {
return
} }
for _, peer := range connectedPeers { for _, peer := range connectedPeers {
@@ -65,7 +67,7 @@ func (c networkClient) ClientOfflineNotify() error {
} }
// 如果存在,判断离线时间 // 如果存在,判断离线时间
if time.Since(peer.LastHandshakeTime) < 3 { if time.Since(peer.LastHandshakeTime).Minutes() >= 3 {
var ipAllocation string var ipAllocation string
for _, iaip := range peer.AllowedIPs { for _, iaip := range peer.AllowedIPs {
ipAllocation += iaip.String() + "," ipAllocation += iaip.String() + ","
@@ -77,7 +79,7 @@ func (c networkClient) ClientOfflineNotify() error {
// 已经离线,发送通知 // 已经离线,发送通知
msg := fmt.Sprintf(` msg := fmt.Sprintf(`
[离线通知] [离线通知]
客户端名称 : %v 客户端名称 : %v
客户端IP : %v 客户端IP : %v
最后在线时间 : %v 最后在线时间 : %v
@@ -91,5 +93,5 @@ func (c networkClient) ClientOfflineNotify() error {
} }
return nil return
} }

View File

@@ -22,8 +22,6 @@ func init() {
log.Errorf("执行脚本失败: %v", err.Error()) log.Errorf("执行脚本失败: %v", err.Error())
} }
// 启动定时任务
go cron.Task()
} }
func main() { func main() {
@@ -35,6 +33,9 @@ func main() {
pprof.Register(handler, "/monitoring") pprof.Register(handler, "/monitoring")
} }
// 启动定时任务
cron.Task()
httpServe := http.Server{ httpServe := http.Server{
Addr: fmt.Sprintf(":%d", config.Config.Http.Port), Addr: fmt.Sprintf(":%d", config.Config.Http.Port),
Handler: handler, Handler: handler,

View File

@@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"gitee.ltd/lxh/logger/log" "gitee.ltd/lxh/logger/log"
jsoniter "github.com/json-iterator/go" jsoniter "github.com/json-iterator/go"
"strings"
"wireguard-ui/global/client" "wireguard-ui/global/client"
"wireguard-ui/model" "wireguard-ui/model"
) )
@@ -26,7 +27,7 @@ func WechatNotify(setting *model.Setting) wechatNotify {
Addr: sm["addr"], Addr: sm["addr"],
Path: sm["path"], Path: sm["path"],
Method: sm["method"], Method: sm["method"],
toUserId: sm["toUserId"], toUserId: sm["toUserWxId"],
} }
} }
@@ -36,6 +37,7 @@ func WechatNotify(setting *model.Setting) wechatNotify {
// @param msg // @param msg
// @return error // @return error
func (w wechatNotify) SendTextMessage(msg string) error { func (w wechatNotify) SendTextMessage(msg string) error {
log.Debugf("发送通知到微信: %v", msg)
req := client.HttpClient.R() req := client.HttpClient.R()
req.SetHeader("Content-Type", "application/json") req.SetHeader("Content-Type", "application/json")
@@ -43,7 +45,13 @@ func (w wechatNotify) SendTextMessage(msg string) error {
"wxid": w.toUserId, "wxid": w.toUserId,
"msg": msg, "msg": msg,
}) })
result, err := req.Post(fmt.Sprintf("%s:%s", w.Addr, w.Path))
req.URL = fmt.Sprintf("%s:%s", w.Addr, w.Path)
switch strings.ToUpper(w.Method) {
case "POST":
req.Method = "POST"
}
result, err := req.SetDebug(true).Send()
if err != nil { if err != nil {
return err return err
} }