feat:spider analysis

#163
This commit is contained in:
samwaf
2025-05-02 15:43:23 +08:00
parent 1aba5aa3c8
commit a3aa4bbfd7
13 changed files with 127 additions and 37 deletions
+3
View File
@@ -36,6 +36,7 @@ type APIGroup struct {
WafBlockingPageApi
WafGPTApi
WafOtpApi
WafAnalysisApi
}
var APIGroupAPP = new(APIGroup)
@@ -85,4 +86,6 @@ var (
wafBlockingPageService = waf_service.WafBlockingPageServiceApp
wafOtpService = waf_service.WafOtpServiceApp
wafAnalysisService = waf_service.WafAnalysisServiceApp
)
+36
View File
@@ -0,0 +1,36 @@
package api
import (
"SamWaf/model/common/response"
"SamWaf/model/request"
"github.com/gin-gonic/gin"
)
type WafAnalysisApi struct {
}
// StatAnalysisDayCountryRangeApi 数据分析界面- 国家级别分析
func (w *WafAnalysisApi) StatAnalysisDayCountryRangeApi(c *gin.Context) {
var req request.WafStatsAnalysisDayRangeCountryReq
err := c.ShouldBind(&req)
if err == nil {
wafStat := wafAnalysisService.StatAnalysisDayCountryRangeApi(req)
response.OkWithDetailed(wafStat, "获取成功", c)
} else {
response.FailWithMessage("解析失败", c)
}
}
// AnalysisSpiderRangeApi 数据分析界面- 爬虫分析
func (w *WafAnalysisApi) AnalysisSpiderRangeApi(c *gin.Context) {
var req request.WafAnalysisSpiderReq
err := c.ShouldBind(&req)
if err == nil {
bean := wafAnalysisService.AnalysisSpiderApi(req)
response.OkWithDetailed(bean, "获取成功", c)
} else {
response.FailWithMessage("解析失败", c)
}
}
+6 -4
View File
@@ -121,13 +121,15 @@ func (w *WafHostAPi) GetAllListApi(c *gin.Context) {
if wafHosts[i].Ssl == 1 {
allHostRep[i] = response2.AllHostRep{
Host: fmt.Sprintf("%s:%d(SSL)", wafHosts[i].Host, wafHosts[i].Port),
Code: wafHosts[i].Code,
Host: fmt.Sprintf("%s:%d(SSL)", wafHosts[i].Host, wafHosts[i].Port),
Code: wafHosts[i].Code,
PreHost: fmt.Sprintf("%s:%d", wafHosts[i].Host, wafHosts[i].Port),
}
} else {
allHostRep[i] = response2.AllHostRep{
Host: fmt.Sprintf("%s:%d", wafHosts[i].Host, wafHosts[i].Port),
Code: wafHosts[i].Code,
Host: fmt.Sprintf("%s:%d", wafHosts[i].Host, wafHosts[i].Port),
Code: wafHosts[i].Code,
PreHost: fmt.Sprintf("%s:%d", wafHosts[i].Host, wafHosts[i].Port),
}
}
-13
View File
@@ -48,16 +48,3 @@ func (w *WafStatApi) StatHomeSumDayTopIPRangeApi(c *gin.Context) {
response.FailWithMessage("解析失败", c)
}
}
// 数据分析界面- 国家级别分析
func (w *WafStatApi) StatAnalysisDayCountryRangeApi(c *gin.Context) {
var req request.WafStatsAnalysisDayRangeCountryReq
err := c.ShouldBind(&req)
if err == nil {
wafStat := wafStatService.StatAnalysisDayCountryRangeApi(req)
response.OkWithDetailed(wafStat, "获取成功", c)
} else {
response.FailWithMessage("解析失败", c)
}
}
+8
View File
@@ -0,0 +1,8 @@
package request
// 爬虫日志分析
type WafAnalysisSpiderReq struct {
StartDay string `json:"start_day" form:"start_day"`
EndDay string `json:"end_day" form:"end_day"`
Host string `json:"host" form:"host"`
}
+3 -2
View File
@@ -3,8 +3,9 @@ package response
import "SamWaf/customtype"
type AllHostRep struct {
Code string `json:"value"` //唯一码
Host string `json:"label"` //域名
Code string `json:"value"` //唯一码
Host string `json:"label"` //域名
PreHost string `json:"pre_host"` //纯域名和端口
}
type AllShareDbRep struct {
+7
View File
@@ -0,0 +1,7 @@
package response
// WafAnalysisSpiderResp 爬虫分析数据值
type WafAnalysisSpiderResp struct {
Name string `json:"name" form:"name"`
Value int64 `json:"value" form:"value"`
}
+1
View File
@@ -34,6 +34,7 @@ type ApiGroup struct {
WafBlockingPageRouter
WafGPTRouter
WafOtpRouter
AnalysisRouter
}
type PublicApiGroup struct {
LoginRouter
+17
View File
@@ -0,0 +1,17 @@
package router
import (
"SamWaf/api"
"github.com/gin-gonic/gin"
)
type AnalysisRouter struct {
}
func (receiver *AnalysisRouter) InitAnalysisRouter(group *gin.RouterGroup) {
analysisApi := api.APIGroupAPP.WafAnalysisApi
router := group.Group("")
//数据分析
router.GET("/samwaf/analysis/wafanalysisdaycountryrange", analysisApi.StatAnalysisDayCountryRangeApi)
router.GET("/samwaf/analysis/spider", analysisApi.AnalysisSpiderRangeApi)
}
-3
View File
@@ -17,7 +17,4 @@ func (receiver *StatRouter) InitStatRouter(group *gin.RouterGroup) {
router.GET("/samwaf/wafstatsumdaytopiprange", statApi.StatHomeSumDayTopIPRangeApi)
router.GET("/samwaf/statsysinfo", statApi.StatSysinfoApi)
router.GET("/samwaf/statrumtimesysinfo", statApi.StatRumtimeSysinfoApi)
//数据分析
router.GET("/samwaf/wafanalysisdaycountryrange", statApi.StatAnalysisDayCountryRangeApi)
}
+45
View File
@@ -0,0 +1,45 @@
package waf_service
import (
"SamWaf/global"
"SamWaf/innerbean"
"SamWaf/model"
"SamWaf/model/request"
response2 "SamWaf/model/response"
)
type WafAnalysisService struct{}
var WafAnalysisServiceApp = new(WafAnalysisService)
// StatAnalysisDayCountryRangeApi 通过时间获取国家级别的 攻击数 访问数
func (receiver *WafAnalysisService) StatAnalysisDayCountryRangeApi(req request.WafStatsAnalysisDayRangeCountryReq) []response2.WafAnalysisDayStats {
var CountOfRange []response2.WafAnalysisDayStats
if req.AttackType == "" {
global.GWAF_LOCAL_STATS_DB.Model(&model.StatsIPCityDay{}).Where("day between ? and ? and country<>? and country<>? ",
req.StartDay, req.EndDay, "0", "内网").Select(" country as Name ,sum(count) as Value").Group("country").Order("sum(count) desc").Scan(&CountOfRange)
} else {
global.GWAF_LOCAL_STATS_DB.Model(&model.StatsIPCityDay{}).Where("day between ? and ? and type = ? and country<>? and country<>? ",
req.StartDay, req.EndDay, req.AttackType, "0", "内网").Select(" country as Name ,sum(count) as Value").Group("country").Order("sum(count) desc").Scan(&CountOfRange)
}
return CountOfRange
}
// AnalysisSpiderApi 爬虫分析
func (receiver *WafAnalysisService) AnalysisSpiderApi(req request.WafAnalysisSpiderReq) []response2.WafAnalysisSpiderResp {
var CountOfRange []response2.WafAnalysisSpiderResp
if req.Host == "" {
global.GWAF_LOCAL_LOG_DB.Model(&innerbean.WebLog{}).Where("day between ? and ? and is_bot=1 ",
req.StartDay, req.EndDay).Select(" guest_id_entification as Name ,count(1) as Value").
Group("guest_id_entification").Order("count(1) desc").Scan(&CountOfRange)
} else {
global.GWAF_LOCAL_LOG_DB.Model(&innerbean.WebLog{}).Where("day between ? and ? and is_bot=1 and host_code = ? ",
req.StartDay, req.EndDay, req.Host).
Select(" guest_id_entification as Name ,count(1) as Value").
Group("guest_id_entification").Order("count(1) desc").Scan(&CountOfRange)
}
return CountOfRange
}
-15
View File
@@ -162,21 +162,6 @@ func (receiver *WafStatService) StatHomeSumDayTopIPRangeApi(req request.WafStats
nil
}
// 通过时间获取国家级别的 攻击数 访问数
func (receiver *WafStatService) StatAnalysisDayCountryRangeApi(req request.WafStatsAnalysisDayRangeCountryReq) []response2.WafAnalysisDayStats {
var CountOfRange []response2.WafAnalysisDayStats
if req.AttackType == "" {
global.GWAF_LOCAL_STATS_DB.Model(&model.StatsIPCityDay{}).Where("day between ? and ? and country<>? and country<>? ",
req.StartDay, req.EndDay, "0", "内网").Select(" country as Name ,sum(count) as Value").Group("country").Order("sum(count) desc").Scan(&CountOfRange)
} else {
global.GWAF_LOCAL_STATS_DB.Model(&model.StatsIPCityDay{}).Where("day between ? and ? and type = ? and country<>? and country<>? ",
req.StartDay, req.EndDay, req.AttackType, "0", "内网").Select(" country as Name ,sum(count) as Value").Group("country").Order("sum(count) desc").Scan(&CountOfRange)
}
return CountOfRange
}
// 获取系统基本信息
func (receiver *WafStatService) StatHomeSysinfo(c *gin.Context) response2.WafHomeSysinfoStat {
tokenStr := c.GetHeader("X-Token")
+1
View File
@@ -67,6 +67,7 @@ func (web *WafWebManager) initRouter(r *gin.Engine) {
router.ApiGroupApp.InitWafBlockingPageRouter(RouterGroup)
router.ApiGroupApp.InitGPTRouter(RouterGroup)
router.ApiGroupApp.InitWafOtpRouter(RouterGroup)
router.ApiGroupApp.InitAnalysisRouter(RouterGroup)
}
if global.GWAF_RELEASE == "true" {