From 24ccf7465abf15e9be81cbbadd866ff2da839b15 Mon Sep 17 00:00:00 2001 From: samwaf Date: Thu, 27 Oct 2022 19:03:58 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=95=B0=E6=8D=AE=E7=BB=9F?= =?UTF-8?q?=E8=AE=A1=E9=83=A8=E5=88=86=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- global/global.go | 19 ++++++++------ innerbean/web_log.go | 4 ++- localdb.go | 2 ++ localtaskcounter.go | 59 ++++++++++++++++++++++++++++++++++++++++++++ main.go | 4 ++- model/stats.go | 35 ++++++++++++++++++++++++++ readme.txt | 20 ++++++++++++++- test/Timemy_test.go | 12 +++++++++ wafengine.go | 4 +++ 9 files changed, 148 insertions(+), 11 deletions(-) create mode 100644 localtaskcounter.go create mode 100644 model/stats.go create mode 100644 test/Timemy_test.go diff --git a/global/global.go b/global/global.go index e951654..3ee7cf5 100644 --- a/global/global.go +++ b/global/global.go @@ -1,6 +1,9 @@ package global -import "gorm.io/gorm" +import ( + "gorm.io/gorm" + "time" +) const ( Version_name = "v1.0.1" @@ -8,11 +11,11 @@ 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_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 // 上次时间 ) diff --git a/innerbean/web_log.go b/innerbean/web_log.go index 70e6bb6..1b9839f 100644 --- a/innerbean/web_log.go +++ b/innerbean/web_log.go @@ -18,7 +18,9 @@ type WebLog struct { BODY string `json:"body"` REQ_UUID string `json:"req_uuid"` USER_CODE string `json:"user_code"` - HOST_CODE string `json:"host_code"` //关联查询 + TenantId string `json:"tenant_id"` //租户ID(主要键) + HOST_CODE string `json:"host_code"` //主机ID (主要键) + Day int `json:"day"` //日 (主要键) ACTION string `json:"action"` RULE string `json:"rule"` } diff --git a/localdb.go b/localdb.go index fd68291..42bb9f5 100644 --- a/localdb.go +++ b/localdb.go @@ -19,6 +19,8 @@ func InitDb() { db.AutoMigrate(&innerbean.WebLog{}) db.AutoMigrate(&model.Hosts{}) db.AutoMigrate(&model.Rules{}) + db.AutoMigrate(&model.StatsTotal{}) + db.AutoMigrate(&model.StatsDay{}) //重启需要删除无效规则 db.Where("user_code = ? and rule_status = 999", global.GWAF_USER_CODE).Delete(model.Rules{}) diff --git a/localtaskcounter.go b/localtaskcounter.go new file mode 100644 index 0000000..5c29d66 --- /dev/null +++ b/localtaskcounter.go @@ -0,0 +1,59 @@ +package main + +import ( + "SamWaf/global" + "SamWaf/model" + "time" +) + +type CountResult struct { + UserCode string `json:"user_code"` //用户码(主要键) + TenantId string `json:"tenant_id"` //租户ID(主要键) + HostCode string `json:"host_code"` //主机ID (主要键) + Day int `json:"day"` //年月日(主要键) + Host string `json:"host"` //域名 + ACTION string `json:"action"` + Count int `json:"count"` //数量 +} + +/** +定时统计 +*/ + +func TaskCounter() { + + var result []CountResult + global.GWAF_LOCAL_DB.Debug().Raw("SELECT host_code, user_code,tenant_id ,action,count(req_uuid) as count,day,host FROM \"web_logs\" where create_time>? GROUP BY host_code, user_code,action,tenant_id,day,host", global.GWAF_LAST_UPDATE_TIME.Format("2006-01-02 15:04:05")).Scan(&result) + /**** + 1.如果不存在则创建 + 2.如果存在则累加这个周期的统计数 + */ + for _, value := range result { + var statDay model.StatsDay + err := global.GWAF_LOCAL_DB.Debug().Where("tenant_id=? and user_code=? and host_code=? and type=? and day=?", + value.TenantId, value.UserCode, value.HostCode, value.ACTION, value.Day).Find(&statDay).Error + if err != nil { // && errors.Is(err, gorm.ErrRecordNotFound) + statDayMap := map[string]interface{}{ + "Count": value.Count + statDay.Count, + "last_update_time": time.Now(), + } + + global.GWAF_LOCAL_DB.Debug().Model(model.StatsDay{}).Where("tenant_id=? and user_code=? and host_code=? and type=? and day=?", + value.TenantId, value.UserCode, value.HostCode, value.ACTION, value.Day).Updates(statDayMap) + } else { + statDay2 := &model.StatsDay{ + UserCode: value.UserCode, + TenantId: value.TenantId, + HostCode: value.HostCode, + Day: value.Day, + Host: value.Host, + Type: value.ACTION, + Count: value.Count, + CreateTime: time.Now(), + LastUpdateTime: time.Now(), + } + global.GWAF_LOCAL_DB.Debug().Create(statDay2) + } + } + global.GWAF_LAST_UPDATE_TIME = time.Now() +} diff --git a/main.go b/main.go index 53e099f..5e8558a 100644 --- a/main.go +++ b/main.go @@ -16,6 +16,7 @@ func main() { if !global.GWAF_RELEASE { zlog.Info("调试版本") } + global.GWAF_LAST_UPDATE_TIME = time.Now() /*runtime.GOMAXPROCS(1) // 限制 CPU 使用数,避免过载 runtime.SetMutexProfileFraction(1) // 开启对锁调用的跟踪 runtime.SetBlockProfileRate(1) // 开启对阻塞操作的跟踪 @@ -77,9 +78,10 @@ func main() { timezone, _ := time.LoadLocation("Asia/Shanghai") s := gocron.NewScheduler(timezone) - // 每秒执行一次 + // 每秒执行一次 TODO 改数据成分钟统计 s.Every(10).Seconds().Do(func() { zlog.Debug("i am alive") + go TaskCounter() }) s.StartAsync() diff --git a/model/stats.go b/model/stats.go new file mode 100644 index 0000000..95c22e5 --- /dev/null +++ b/model/stats.go @@ -0,0 +1,35 @@ +package model + +import "time" + +/* +* +整体统计 +*/ +type StatsTotal struct { + Id int `gorm:"primary_key" json:" - "` + UserCode string `json:"user_code"` //用户码(主要键) + TenantId string `json:"tenant_id"` //租户ID + AttackCount int `json:"attack_count"` //攻击数量 + VisitCount int `json:"visit_count"` //访问数量 + HistoryAttackCount int `json:"history_attack_count"` //历史攻击数量 + HistoryVisitCount int `json:"history_visit_count"` //历史访问数量 + UpdateTime time.Time `json:"update_time"` //更新时间 +} + +/* +* +按天统计和不同类型统计 +*/ +type StatsDay struct { + Id int `gorm:"primary_key" json:" - "` + UserCode string `json:"user_code"` //用户码(主要键) + TenantId string `json:"tenant_id"` //租户ID(主要键) + HostCode string `json:"host_code"` //网站唯一码(主要键) + Day int `json:"day"` //年月日(主要键) + Host string `json:"host"` //域名 + Type string `json:"type"` //类型 放行,阻止 + Count int `json:"count"` //数量 + CreateTime time.Time `json:"create_time"` //创建时间 + LastUpdateTime time.Time `json:"last_update_time"` //上次更新时间 +} diff --git a/readme.txt b/readme.txt index b34a39a..3df6049 100644 --- a/readme.txt +++ b/readme.txt @@ -2,4 +2,22 @@ GOPROXY=https://mirrors.aliyun.com/goproxy/,direct;GO111MODULE=auto GOPROXY=https://goproxy.cn,direct;GO111MODULE=auto 静态打包 -go-bindata-assetfs.exe -o=vue/vue.go -pkg=vue vue/dist/... \ No newline at end of file +go-bindata-assetfs.exe -o=vue/vue.go -pkg=vue vue/dist/... + + +go语言实现string转换int + +string转成int: + +int, err := strconv.Atoi(string) +string转成int64: + +int64, err := strconv.ParseInt(string, 10, 64) +附: + +int转成string: + +string := strconv.Itoa(int) +int64转成string: + +string := strconv.FormatInt(int64,10) \ No newline at end of file diff --git a/test/Timemy_test.go b/test/Timemy_test.go new file mode 100644 index 0000000..6db0ff7 --- /dev/null +++ b/test/Timemy_test.go @@ -0,0 +1,12 @@ +package test + +import ( + "fmt" + "testing" + "time" +) + +func TestTimemyt(t *testing.T) { + tmpTime := time.Now() + fmt.Println(tmpTime.Format("20060102")) // 输出: 2019-04-30 +} diff --git a/wafengine.go b/wafengine.go index 36c1bca..83080ab 100644 --- a/wafengine.go +++ b/wafengine.go @@ -124,6 +124,7 @@ func (h *baseHandle) ServeHTTP(w http.ResponseWriter, r *http.Request) { // 取出客户IP ipAndPort := strings.Split(r.RemoteAddr, ":") region := GetCountry(ipAndPort[0]) + currentDay, _ := strconv.Atoi(time.Now().Format("20060102")) weblogbean := innerbean.WebLog{ HOST: host, URL: r.RequestURI, @@ -142,8 +143,11 @@ func (h *baseHandle) ServeHTTP(w http.ResponseWriter, r *http.Request) { BODY: string(bodyByte), REQ_UUID: uuid.NewV4().String(), USER_CODE: global.GWAF_USER_CODE, + HOST_CODE: hostTarget[host].Host.Code, + TenantId: global.GWAF_TENANT_ID, RULE: "", ACTION: "通过", + Day: currentDay, } //ip计数器