Files
EdgeNode/internal/waf/checkpoints/cc2.go

108 lines
2.8 KiB
Go
Raw Normal View History

2021-07-19 10:49:56 +08:00
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package checkpoints
import (
2023-03-08 16:59:44 +08:00
"fmt"
2021-07-19 10:49:56 +08:00
"github.com/TeaOSLab/EdgeNode/internal/ttlcache"
"github.com/TeaOSLab/EdgeNode/internal/waf/requests"
"github.com/TeaOSLab/EdgeNode/internal/zero"
2021-07-19 10:49:56 +08:00
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/types"
"path/filepath"
2021-07-19 10:49:56 +08:00
"strings"
"time"
)
2022-04-09 18:44:51 +08:00
var ccCache = ttlcache.NewCache()
2021-07-19 10:49:56 +08:00
var commonFileExtensionsMap = map[string]zero.Zero{
".ico": zero.New(),
".jpg": zero.New(),
".jpeg": zero.New(),
".gif": zero.New(),
".png": zero.New(),
".webp": zero.New(),
".woff2": zero.New(),
".js": zero.New(),
".css": zero.New(),
}
2021-07-19 10:49:56 +08:00
// CC2Checkpoint 新的CC
type CC2Checkpoint struct {
Checkpoint
}
func (this *CC2Checkpoint) RequestValue(req requests.Request, param string, options maps.Map, ruleId int64) (value interface{}, hasRequestBody bool, sysErr error, userErr error) {
2021-07-19 10:49:56 +08:00
var keys = options.GetSlice("keys")
var keyValues = []string{}
2023-03-08 16:59:44 +08:00
var hasRemoteAddr = false
2021-07-19 10:49:56 +08:00
for _, key := range keys {
2023-03-08 16:59:44 +08:00
if key == "${remoteAddr}" || key == "${rawRemoteAddr}" {
hasRemoteAddr = true
}
2021-07-19 10:49:56 +08:00
keyValues = append(keyValues, req.Format(types.String(key)))
}
if len(keyValues) == 0 {
return
}
var period = options.GetInt64("period")
if period <= 0 {
period = 60
}
var threshold = options.GetInt64("threshold")
if threshold <= 0 {
threshold = 1000
}
var ignoreCommonFiles = options.GetBool("ignoreCommonFiles")
if ignoreCommonFiles {
var rawReq = req.WAFRaw()
if len(rawReq.Referer()) > 0 {
var ext = filepath.Ext(rawReq.URL.Path)
if len(ext) > 0 {
_, ok := commonFileExtensionsMap[strings.ToLower(ext)]
if ok {
return
}
}
}
}
2023-03-08 16:59:44 +08:00
var expiresAt = time.Now().Unix() + period
var ccKey = "WAF-CC-" + types.String(ruleId) + "-" + strings.Join(keyValues, "@")
2023-03-08 16:59:44 +08:00
value = ccCache.IncreaseInt64(ccKey, 1, expiresAt, false)
// 基于指纹统计
if hasRemoteAddr {
var fingerprint = req.WAFFingerprint()
if len(fingerprint) > 0 {
var fpKeyValues = []string{}
for _, key := range keys {
if key == "${remoteAddr}" || key == "${rawRemoteAddr}" {
fpKeyValues = append(fpKeyValues, fmt.Sprintf("%x", fingerprint))
continue
}
fpKeyValues = append(fpKeyValues, req.Format(types.String(key)))
}
var fpCCKey = "WAF-CC-" + types.String(ruleId) + "-" + strings.Join(fpKeyValues, "@")
var fpValue = ccCache.IncreaseInt64(fpCCKey, 1, expiresAt, false)
if fpValue > value.(int64) {
value = fpValue
}
}
}
2021-07-19 10:49:56 +08:00
return
}
func (this *CC2Checkpoint) ResponseValue(req requests.Request, resp *requests.Response, param string, options maps.Map, ruleId int64) (value interface{}, hasRequestBody bool, sysErr error, userErr error) {
if this.IsRequest() {
return this.RequestValue(req, param, options, ruleId)
}
2021-07-19 10:49:56 +08:00
return
}