From bcb4592549eda5736eb366dd4262c9816f98ef97 Mon Sep 17 00:00:00 2001 From: GoEdgeLab Date: Sat, 8 Jan 2022 12:20:18 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=AD=A3=E5=88=99=E8=A1=A8?= =?UTF-8?q?=E8=BE=BE=E5=BC=8F/=E4=BF=AE=E5=A4=8D=E4=B8=80=E4=BA=9B?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/re/regexp.go | 16 +++++++++++++++- internal/waf/utils/utils_test.go | 24 +++++++++++++++++------- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/internal/re/regexp.go b/internal/re/regexp.go index 2762194..cf978d5 100644 --- a/internal/re/regexp.go +++ b/internal/re/regexp.go @@ -7,7 +7,7 @@ import ( "strings" ) -var prefixReg = regexp.MustCompile(`^\(\?(\w+)\)`) // (?x) +var prefixReg = regexp.MustCompile(`^\(\?([\w\s]+)\)`) // (?x) var prefixReg2 = regexp.MustCompile(`^\(\?([\w\s]*:)`) // (?x: ... var braceZero = regexp.MustCompile(`^{\s*0*\s*}`) // {0} var braceZero2 = regexp.MustCompile(`^{\s*0*\s*,`) // {0, x} @@ -18,6 +18,7 @@ type Regexp struct { isStrict bool isCaseInsensitive bool + keywords []string keywordsMap RuneMap } @@ -67,11 +68,24 @@ func (this *Regexp) init() { } var keywords = this.ParseKeywords(exp) + this.keywords = keywords if len(keywords) > 0 { this.keywordsMap = NewRuneTree(keywords) } } +func (this *Regexp) Keywords() []string { + return this.keywords +} + +func (this *Regexp) Raw() *regexp.Regexp { + return this.rawRegexp +} + +func (this *Regexp) IsCaseInsensitive() bool { + return this.isCaseInsensitive +} + func (this *Regexp) MatchString(s string) bool { if this.keywordsMap != nil { var b = this.keywordsMap.Lookup(s, this.isCaseInsensitive) diff --git a/internal/waf/utils/utils_test.go b/internal/waf/utils/utils_test.go index 991ac85..4ee83bd 100644 --- a/internal/waf/utils/utils_test.go +++ b/internal/waf/utils/utils_test.go @@ -1,6 +1,7 @@ package utils import ( + "github.com/TeaOSLab/EdgeNode/internal/re" "net/http" "regexp" "runtime" @@ -11,14 +12,14 @@ import ( ) func TestMatchStringCache(t *testing.T) { - regex := regexp.MustCompile(`\d+`) + regex := re.MustCompile(`\d+`) t.Log(MatchStringCache(regex, "123")) t.Log(MatchStringCache(regex, "123")) t.Log(MatchStringCache(regex, "123")) } func TestMatchBytesCache(t *testing.T) { - regex := regexp.MustCompile(`\d+`) + regex := re.MustCompile(`\d+`) t.Log(MatchBytesCache(regex, []byte("123"))) t.Log(MatchBytesCache(regex, []byte("123"))) t.Log(MatchBytesCache(regex, []byte("123"))) @@ -52,7 +53,7 @@ func BenchmarkMatchStringCache(b *testing.B) { runtime.GOMAXPROCS(1) data := strings.Repeat("HELLO", 512) - regex := regexp.MustCompile(`(?iU)\b(eval|system|exec|execute|passthru|shell_exec|phpinfo)\b`) + regex := re.MustCompile(`(?iU)\b(eval|system|exec|execute|passthru|shell_exec|phpinfo)\b`) _ = MatchStringCache(regex, data) for i := 0; i < b.N; i++ { @@ -63,9 +64,18 @@ func BenchmarkMatchStringCache(b *testing.B) { func BenchmarkMatchStringCache_WithoutCache(b *testing.B) { runtime.GOMAXPROCS(1) - data := strings.Repeat("HELLO", 512) - regex := regexp.MustCompile(`(?iU)\b(eval|system|exec|execute|passthru|shell_exec|phpinfo)\b`) + data := strings.Repeat("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36", 8) + regex := re.MustCompile(`(?iU)\b(eval|system|exec|execute|passthru|shell_exec|phpinfo)\b`) + for i := 0; i < b.N; i++ { + _ = regex.MatchString(data) + } +} +func BenchmarkMatchStringCache_WithoutCache2(b *testing.B) { + runtime.GOMAXPROCS(1) + + data := strings.Repeat("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36", 8) + regex := regexp.MustCompile(`(?iU)\b(eval|system|exec|execute|passthru|shell_exec|phpinfo)\b`) for i := 0; i < b.N; i++ { _ = regex.MatchString(data) } @@ -74,8 +84,8 @@ func BenchmarkMatchStringCache_WithoutCache(b *testing.B) { func BenchmarkMatchBytesCache_WithoutCache(b *testing.B) { runtime.GOMAXPROCS(1) - data := []byte(strings.Repeat("HELLO", 512)) - regex := regexp.MustCompile(`(?iU)\b(eval|system|exec|execute|passthru|shell_exec|phpinfo)\b`) + data := []byte(strings.Repeat("HELLO", 128)) + regex := re.MustCompile(`(?iU)\b(eval|system|exec|execute|passthru|shell_exec|phpinfo)\b`) for i := 0; i < b.N; i++ { _ = regex.Match(data)