From 3a9fec5196baf4e8038449ebc888ce905cabe742 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A5=A5=E8=B6=85?= Date: Sat, 13 Apr 2024 20:39:21 +0800 Subject: [PATCH] =?UTF-8?q?CC=E9=98=B2=E6=8A=A4=E5=A2=9E=E5=8A=A0=E2=80=9C?= =?UTF-8?q?=E5=BF=BD=E7=95=A5=E5=B8=B8=E7=94=A8=E6=96=87=E4=BB=B6=E2=80=9D?= =?UTF-8?q?=E9=80=89=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/utils/common_files.go | 38 +++++++++++++++++++++++++++++ internal/utils/common_files_test.go | 20 +++++++++++++++ internal/waf/checkpoints/cc2.go | 34 ++++++-------------------- 3 files changed, 65 insertions(+), 27 deletions(-) create mode 100644 internal/utils/common_files.go create mode 100644 internal/utils/common_files_test.go diff --git a/internal/utils/common_files.go b/internal/utils/common_files.go new file mode 100644 index 0000000..6fc2dc2 --- /dev/null +++ b/internal/utils/common_files.go @@ -0,0 +1,38 @@ +// Copyright 2024 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn . + +package utils + +import ( + "github.com/TeaOSLab/EdgeNode/internal/zero" + "strings" +) + +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(), + ".ttf": zero.New(), + ".otf": zero.New(), + ".fnt": zero.New(), + ".svg": zero.New(), + ".map": zero.New(), +} + +// IsCommonFileExtension 判断是否为常用文件扩展名 +// 不区分大小写,且不限于是否加点符号(.) +func IsCommonFileExtension(ext string) bool { + if len(ext) == 0 { + return false + } + if ext[0] != '.' { + ext = "." + ext + } + _, ok := commonFileExtensionsMap[strings.ToLower(ext)] + return ok +} diff --git a/internal/utils/common_files_test.go b/internal/utils/common_files_test.go new file mode 100644 index 0000000..7f019f6 --- /dev/null +++ b/internal/utils/common_files_test.go @@ -0,0 +1,20 @@ +// Copyright 2024 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn . + +package utils_test + +import ( + "github.com/TeaOSLab/EdgeNode/internal/utils" + "github.com/iwind/TeaGo/assert" + "testing" +) + +func TestIsCommonFileExtension(t *testing.T) { + var a = assert.NewAssertion(t) + + a.IsTrue(utils.IsCommonFileExtension(".jpg")) + a.IsTrue(utils.IsCommonFileExtension("png")) + a.IsTrue(utils.IsCommonFileExtension("PNG")) + a.IsTrue(utils.IsCommonFileExtension(".PNG")) + a.IsTrue(utils.IsCommonFileExtension("Png")) + a.IsFalse(utils.IsCommonFileExtension("zip")) +} diff --git a/internal/waf/checkpoints/cc2.go b/internal/waf/checkpoints/cc2.go index 03857b3..d1abd58 100644 --- a/internal/waf/checkpoints/cc2.go +++ b/internal/waf/checkpoints/cc2.go @@ -4,32 +4,16 @@ package checkpoints import ( "fmt" + "github.com/TeaOSLab/EdgeNode/internal/utils" "github.com/TeaOSLab/EdgeNode/internal/utils/counters" "github.com/TeaOSLab/EdgeNode/internal/waf/requests" - "github.com/TeaOSLab/EdgeNode/internal/waf/utils" - "github.com/TeaOSLab/EdgeNode/internal/zero" + wafutils "github.com/TeaOSLab/EdgeNode/internal/waf/utils" "github.com/iwind/TeaGo/maps" "github.com/iwind/TeaGo/types" "path/filepath" "strings" ) -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(), - ".ttf": zero.New(), - ".otf": zero.New(), - ".fnt": zero.New(), - ".svg": zero.New(), -} - // CC2Checkpoint 新的CC type CC2Checkpoint struct { Checkpoint @@ -61,16 +45,12 @@ func (this *CC2Checkpoint) RequestValue(req requests.Request, param string, opti threshold = 1000 }**/ - var ignoreCommonFiles = options.GetBool("ignoreCommonFiles") - if ignoreCommonFiles { + if options.GetBool("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 - } + if len(ext) > 0 && utils.IsCommonFileExtension(ext) { + return } } } @@ -114,6 +94,6 @@ func (this *CC2Checkpoint) ResponseValue(req requests.Request, resp *requests.Re return } -func (this *CC2Checkpoint) CacheLife() utils.CacheLife { - return utils.CacheDisabled +func (this *CC2Checkpoint) CacheLife() wafutils.CacheLife { + return wafutils.CacheDisabled }