mirror of
https://github.com/TeaOSLab/EdgeCommon.git
synced 2026-04-29 10:15:17 +08:00
5秒盾支持例外URL和限制URL
This commit is contained in:
@@ -103,3 +103,16 @@ func (this *HTTPHeaderConfig) Init() error {
|
||||
func (this *HTTPHeaderConfig) HasVariables() bool {
|
||||
return this.hasVariables
|
||||
}
|
||||
|
||||
// Match 判断是否匹配状态码
|
||||
func (this *HTTPHeaderConfig) Match(statusCode int) bool {
|
||||
if !this.IsOn {
|
||||
return false
|
||||
}
|
||||
|
||||
if this.Status == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return this.Status.Match(statusCode)
|
||||
}
|
||||
|
||||
66
pkg/serverconfigs/shared/url_pattern.go
Normal file
66
pkg/serverconfigs/shared/url_pattern.go
Normal file
@@ -0,0 +1,66 @@
|
||||
// Copyright 2023 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
|
||||
package shared
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type URLPatternType = string
|
||||
|
||||
const (
|
||||
URLPatternTypeWildcard URLPatternType = "wildcard" // 通配符
|
||||
URLPatternTypeRegexp URLPatternType = "regexp" // 正则表达式
|
||||
)
|
||||
|
||||
type URLPattern struct {
|
||||
Type URLPatternType `yaml:"type" json:"type"`
|
||||
Pattern string `yaml:"pattern" json:"pattern"`
|
||||
|
||||
reg *regexp.Regexp
|
||||
}
|
||||
|
||||
func (this *URLPattern) Init() error {
|
||||
if len(this.Pattern) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
switch this.Type {
|
||||
case URLPatternTypeWildcard:
|
||||
// 只支持星号
|
||||
var pieces = strings.Split(this.Pattern, "*")
|
||||
for index, piece := range pieces {
|
||||
pieces[index] = regexp.QuoteMeta(piece)
|
||||
}
|
||||
reg, err := regexp.Compile("(?i)" /** 大小写不敏感 **/ + strings.Join(pieces, "(.*)"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
this.reg = reg
|
||||
case URLPatternTypeRegexp:
|
||||
var pattern = this.Pattern
|
||||
if !strings.HasPrefix(pattern, "(?i)") { // 大小写不敏感
|
||||
pattern = "(?i)" + pattern
|
||||
}
|
||||
reg, err := regexp.Compile(pattern)
|
||||
if err != nil {
|
||||
return errors.New("compile '" + pattern + "' failed: " + err.Error())
|
||||
}
|
||||
this.reg = reg
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *URLPattern) Match(url string) bool {
|
||||
if len(this.Pattern) == 0 && len(url) == 0 {
|
||||
return true
|
||||
}
|
||||
|
||||
if this.reg != nil {
|
||||
return this.reg.MatchString(url)
|
||||
}
|
||||
return false
|
||||
}
|
||||
93
pkg/serverconfigs/shared/url_pattern_test.go
Normal file
93
pkg/serverconfigs/shared/url_pattern_test.go
Normal file
@@ -0,0 +1,93 @@
|
||||
// Copyright 2023 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
|
||||
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
|
||||
url string
|
||||
result bool
|
||||
}
|
||||
|
||||
for _, ut := range []*unitTest{
|
||||
{
|
||||
patternType: "wildcard",
|
||||
pattern: "*",
|
||||
url: "https://example.com",
|
||||
result: true,
|
||||
},
|
||||
{
|
||||
patternType: "wildcard",
|
||||
pattern: "https://example*",
|
||||
url: "https://example.com",
|
||||
result: true,
|
||||
},
|
||||
{
|
||||
patternType: "wildcard",
|
||||
pattern: "*com",
|
||||
url: "https://example.com",
|
||||
result: true,
|
||||
},
|
||||
{
|
||||
patternType: "wildcard",
|
||||
pattern: "*COM",
|
||||
url: "https://example.com",
|
||||
result: true,
|
||||
},
|
||||
{
|
||||
patternType: "wildcard",
|
||||
pattern: "http://*",
|
||||
url: "https://example.com",
|
||||
result: false,
|
||||
},
|
||||
{
|
||||
patternType: "regexp",
|
||||
pattern: ".*",
|
||||
url: "https://example.com",
|
||||
result: true,
|
||||
},
|
||||
{
|
||||
patternType: "regexp",
|
||||
pattern: "^https://.*",
|
||||
url: "https://example.com",
|
||||
result: true,
|
||||
},
|
||||
{
|
||||
patternType: "regexp",
|
||||
pattern: "^https://.*EXAMPLE.COM",
|
||||
url: "https://example.com",
|
||||
result: true,
|
||||
},
|
||||
{
|
||||
patternType: "regexp",
|
||||
pattern: "(?i)https://.*EXAMPLE.COM/\\d+",
|
||||
url: "https://example.com/123456",
|
||||
result: true,
|
||||
},
|
||||
{
|
||||
patternType: "regexp",
|
||||
pattern: "(?i)https://.*EXAMPLE.COM/\\d+$",
|
||||
url: "https://example.com/123456/789",
|
||||
result: false,
|
||||
},
|
||||
} {
|
||||
var p = &shared.URLPattern{
|
||||
Type: ut.patternType,
|
||||
Pattern: ut.pattern,
|
||||
}
|
||||
err := p.Init()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
a.IsTrue(p.Match(ut.url) == ut.result)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user