feat:增加通知频率控制

This commit is contained in:
samwaf
2023-08-24 14:24:30 +08:00
parent cf1dc95127
commit e9438a1d4f
5 changed files with 36 additions and 7 deletions

4
alert/alert.go Normal file
View File

@@ -0,0 +1,4 @@
package alert
type AlertInfo struct {
}

10
cache/waf_cache.go vendored
View File

@@ -52,6 +52,13 @@ func (wafCache *WafCache) GetString(key string) (string, error) {
}
return "", errors.New("数据不存在")
}
func (wafCache *WafCache) GetInt(key string) (int, error) {
key1Value := wafCache.Get(key)
if str, ok := key1Value.(int); ok {
return str, nil
}
return -1, errors.New("数据不存在")
}
func (wafCache *WafCache) IsKeyExist(key string) bool {
wafCache.mu.Lock()
defer wafCache.mu.Unlock()
@@ -92,17 +99,14 @@ func (wafCache *WafCache) GetLastTime(key string) (time.Time, error) {
return time.Time{}, errors.New("数据已过期")
}
func (wafCache *WafCache) ClearExpirationCache() {
println("准备检测过期键")
now := time.Now()
for key, item := range wafCache.cache {
if now.Sub(item.createTime) > item.ttl {
println("删除" + key)
delete(wafCache.cache, key)
}
}
}
func (wafCache *WafCache) ClearExpirationCacheRoutine() {
println("启动协程检测过期的")
ticker := time.NewTicker(60 * time.Second)
defer ticker.Stop()
for range ticker.C {

View File

@@ -1,6 +1,7 @@
package global
import (
"SamWaf/cache"
"SamWaf/model"
"SamWaf/model/spec"
"github.com/bytedance/godlp/dlpheader"
@@ -40,8 +41,10 @@ var (
GWAF_CHAN_MSG = make(chan spec.ChanCommonHost, 10) //全局通讯包
GCACHE_WECHAT_ACCESS string = "" //微信访问密钥
GCACHE_IP_CBUFF []byte // IP相关缓存
/*****CACHE相关*********/
GCACHE_WAFCACHE *cache.WafCache //cache
GCACHE_WECHAT_ACCESS string = "" //微信访问密钥
GCACHE_IP_CBUFF []byte // IP相关缓存
GDATA_DELETE_INTERVAL = 100 // 删除100天前的数据

View File

@@ -1,6 +1,7 @@
package main
import (
"SamWaf/cache"
"SamWaf/enums"
"SamWaf/global"
"SamWaf/innerbean"
@@ -47,7 +48,8 @@ func (m *wafSystenService) Stop(s service.Service) error {
func (m *wafSystenService) run() {
// 在这里编写你的服务逻辑代码
fmt.Println("Service is running...")
//初始化cache
global.GCACHE_WAFCACHE = cache.InitWafCache()
rversion := "初始化系统 版本号:" + global.GWAF_RELEASE_VERSION_NAME + "(" + global.GWAF_RELEASE_VERSION + ")"
if global.GWAF_RELEASE == "false" {
rversion = rversion + " 调试版本"

View File

@@ -50,10 +50,26 @@ func ProcessDequeEngine() {
for !global.GQEQUE_MESSAGE_DB.Empty() {
messageinfo := global.GQEQUE_MESSAGE_DB.PopFront().(interface{})
isCanSend := false
switch messageinfo.(type) {
case innerbean.RuleMessageInfo:
rulemessage := messageinfo.(innerbean.RuleMessageInfo)
utils.NotifyHelperApp.SendRuleInfo(rulemessage)
couter := 1
if global.GCACHE_WAFCACHE.IsKeyExist(rulemessage.RuleInfo) {
hitCounter, isok := global.GCACHE_WAFCACHE.GetInt(rulemessage.RuleInfo)
if isok == nil {
if hitCounter == 3 || hitCounter == 30 {
isCanSend = true
}
couter = couter + 1
}
} else {
isCanSend = true
}
global.GCACHE_WAFCACHE.SetWithTTl(rulemessage.RuleInfo, 1, 30*time.Minute)
if isCanSend {
utils.NotifyHelperApp.SendRuleInfo(rulemessage)
}
if rulemessage.BaseMessageInfo.OperaType == "命中保护规则" {
//发送websocket
for _, ws := range global.GWebSocket {