feat: 添加对回调请求校验的能力,解决可被其他人调用的安全隐患 (#171)

This commit is contained in:
二丫讲梵
2023-04-04 15:36:22 +08:00
committed by GitHub
parent 29fc9243d6
commit ef030f0498
10 changed files with 111 additions and 50 deletions

View File

@@ -1,14 +1,9 @@
package public
import (
"fmt"
"strings"
"github.com/eryajf/chatgpt-dingtalk/config"
"github.com/eryajf/chatgpt-dingtalk/pkg/cache"
"github.com/eryajf/chatgpt-dingtalk/pkg/db"
"github.com/eryajf/chatgpt-dingtalk/pkg/dingbot"
"github.com/eryajf/chatgpt-dingtalk/pkg/logger"
)
var UserService cache.UserServiceInterface
@@ -30,42 +25,6 @@ func InitSvc() {
// }
}
func FirstCheck(rmsg *dingbot.ReceiveMsg) bool {
lc := UserService.GetUserMode(rmsg.GetSenderIdentifier())
if lc == "" {
if Config.DefaultMode == "串聊" {
return true
} else {
return false
}
}
if lc != "" && strings.Contains(lc, "串聊") {
return true
}
return false
}
// ProcessRequest 分析处理请求逻辑
// 主要提供单日请求限额的功能
func CheckRequest(rmsg *dingbot.ReceiveMsg) bool {
if Config.MaxRequest == 0 {
return true
}
count := UserService.GetUseRequestCount(rmsg.GetSenderIdentifier())
// 判断访问次数是否超过限制
if count >= Config.MaxRequest {
logger.Info(fmt.Sprintf("亲爱的: %s您今日请求次数已达上限请明天再来交互发问资源有限请务必斟酌您的问题给您带来不便敬请谅解!", rmsg.SenderNick))
_, err := rmsg.ReplyToDingtalk(string(dingbot.TEXT), fmt.Sprintf("一个好的问题,胜过十个好的答案!\n亲爱的: %s您今日请求次数已达上限请明天再来交互发问资源有限请务必斟酌您的问题给您带来不便敬请谅解!", rmsg.SenderNick))
if err != nil {
logger.Warning(fmt.Errorf("send message error: %v", err))
}
return false
}
// 访问次数未超过限制将计数加1
UserService.SetUseRequestCount(rmsg.GetSenderIdentifier(), count+1)
return true
}
var Welcome string = `# 发送信息
若您想给机器人发送信息,有如下两种方式: