优化处理消息逻辑;修复删除问题

This commit is contained in:
samwaf
2022-12-01 22:58:57 +08:00
parent 6189ffa2a2
commit 94d6c68a70
19 changed files with 160 additions and 61 deletions

View File

@@ -1,10 +1,12 @@
package api
import (
"SamWaf/enums"
"SamWaf/global"
"SamWaf/model"
"SamWaf/model/common/response"
"SamWaf/model/request"
"SamWaf/model/spec"
"errors"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
@@ -106,8 +108,11 @@ func (w *WafAntiCCApi) ModifyAntiCCApi(c *gin.Context) {
func (w *WafAntiCCApi) NotifyWaf(host_code string) {
var antiCC model.AntiCC
global.GWAF_LOCAL_DB.Debug().Where("host_code = ? ", host_code).Limit(1).Find(&antiCC)
if antiCC.Id != "" {
global.GWAF_CHAN_ANTICC <- antiCC
var chanInfo = spec.ChanCommonHost{
HostCode: host_code,
Type: enums.ChanTypeAnticc,
Content: antiCC,
}
global.GWAF_CHAN_MSG <- chanInfo
}

View File

@@ -1,10 +1,12 @@
package api
import (
"SamWaf/enums"
"SamWaf/global"
"SamWaf/model"
"SamWaf/model/common/response"
"SamWaf/model/request"
"SamWaf/model/spec"
"errors"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
@@ -74,7 +76,7 @@ func (w *WafBlockIpApi) DelBlockIpApi(c *gin.Context) {
response.FailWithMessage("发生错误", c)
} else {
w.NotifyWaf(bean.HostCode)
response.FailWithMessage("删除成功", c)
response.OkWithMessage("删除成功", c)
}
} else {
@@ -106,5 +108,10 @@ func (w *WafBlockIpApi) ModifyBlockIpApi(c *gin.Context) {
func (w *WafBlockIpApi) NotifyWaf(host_code string) {
var ipWhites []model.IPBlockList
global.GWAF_LOCAL_DB.Debug().Where("host_code = ? ", host_code).Find(&ipWhites)
global.GWAF_CHAN_IpBlock <- ipWhites
var chanInfo = spec.ChanCommonHost{
HostCode: host_code,
Type: enums.ChanTypeBlockIP,
Content: ipWhites,
}
global.GWAF_CHAN_MSG <- chanInfo
}

View File

@@ -1,10 +1,12 @@
package api
import (
"SamWaf/enums"
"SamWaf/global"
"SamWaf/model"
"SamWaf/model/common/response"
"SamWaf/model/request"
"SamWaf/model/spec"
"errors"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
@@ -74,7 +76,7 @@ func (w *WafBlockUrlApi) DelBlockUrlApi(c *gin.Context) {
response.FailWithMessage("发生错误", c)
} else {
w.NotifyWaf(bean.HostCode)
response.FailWithMessage("删除成功", c)
response.OkWithMessage("删除成功", c)
}
} else {
@@ -106,5 +108,10 @@ func (w *WafBlockUrlApi) ModifyBlockUrlApi(c *gin.Context) {
func (w *WafBlockUrlApi) NotifyWaf(host_code string) {
var urlWhites []model.URLBlockList
global.GWAF_LOCAL_DB.Debug().Where("host_code = ? ", host_code).Find(&urlWhites)
global.GWAF_CHAN_UrlBlock <- urlWhites
var chanInfo = spec.ChanCommonHost{
HostCode: host_code,
Type: enums.ChanTypeBlockURL,
Content: urlWhites,
}
global.GWAF_CHAN_MSG <- chanInfo
}

View File

@@ -125,3 +125,18 @@ func (w *WafHostAPi) ModifyGuardStatusApi(c *gin.Context) {
response.FailWithMessage("解析失败", c)
}
}
/*
*
通知到waf引擎实时生效(TODO 此处如果是删除 应该是解除所有相关的,如果是新增编辑等?)
*/
func (w *WafHostAPi) NotifyWaf(host_code string) {
/*var idpUrls []model.Hosts
global.GWAF_LOCAL_DB.Debug().Where("host_code = ? ", host_code).Find(&idpUrls)
var chanInfo = spec.ChanCommonHost{
HostCode: host_code,
Type: enums.ChanTypeHost,
Content: idpUrls,
}
global.GWAF_CHAN_MSG <- chanInfo*/
}

View File

@@ -1,10 +1,12 @@
package api
import (
"SamWaf/enums"
"SamWaf/global"
"SamWaf/model"
"SamWaf/model/common/response"
"SamWaf/model/request"
"SamWaf/model/spec"
"errors"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
@@ -74,7 +76,7 @@ func (w *WafLdpUrlApi) DelLdpUrlApi(c *gin.Context) {
response.FailWithMessage("发生错误", c)
} else {
w.NotifyWaf(bean.HostCode)
response.FailWithMessage("删除成功", c)
response.OkWithMessage("删除成功", c)
}
} else {
@@ -106,5 +108,10 @@ func (w *WafLdpUrlApi) ModifyLdpUrlApi(c *gin.Context) {
func (w *WafLdpUrlApi) NotifyWaf(host_code string) {
var idpUrls []model.LDPUrl
global.GWAF_LOCAL_DB.Debug().Where("host_code = ? ", host_code).Find(&idpUrls)
global.GWAF_CHAN_LdpUrl <- idpUrls
var chanInfo = spec.ChanCommonHost{
HostCode: host_code,
Type: enums.ChanTypeLdp,
Content: idpUrls,
}
global.GWAF_CHAN_MSG <- chanInfo
}

View File

@@ -1,10 +1,12 @@
package api
import (
"SamWaf/enums"
"SamWaf/global"
"SamWaf/model"
"SamWaf/model/common/response"
"SamWaf/model/request"
"SamWaf/model/spec"
"SamWaf/utils"
"errors"
"github.com/gin-gonic/gin"
@@ -164,5 +166,10 @@ func (w *WafRuleAPi) ModifyRuleApi(c *gin.Context) {
func (w *WafRuleAPi) NotifyWaf(host_code string) {
var ruleconfig []model.Rules
global.GWAF_LOCAL_DB.Debug().Where("host_code = ? ", host_code).Find(&ruleconfig)
global.GWAF_CHAN_RULE <- ruleconfig
var chanInfo = spec.ChanCommonHost{
HostCode: host_code,
Type: enums.ChanTypeRule,
Content: ruleconfig,
}
global.GWAF_CHAN_MSG <- chanInfo
}

View File

@@ -1,10 +1,12 @@
package api
import (
"SamWaf/enums"
"SamWaf/global"
"SamWaf/model"
"SamWaf/model/common/response"
"SamWaf/model/request"
"SamWaf/model/spec"
"errors"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
@@ -74,7 +76,7 @@ func (w *WafWhiteIpApi) DelWhiteIpApi(c *gin.Context) {
response.FailWithMessage("发生错误", c)
} else {
w.NotifyWaf(bean.HostCode)
response.FailWithMessage("删除成功", c)
response.OkWithMessage("删除成功", c)
}
} else {
@@ -106,5 +108,10 @@ func (w *WafWhiteIpApi) ModifyWhiteIpApi(c *gin.Context) {
func (w *WafWhiteIpApi) NotifyWaf(host_code string) {
var ipWhites []model.IPWhiteList
global.GWAF_LOCAL_DB.Debug().Where("host_code = ? ", host_code).Find(&ipWhites)
global.GWAF_CHAN_IpWhite <- ipWhites
var chanInfo = spec.ChanCommonHost{
HostCode: host_code,
Type: enums.ChanTypeWhiteIP,
Content: ipWhites,
}
global.GWAF_CHAN_MSG <- chanInfo
}

View File

@@ -1,10 +1,12 @@
package api
import (
"SamWaf/enums"
"SamWaf/global"
"SamWaf/model"
"SamWaf/model/common/response"
"SamWaf/model/request"
"SamWaf/model/spec"
"errors"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
@@ -74,7 +76,7 @@ func (w *WafWhiteUrlApi) DelWhiteUrlApi(c *gin.Context) {
response.FailWithMessage("发生错误", c)
} else {
w.NotifyWaf(bean.HostCode)
response.FailWithMessage("删除成功", c)
response.OkWithMessage("删除成功", c)
}
} else {
@@ -106,5 +108,10 @@ func (w *WafWhiteUrlApi) ModifyWhiteUrlApi(c *gin.Context) {
func (w *WafWhiteUrlApi) NotifyWaf(host_code string) {
var urlWhites []model.URLWhiteList
global.GWAF_LOCAL_DB.Debug().Where("host_code = ? ", host_code).Find(&urlWhites)
global.GWAF_CHAN_UrlWhite <- urlWhites
var chanInfo = spec.ChanCommonHost{
HostCode: host_code,
Type: enums.ChanTypeWhiteURL,
Content: urlWhites,
}
global.GWAF_CHAN_MSG <- chanInfo
}

12
enums/chan_enum.go Normal file
View File

@@ -0,0 +1,12 @@
package enums
const (
ChanTypeHost = iota
ChanTypeRule
ChanTypeAnticc
ChanTypeLdp
ChanTypeWhiteIP
ChanTypeWhiteURL
ChanTypeBlockIP
ChanTypeBlockURL
)

View File

@@ -2,6 +2,7 @@ package global
import (
"SamWaf/model"
"SamWaf/model/spec"
"github.com/bytedance/godlp/dlpheader"
"gorm.io/gorm"
"time"
@@ -27,8 +28,11 @@ var (
GWAF_CHAN_RULE = make(chan []model.Rules, 10) //规则链
GWAF_CHAN_ANTICC = make(chan model.AntiCC, 10) //抵御CC链
GWAF_CHAN_UrlWhite = make(chan []model.URLWhiteList, 10) //URL白名单链
GWAF_CHAN_IpWhite = make(chan []model.IPWhiteList, 10) //IP白名单链
GWAF_CHAN_IpWhite = make(chan spec.ChanCommonHost, 10) //IP白名单链
GWAF_CHAN_LdpUrl = make(chan []model.LDPUrl, 10) //URL隐私保护链
GWAF_CHAN_UrlBlock = make(chan []model.URLBlockList, 10) //URL阻止链
GWAF_CHAN_IpBlock = make(chan []model.IPBlockList, 10) //IP阻止链
GWAF_CHAN_IpBlock = make(chan spec.ChanCommonHost, 10) //IP阻止链
GWAF_CHAN_MSG = make(chan spec.ChanCommonHost, 10) //全局通讯工具
)

View File

@@ -456,7 +456,7 @@
}
})
.then((res) => {
let resdata = res.data
let resdata = res
console.log(resdata)
if (resdata.code === 0) {

View File

@@ -431,9 +431,8 @@
id: id,
}
})
.then((res) => {
let resdata = res.data
console.log(resdata)
.then((res) => {
let resdata = res
if (resdata.code === 0) {
that.pagination.current = 1

View File

@@ -432,7 +432,7 @@
}
})
.then((res) => {
let resdata = res.data
let resdata = res
console.log(resdata)
if (resdata.code === 0) {

View File

@@ -473,7 +473,7 @@
}
})
.then((res) => {
let resdata = res.data
let resdata = res
console.log(resdata)
if (resdata.code === 0) {

View File

@@ -432,7 +432,7 @@
}
})
.then((res) => {
let resdata = res.data
let resdata = res
console.log(resdata)
if (resdata.code === 0) {

View File

@@ -432,7 +432,7 @@
}
})
.then((res) => {
let resdata = res.data
let resdata = res
console.log(resdata)
if (resdata.code === 0) {

67
main.go
View File

@@ -1,7 +1,9 @@
package main
import (
"SamWaf/enums"
"SamWaf/global"
"SamWaf/model"
"SamWaf/plugin"
"SamWaf/utils/zlog"
dlp "github.com/bytedance/godlp"
@@ -61,36 +63,41 @@ func main() {
global.GWAF_DLP.ApplyConfigDefault()
for {
select {
case remoteConfig := <-global.GWAF_CHAN_RULE:
//TODO 需要把删除的那部分数据从数据口里面去掉
hostTarget[hostCode[remoteConfig[0].HostCode]].RuleData = remoteConfig
hostTarget[hostCode[remoteConfig[0].HostCode]].Rule.LoadRules(remoteConfig)
zlog.Debug("远程配置", zap.Any("remoteConfig", remoteConfig))
break
case remoteAntiCC := <-global.GWAF_CHAN_ANTICC:
hostTarget[hostCode[remoteAntiCC.HostCode]].pluginIpRateLimiter = plugin.NewIPRateLimiter(rate.Limit(remoteAntiCC.Rate), remoteAntiCC.Limit)
zlog.Debug("远程配置", zap.Any("remoteAntiCC", remoteAntiCC))
break
case remoteUrlWhite := <-global.GWAF_CHAN_UrlWhite:
hostTarget[hostCode[remoteUrlWhite[0].HostCode]].UrlWhiteLists = remoteUrlWhite
zlog.Debug("远程配置", zap.Any("UrlWhiteLists", remoteUrlWhite))
break
case remoteIpWhite := <-global.GWAF_CHAN_IpWhite:
hostTarget[hostCode[remoteIpWhite[0].HostCode]].IPWhiteLists = remoteIpWhite
zlog.Debug("远程配置", zap.Any("IPWhiteLists", remoteIpWhite))
break
case remoteLdpUrls := <-global.GWAF_CHAN_LdpUrl:
hostTarget[hostCode[remoteLdpUrls[0].HostCode]].LdpUrlLists = remoteLdpUrls
zlog.Debug("远程配置", zap.Any("LdpUrlLists", remoteLdpUrls))
break
case remoteUrlBlock := <-global.GWAF_CHAN_UrlBlock:
hostTarget[hostCode[remoteUrlBlock[0].HostCode]].UrlBlockLists = remoteUrlBlock
zlog.Debug("远程配置", zap.Any("UrlBlockLists", remoteUrlBlock))
break
case remoteIpBlock := <-global.GWAF_CHAN_IpBlock:
hostTarget[hostCode[remoteIpBlock[0].HostCode]].IPBlockLists = remoteIpBlock
zlog.Debug("远程配置", zap.Any("IPBlockLists", remoteIpBlock))
break
case msg := <-global.GWAF_CHAN_MSG:
switch msg.Type {
case enums.ChanTypeWhiteIP:
hostTarget[hostCode[msg.HostCode]].IPWhiteLists = msg.Content.([]model.IPWhiteList)
zlog.Debug("远程配置", zap.Any("IPWhiteLists", msg.Content.([]model.IPWhiteList)))
break
case enums.ChanTypeWhiteURL:
hostTarget[hostCode[msg.HostCode]].UrlWhiteLists = msg.Content.([]model.URLWhiteList)
zlog.Debug("远程配置", zap.Any("UrlWhiteLists", msg.Content.([]model.URLWhiteList)))
break
case enums.ChanTypeBlockIP:
hostTarget[hostCode[msg.HostCode]].IPBlockLists = msg.Content.([]model.IPBlockList)
zlog.Debug("远程配置", zap.Any("IPBlockLists", msg))
break
case enums.ChanTypeBlockURL:
hostTarget[hostCode[msg.HostCode]].UrlBlockLists = msg.Content.([]model.URLBlockList)
zlog.Debug("远程配置", zap.Any("UrlBlockLists", msg.Content.([]model.URLBlockList)))
break
case enums.ChanTypeLdp:
hostTarget[hostCode[msg.HostCode]].LdpUrlLists = msg.Content.([]model.LDPUrl)
zlog.Debug("远程配置", zap.Any("LdpUrlLists", msg.Content.([]model.LDPUrl)))
break
case enums.ChanTypeRule:
hostTarget[hostCode[msg.HostCode]].RuleData = msg.Content.([]model.Rules)
hostTarget[hostCode[msg.HostCode]].Rule.LoadRules(msg.Content.([]model.Rules))
zlog.Debug("远程配置", zap.Any("Rule", msg.Content.([]model.Rules)))
break
case enums.ChanTypeAnticc:
hostTarget[hostCode[msg.HostCode]].pluginIpRateLimiter = plugin.NewIPRateLimiter(rate.Limit(msg.Content.(model.AntiCC).Rate), msg.Content.(model.AntiCC).Limit)
zlog.Debug("远程配置", zap.Any("Anticc", msg.Content.(model.AntiCC)))
break
case enums.ChanTypeHost: //此处待定
break
} //end switch
case engineStatus := <-global.GWAF_CHAN_ENGINE:
if engineStatus == 1 {
zlog.Info("准备关闭WAF引擎")

View File

@@ -0,0 +1,7 @@
package spec
type ChanCommonHost struct {
HostCode string
Type int
Content interface{}
}

View File

@@ -186,8 +186,8 @@ func (h *baseHandle) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if hostTarget[host].IPBlockLists != nil {
for i := 0; i < len(hostTarget[host].IPBlockLists); i++ {
if hostTarget[host].IPBlockLists[i].Ip == weblogbean.SRC_IP {
jumpGuardFlag = true
break
EchoErrorInfo(w, r, weblogbean, "IP黑名单", "您的访问被阻止了IP限制")
return
}
}
}
@@ -195,8 +195,8 @@ func (h *baseHandle) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if hostTarget[host].UrlBlockLists != nil {
for i := 0; i < len(hostTarget[host].UrlBlockLists); i++ {
if hostTarget[host].UrlBlockLists[i].Url == weblogbean.URL {
jumpGuardFlag = true
break
EchoErrorInfo(w, r, weblogbean, "URL黑名单", "您的访问被阻止了URL限制")
return
}
}
}
@@ -209,8 +209,9 @@ func (h *baseHandle) ServeHTTP(w http.ResponseWriter, r *http.Request) {
weblogbean.RULE = "触发IP频次访问限制"
weblogbean.ACTION = "阻止"
global.GWAF_LOCAL_DB.Create(weblogbean)
w.Write([]byte("<html><head><title>您的访问被阻止</title></head><body><center><h1>您的访问被阻止超量了</h1> <br> 访问识别码:<h3>" + weblogbean.REQ_UUID + "</h3></center></body> </html>"))
zlog.Debug("触发IP频次访问限制 已经被限制访问了")
//w.Write([]byte("<html><head><title>您的访问被阻止</title></head><body><center><h1>您的访问被阻止超量了</h1> <br> 访问识别码:<h3>" + weblogbean.REQ_UUID + "</h3></center></body> </html>"))
//zlog.Debug("触发IP频次访问限制 已经被限制访问了")
EchoErrorInfo(w, r, weblogbean, "触发IP频次访问限制", "您的访问被阻止超量了")
return
}
}
@@ -231,8 +232,8 @@ func (h *baseHandle) ServeHTTP(w http.ResponseWriter, r *http.Request) {
expiration = expiration.AddDate(1, 0, 0)
cookie := http.Cookie{Name: "IDENFY", Value: weblogbean.REQ_UUID, Expires: expiration}
http.SetCookie(w, &cookie)*/
w.Write([]byte("<html><head><title>您的访问被阻止</title></head><body><center><h1>您的访问被阻止触发规则</h1> <br> 访问识别码:<h3>" + weblogbean.REQ_UUID + "</h3></center></body> </html>"))
//w.Write([]byte("<html><head><title>您的访问被阻止</title></head><body><center><h1>您的访问被阻止触发规则</h1> <br> 访问识别码:<h3>" + weblogbean.REQ_UUID + "</h3></center></body> </html>"))
EchoErrorInfo(w, r, weblogbean, rulestr, "您的访问被阻止触发规则")
return
}
} else {
@@ -268,6 +269,13 @@ func (h *baseHandle) ServeHTTP(w http.ResponseWriter, r *http.Request) {
global.GWAF_LOCAL_DB.Create(weblogbean)
}
}
func EchoErrorInfo(w http.ResponseWriter, r *http.Request, weblogbean innerbean.WebLog, ruleName string, blockInfo string) {
weblogbean.RULE = ruleName
weblogbean.ACTION = "阻止"
global.GWAF_LOCAL_DB.Create(weblogbean)
w.Write([]byte("<html><head><title>您的访问被阻止</title></head><body><center><h1>" + blockInfo + "</h1> <br> 访问识别码:<h3>" + weblogbean.REQ_UUID + "</h3></center></body> </html>"))
zlog.Debug(ruleName)
}
func errorHandler() func(http.ResponseWriter, *http.Request, error) {
return func(w http.ResponseWriter, req *http.Request, err error) {
zlog.Debug("Got error response:", zap.Any("err", err))