From 6fd146cc4299feffd3e204c0681147fea235c110 Mon Sep 17 00:00:00 2001 From: samwaf Date: Tue, 1 Nov 2022 18:01:19 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B4=E4=BD=93=E4=B8=9A=E5=8A=A1=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E9=87=8D=E6=96=B0=E6=A2=B3=E7=90=86=E5=B0=81=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/SamWaf.iml | 1 + api/entrance.go | 19 + api/waf_engine.go | 16 + api/waf_host.go | 110 ++++ api/waf_log.go | 36 ++ api/waf_rule.go | 138 +++++ api/waf_stat.go | 15 + global/global.go | 19 +- localserver.go | 585 +----------------- .../dashboard/base/components/TopPanel.vue | 4 +- .../src/pages/waf-host/host/detail/index.vue | 4 +- localwaf/src/pages/waf-host/host/index.vue | 24 +- .../src/pages/waf-host/rule/edit/index.vue | 8 +- localwaf/src/pages/waf-host/rule/index.vue | 20 +- .../src/pages/waf-log/attack/detail/index.vue | 8 +- localwaf/src/pages/waf-log/attack/index.vue | 4 +- main.go | 2 +- model/common/response/response.go | 47 ++ router/entrance.go | 11 + router/waf_engine.go | 16 + router/waf_host.go | 20 + router/waf_log.go | 16 + router/waf_rule.go | 19 + router/waf_stat.go | 15 + service/waf_service/entrance.go | 7 + service/waf_service/waf_host.go | 101 +++ service/waf_service/waf_log.go | 32 + service/waf_service/waf_rule.go | 102 +++ service/waf_service/waf_stat.go | 28 + wafengine.go | 12 +- 30 files changed, 814 insertions(+), 625 deletions(-) create mode 100644 api/entrance.go create mode 100644 api/waf_engine.go create mode 100644 api/waf_host.go create mode 100644 api/waf_log.go create mode 100644 api/waf_rule.go create mode 100644 api/waf_stat.go create mode 100644 router/entrance.go create mode 100644 router/waf_engine.go create mode 100644 router/waf_host.go create mode 100644 router/waf_log.go create mode 100644 router/waf_rule.go create mode 100644 router/waf_stat.go create mode 100644 service/waf_service/entrance.go create mode 100644 service/waf_service/waf_host.go create mode 100644 service/waf_service/waf_log.go create mode 100644 service/waf_service/waf_rule.go create mode 100644 service/waf_service/waf_stat.go diff --git a/.idea/SamWaf.iml b/.idea/SamWaf.iml index c956989..5e764c4 100644 --- a/.idea/SamWaf.iml +++ b/.idea/SamWaf.iml @@ -1,5 +1,6 @@ + diff --git a/api/entrance.go b/api/entrance.go new file mode 100644 index 0000000..312a1a2 --- /dev/null +++ b/api/entrance.go @@ -0,0 +1,19 @@ +package api + +import "SamWaf/service/waf_service" + +type APIGroup struct { + WafHostAPi + WafStatApi + WafLogAPi + WafRuleAPi + WafEngineApi +} + +var APIGroupAPP = new(APIGroup) +var ( + wafHostService = waf_service.WafHostServiceApp + wafLogService = waf_service.WafLogServiceApp + wafStatService = waf_service.WafStatServiceApp + wafRuleService = waf_service.WafRuleServiceApp +) diff --git a/api/waf_engine.go b/api/waf_engine.go new file mode 100644 index 0000000..f644a88 --- /dev/null +++ b/api/waf_engine.go @@ -0,0 +1,16 @@ +package api + +import ( + "SamWaf/global" + "SamWaf/model/common/response" + "github.com/gin-gonic/gin" +) + +type WafEngineApi struct { +} + +func (w *WafEngineApi) ResetWaf(c *gin.Context) { + //重启WAF引擎 + global.GWAF_CHAN_ENGINE <- 1 + response.OkWithMessage("重启指令发起成功", c) +} diff --git a/api/waf_host.go b/api/waf_host.go new file mode 100644 index 0000000..f8213cc --- /dev/null +++ b/api/waf_host.go @@ -0,0 +1,110 @@ +package api + +import ( + "SamWaf/global" + "SamWaf/model/common/response" + "SamWaf/model/request" + "errors" + "github.com/gin-gonic/gin" + "gorm.io/gorm" +) + +type WafHostAPi struct { +} + +func (w *WafHostAPi) AddApi(c *gin.Context) { + var req request.WafHostAddReq + err := c.ShouldBind(&req) + if err == nil { + err = wafHostService.CheckIsExistApi(req) + if err != nil { + response.FailWithMessage("当前网站和端口已经存在", c) + } + err = wafHostService.AddApi(req) + if err == nil { + + response.OkWithMessage("添加成功", c) + } else { + + response.FailWithMessage("添加失败", c) + } + } else { + response.FailWithMessage("解析失败", c) + } +} +func (w *WafHostAPi) GetDetailApi(c *gin.Context) { + var req request.WafHostDetailReq + err := c.ShouldBind(&req) + if err == nil { + wafHost := wafHostService.GetDetailApi(req) + response.OkWithDetailed(wafHost, "获取成功", c) + } else { + response.FailWithMessage("解析失败", c) + } +} +func (w *WafHostAPi) GetListApi(c *gin.Context) { + var req request.WafHostSearchReq + err := c.ShouldBind(&req) + if err == nil { + wafHosts, total, _ := wafHostService.GetListApi(req) + response.OkWithDetailed(response.PageResult{ + List: wafHosts, + Total: total, + PageIndex: req.PageIndex, + PageSize: req.PageSize, + }, "获取成功", c) + } else { + response.FailWithMessage("解析失败", c) + } +} +func (w *WafHostAPi) DelHostApi(c *gin.Context) { + var req request.WafHostDelReq + err := c.ShouldBind(&req) + if err == nil { + err = wafHostService.DelHostApi(req) + if err != nil && errors.Is(err, gorm.ErrRecordNotFound) { + response.FailWithMessage("请检测参数", c) + } else if err != nil { + response.FailWithMessage("发生错误", c) + } else { + response.FailWithMessage("删除成功", c) + } + + } else { + response.FailWithMessage("解析失败", c) + } +} + +func (w *WafHostAPi) ModifyHostApi(c *gin.Context) { + var req request.WafHostEditReq + err := c.ShouldBind(&req) + if err == nil { + err = wafHostService.ModifyApi(req) + if err != nil { + response.FailWithMessage("编辑发生错误", c) + } else { + response.FailWithMessage("编辑成功", c) + } + + } else { + response.FailWithMessage("解析失败", c) + } +} +func (w *WafHostAPi) ModifyGuardStatusApi(c *gin.Context) { + var req request.WafHostGuardStatusReq + err := c.ShouldBind(&req) + if err == nil { + err = wafHostService.ModifyGuardStatusApi(req) + if err != nil { + response.FailWithMessage("更新状态发生错误", c) + } else { + wafHost := wafHostService.GetDetailByCodeApi(req.CODE) + //发送状态改变通知 + global.GWAF_CHAN_HOST <- wafHost + response.FailWithMessage("状态更新成功", c) + } + + } else { + response.FailWithMessage("解析失败", c) + } +} diff --git a/api/waf_log.go b/api/waf_log.go new file mode 100644 index 0000000..15b1e45 --- /dev/null +++ b/api/waf_log.go @@ -0,0 +1,36 @@ +package api + +import ( + "SamWaf/model/common/response" + "SamWaf/model/request" + "github.com/gin-gonic/gin" +) + +type WafLogAPi struct { +} + +func (w *WafLogAPi) GetDetailApi(c *gin.Context) { + var req request.WafAttackLogDetailReq + err := c.ShouldBind(&req) + if err == nil { + wafLog, _ := wafLogService.GetDetailApi(req) + response.OkWithDetailed(wafLog, "获取成功", c) + } else { + response.FailWithMessage("解析失败", c) + } +} +func (w *WafLogAPi) GetListApi(c *gin.Context) { + var req request.WafAttackLogSearch + err := c.ShouldBind(&req) + if err == nil { + wafLogs, total, _ := wafLogService.GetListApi(req) + response.OkWithDetailed(response.PageResult{ + List: wafLogs, + Total: total, + PageIndex: req.PageIndex, + PageSize: req.PageSize, + }, "获取成功", c) + } else { + response.FailWithMessage("解析失败", c) + } +} diff --git a/api/waf_rule.go b/api/waf_rule.go new file mode 100644 index 0000000..6a2e7a1 --- /dev/null +++ b/api/waf_rule.go @@ -0,0 +1,138 @@ +package api + +import ( + "SamWaf/model" + "SamWaf/model/common/response" + "SamWaf/model/request" + "SamWaf/utils" + "errors" + "github.com/gin-gonic/gin" + uuid "github.com/satori/go.uuid" + "gorm.io/gorm" + "strings" +) + +type WafRuleAPi struct { +} + +func (w *WafRuleAPi) AddApi(c *gin.Context) { + ruleHelper := &utils.RuleHelper{} + var req request.WafRuleAddReq + err := c.ShouldBind(&req) + if err == nil { + var ruleTool = model.RuleTool{} + ruleInfo, err := ruleTool.LoadRule(req.RuleJson) + if err != nil { + response.FailWithMessage("规则解析错误", c) + return + } + err = wafRuleService.CheckIsExistApi(ruleInfo.RuleBase.RuleName, ruleInfo.RuleBase.RuleDomainCode) + if err != nil { + response.FailWithMessage("当前规则名称已存在", c) + } + chsName := ruleInfo.RuleBase.RuleName + var ruleCode = uuid.NewV4().String() + ruleInfo.RuleBase.RuleName = strings.Replace(ruleCode, "-", "", -1) + + var ruleContent = ruleTool.GenRuleInfo(ruleInfo, chsName) + if req.IsManualRule == 1 { + ruleContent = ruleInfo.RuleContent + //检查规则是否合法 + err = ruleHelper.CheckRuleAvailable(ruleContent) + if err != nil { + response.FailWithMessage("规则校验失败", c) + return + } + } + + err = wafRuleService.AddApi(req, ruleCode, chsName, ruleInfo.RuleBase.RuleDomainCode, ruleContent) + if err == nil { + + response.OkWithMessage("添加成功", c) + } else { + + response.FailWithMessage("添加失败", c) + } + } else { + response.FailWithMessage("解析失败", c) + } +} +func (w *WafRuleAPi) GetDetailApi(c *gin.Context) { + var req request.WafRuleDetailReq + err := c.ShouldBind(&req) + if err == nil { + wafHost := wafRuleService.GetDetailApi(req) + response.OkWithDetailed(wafHost, "获取成功", c) + } else { + response.FailWithMessage("解析失败", c) + } +} +func (w *WafRuleAPi) GetListApi(c *gin.Context) { + var req request.WafRuleSearchReq + err := c.ShouldBind(&req) + if err == nil { + wafRules, total, _ := wafRuleService.GetListApi(req) + response.OkWithDetailed(response.PageResult{ + List: wafRules, + Total: total, + PageIndex: req.PageIndex, + PageSize: req.PageSize, + }, "获取成功", c) + } else { + response.FailWithMessage("解析失败", c) + } +} +func (w *WafRuleAPi) DelRuleApi(c *gin.Context) { + var req request.WafRuleDelReq + err := c.ShouldBind(&req) + if err == nil { + err = wafRuleService.DelRuleApi(req) + if err != nil && errors.Is(err, gorm.ErrRecordNotFound) { + response.FailWithMessage("请检测参数", c) + } else if err != nil { + response.FailWithMessage("发生错误", c) + } else { + response.FailWithMessage("删除成功", c) + } + + } else { + response.FailWithMessage("解析失败", c) + } +} + +func (w *WafRuleAPi) ModifyRuleApi(c *gin.Context) { + ruleHelper := &utils.RuleHelper{} + var req request.WafRuleEditReq + err := c.ShouldBind(&req) + if err == nil { + var ruleTool = model.RuleTool{} + ruleInfo, err := ruleTool.LoadRule(req.RuleJson) + if err != nil { + response.FailWithMessage("解析错误", c) + return + } + rule := wafRuleService.GetDetailByCodeApi(req.CODE) + var ruleName = ruleInfo.RuleBase.RuleName //中文名 + ruleInfo.RuleBase.RuleName = strings.Replace(rule.RuleCode, "-", "", -1) + var ruleContent = ruleTool.GenRuleInfo(ruleInfo, ruleName) + if req.IsManualRule == 1 { + ruleContent = ruleInfo.RuleContent + //检查规则是否合法 + err = ruleHelper.CheckRuleAvailable(ruleContent) + if err != nil { + response.FailWithMessage("规则校验失败", c) + return + } + } + + err = wafRuleService.ModifyApi(req, ruleName, ruleInfo.RuleBase.RuleDomainCode, ruleContent) + if err != nil { + response.FailWithMessage("编辑发生错误", c) + } else { + response.FailWithMessage("编辑成功", c) + } + + } else { + response.FailWithMessage("解析失败", c) + } +} diff --git a/api/waf_stat.go b/api/waf_stat.go new file mode 100644 index 0000000..0f46bfd --- /dev/null +++ b/api/waf_stat.go @@ -0,0 +1,15 @@ +package api + +import ( + "SamWaf/model/common/response" + "github.com/gin-gonic/gin" +) + +type WafStatApi struct { +} + +func (w *WafStatApi) StatHomeApi(c *gin.Context) { + + wafStat, _ := wafStatService.StatHomeApi() + response.OkWithDetailed(wafStat, "获取成功", c) +} diff --git a/global/global.go b/global/global.go index f6ef6a8..50bb794 100644 --- a/global/global.go +++ b/global/global.go @@ -1,6 +1,7 @@ package global import ( + "SamWaf/model" "github.com/bytedance/godlp/dlpheader" "gorm.io/gorm" "time" @@ -12,12 +13,14 @@ const ( ) var ( - GWAF_LOCAL_DB *gorm.DB //通用本地数据库,尊重用户隐私 - GWAF_REMOTE_DB *gorm.DB //仅当用户使用云数据库 - GWAF_LOCAL_SERVER_PORT int = 26666 // 本地local端口 - GWAF_USER_CODE string // 当前识别号 - GWAF_TENANT_ID string // 当前租户ID - GWAF_RELEASE bool = false // 当前是否为发行版 - GWAF_LAST_UPDATE_TIME time.Time // 上次时间 - GWAF_DLP dlpheader.EngineAPI // 脱敏引擎 + GWAF_LOCAL_DB *gorm.DB //通用本地数据库,尊重用户隐私 + GWAF_REMOTE_DB *gorm.DB //仅当用户使用云数据库 + GWAF_LOCAL_SERVER_PORT int = 26666 // 本地local端口 + GWAF_USER_CODE string // 当前识别号 + GWAF_TENANT_ID string // 当前租户ID + GWAF_RELEASE bool = false // 当前是否为发行版 + GWAF_LAST_UPDATE_TIME time.Time // 上次时间 + GWAF_DLP dlpheader.EngineAPI // 脱敏引擎 + GWAF_CHAN_HOST = make(chan model.Hosts, 10) //主机链 + GWAF_CHAN_ENGINE = make(chan int, 10) //引擎链 ) diff --git a/localserver.go b/localserver.go index 085fd16..e9cd11c 100644 --- a/localserver.go +++ b/localserver.go @@ -2,26 +2,24 @@ package main import ( "SamWaf/global" - "SamWaf/innerbean" - "SamWaf/model" - "SamWaf/model/common/response" - "SamWaf/model/request" - response2 "SamWaf/model/response" - "SamWaf/utils" - "SamWaf/utils/zlog" + "SamWaf/router" "SamWaf/vue" - "errors" assetfs "github.com/elazarl/go-bindata-assetfs" "github.com/gin-gonic/gin" - uuid "github.com/satori/go.uuid" - "gorm.io/gorm" "log" "net/http" "strconv" - "strings" - "time" ) +func InitRouter(r *gin.Engine) { + RouterGroup := r.Group("") + router.ApiGroupApp.InitHostRouter(RouterGroup) + router.ApiGroupApp.InitLogRouter(RouterGroup) + router.ApiGroupApp.InitRuleRouter(RouterGroup) + router.ApiGroupApp.InitEngineRouter(RouterGroup) + router.ApiGroupApp.InitStatRouter(RouterGroup) + +} func Cors() gin.HandlerFunc { return func(c *gin.Context) { method := c.Request.Method @@ -52,568 +50,7 @@ func StartLocalServer() { if global.GWAF_RELEASE { index(r) } - ruleHelper := &utils.RuleHelper{} - r.GET("/samwaf/resetWAF", func(c *gin.Context) { - /*defer func() { - c.JSON(http.StatusOK, response.Response{ - HostCode: -1, - Data: "", - Msg: "重启指令失败", - }) - }()*/ - //重启WAF引擎 - engineChan <- 1 - c.JSON(http.StatusOK, response.Response{ - Code: 200, - Data: "", - Msg: "已发起重启指令", - }) - }) - r.GET("/samwaf/wafstat", func(c *gin.Context) { - - c.JSON(http.StatusOK, response.Response{ - Code: 200, - Data: response2.WafStat{ - AttackCountOfToday: 0, - VisitCountOfToday: 0, - AttackCountOfYesterday: 0, - VisitCountOfYesterday: 0, - AttackCountOfLastWeekToday: 0, - VisitCountOfLastWeekToday: 0, - NormalIpCountOfToday: 0, - IllegalIpCountOfToday: 0, - NormalCountryCountOfToday: 0, - IllegalCountryCountOfToday: 0, - NormalProvinceCountOfToday: 0, - IllegalProvinceCountOfToday: 0, - NormalCityCountOfToday: 0, - IllegalCityCountOfToday: 0, - }, - Msg: "统计信息", - }) - }) - var waf_attack request.WafAttackLogSearch - r.GET("/samwaf/waflog/attack/list", func(c *gin.Context) { - err := c.ShouldBind(&waf_attack) - if err == nil { - - var total int64 = 0 - var weblogs []innerbean.WebLog - global.GWAF_LOCAL_DB.Debug().Limit(waf_attack.PageSize).Offset(waf_attack.PageSize * (waf_attack.PageIndex - 1)).Order("create_time desc").Find(&weblogs) - global.GWAF_LOCAL_DB.Debug().Model(&innerbean.WebLog{}).Count(&total) - - c.JSON(http.StatusOK, response.Response{ - Code: 200, - Data: response.PageResult{ - List: weblogs, - Total: total, - PageIndex: waf_attack.PageIndex, - PageSize: waf_attack.PageSize, - }, - Msg: "获取成功", - }) - } - - }) - - var waf_attack_detail_req request.WafAttackLogDetailReq - r.GET("/samwaf/waflog/attack/detail", func(c *gin.Context) { - err := c.ShouldBind(&waf_attack_detail_req) - if err == nil { - - var weblog innerbean.WebLog - global.GWAF_LOCAL_DB.Debug().Where("REQ_UUID=?", waf_attack_detail_req.REQ_UUID).Find(&weblog) - - c.JSON(http.StatusOK, response.Response{ - Code: 200, - Data: weblog, - Msg: "获取成功", - }) - } - - }) - - var waf_host_req request.WafHostSearchReq - r.GET("/samwaf/wafhost/host/list", func(c *gin.Context) { - err := c.ShouldBind(&waf_host_req) - if err == nil { - - var total int64 = 0 - var webhosts []model.Hosts - global.GWAF_LOCAL_DB.Debug().Limit(waf_host_req.PageSize).Offset(waf_host_req.PageSize * (waf_host_req.PageIndex - 1)).Find(&webhosts) - global.GWAF_LOCAL_DB.Debug().Model(&model.Hosts{}).Count(&total) - - c.JSON(http.StatusOK, response.Response{ - Code: 200, - Data: response.PageResult{ - List: webhosts, - Total: total, - PageIndex: waf_attack.PageIndex, - PageSize: waf_attack.PageSize, - }, - Msg: "获取成功", - }) - } - - }) - var waf_host_detail_req request.WafHostDetailReq - r.GET("/samwaf/wafhost/host/detail", func(c *gin.Context) { - err := c.ShouldBind(&waf_host_detail_req) - if err == nil { - - var webhost model.Hosts - global.GWAF_LOCAL_DB.Debug().Where("CODE=?", waf_host_detail_req.CODE).Find(&webhost) - - c.JSON(http.StatusOK, response.Response{ - Code: 200, - Data: webhost, - Msg: "获取成功", - }) - } - - }) - - var waf_host_add_req request.WafHostAddReq - r.POST("/samwaf/wafhost/host/add", func(c *gin.Context) { - err := c.ShouldBind(&waf_host_add_req) - if err == nil { - - if (!errors.Is(global.GWAF_LOCAL_DB.First(&model.Hosts{}, "host = ? and port= ?", waf_host_add_req.Host, waf_host_add_req.Port).Error, gorm.ErrRecordNotFound)) { - c.JSON(http.StatusOK, response.Response{ - Code: 404, - Msg: "当前网站和端口已经存在", //可以后续考虑再次加入已存在的host的返回,前台进行编辑 - }) - return - } - var waf_host = &model.Hosts{ - USER_CODE: global.GWAF_USER_CODE, - Tenant_id: global.GWAF_TENANT_ID, - Code: uuid.NewV4().String(), - Host: waf_host_add_req.Host, - Port: waf_host_add_req.Port, - Ssl: waf_host_add_req.Ssl, - GUARD_STATUS: 0, - REMOTE_SYSTEM: waf_host_add_req.REMOTE_SYSTEM, - REMOTE_APP: waf_host_add_req.REMOTE_APP, - Remote_host: waf_host_add_req.Remote_host, - Remote_port: waf_host_add_req.Remote_port, - Certfile: waf_host_add_req.Certfile, - Keyfile: waf_host_add_req.Keyfile, - REMARKS: waf_host_add_req.REMARKS, - CREATE_TIME: time.Now(), - UPDATE_TIME: time.Now(), - } - //waf_host_add_req.USER_CODE = - global.GWAF_LOCAL_DB.Debug().Create(waf_host) - - c.JSON(http.StatusOK, response.Response{ - Code: 200, - Data: "", - Msg: "添加成功", - }) - } else { - zlog.Debug("添加解析失败") - c.JSON(http.StatusOK, response.Response{ - Code: -1, - Data: err.Error(), - Msg: "添加失败", - }) - return - } - - }) - - var waf_host_del_req request.WafHostDelReq - r.GET("/samwaf/wafhost/host/del", func(c *gin.Context) { - err := c.ShouldBind(&waf_host_del_req) - if err == nil { - - var webhost model.Hosts - err = global.GWAF_LOCAL_DB.Where("CODE = ?", waf_host_del_req.CODE).First(&webhost).Error - if err != nil && errors.Is(err, gorm.ErrRecordNotFound) { - c.JSON(http.StatusOK, response.Response{ - Code: -1, - Data: err.Error(), - Msg: "请检测参数", - }) - return - } - if err != nil { - c.JSON(http.StatusOK, response.Response{ - Code: -1, - Data: err.Error(), - Msg: "发生错误", - }) - return - } - err = global.GWAF_LOCAL_DB.Where("CODE = ?", waf_host_del_req.CODE).Delete(model.Hosts{}).Error - - if err != nil { - c.JSON(http.StatusOK, response.Response{ - Code: -1, - Data: err.Error(), - Msg: "删除失败", - }) - return - } - - c.JSON(http.StatusOK, response.Response{ - Code: 200, - Data: "", - Msg: "删除成功", - }) - } - - }) - - var waf_host_edit_req request.WafHostEditReq - r.POST("/samwaf/wafhost/host/edit", func(c *gin.Context) { - err := c.ShouldBind(&waf_host_edit_req) - if err == nil { - - var webhost model.Hosts - global.GWAF_LOCAL_DB.Debug().Where("host = ? and port= ?", waf_host_edit_req.Host, waf_host_edit_req.Port).Find(&webhost) - - if webhost.Id != 0 && webhost.Code != waf_host_edit_req.CODE { - c.JSON(http.StatusOK, response.Response{ - Code: 404, - Msg: "当前网站和端口已经存在", //可以后续考虑再次加入已存在的host的返回,前台进行编辑 - }) - return - } - hostMap := map[string]interface{}{ - "Host": waf_host_edit_req.Host, - "Port": waf_host_edit_req.Port, - "Ssl": waf_host_edit_req.Ssl, - "GUARD_STATUS": 0, - "REMOTE_SYSTEM": waf_host_edit_req.REMOTE_SYSTEM, - "REMOTE_APP": waf_host_edit_req.REMOTE_APP, - "Remote_host": waf_host_edit_req.Remote_host, - "Remote_port": waf_host_edit_req.Remote_port, - "REMARKS": waf_host_edit_req.REMARKS, - - "Certfile": waf_host_edit_req.Certfile, - "Keyfile": waf_host_edit_req.Keyfile, - "UPDATE_TIME": time.Now(), - } - //var edit_waf_host model.Hosts - //global.GWAF_LOCAL_DB.Debug().Where("CODE=?", waf_host_edit_req.CODE).Find(edit_waf_host) - - err = global.GWAF_LOCAL_DB.Debug().Model(model.Hosts{}).Where("CODE=?", waf_host_edit_req.CODE).Updates(hostMap).Error - if err != nil { - c.JSON(http.StatusOK, response.Response{ - Code: 200, - Data: err.Error(), - Msg: "编辑失败", - }) - } else { - c.JSON(http.StatusOK, response.Response{ - Code: 200, - Data: "", - Msg: "编辑成功", - }) - } - - } else { - zlog.Debug("添加解析失败") - c.JSON(http.StatusOK, response.Response{ - Code: -1, - Data: err.Error(), - Msg: "编辑失败", - }) - return - } - - }) - - var waf_host_guard_status_req request.WafHostGuardStatusReq - r.GET("/samwaf/wafhost/host/guardstatus", func(c *gin.Context) { - err := c.ShouldBind(&waf_host_guard_status_req) - if err == nil { - - hostMap := map[string]interface{}{ - "GUARD_STATUS": waf_host_guard_status_req.GUARD_STATUS, - "UPDATE_TIME": time.Now(), - } - - err = global.GWAF_LOCAL_DB.Debug().Model(model.Hosts{}).Where("CODE=?", waf_host_guard_status_req.CODE).Updates(hostMap).Error - if err != nil { - c.JSON(http.StatusOK, response.Response{ - Code: 200, - Data: err.Error(), - Msg: "状态失败", - }) - } else { - var webHost model.Hosts - err = global.GWAF_LOCAL_DB.Where("CODE = ?", waf_host_guard_status_req.CODE).First(&webHost).Error - //发送状态改变通知 - hostChan <- webHost - c.JSON(http.StatusOK, response.Response{ - Code: 200, - Data: "", - Msg: "状态成功", - }) - } - - } else { - zlog.Debug("状态解析失败") - c.JSON(http.StatusOK, response.Response{ - Code: -1, - Data: err.Error(), - Msg: "状态失败", - }) - return - } - - }) - - var waf_rule_detail_req request.WafRuleDetailReq - r.GET("/samwaf/wafhost/rule/detail", func(c *gin.Context) { - err := c.ShouldBind(&waf_rule_detail_req) - if err == nil { - - var rules model.Rules - global.GWAF_LOCAL_DB.Debug().Where("RULE_CODE=?", waf_rule_detail_req.CODE).Find(&rules) - - c.JSON(http.StatusOK, response.Response{ - Code: 200, - Data: rules, - Msg: "获取成功", - }) - } - - }) - var waf_rule_search_req request.WafRuleSearchReq - r.GET("/samwaf/wafhost/rule/list", func(c *gin.Context) { - err := c.ShouldBind(&waf_rule_search_req) - if err == nil { - - var total int64 = 0 - var rules []model.Rules - global.GWAF_LOCAL_DB.Debug().Where("user_code=? and rule_status= 1", global.GWAF_USER_CODE).Limit(waf_rule_search_req.PageSize).Offset(waf_rule_search_req.PageSize * (waf_rule_search_req.PageIndex - 1)).Find(&rules) - global.GWAF_LOCAL_DB.Debug().Model(&model.Rules{}).Count(&total) - - c.JSON(http.StatusOK, response.Response{ - Code: 200, - Data: response.PageResult{ - List: rules, - Total: total, - PageIndex: waf_attack.PageIndex, - PageSize: waf_attack.PageSize, - }, - Msg: "获取成功", - }) - } - - }) - - var waf_rule_add_req request.WafRuleAddReq - r.POST("/samwaf/wafhost/rule/add", func(c *gin.Context) { - err := c.ShouldBind(&waf_rule_add_req) - if err == nil { - - var rule_tool = model.RuleTool{} - rule_info, err := rule_tool.LoadRule(waf_rule_add_req.RuleJson) - if err != nil { - c.JSON(http.StatusOK, response.Response{ - Code: -1, - Msg: "解析错误", - }) - return - } - - var rulename = rule_info.RuleBase.RuleName //中文名 - if (!errors.Is(global.GWAF_LOCAL_DB.First(&model.Rules{}, "rule_name = ? and rule_code = ?", rulename, rule_info.RuleBase.RuleDomainCode).Error, gorm.ErrRecordNotFound)) { - c.JSON(http.StatusOK, response.Response{ - Code: 404, - Msg: "当前规则名称已存在", //可以后续考虑再次加入已存在的返回,前台进行编辑 - }) - return - } - - var rule_code = uuid.NewV4().String() - rule_info.RuleBase.RuleName = strings.Replace(rule_code, "-", "", -1) - - var ruleContent = rule_tool.GenRuleInfo(rule_info, rulename) - if waf_rule_add_req.IsManualRule == 1 { - ruleContent = rule_info.RuleContent - //检查规则是否合法 - err = ruleHelper.CheckRuleAvailable(ruleContent) - if err != nil { - c.JSON(http.StatusOK, response.Response{ - Code: -1, - Data: err.Error(), - Msg: "规则校验失败", - }) - return - } - } - - var waf_rule = &model.Rules{ - TenantId: global.GWAF_TENANT_ID, - HostCode: rule_info.RuleBase.RuleDomainCode, //网站CODE - RuleCode: rule_code, - RuleName: rulename, - RuleContent: ruleContent, - RuleContentJSON: waf_rule_add_req.RuleJson, //TODO 后续考虑是否应该再从结构转一次 - RuleVersionName: "初版", - RuleVersion: 1, - UserCode: global.GWAF_USER_CODE, - IsPublicRule: 0, - IsManualRule: waf_rule_add_req.IsManualRule, - RuleStatus: 1, - } - //waf_host_add_req.USER_CODE = - global.GWAF_LOCAL_DB.Debug().Create(waf_rule) - - c.JSON(http.StatusOK, response.Response{ - Code: 200, - Data: "", - Msg: "添加成功", - }) - } else { - log.Println("添加解析失败") - c.JSON(http.StatusOK, response.Response{ - Code: -1, - Data: err.Error(), - Msg: "添加失败", - }) - return - } - - }) - - var waf_rule_edit_req request.WafRuleEditReq - r.POST("/samwaf/wafhost/rule/edit", func(c *gin.Context) { - err := c.ShouldBind(&waf_rule_edit_req) - if err == nil { - - var ruleTool = model.RuleTool{} - ruleInfo, err := ruleTool.LoadRule(waf_rule_edit_req.RuleJson) - if err != nil { - c.JSON(http.StatusOK, response.Response{ - Code: -1, - Msg: "解析错误", - }) - return - } - var ruleName = ruleInfo.RuleBase.RuleName //中文名 - - var rule model.Rules - global.GWAF_LOCAL_DB.Debug().Where("rule_name = ? and host_code= ?", - ruleName, ruleInfo.RuleBase.RuleDomainCode).Find(&rule) - - if rule.Id != 0 && rule.RuleCode != waf_rule_edit_req.CODE { - c.JSON(http.StatusOK, response.Response{ - Code: 404, - Msg: "当前规则名称已经存在", //可以后续考虑再次加入已存在的返回,前台进行编辑 - }) - return - } - - global.GWAF_LOCAL_DB.Debug().Where("rule_code=?", waf_rule_edit_req.CODE).Find(&rule) - - ruleInfo.RuleBase.RuleName = strings.Replace(rule.RuleCode, "-", "", -1) - var ruleContent = ruleTool.GenRuleInfo(ruleInfo, ruleName) - if waf_rule_edit_req.IsManualRule == 1 { - ruleContent = ruleInfo.RuleContent - //检查规则是否合法 - err = ruleHelper.CheckRuleAvailable(ruleContent) - if err != nil { - c.JSON(http.StatusOK, response.Response{ - Code: -1, - Data: err.Error(), - Msg: "规则校验失败", - }) - return - } - } - ruleMap := map[string]interface{}{ - "HostCode": ruleInfo.RuleBase.RuleDomainCode, //TODO 注意字典名称 - "RuleName": ruleName, - "RuleContent": ruleContent, - "RuleContentJSON": waf_rule_edit_req.RuleJson, //TODO 后续考虑是否应该再从结构转一次 - "RuleVersionName": "初版", - "RuleVersion": rule.RuleVersion + 1, - "User_code": global.GWAF_USER_CODE, - "IsPublicRule": 0, - "IsManualRule": waf_rule_edit_req.IsManualRule, - "RuleStatus": "1", - //"UPDATE_TIME": time.Now(), - } - err = global.GWAF_LOCAL_DB.Debug().Model(model.Rules{}).Where("rule_code=?", waf_rule_edit_req.CODE).Updates(ruleMap).Error - if err != nil { - c.JSON(http.StatusOK, response.Response{ - Code: 200, - Data: err.Error(), - Msg: "编辑失败", - }) - } else { - c.JSON(http.StatusOK, response.Response{ - Code: 200, - Data: "", - Msg: "编辑成功", - }) - } - - } else { - log.Println("添加解析失败") - c.JSON(http.StatusOK, response.Response{ - Code: -1, - Data: err.Error(), - Msg: "编辑失败", - }) - return - } - - }) - - var waf_rule_del_req request.WafRuleDelReq - r.GET("/samwaf/wafhost/rule/del", func(c *gin.Context) { - err := c.ShouldBind(&waf_rule_del_req) - if err == nil { - - var rule model.Rules - err = global.GWAF_LOCAL_DB.Where("rule_code = ?", waf_rule_del_req.CODE).First(&rule).Error - if err != nil && errors.Is(err, gorm.ErrRecordNotFound) { - c.JSON(http.StatusOK, response.Response{ - Code: -1, - Data: err.Error(), - Msg: "请检测参数", - }) - return - } - if err != nil { - c.JSON(http.StatusOK, response.Response{ - Code: -1, - Data: err.Error(), - Msg: "发生错误", - }) - return - } - rule_map := map[string]interface{}{ - "RuleStatus": "999", - "RuleVersion": 999999, - } - err = global.GWAF_LOCAL_DB.Model(model.Rules{}).Where("rule_code = ?", waf_rule_del_req.CODE).Updates(rule_map).Error - - if err != nil { - c.JSON(http.StatusOK, response.Response{ - Code: -1, - Data: err.Error(), - Msg: "删除失败", - }) - return - } - - c.JSON(http.StatusOK, response.Response{ - Code: 200, - Data: "", - Msg: "删除成功", - }) - } - - }) + InitRouter(r) r.Run(":" + strconv.Itoa(global.GWAF_LOCAL_SERVER_PORT)) // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080") log.Println("本地 port:%d", global.GWAF_LOCAL_SERVER_PORT) diff --git a/localwaf/src/pages/dashboard/base/components/TopPanel.vue b/localwaf/src/pages/dashboard/base/components/TopPanel.vue index ad2702d..1a325ed 100644 --- a/localwaf/src/pages/dashboard/base/components/TopPanel.vue +++ b/localwaf/src/pages/dashboard/base/components/TopPanel.vue @@ -110,9 +110,9 @@ export default { .get('/wafstat', { }) .then((res) => { - let resdata = res.data + let resdata = res console.log(resdata) - if (resdata.code === 200) { + if (resdata.code === 0) { //const { list = [] } = resdata.data.list; diff --git a/localwaf/src/pages/waf-host/host/detail/index.vue b/localwaf/src/pages/waf-host/host/detail/index.vue index 0465fc8..6b67ef0 100644 --- a/localwaf/src/pages/waf-host/host/detail/index.vue +++ b/localwaf/src/pages/waf-host/host/detail/index.vue @@ -130,9 +130,9 @@ } }) .then((res) => { - let resdata = res.data + let resdata = res console.log(resdata) - if (resdata.code === 200) { + if (resdata.code === 0) { //const { list = [] } = resdata.data.list; diff --git a/localwaf/src/pages/waf-host/host/index.vue b/localwaf/src/pages/waf-host/host/index.vue index d10d13c..fd0086e 100644 --- a/localwaf/src/pages/waf-host/host/index.vue +++ b/localwaf/src/pages/waf-host/host/index.vue @@ -389,9 +389,9 @@ } }) .then((res) => { - let resdata = res.data + let resdata = res console.log(resdata) - if (resdata.code === 200) { + if (resdata.code === 0) { //const { list = [] } = resdata.data.list; @@ -475,9 +475,9 @@ ...postdata }) .then((res) => { - let resdata = res.data + let resdata = res console.log(resdata) - if (resdata.code === 200) { + if (resdata.code === 0) { that.$message.success(resdata.msg); that.addFormVisible = false; that.pagination.current = 1 @@ -511,9 +511,9 @@ ...postdata }) .then((res) => { - let resdata = res.data + let resdata = res console.log(resdata) - if (resdata.code === 200) { + if (resdata.code === 0) { that.$message.success(resdata.msg); that.editFormVisible = false; that.pagination.current = 1 @@ -559,9 +559,9 @@ } }) .then((res) => { - let resdata = res.data + let resdata = res console.log(resdata) - if (resdata.code === 200) { + if (resdata.code === 0) { that.pagination.current = 1 that.getList("") @@ -593,9 +593,9 @@ } }) .then((res) => { - let resdata = res.data + let resdata = res console.log(resdata) - if (resdata.code === 200) { + if (resdata.code === 0) { that.detail_data = resdata.data; that.detail_data.ssl = that.detail_data.ssl.toString() that.formEditData = { @@ -647,9 +647,9 @@ } }) .then((res) => { - let resdata = res.data + let resdata = res console.log(resdata) - if (resdata.code === 200) { + if (resdata.code === 0) { that.getList("") that.$message.close(msg); that.$message.success(resdata.msg) diff --git a/localwaf/src/pages/waf-host/rule/edit/index.vue b/localwaf/src/pages/waf-host/rule/edit/index.vue index 23c5746..0b1a6fa 100644 --- a/localwaf/src/pages/waf-host/rule/edit/index.vue +++ b/localwaf/src/pages/waf-host/rule/edit/index.vue @@ -455,9 +455,9 @@ } }) .then((res) => { - let resdata = res.data + let resdata = res console.log(resdata) - if (resdata.code === 200) { + if (resdata.code === 0) { //const { list = [] } = resdata.data; @@ -499,9 +499,9 @@ ...postdata }) .then((res) => { - let resdata = res.data + let resdata = res console.log(resdata) - if (resdata.code === 200) { + if (resdata.code === 0) { that.$message.success(resdata.msg); this.$router.push( { diff --git a/localwaf/src/pages/waf-host/rule/index.vue b/localwaf/src/pages/waf-host/rule/index.vue index e1aa263..c7d987c 100644 --- a/localwaf/src/pages/waf-host/rule/index.vue +++ b/localwaf/src/pages/waf-host/rule/index.vue @@ -190,9 +190,9 @@ export default Vue.extend({ } }) .then((res) => { - let resdata = res.data + let resdata = res console.log(resdata) - if (resdata.code === 200) { + if (resdata.code === 0) { this.data = resdata.data.list; this.pagination = { ...this.pagination, @@ -262,9 +262,9 @@ export default Vue.extend({ ...postdata }) .then((res) => { - let resdata = res.data + let resdata = res console.log(resdata) - if (resdata.code === 200) { + if (resdata.code === 0) { that.$message.success(resdata.msg); that.addFormVisible = false; that.pagination.current = 1 @@ -294,9 +294,9 @@ export default Vue.extend({ ...postdata }) .then((res) => { - let resdata = res.data + let resdata = res console.log(resdata) - if (resdata.code === 200) { + if (resdata.code === 0) { that.$message.success(resdata.msg); that.editFormVisible = false; that.pagination.current = 1 @@ -341,9 +341,9 @@ export default Vue.extend({ } }) .then((res) => { - let resdata = res.data + let resdata = res console.log(resdata) - if (resdata.code === 200) { + if (resdata.code === 0) { that.pagination.current = 1 that.getList("") @@ -375,9 +375,9 @@ export default Vue.extend({ } }) .then((res) => { - let resdata = res.data + let resdata = res console.log(resdata) - if (resdata.code === 200) { + if (resdata.code === 0) { that.detail_data = resdata.data; that.detail_data.ssl = that.detail_data.ssl.toString() that.formEditData = {...that.detail_data} diff --git a/localwaf/src/pages/waf-log/attack/detail/index.vue b/localwaf/src/pages/waf-log/attack/detail/index.vue index 84f20bc..7988e49 100644 --- a/localwaf/src/pages/waf-log/attack/detail/index.vue +++ b/localwaf/src/pages/waf-log/attack/detail/index.vue @@ -1,4 +1,4 @@ -