Skip to content

Commit

Permalink
Beta38 (#38)
Browse files Browse the repository at this point in the history
* beta1

* beta2

* beta3

* beta4

* beta5

* beta6

* beta7

* beta8

* beta9

* beta10

* beta11

* beta12

* beta13

* beta14

* beta15

* beta16

* beta16

* beta19

* beta20

* beta21

* beta22

* beta23

* beta24

* beta25

* beta27

* beta28

* beta29

* beta30

* beta31

* beta33

* beta34

* beta35

* beta36

* beta37

* beta38
  • Loading branch information
Hoshinonyaruko authored Apr 1, 2024
1 parent 7fa8d68 commit 85312f8
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
config.yml
*.sqlite
sensitive_words.txt
sensitive_words_in.txt
sensitive_words_out.txt
white.txt

# Go specific
Expand Down
42 changes: 38 additions & 4 deletions acnode/acnode.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,24 @@ import (

// 定义包级别的全局变量
var ac *AhoCorasick
var acout *AhoCorasick
var wac *AhoCorasick

// init函数用于初始化操作
func init() {
ac = NewAhoCorasick()
acout = NewAhoCorasick()
wac = NewAhoCorasick()

// 载入敏感词库
if err := loadWordsIntoAC(ac, "sensitive_words.txt"); err != nil {
log.Fatalf("初始化敏感词库失败:%v", err)
// 载入敏感词库 入
if err := loadWordsIntoAC(ac, "sensitive_words_in.txt"); err != nil {
log.Fatalf("初始化敏感入词库失败:%v", err)
// 注意,log.Fatalf会调用os.Exit(1)终止程序,因此后面的return不是必须的
}

// 载入敏感词库 出
if err := loadWordsIntoAC(acout, "sensitive_words_out.txt"); err != nil {
log.Fatalf("初始化敏感出词库失败:%v", err)
// 注意,log.Fatalf会调用os.Exit(1)终止程序,因此后面的return不是必须的
}

Expand Down Expand Up @@ -317,7 +325,7 @@ func convertToUnicodeEscape(str string) string {
}

// 改写后的函数,接受word参数,并返回处理结果
func CheckWord(word string) string {
func CheckWordIN(word string) string {
if word == "" {
log.Println("错误请求:缺少 'word' 参数")
return "错误:缺少 'word' 参数"
Expand All @@ -341,3 +349,29 @@ func CheckWord(word string) string {

return result
}

// 改写后的函数,接受word参数,并返回处理结果
func CheckWordOUT(word string) string {
if word == "" {
log.Println("错误请求:缺少 'word' 参数")
return "错误:缺少 'word' 参数"
}

if len([]rune(word)) > 5000 {
if strings.Contains(word, "[CQ:image,file=base64://") {
// 当word包含特定字符串时原样返回
fmtf.Printf("原样返回的文本:%s", word)
return word
}
log.Printf("错误请求:字符数超过最大限制(5000字符)。内容:%s", word)
return "错误:字符数超过最大限制(5000字符)"
}

// 使用全局的wac进行白名单匹配
whiteListedPositions := wac.MatchPositions(word)

// 使用全局的ac进行过滤,并结合白名单
result := acout.FilterWithWhitelist(word, whiteListedPositions)

return result
}
2 changes: 1 addition & 1 deletion applogic/gensokyo.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func (app *App) GensokyoHandler(w http.ResponseWriter, r *http.Request) {
//审核部分 文本替换规则
newmsg := message.Message.(string)
if config.GetSensitiveMode() {
newmsg = acnode.CheckWord(newmsg)
newmsg = acnode.CheckWordIN(newmsg)
}

//提示词安全部分
Expand Down
6 changes: 3 additions & 3 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func SendGroupMessage(groupID int64, message string) error {
url := baseURL + "/send_group_msg"

if config.GetSensitiveModeType() == 1 {
message = acnode.CheckWord(message)
message = acnode.CheckWordOUT(message)
}

// 构造请求体
Expand Down Expand Up @@ -155,7 +155,7 @@ func SendPrivateMessage(UserID int64, message string) error {
url := baseURL + "/send_private_msg"

if config.GetSensitiveModeType() == 1 {
message = acnode.CheckWord(message)
message = acnode.CheckWordOUT(message)
}

// 构造请求体
Expand Down Expand Up @@ -194,7 +194,7 @@ func SendPrivateMessageSSE(UserID int64, message structs.InterfaceBody) error {

// 检查是否需要启用敏感词过滤
if config.GetSensitiveModeType() == 1 && message.Content != "" {
message.Content = acnode.CheckWord(message.Content)
message.Content = acnode.CheckWordOUT(message.Content)
}

// 构造请求体,包括InterfaceBody
Expand Down

0 comments on commit 85312f8

Please sign in to comment.