mirror of
https://gitee.com/samwaf/SamWaf.git
synced 2026-10-31 02:01:44 +08:00
增加数据统计部分代码
This commit is contained in:
+11
-8
@@ -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 // 上次时间
|
||||
)
|
||||
|
||||
@@ -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"`
|
||||
}
|
||||
|
||||
@@ -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{})
|
||||
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
@@ -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()
|
||||
|
||||
|
||||
@@ -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"` //上次更新时间
|
||||
}
|
||||
+19
-1
@@ -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/...
|
||||
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)
|
||||
@@ -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
|
||||
}
|
||||
@@ -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计数器
|
||||
|
||||
|
||||
Reference in New Issue
Block a user