diff --git a/internal/caches/reader_file.go b/internal/caches/reader_file.go index 0b13a0b..115724c 100644 --- a/internal/caches/reader_file.go +++ b/internal/caches/reader_file.go @@ -338,6 +338,11 @@ func (this *FileReader) ContainsRange(r rangeutils.Range) (r2 rangeutils.Range, return r, true } +// FP 原始的文件句柄 +func (this *FileReader) FP() *os.File { + return this.fp +} + func (this *FileReader) Close() error { if this.openFileCache != nil { if this.isClosed { diff --git a/internal/metrics/stat.go b/internal/metrics/stat.go index 759872d..750359e 100644 --- a/internal/metrics/stat.go +++ b/internal/metrics/stat.go @@ -3,9 +3,9 @@ package metrics import ( - "encoding/json" - "github.com/cespare/xxhash" + "github.com/TeaOSLab/EdgeNode/internal/utils/fnv" "strconv" + "strings" ) type Stat struct { @@ -17,6 +17,6 @@ type Stat struct { } func SumStat(serverId int64, keys []string, time string, version int32, itemId int64) string { - keysData, _ := json.Marshal(keys) - return strconv.FormatUint(xxhash.Sum64String(strconv.FormatInt(serverId, 10)+"@"+string(keysData)+"@"+time+"@"+strconv.Itoa(int(version))+"@"+strconv.FormatInt(itemId, 10)), 10) + keysData := strings.Join(keys, "$EDGE$") + return strconv.FormatUint(fnv.HashString(strconv.FormatInt(serverId, 10)+"@"+keysData+"@"+time+"@"+strconv.Itoa(int(version))+"@"+strconv.FormatInt(itemId, 10)), 10) } diff --git a/internal/re/regexp.go b/internal/re/regexp.go index 2bb9938..6eb6680 100644 --- a/internal/re/regexp.go +++ b/internal/re/regexp.go @@ -3,9 +3,11 @@ package re import ( + "github.com/iwind/TeaGo/types" "regexp" "regexp/syntax" "strings" + "sync/atomic" ) var prefixReg = regexp.MustCompile(`^\(\?([\w\s]+)\)`) // (?x) @@ -13,6 +15,8 @@ var prefixReg2 = regexp.MustCompile(`^\(\?([\w\s]*:)`) // (?x: ... var braceZeroReg = regexp.MustCompile(`^{\s*0*\s*}`) // {0} var braceZeroReg2 = regexp.MustCompile(`^{\s*0*\s*,`) // {0, x} +var lastId uint64 + type Regexp struct { exp string rawRegexp *regexp.Regexp @@ -21,6 +25,9 @@ type Regexp struct { isCaseInsensitive bool keywords []string keywordsMap RuneMap + + id uint64 + idString string } func MustCompile(exp string) *Regexp { @@ -50,6 +57,9 @@ func NewRegexp(rawRegexp *regexp.Regexp) *Regexp { } func (this *Regexp) init() { + this.id = atomic.AddUint64(&lastId, 1) + this.idString = "re:" + types.String(this.id) + if len(this.exp) == 0 { return } @@ -202,6 +212,10 @@ func (this *Regexp) ParseKeywords(exp string) (keywords []string) { return } +func (this *Regexp) IdString() string { + return this.idString +} + func (this *Regexp) parseKeyword(subExp string) (result []rune) { if len(subExp) == 0 { return nil diff --git a/internal/waf/utils/utils.go b/internal/waf/utils/utils.go index c223ee1..975d0c3 100644 --- a/internal/waf/utils/utils.go +++ b/internal/waf/utils/utils.go @@ -1,7 +1,6 @@ package utils import ( - "fmt" "github.com/TeaOSLab/EdgeNode/internal/re" "github.com/TeaOSLab/EdgeNode/internal/ttlcache" "github.com/cespare/xxhash" @@ -19,13 +18,13 @@ func MatchStringCache(regex *re.Regexp, s string) bool { return regex.MatchString(s) } - hash := xxhash.Sum64String(s) - key := fmt.Sprintf("%p_", regex) + strconv.FormatUint(hash, 10) - item := cache.Read(key) + var hash = xxhash.Sum64String(s) + var key = regex.IdString() + "@" + strconv.FormatUint(hash, 10) + var item = cache.Read(key) if item != nil { return types.Int8(item.Value) == 1 } - b := regex.MatchString(s) + var b = regex.MatchString(s) if b { cache.Write(key, 1, time.Now().Unix()+1800) } else { @@ -41,16 +40,16 @@ func MatchBytesCache(regex *re.Regexp, byteSlice []byte) bool { return regex.Match(byteSlice) } - hash := xxhash.Sum64(byteSlice) - key := fmt.Sprintf("%p_", regex) + strconv.FormatUint(hash, 10) - item := cache.Read(key) + var hash = xxhash.Sum64(byteSlice) + var key = regex.IdString() + "@" + strconv.FormatUint(hash, 10) + var item = cache.Read(key) if item != nil { return types.Int8(item.Value) == 1 } if item != nil { return types.Int8(item.Value) == 1 } - b := regex.Match(byteSlice) + var b = regex.Match(byteSlice) if b { cache.Write(key, 1, time.Now().Unix()+1800) } else {