fix: 修复QQ存在的panic风险

This commit is contained in:
mei
2025-10-06 22:15:35 +08:00
parent a253e15e95
commit e2b68fef1e
5 changed files with 44 additions and 16 deletions
+1
View File
@@ -26,6 +26,7 @@
"signal_secret": "", // 发送 signal请求时需要的密钥 "signal_secret": "", // 发送 signal请求时需要的密钥
"debug": false, // debug 模式 "debug": false, // debug 模式
"disconnect_duration": 5, // 失连时间,超出后会触发系统 "disconnect_duration": 5, // 失连时间,超出后会触发系统
"enable_qq": true, // 是否启用QQ
"cat_bot_url": "", // CatBot 的 webhook地址(https://github.com/ssdomei232/CatBot) "cat_bot_url": "", // CatBot 的 webhook地址(https://github.com/ssdomei232/CatBot)
"cat_bot_key": "", // CatBot 的 key "cat_bot_key": "", // CatBot 的 key
"qq_send_group": [], // 要发送QQ消息的群号 "qq_send_group": [], // 要发送QQ消息的群号
+1
View File
@@ -8,6 +8,7 @@
"signal_secret": "", "signal_secret": "",
"debug": false, "debug": false,
"disconnect_duration": 5, "disconnect_duration": 5,
"enable_qq": true,
"cat_bot_url": "", "cat_bot_url": "",
"cat_bot_key": "", "cat_bot_key": "",
"qq_send_group": [], "qq_send_group": [],
+1
View File
@@ -10,6 +10,7 @@ type Config struct {
SignalSecret string `json:"signal_secret"` SignalSecret string `json:"signal_secret"`
Debug bool `json:"debug"` Debug bool `json:"debug"`
DisconnectDuration int `json:"disconnect_duration"` // Hours DisconnectDuration int `json:"disconnect_duration"` // Hours
EnableQQ bool `json:"enable_qq"`
CatBotUrl string `json:"cat_bot_url"` CatBotUrl string `json:"cat_bot_url"`
CatBotKey string `json:"cat_bot_key"` CatBotKey string `json:"cat_bot_key"`
QQSendGroup []int `json:"qq_send_group"` QQSendGroup []int `json:"qq_send_group"`
+36 -13
View File
@@ -3,11 +3,11 @@ package internal
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"io"
"log" "log"
"mime/multipart" "mime/multipart"
"net/http" "net/http"
"strconv" "strconv"
"time"
"github.com/ssdomei232/goodBaby/configs" "github.com/ssdomei232/goodBaby/configs"
) )
@@ -16,6 +16,7 @@ func SendQQ() {
config, err := configs.GetConfig() config, err := configs.GetConfig()
if err != nil { if err != nil {
log.Printf("获取配置文件失败: %v", err) log.Printf("获取配置文件失败: %v", err)
return // 添加return避免继续执行
} }
url := fmt.Sprintf("%s/?secret=%s", config.CatBotUrl, config.CatBotKey) url := fmt.Sprintf("%s/?secret=%s", config.CatBotUrl, config.CatBotKey)
@@ -24,43 +25,65 @@ func SendQQ() {
for _, groupId := range config.QQSendGroup { for _, groupId := range config.QQSendGroup {
sendQQMsg(url, groupId, stopMsg) sendQQMsg(url, groupId, stopMsg)
} }
} }
func sendQQMsg(url string, groupId int, msg string) { func sendQQMsg(url string, groupId int, msg string) {
method := "POST" method := "POST"
payload := &bytes.Buffer{} payload := &bytes.Buffer{}
writer := multipart.NewWriter(payload) writer := multipart.NewWriter(payload)
_ = writer.WriteField("group_id", strconv.Itoa(groupId))
_ = writer.WriteField("message", msg) // 处理写入字段的错误
if err := writer.WriteField("group_id", strconv.Itoa(groupId)); err != nil {
log.Printf("写入group_id字段失败: %v", err)
return
}
if err := writer.WriteField("message", msg); err != nil {
log.Printf("写入message字段失败: %v", err)
return
}
err := writer.Close() err := writer.Close()
if err != nil { if err != nil {
log.Println(err) log.Printf("关闭multipart writer失败: %v", err)
return return
} }
client := &http.Client{} client := &http.Client{}
req, err := http.NewRequest(method, url, payload) req, err := http.NewRequest(method, url, payload)
if err != nil { if err != nil {
fmt.Println(err) log.Printf("创建HTTP请求失败: %v", err)
return return
} }
req.Header.Set("Content-Type", writer.FormDataContentType()) req.Header.Set("Content-Type", writer.FormDataContentType())
var res *http.Response var res *http.Response
var maxRetries = 3
for i := range maxRetries { for i := range maxRetries {
res, err = client.Do(req) res, err = client.Do(req)
if err == nil { if err == nil {
break // 请求成功,跳出循环 break // 请求成功,跳出循环
} }
log.Printf("请求QQ API失败,正在重试... (%d/%d)", i+1, maxRetries) if i < maxRetries {
time.Sleep(retryDelay * time.Duration(i+1)) // 递增延迟
}
log.Printf("请求QQ API失败,正在重试... (%d/%d): %v", i+1, maxRetries, err)
} }
defer res.Body.Close()
_, err = io.ReadAll(res.Body) // 检查res是否为nil,避免panic
if err != nil { if res == nil {
fmt.Println(err) log.Println("QQ API请求失败,无法获取响应")
return return
} }
// 使用defer确保资源被释放,但要确保res不为nil
defer func() {
if res != nil && res.Body != nil {
res.Body.Close()
}
}()
// 可选:记录响应结果
log.Println("QQ消息发送完成")
} }
+5 -3
View File
@@ -30,7 +30,7 @@ func init() {
timer = time.NewTimer(duration) timer = time.NewTimer(duration)
go func() { go func() {
<-timer.C <-timer.C
trigger() trigger(config)
}() }()
// 初始化bilibili客户端 // 初始化bilibili客户端
@@ -39,8 +39,10 @@ func init() {
startCookieChecker() // 启动定期检查cookie有效性 startCookieChecker() // 启动定期检查cookie有效性
} }
func trigger() { func trigger(config *configs.Config) {
go internal.SendQQ() if config.EnableQQ {
go internal.SendQQ()
}
go internal.SendMail() go internal.SendMail()
go internal.Github() go internal.Github()
go internal.SendBili(biliClient) go internal.SendBili(biliClient)