Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 996ea61

Browse files
committed
3 天内注册的用户,如果某个时间段内发文超过一定次数,判断为 spam,直接停用账号,并删除其内容
1 parent f2ae624 commit 996ea61

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

http/middleware/sensitive.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package middleware
99
import (
1010
"net/http"
1111
"strings"
12+
"time"
1213

1314
"github.com/studygolang/studygolang/context"
1415
"github.com/studygolang/studygolang/logic"
@@ -23,11 +24,16 @@ import (
2324
var (
2425
titleSensitives []string
2526
contentSensitives string
27+
28+
midNightSpam []string
29+
num int
2630
)
2731

2832
func init() {
2933
titleSensitives = strings.Split(config.ConfigFile.MustValue("sensitive", "title"), ",")
3034
contentSensitives = config.ConfigFile.MustValue("sensitive", "content")
35+
midNightSpam = strings.Split(config.ConfigFile.MustValue("spam", "mid_night"), ",")
36+
num = config.ConfigFile.MustInt("spam", "num")
3137
}
3238

3339
// Sensivite 用于 echo 框架的过滤发布敏感词(广告)
@@ -61,6 +67,24 @@ func Sensivite() echo.MiddlewareFunc {
6167
return ctx.String(http.StatusOK, `{"ok":0,"error":"对不起,您的账号已被冻结!"}`)
6268
}
6369

70+
// 半夜 spam 控制
71+
if num > 0 && len(midNightSpam) == 2 {
72+
curHour := time.Now().Hour()
73+
startHour := goutils.MustInt(midNightSpam[0])
74+
endHour := goutils.MustInt(midNightSpam[1])
75+
// 比如 23 ~ 8(不包括 8 点)
76+
if startHour > endHour {
77+
if curHour >= startHour || curHour < endHour {
78+
logic.SpamRecord(context.EchoContext(ctx), user, num)
79+
}
80+
} else {
81+
// 比如 0 ~ 8(不包括 8 点)
82+
if curHour >= startHour && curHour < endHour {
83+
logic.SpamRecord(context.EchoContext(ctx), user, num)
84+
}
85+
}
86+
}
87+
6488
if err := next(ctx); err != nil {
6589
return err
6690
}

logic/common.go

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ package logic
99
import (
1010
"errors"
1111
"fmt"
12-
"github.com/studygolang/studygolang/model"
13-
"github.com/studygolang/studygolang/util"
1412
"os"
1513
"regexp"
1614
"strconv"
1715
"time"
1816

17+
"github.com/studygolang/studygolang/model"
18+
"github.com/studygolang/studygolang/util"
19+
1920
"github.com/gorilla/schema"
2021
"github.com/polaris1119/goutils"
2122
"github.com/polaris1119/logger"
@@ -241,6 +242,32 @@ func NeedCaptcha(user *model.Me) bool {
241242
return false
242243
}
243244

245+
// SpamRecord 控制半夜 Spam
246+
// 避免误判,只针对最近 3 天内注册的用户
247+
func SpamRecord(ctx context.Context, user *model.Me, maxNum int) {
248+
if time.Now().Add(-3 * 24 * time.Hour).After(user.CreatedAt) {
249+
return
250+
}
251+
252+
redis := nosql.NewRedisFromPool()
253+
defer redis.Close()
254+
255+
key := getSpamMidNightNumKey(user.Uid)
256+
publishTimes := goutils.MustInt(redis.GET(key))
257+
if publishTimes >= maxNum-1 {
258+
DefaultUser.UpdateUserStatus(ctx, user.Uid, model.UserStatusOutage)
259+
260+
// 将用户 IP 加入黑名单
261+
DefaultRisk.AddBlackIPByUID(user.Uid)
262+
263+
DefaultUser.DeleteUserContent(ctx, user.Uid)
264+
265+
logger.Infoln("uid=", user.Uid, "spam, so delete TA's content")
266+
} else {
267+
redis.SET(key, publishTimes+1, 86400)
268+
}
269+
}
270+
244271
// incrPublishTimes 增加用户发布次数
245272
func incrPublishTimes(uid int) {
246273
redis := nosql.NewRedisFromPool()
@@ -268,6 +295,10 @@ func getLastPublishTimeKey(uid int) string {
268295
return "last:publish:time:user:" + strconv.Itoa(uid)
269296
}
270297

298+
func getSpamMidNightNumKey(uid int) string {
299+
return "spam:mid:night:num:user:" + strconv.Itoa(uid)
300+
}
301+
271302
func website() string {
272303
host := "http://"
273304
if WebsiteSetting.OnlyHttps {

0 commit comments

Comments
 (0)