mirror of
https://gitee.com/samwaf/SamWaf.git
synced 2026-10-31 02:01:44 +08:00
增加定时器,与前端看板内容获取
This commit is contained in:
+6
-5
@@ -8,10 +8,11 @@ const (
|
||||
)
|
||||
|
||||
var (
|
||||
GWAF_LOCAL_DB *gorm.DB //通用本地数据库,尊重用户隐私
|
||||
GWAF_REMOTE_DB *gorm.DB //仅当用户使用云数据库
|
||||
GWAF_LOCAL_SERVER_PORT int = 0 // 本地local端口
|
||||
GWAF_USER_CODE string // 当前识别号
|
||||
GWAF_TENANT_ID string // 当前租户ID
|
||||
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 // 当前是否为发行版
|
||||
|
||||
)
|
||||
|
||||
@@ -4,7 +4,9 @@ go 1.19
|
||||
|
||||
require (
|
||||
github.com/elastic/go-elasticsearch/v7 v7.17.1
|
||||
github.com/elazarl/go-bindata-assetfs v1.0.1
|
||||
github.com/gin-gonic/gin v1.8.1
|
||||
github.com/go-co-op/gocron v1.17.1
|
||||
github.com/hyperjumptech/grule-rule-engine v1.11.0
|
||||
github.com/lionsoul2014/ip2region/binding/golang v0.0.0-20220907060842-b2ba5d58e48d
|
||||
github.com/satori/go.uuid v1.2.0
|
||||
@@ -21,7 +23,6 @@ require (
|
||||
github.com/antlr/antlr4/runtime/Go/antlr v0.0.0-20220527190237-ee62e23da966 // indirect
|
||||
github.com/bmatcuk/doublestar v1.3.2 // indirect
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/elazarl/go-bindata-assetfs v1.0.1 // indirect
|
||||
github.com/emirpasic/gods v1.12.0 // indirect
|
||||
github.com/fsnotify/fsnotify v1.5.4 // indirect
|
||||
github.com/gin-contrib/sse v0.1.0 // indirect
|
||||
@@ -47,6 +48,7 @@ require (
|
||||
github.com/pelletier/go-toml v1.9.5 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/robfig/cron/v3 v3.0.1 // indirect
|
||||
github.com/sergi/go-diff v1.0.0 // indirect
|
||||
github.com/sirupsen/logrus v1.8.1 // indirect
|
||||
github.com/spf13/afero v1.8.2 // indirect
|
||||
@@ -61,6 +63,7 @@ require (
|
||||
go.uber.org/multierr v1.6.0 // indirect
|
||||
golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4 // indirect
|
||||
golang.org/x/net v0.0.0-20220520000938-2e3eb7b945c2 // indirect
|
||||
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
|
||||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
|
||||
golang.org/x/text v0.3.7 // indirect
|
||||
google.golang.org/protobuf v1.28.0 // indirect
|
||||
|
||||
@@ -86,6 +86,8 @@ github.com/gin-gonic/gin v1.8.1 h1:4+fr/el88TOO3ewCmQr8cx/CtZ/umlIRIs5M4NTNjf8=
|
||||
github.com/gin-gonic/gin v1.8.1/go.mod h1:ji8BvRH1azfM+SYow9zQ6SZMvR8qOMZHmsCuWR9tTTk=
|
||||
github.com/gliderlabs/ssh v0.2.2 h1:6zsha5zo/TWhRhwqCD3+EarCAgZ2yN28ipRnGPnwkI0=
|
||||
github.com/gliderlabs/ssh v0.2.2/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0=
|
||||
github.com/go-co-op/gocron v1.17.1 h1:oEu3xGNVn9IGukN3JPzOsfaBoTGYmUVHtR9d1cv1cq8=
|
||||
github.com/go-co-op/gocron v1.17.1/go.mod h1:IpDBSaJOVfFw7hXZuTag3SCSkqazXBBUkbQ1m1aesBs=
|
||||
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
|
||||
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
|
||||
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
|
||||
@@ -224,6 +226,8 @@ github.com/pkg/sftp v1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qR
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
|
||||
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
|
||||
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
|
||||
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
|
||||
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
|
||||
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
|
||||
@@ -384,6 +388,7 @@ golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJ
|
||||
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
|
||||
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
|
||||
+6
-2
@@ -43,11 +43,15 @@ func Cors() gin.HandlerFunc {
|
||||
}
|
||||
}
|
||||
func StartLocalServer() {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
if global.GWAF_RELEASE {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
}
|
||||
r := gin.Default()
|
||||
r.Use(Cors()) //解决跨域
|
||||
|
||||
index(r)
|
||||
if global.GWAF_RELEASE {
|
||||
index(r)
|
||||
}
|
||||
ruleHelper := &utils.RuleHelper{}
|
||||
r.GET("/samwaf/resetWAF", func(c *gin.Context) {
|
||||
/*defer func() {
|
||||
|
||||
@@ -47,7 +47,7 @@
|
||||
</t-col>
|
||||
</t-row>
|
||||
</template>
|
||||
<script>
|
||||
<script lang="ts">
|
||||
import { LineChart, BarChart } from 'echarts/charts';
|
||||
import * as echarts from 'echarts/core';
|
||||
import { CanvasRenderer } from 'echarts/renderers';
|
||||
@@ -58,7 +58,7 @@ import Trend from '@/components/trend/index.vue';
|
||||
|
||||
import { constructInitDashboardDataset } from '../index';
|
||||
import { changeChartsTheme } from '@/utils/color';
|
||||
import { PANE_LIST } from '@/service/service-base';
|
||||
//import { PANE_LIST } from '@/service/service-base';
|
||||
|
||||
echarts.use([LineChart, BarChart, CanvasRenderer]);
|
||||
|
||||
@@ -73,7 +73,7 @@ export default {
|
||||
data() {
|
||||
return {
|
||||
resizeTime: 1,
|
||||
panelList: PANE_LIST,
|
||||
panelList: [],
|
||||
};
|
||||
},
|
||||
computed: {
|
||||
@@ -90,6 +90,10 @@ export default {
|
||||
this.renderCharts();
|
||||
},
|
||||
},
|
||||
created() {
|
||||
|
||||
this.getWafStat()
|
||||
},
|
||||
mounted() {
|
||||
this.$nextTick(() => {
|
||||
this.updateContainer();
|
||||
@@ -100,6 +104,50 @@ export default {
|
||||
},
|
||||
|
||||
methods: {
|
||||
getWafStat() {
|
||||
let that = this
|
||||
this.$request
|
||||
.get('/wafstat', {
|
||||
})
|
||||
.then((res) => {
|
||||
let resdata = res.data
|
||||
console.log(resdata)
|
||||
if (resdata.code === 200) {
|
||||
|
||||
//const { list = [] } = resdata.data.list;
|
||||
|
||||
that.wafstat_data = resdata.data;
|
||||
that.panelList.push({
|
||||
title: '今日攻击数',
|
||||
number: that.wafstat_data.AttackCountOfToday,
|
||||
upTrend: '20.5%',
|
||||
leftType: 'echarts-line',
|
||||
})
|
||||
that.panelList.push({
|
||||
title: '今天总访问量',
|
||||
number: that.wafstat_data.VisitCountOfToday,
|
||||
upTrend: '20.5%',
|
||||
leftType: 'echarts-line',
|
||||
})
|
||||
that.panelList.push({
|
||||
title: '今天异常IP(个)',
|
||||
number: that.wafstat_data.IllegalIpCountOfToday,
|
||||
upTrend: '20.5%',
|
||||
leftType: 'echarts-line',
|
||||
})
|
||||
that.panelList.push({
|
||||
title: '今天正常IP(个)',
|
||||
number: that.wafstat_data.NormalIpCountOfToday,
|
||||
upTrend: '20.5%',
|
||||
leftType: 'echarts-line',
|
||||
})
|
||||
}
|
||||
})
|
||||
.catch((e: Error) => {
|
||||
console.log(e);
|
||||
})
|
||||
.finally(() => {});
|
||||
},
|
||||
updateContainer() {
|
||||
if (document.documentElement.clientWidth >= 1400 && document.documentElement.clientWidth < 1920) {
|
||||
this.resizeTime = (document.documentElement.clientWidth / 2080).toFixed(2);
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"SamWaf/global"
|
||||
"SamWaf/model"
|
||||
"SamWaf/utils/zlog"
|
||||
"github.com/go-co-op/gocron"
|
||||
"go.uber.org/zap"
|
||||
"net/http"
|
||||
"strconv"
|
||||
@@ -12,6 +13,9 @@ import (
|
||||
|
||||
func main() {
|
||||
zlog.Info("初始化系统")
|
||||
if !global.GWAF_RELEASE {
|
||||
zlog.Info("调试版本")
|
||||
}
|
||||
/*runtime.GOMAXPROCS(1) // 限制 CPU 使用数,避免过载
|
||||
runtime.SetMutexProfileFraction(1) // 开启对锁调用的跟踪
|
||||
runtime.SetBlockProfileRate(1) // 开启对阻塞操作的跟踪
|
||||
@@ -68,6 +72,17 @@ func main() {
|
||||
}
|
||||
|
||||
}()
|
||||
|
||||
//定时器
|
||||
timezone, _ := time.LoadLocation("Asia/Shanghai")
|
||||
s := gocron.NewScheduler(timezone)
|
||||
|
||||
// 每秒执行一次
|
||||
s.Every(10).Seconds().Do(func() {
|
||||
zlog.Debug("i am alive")
|
||||
})
|
||||
s.StartAsync()
|
||||
|
||||
for {
|
||||
select {
|
||||
case remoteConfig := <-hostRuleChan:
|
||||
|
||||
+8
-8
@@ -17,7 +17,6 @@ import (
|
||||
"github.com/spf13/viper"
|
||||
"go.uber.org/zap"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
_ "net/http/pprof"
|
||||
@@ -81,9 +80,9 @@ func GetCountry(ip string) []string {
|
||||
return []string{"无", "无"}
|
||||
}
|
||||
|
||||
fmt.Printf("{region: %s, took: %s}\n", region, time.Since(tStart))
|
||||
zlog.Debug("{region: %s, took: %s}\n", region, time.Since(tStart))
|
||||
regions := strings.Split(region, "|")
|
||||
println(regions)
|
||||
|
||||
return regions
|
||||
/*if regions[0] == "中国" {
|
||||
return true
|
||||
@@ -154,7 +153,7 @@ func (h *baseHandle) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
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.Info("已经被限制访问了")
|
||||
zlog.Debug("已经被限制访问了")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -186,7 +185,7 @@ func (h *baseHandle) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
} else {
|
||||
fmt.Println("规则 ", err)
|
||||
zlog.Debug("规则 ", err)
|
||||
}
|
||||
}
|
||||
// 取出代理ip
|
||||
@@ -260,7 +259,7 @@ func Start_WAF() {
|
||||
global.GWAF_USER_CODE = config.GetString("user_code") // 读取配置
|
||||
global.GWAF_TENANT_ID = global.GWAF_USER_CODE
|
||||
global.GWAF_LOCAL_SERVER_PORT = config.GetInt("local_port") //读取本地端口
|
||||
fmt.Println(" load ini: ", global.GWAF_USER_CODE)
|
||||
zlog.Debug(" load ini: ", global.GWAF_USER_CODE)
|
||||
|
||||
var hosts []model.Hosts
|
||||
|
||||
@@ -274,7 +273,8 @@ func Start_WAF() {
|
||||
// 1、从 dbPath 加载整个 xdb 到内存
|
||||
cBuff, err := xdb.LoadContentFromFile(dbPath)
|
||||
if err != nil {
|
||||
fmt.Printf("failed to load content from `%s`: %s\n", dbPath, err)
|
||||
zlog.Info("加载ip库错误")
|
||||
zlog.Debug("failed to load content from `%s`: %s\n", dbPath, err)
|
||||
return
|
||||
}
|
||||
ipcBuff = cBuff
|
||||
@@ -401,7 +401,7 @@ func CLoseWAF() {
|
||||
defer func() {
|
||||
e := recover()
|
||||
if e != nil { // 捕获该协程的panic 111111
|
||||
log.Println("关闭 recover ", e)
|
||||
zlog.Debug("关闭 recover ", e)
|
||||
}
|
||||
}()
|
||||
engineCurrentStatus = 0
|
||||
|
||||
Reference in New Issue
Block a user