mirror of
https://github.com/TeaOSLab/EdgeCommon.git
synced 2026-01-06 03:46:17 +08:00
提升通过域名查找服务的性能,轻松支持海量域名
This commit is contained in:
@@ -27,6 +27,10 @@ func MatchDomain(pattern string, domain string) (isMatched bool) {
|
||||
return
|
||||
}
|
||||
|
||||
if pattern == domain {
|
||||
return true
|
||||
}
|
||||
|
||||
if pattern == "*" {
|
||||
return true
|
||||
}
|
||||
@@ -61,3 +65,19 @@ func MatchDomain(pattern string, domain string) (isMatched bool) {
|
||||
}
|
||||
return isMatched
|
||||
}
|
||||
|
||||
// IsFuzzyDomain 判断是否为特殊域名
|
||||
func IsFuzzyDomain(domain string) bool {
|
||||
if len(domain) == 0 {
|
||||
return true
|
||||
}
|
||||
if domain[0] == '.' || domain[0] == '~' {
|
||||
return true
|
||||
}
|
||||
for _, c := range domain {
|
||||
if c == '*' {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -81,3 +81,14 @@ func TestMatchDomain(t *testing.T) {
|
||||
a.IsTrue(ok)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsSpecialDomain(t *testing.T) {
|
||||
var a = assert.NewAssertion(t)
|
||||
|
||||
a.IsTrue(IsFuzzyDomain(""))
|
||||
a.IsTrue(IsFuzzyDomain(".hello.com"))
|
||||
a.IsTrue(IsFuzzyDomain("*.hello.com"))
|
||||
a.IsTrue(IsFuzzyDomain("hello.*.com"))
|
||||
a.IsTrue(IsFuzzyDomain("~^hello\\.com"))
|
||||
a.IsFalse(IsFuzzyDomain("hello.com"))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user