-
Notifications
You must be signed in to change notification settings - Fork 731
/
sensitive.go
135 lines (111 loc) · 3.6 KB
/
sensitive.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright 2016 The StudyGolang Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// http://studygolang.com
// Author: polaris polaris@studygolang.com
package middleware
import (
"net/http"
"strings"
"time"
"github.com/studygolang/studygolang/context"
"github.com/studygolang/studygolang/internal/logic"
"github.com/studygolang/studygolang/internal/model"
echo "github.com/labstack/echo/v4"
"github.com/polaris1119/config"
"github.com/polaris1119/goutils"
"github.com/polaris1119/logger"
)
var (
titleSensitives []string
contentSensitives string
midNightSpam []string
num int
)
func init() {
titleSensitives = strings.Split(config.ConfigFile.MustValue("sensitive", "title"), ",")
contentSensitives = config.ConfigFile.MustValue("sensitive", "content")
midNightSpam = strings.Split(config.ConfigFile.MustValue("spam", "mid_night"), ",")
num = config.ConfigFile.MustInt("spam", "num")
}
// Sensivite 用于 echo 框架的过滤发布敏感词(广告)
func Sensivite() echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(ctx echo.Context) error {
content := ctx.FormValue("content")
title := ctx.FormValue("title")
user := ctx.Get("user").(*model.Me)
if title != "" {
for _, s := range titleSensitives {
if hasSensitiveChar(title, s) {
// 把账号冻结
logic.DefaultUser.UpdateUserStatus(context.EchoContext(ctx), user.Uid, model.UserStatusFreeze)
logger.Infoln("user=", user.Uid, "publish ad, title=", title, ". freeze")
// IP 加入黑名单
addBlackIP(ctx)
return ctx.String(http.StatusOK, `{"ok":0,"error":"对不起,您的账号已被冻结!"}`)
}
}
}
if hasSensitive(title, contentSensitives) || hasSensitive(content, contentSensitives) {
// 把账号冻结
logic.DefaultUser.UpdateUserStatus(context.EchoContext(ctx), user.Uid, model.UserStatusFreeze)
logger.Infoln("user=", user.Uid, "publish ad, title=", title, ";content=", content, ". freeze")
// IP 加入黑名单
addBlackIP(ctx)
return ctx.String(http.StatusOK, `{"ok":0,"error":"对不起,您的账号已被冻结!"}`)
}
// 半夜 spam 控制;评论不算
if title != "" && num > 0 && len(midNightSpam) == 2 {
curHour := time.Now().Hour()
startHour := goutils.MustInt(midNightSpam[0])
endHour := goutils.MustInt(midNightSpam[1])
// 比如 23 ~ 8(不包括 8 点)
if startHour > endHour {
if curHour >= startHour || curHour < endHour {
logic.SpamRecord(context.EchoContext(ctx), user, num)
}
} else {
// 比如 0 ~ 8(不包括 8 点)
if curHour >= startHour && curHour < endHour {
logic.SpamRecord(context.EchoContext(ctx), user, num)
}
}
}
if err := next(ctx); err != nil {
return err
}
return nil
}
}
}
// hasSensitive 是否有敏感词
func hasSensitive(content, sensitive string) bool {
if content == "" || sensitive == "" {
return false
}
sensitives := strings.Split(sensitive, ",")
for _, s := range sensitives {
if strings.Contains(content, s) {
return true
}
}
return false
}
// hasSensitiveChar 是否包含敏感字(多个词都包含)
func hasSensitiveChar(title, sensitive string) bool {
if title == "" || sensitive == "" {
return false
}
sensitives := strings.Split(sensitive, "")
for _, s := range sensitives {
if !strings.Contains(title, s) {
return false
}
}
return true
}
func addBlackIP(ctx echo.Context) {
ip := goutils.RemoteIp(ctx.Request())
logic.DefaultRisk.AddBlackIP(ip)
}