增加CC防护相关API定义、配置

This commit is contained in:
GoEdgeLab
2023-03-09 12:10:31 +08:00
parent 0145758e70
commit 0e49f880e1
7 changed files with 647 additions and 306 deletions

View File

@@ -34,7 +34,11 @@ func (this *URLPattern) Init() error {
for index, piece := range pieces {
pieces[index] = regexp.QuoteMeta(piece)
}
reg, err := regexp.Compile("(?i)" /** 大小写不敏感 **/ + "^" + strings.Join(pieces, "(.*)") + "$")
var pattern = strings.Join(pieces, "(.*)")
if len(pattern) > 0 && pattern[0] == '/' {
pattern = "(http|https)://[\\w.-]+" + pattern
}
reg, err := regexp.Compile("(?i)" /** 大小写不敏感 **/ + "^" + pattern + "$")
if err != nil {
return err
}

View File

@@ -4,13 +4,10 @@ package shared_test
import (
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
"github.com/iwind/TeaGo/assert"
"testing"
)
func TestURLPattern_Match(t *testing.T) {
var a = assert.NewAssertion(t)
type unitTest struct {
patternType string
pattern string
@@ -55,6 +52,24 @@ func TestURLPattern_Match(t *testing.T) {
url: "https://example.com",
result: false,
},
{
patternType: "wildcard",
pattern: "https://example.com",
url: "https://example.com",
result: true,
},
{
patternType: "wildcard",
pattern: "/hello/world",
url: "https://example-test.com/hello/world",
result: true,
},
{
patternType: "wildcard",
pattern: "/hello/world",
url: "https://example-test.com/123/hello/world",
result: false,
},
{
patternType: "regexp",
pattern: ".*",
@@ -94,6 +109,9 @@ func TestURLPattern_Match(t *testing.T) {
if err != nil {
t.Fatal(err)
}
a.IsTrue(p.Match(ut.url) == ut.result)
var b = p.Match(ut.url) == ut.result
if !b {
t.Fatal("not matched pattern:", ut.pattern, "url:", ut.url)
}
}
}