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 }