2020-10-08 15:06:42 +08:00
|
|
|
|
package utils
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2023-10-12 20:10:30 +08:00
|
|
|
|
teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
|
2022-01-08 11:45:14 +08:00
|
|
|
|
"github.com/TeaOSLab/EdgeNode/internal/re"
|
2023-10-12 20:10:30 +08:00
|
|
|
|
"github.com/TeaOSLab/EdgeNode/internal/utils/cachehits"
|
2023-10-11 12:21:10 +08:00
|
|
|
|
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
|
2024-05-08 11:10:56 +08:00
|
|
|
|
"github.com/TeaOSLab/EdgeNode/internal/utils/ttlcache"
|
2023-11-16 08:44:07 +08:00
|
|
|
|
"github.com/TeaOSLab/EdgeNode/internal/waf/requests"
|
2023-12-09 11:46:50 +08:00
|
|
|
|
"github.com/cespare/xxhash/v2"
|
2023-11-16 08:44:07 +08:00
|
|
|
|
"github.com/iwind/TeaGo/types"
|
|
|
|
|
|
stringutil "github.com/iwind/TeaGo/utils/string"
|
2020-10-08 15:06:42 +08:00
|
|
|
|
"strconv"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2023-12-09 11:46:50 +08:00
|
|
|
|
var SharedCache = ttlcache.NewCache[int8]()
|
2023-10-12 20:10:30 +08:00
|
|
|
|
var cacheHits *cachehits.Stat
|
|
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
|
if !teaconst.IsMain {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
cacheHits = cachehits.NewStat(5)
|
|
|
|
|
|
}
|
2020-10-08 15:06:42 +08:00
|
|
|
|
|
2023-10-11 12:21:10 +08:00
|
|
|
|
const (
|
2023-12-09 11:46:50 +08:00
|
|
|
|
MaxCacheDataSize = 1024
|
2023-10-11 12:21:10 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type CacheLife = int64
|
|
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
|
CacheDisabled CacheLife = 0
|
|
|
|
|
|
CacheShortLife CacheLife = 600
|
|
|
|
|
|
CacheMiddleLife CacheLife = 1800
|
|
|
|
|
|
CacheLongLife CacheLife = 7200
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2022-01-08 11:45:14 +08:00
|
|
|
|
// MatchStringCache 正则表达式匹配字符串,并缓存结果
|
2023-10-11 12:21:10 +08:00
|
|
|
|
func MatchStringCache(regex *re.Regexp, s string, cacheLife CacheLife) bool {
|
2022-12-14 12:27:07 +08:00
|
|
|
|
if regex == nil {
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-12 20:10:30 +08:00
|
|
|
|
var regIdString = regex.IdString()
|
|
|
|
|
|
|
2023-10-11 12:21:10 +08:00
|
|
|
|
// 如果长度超过一定数量,大概率是不能重用的
|
2023-12-09 11:46:50 +08:00
|
|
|
|
if cacheLife <= 0 || len(s) > MaxCacheDataSize || !cacheHits.IsGood(regIdString) {
|
2020-10-08 15:06:42 +08:00
|
|
|
|
return regex.MatchString(s)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-04-04 18:25:54 +08:00
|
|
|
|
var hash = xxhash.Sum64String(s)
|
2023-10-12 20:10:30 +08:00
|
|
|
|
var key = regIdString + "@" + strconv.FormatUint(hash, 10)
|
2023-12-09 11:46:50 +08:00
|
|
|
|
var item = SharedCache.Read(key)
|
2020-10-08 15:06:42 +08:00
|
|
|
|
if item != nil {
|
2023-10-12 20:10:30 +08:00
|
|
|
|
cacheHits.IncreaseHit(regIdString)
|
2023-10-05 08:28:16 +08:00
|
|
|
|
return item.Value == 1
|
2020-10-08 15:06:42 +08:00
|
|
|
|
}
|
2022-04-04 18:25:54 +08:00
|
|
|
|
var b = regex.MatchString(s)
|
2020-10-08 15:06:42 +08:00
|
|
|
|
if b {
|
2023-12-09 11:46:50 +08:00
|
|
|
|
SharedCache.Write(key, 1, fasttime.Now().Unix()+cacheLife)
|
2020-10-08 15:06:42 +08:00
|
|
|
|
} else {
|
2023-12-09 11:46:50 +08:00
|
|
|
|
SharedCache.Write(key, 0, fasttime.Now().Unix()+cacheLife)
|
2020-10-08 15:06:42 +08:00
|
|
|
|
}
|
2023-10-12 20:10:30 +08:00
|
|
|
|
cacheHits.IncreaseCached(regIdString)
|
2020-10-08 15:06:42 +08:00
|
|
|
|
return b
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-01-08 11:45:14 +08:00
|
|
|
|
// MatchBytesCache 正则表达式匹配字节slice,并缓存结果
|
2023-10-11 12:21:10 +08:00
|
|
|
|
func MatchBytesCache(regex *re.Regexp, byteSlice []byte, cacheLife CacheLife) bool {
|
2022-12-14 12:27:07 +08:00
|
|
|
|
if regex == nil {
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-12 20:10:30 +08:00
|
|
|
|
var regIdString = regex.IdString()
|
|
|
|
|
|
|
2023-10-11 12:21:10 +08:00
|
|
|
|
// 如果长度超过一定数量,大概率是不能重用的
|
2023-12-09 11:46:50 +08:00
|
|
|
|
if cacheLife <= 0 || len(byteSlice) > MaxCacheDataSize || !cacheHits.IsGood(regIdString) {
|
2020-10-08 15:06:42 +08:00
|
|
|
|
return regex.Match(byteSlice)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-04-04 18:25:54 +08:00
|
|
|
|
var hash = xxhash.Sum64(byteSlice)
|
2023-10-12 20:10:30 +08:00
|
|
|
|
var key = regIdString + "@" + strconv.FormatUint(hash, 10)
|
2023-12-09 11:46:50 +08:00
|
|
|
|
var item = SharedCache.Read(key)
|
2020-10-08 15:06:42 +08:00
|
|
|
|
if item != nil {
|
2023-10-12 20:10:30 +08:00
|
|
|
|
cacheHits.IncreaseHit(regIdString)
|
2023-10-05 08:28:16 +08:00
|
|
|
|
return item.Value == 1
|
2020-11-21 21:43:03 +08:00
|
|
|
|
}
|
2022-04-04 18:25:54 +08:00
|
|
|
|
var b = regex.Match(byteSlice)
|
2020-10-08 15:06:42 +08:00
|
|
|
|
if b {
|
2023-12-09 11:46:50 +08:00
|
|
|
|
SharedCache.Write(key, 1, fasttime.Now().Unix()+cacheLife)
|
2020-10-08 15:06:42 +08:00
|
|
|
|
} else {
|
2023-12-09 11:46:50 +08:00
|
|
|
|
SharedCache.Write(key, 0, fasttime.Now().Unix()+cacheLife)
|
2020-10-08 15:06:42 +08:00
|
|
|
|
}
|
2023-10-12 20:10:30 +08:00
|
|
|
|
cacheHits.IncreaseCached(regIdString)
|
2020-10-08 15:06:42 +08:00
|
|
|
|
return b
|
|
|
|
|
|
}
|
2023-11-16 08:44:07 +08:00
|
|
|
|
|
|
|
|
|
|
// ComposeIPType 组合IP类型
|
|
|
|
|
|
func ComposeIPType(setId int64, req requests.Request) string {
|
|
|
|
|
|
return "set:" + types.String(setId) + "@" + stringutil.Md5(req.WAFRaw().UserAgent())
|
|
|
|
|
|
}
|