From c143ad6744ca1e294fd1a0593da946a81642fa70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A1=BE=E5=BF=97=E6=98=8E?= Date: Thu, 26 Sep 2019 16:54:27 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E5=85=A5=E6=95=8F=E6=84=9F=E8=AF=8D?= =?UTF-8?q?=E8=BF=87=E6=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apis/topic.go | 41 ++++++++++++++++++++--------- conf/config.go | 2 ++ etc/sensitive_words.txt | 2 ++ go.mod | 2 ++ go.sum | 4 +++ init.go | 15 ++++++----- main.go | 2 +- utils/dirtyfilter.go | 57 +++++++++++++++++++++++++++++++++++++++++ 8 files changed, 106 insertions(+), 19 deletions(-) create mode 100644 etc/sensitive_words.txt create mode 100644 utils/dirtyfilter.go diff --git a/apis/topic.go b/apis/topic.go index ab13803..a7f13ac 100644 --- a/apis/topic.go +++ b/apis/topic.go @@ -9,6 +9,7 @@ import ( "gopkg.in/mgo.v2/bson" "github.com/jimmykuu/gopher/models" + "github.com/jimmykuu/gopher/utils" ) // Topic 主题 @@ -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() @@ -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{}{ @@ -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) diff --git a/conf/config.go b/conf/config.go index 9f1eb38..06ba3ce 100644 --- a/conf/config.go +++ b/conf/config.go @@ -6,6 +6,7 @@ import ( "os" "runtime" + filter "github.com/antlinker/go-dirtyfilter" "github.com/lunny/tango" ) @@ -39,6 +40,7 @@ var ( TangoVersion = tango.Version() Version string AnalyticsCode template.HTML // 网站统计分析代码 + DirtyManager *filter.DirtyManager ) func InitConfig(configFile string) error { diff --git a/etc/sensitive_words.txt b/etc/sensitive_words.txt new file mode 100644 index 0000000..2fec3b1 --- /dev/null +++ b/etc/sensitive_words.txt @@ -0,0 +1,2 @@ +学历证书 +毕业证 \ No newline at end of file diff --git a/go.mod b/go.mod index c386435..8787da2 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 0eb1f52..b6867cc 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/init.go b/init.go index f61275b..21a5319 100644 --- a/init.go +++ b/init.go @@ -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) diff --git a/main.go b/main.go index e93eb6d..b1b4113 100644 --- a/main.go +++ b/main.go @@ -23,7 +23,7 @@ import ( ) const ( - Version = "2.0.2" + Version = "2.0.3" ) var ( diff --git a/utils/dirtyfilter.go b/utils/dirtyfilter.go new file mode 100644 index 0000000..b9b3551 --- /dev/null +++ b/utils/dirtyfilter.go @@ -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 +}