代码拉取完成,页面将自动刷新
// 敏感词库
package sensitive
import (
"encoding/json"
"errors"
"gitee.com/h79/goutils/common/logger"
"io"
"net/http"
"sync"
"time"
)
type sensMgr struct {
locker sync.Mutex
regexp bool
filterMap map[string]*Filter
}
var mgr *sensMgr
const KDefGroup = "def"
func Init(reg bool) {
mgr = &sensMgr{
regexp: reg,
filterMap: make(map[string]*Filter),
}
Add(KDefGroup)
}
func Add(group string) *Filter {
mgr.locker.Lock()
defer mgr.locker.Unlock()
if filter, ok := mgr.filterMap[group]; ok {
return filter
}
filter := New(mgr.regexp)
mgr.filterMap[group] = filter
return filter
}
func Get(group string) *Filter {
mgr.locker.Lock()
defer mgr.locker.Unlock()
if filter, ok := mgr.filterMap[group]; ok {
return filter
}
return nil
}
func Load(group string, path string) error {
filter := Add(group)
if filter == nil {
return errors.New("filter not exist")
}
if err := filter.LoadWordDict(path); err != nil {
logger.Error("sensitive load, path= '%s'", path)
return err
}
return nil
}
func LoadNetworkFileByGroup(group string, url string) error {
filter := Add(group)
if filter == nil {
return errors.New("filter not exist")
}
if err := filter.LoadWordDictByNetworkFile(url); err != nil {
logger.Error("sensitive load, url= '%s'", url)
return err
}
return nil
}
// LoadUrl
// {"code":200,"data":{"title":[],"":[]}}
func LoadUrl(url string) error {
c := http.Client{
Timeout: 5 * time.Second,
}
rsp, err := c.Get(url)
if err != nil {
logger.Error("sensitive Loaded url=%s, err=%+v", url, err)
return err
}
defer rsp.Body.Close()
out, er := io.ReadAll(rsp.Body)
if er != nil {
logger.Error("sensitive read url=%s, err=%+v", url, er)
return err
}
logger.Debug("sensitive data=%v ", logger.Byte2(logger.DebugLevel, out))
type Result struct {
Code int `json:"code"`
Data map[string][]string `json:"data"`
}
d := Result{}
if er = json.Unmarshal(out, &d); er != nil {
logger.Error("sensitive read url=%s, err=%+v", url, er)
return err
}
if len(d.Data) == 0 {
return nil
}
for k, v := range d.Data {
filter := Add(k)
filter.AddWord(v...)
}
return nil
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。