采用更快的Hash算法

This commit is contained in:
刘祥超
2020-11-21 22:29:57 +08:00
parent 13615f8046
commit d2d0dece4e
12 changed files with 40 additions and 35 deletions

View File

@@ -2,6 +2,7 @@ package checkpoints
import (
"github.com/TeaOSLab/EdgeNode/internal/waf/requests"
"github.com/iwind/TeaGo/maps"
)
// just a sample checkpoint, copy and change it for your new checkpoint
@@ -9,11 +10,11 @@ type SampleRequestCheckpoint struct {
Checkpoint
}
func (this *SampleRequestCheckpoint) RequestValue(req *requests.Request, param string, options map[string]string) (value interface{}, sysErr error, userErr error) {
func (this *SampleRequestCheckpoint) RequestValue(req *requests.Request, param string, options maps.Map) (value interface{}, sysErr error, userErr error) {
return
}
func (this *SampleRequestCheckpoint) ResponseValue(req *requests.Request, resp *requests.Response, param string, options map[string]string) (value interface{}, sysErr error, userErr error) {
func (this *SampleRequestCheckpoint) ResponseValue(req *requests.Request, resp *requests.Response, param string, options maps.Map) (value interface{}, sysErr error, userErr error) {
if this.IsRequest() {
return this.RequestValue(req, param, options)
}

View File

@@ -3,7 +3,7 @@ package waf
import (
"bytes"
"github.com/TeaOSLab/EdgeNode/internal/waf/requests"
"github.com/dchest/siphash"
"github.com/cespare/xxhash"
"github.com/iwind/TeaGo/assert"
"net/http"
"regexp"
@@ -174,7 +174,9 @@ func BenchmarkRuleSet_MatchRequest_Regexp3(b *testing.B) {
}
func BenchmarkHash(b *testing.B) {
runtime.GOMAXPROCS(1)
for i := 0; i < b.N; i++ {
_ = siphash.Hash(0, 0, bytes.Repeat([]byte("HELLO"), 10240))
_ = xxhash.Sum64(bytes.Repeat([]byte("HELLO"), 10240))
}
}

View File

@@ -318,7 +318,7 @@ func testTemplate20001(a *assert.Assertion, t *testing.T, template *WAF) {
// enable bot rule set
for _, g := range template.Inbound {
if g.Code == "bot" {
g.On = true
g.IsOn = true
break
}
}

View File

@@ -3,8 +3,7 @@ package utils
import (
"fmt"
"github.com/TeaOSLab/EdgeNode/internal/ttlcache"
"github.com/TeaOSLab/EdgeNode/internal/utils"
"github.com/dchest/siphash"
"github.com/cespare/xxhash"
"github.com/iwind/TeaGo/types"
"regexp"
"strconv"
@@ -21,7 +20,7 @@ func MatchStringCache(regex *regexp.Regexp, s string) bool {
return regex.MatchString(s)
}
hash := siphash.Hash(0, 0, utils.UnsafeStringToBytes(s))
hash := xxhash.Sum64String(s)
key := fmt.Sprintf("%p_", regex) + strconv.FormatUint(hash, 10)
item := cache.Read(key)
if item != nil {
@@ -43,7 +42,7 @@ func MatchBytesCache(regex *regexp.Regexp, byteSlice []byte) bool {
return regex.Match(byteSlice)
}
hash := siphash.Hash(0, 0, byteSlice)
hash := xxhash.Sum64(byteSlice)
key := fmt.Sprintf("%p_", regex) + strconv.FormatUint(hash, 10)
item := cache.Read(key)
if item != nil {