Skip to content

Commit

Permalink
加入敏感词过滤
Browse files Browse the repository at this point in the history
  • Loading branch information
顾志明 committed Sep 26, 2019
1 parent 69aef65 commit c143ad6
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 19 deletions.
41 changes: 29 additions & 12 deletions apis/topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"gopkg.in/mgo.v2/bson"

"github.com/jimmykuu/gopher/models"
"github.com/jimmykuu/gopher/utils"
)

// Topic 主题
Expand Down Expand Up @@ -135,6 +136,14 @@ func (a *Topic) Post() interface{} {
}
}

// 敏感词检查
if utils.HasSensitiveWords(form.Title) {
return map[string]interface{}{
"status": 0,
"message": "含有敏感词,禁止发布主题",
}
}

var c = a.DB.C(models.CONTENTS)

id := bson.NewObjectId()
Expand Down Expand Up @@ -203,11 +212,30 @@ func (a *Topic) Put() interface{} {
}
}

var form TopicForm
a.ReadJSON(&form)

result, err := govalidator.ValidateStruct(form)
if !result {
return map[string]interface{}{
"status": 0,
"message": err.Error(),
}
}

// 敏感词检查
if utils.HasSensitiveWords(form.Title) {
return map[string]interface{}{
"status": 0,
"message": "含有敏感词,禁止发布主题",
}
}

c := a.DB.C(models.CONTENTS)

topic := models.Topic{}

err := c.Find(bson.M{"_id": bson.ObjectIdHex(topicID), "content.type": models.TypeTopic}).One(&topic)
err = c.Find(bson.M{"_id": bson.ObjectIdHex(topicID), "content.type": models.TypeTopic}).One(&topic)

if err != nil {
return map[string]interface{}{
Expand All @@ -223,17 +251,6 @@ func (a *Topic) Put() interface{} {
}
}

var form TopicForm
a.ReadJSON(&form)

result, err := govalidator.ValidateStruct(form)
if !result {
return map[string]interface{}{
"status": 0,
"message": err.Error(),
}
}

var newNodeID = bson.ObjectIdHex(form.NodeID)

c = a.DB.C(models.CONTENTS)
Expand Down
2 changes: 2 additions & 0 deletions conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"runtime"

filter "github.com/antlinker/go-dirtyfilter"
"github.com/lunny/tango"
)

Expand Down Expand Up @@ -39,6 +40,7 @@ var (
TangoVersion = tango.Version()
Version string
AnalyticsCode template.HTML // 网站统计分析代码
DirtyManager *filter.DirtyManager
)

func InitConfig(configFile string) error {
Expand Down
2 changes: 2 additions & 0 deletions etc/sensitive_words.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
学历证书
毕业证
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ go 1.12
require (
github.com/Unknwon/com v0.0.0-20190321035513-0fed4efef755 // indirect
github.com/Youngyezi/geetest v0.0.0-20180629105258-e3b16a2be0f4
github.com/antlinker/go-cmap v0.0.0-20160407022646-0c5e57012e96 // indirect
github.com/antlinker/go-dirtyfilter v1.2.0
github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/jimmykuu/webhelpers v0.0.0-20160107152426-014fab4e0ee0
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ github.com/Unknwon/com v0.0.0-20190321035513-0fed4efef755 h1:1B7wb36fHLSwZfHg6ng
github.com/Unknwon/com v0.0.0-20190321035513-0fed4efef755/go.mod h1:voKvFVpXBJxdIPeqjoJuLK+UVcRlo/JLjeToGxPYu68=
github.com/Youngyezi/geetest v0.0.0-20180629105258-e3b16a2be0f4 h1:1Njrcys80BKBe9+Tn78lqhVJos0o6ol9z2gNKrAJiug=
github.com/Youngyezi/geetest v0.0.0-20180629105258-e3b16a2be0f4/go.mod h1:8vgkUAwkriXV9URJ3BRByRWNydpBVxRppW5jwt+H/HE=
github.com/antlinker/go-cmap v0.0.0-20160407022646-0c5e57012e96 h1:9jCOqZ1UyRwI5JPMUuYnIpLNgBPcsRXsjH0JZTDbvts=
github.com/antlinker/go-cmap v0.0.0-20160407022646-0c5e57012e96/go.mod h1:G+LGOmf0CtTskZRVr2cOGafQmsphVLDPfOIqAXGOTQI=
github.com/antlinker/go-dirtyfilter v1.2.0 h1:4r4fREWbL+vQaB65dCxYSzG679MqFUKtSKIE1S4qt38=
github.com/antlinker/go-dirtyfilter v1.2.0/go.mod h1:QQqzUFiff9pyPiL1SnK9T3JELk74iXSMZL3/iHUbEWA=
github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a h1:idn718Q4B6AGu/h5Sxe66HYVdqdGu2l9Iebqhi/AEoA=
github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
15 changes: 9 additions & 6 deletions init.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,20 @@ import (

"github.com/jimmykuu/gopher/conf"
"github.com/jimmykuu/gopher/models"
)

var (
analyticsCode template.HTML // 网站统计分析代码
shareCode template.HTML // 分享代码
utils1 "github.com/jimmykuu/gopher/utils"
)

func init() {
conf.Version = Version

err := conf.InitConfig("etc/config.json")
// 加载敏感词
file, err := os.Open("etc/sensitive_words.txt")
if err != nil {
panic(err)
}
conf.DirtyManager = utils1.NewDirtyManager(file)

err = conf.InitConfig("etc/config.json")
if err != nil {
fmt.Println(err)
os.Exit(1)
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
)

const (
Version = "2.0.2"
Version = "2.0.3"
)

var (
Expand Down
57 changes: 57 additions & 0 deletions utils/dirtyfilter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package utils

import (
"bufio"
"fmt"
"io"
"strings"

filter "github.com/antlinker/go-dirtyfilter"
"github.com/antlinker/go-dirtyfilter/store"

"github.com/jimmykuu/gopher/conf"
)

// NewDirtyManager 新建敏感词管理器
func NewDirtyManager(reader io.Reader) *filter.DirtyManager {
buf := bufio.NewReader(reader)
var words = []string{}
for {
line, err := buf.ReadString('\n')
line = strings.TrimSpace(line)

if line != "" {
words = append(words, line)
}

if err != nil {
if err == io.EOF {
fmt.Println("File read ok!")
break
}
}
}

memStore, err := store.NewMemoryStore(store.MemoryConfig{
DataSource: words,
})
if err != nil {
panic(err)
}

return filter.NewDirtyManager(memStore)
}

// HasSensitiveWords 是否敏感词
func HasSensitiveWords(text string) bool {
result, err := conf.DirtyManager.Filter().Filter(text, '*', '@')
if err != nil {
fmt.Println(err)

return true
}

fmt.Println(result)

return len(result) > 0
}

0 comments on commit c143ad6

Please sign in to comment.