优化域名匹配,现在 example.com:* 可以匹配 example.com

This commit is contained in:
刘祥超
2023-01-10 10:03:43 +08:00
parent 0863912e0c
commit c024049778
2 changed files with 9 additions and 1 deletions

View File

@@ -64,7 +64,7 @@ func MatchDomain(pattern string, domain string) (isMatched bool) {
var portIndex = strings.LastIndex(patternPiece, ":*") var portIndex = strings.LastIndex(patternPiece, ":*")
if portIndex >= 0 { if portIndex >= 0 {
var prefix = patternPiece[:portIndex] var prefix = patternPiece[:portIndex]
if strings.HasPrefix(domainPieces[index], prefix+":") { if strings.HasPrefix(domainPieces[index], prefix+":") || domainPieces[index] == prefix {
continue continue
} }
} }

View File

@@ -94,6 +94,14 @@ func TestMatchDomain(t *testing.T) {
var ok = MatchDomains([]string{"*.example.com:8001"}, "a.example.com:8001") var ok = MatchDomains([]string{"*.example.com:8001"}, "a.example.com:8001")
a.IsTrue(ok) a.IsTrue(ok)
} }
{
var ok = MatchDomains([]string{"a.example.com:*"}, "a.example.com:8001")
a.IsTrue(ok)
}
{
var ok = MatchDomains([]string{"a.example.com:*"}, "a.example.com")
a.IsTrue(ok)
}
{ {
var ok = MatchDomains([]string{"*.example.com:*"}, "a.example.com:8001") var ok = MatchDomains([]string{"*.example.com:*"}, "a.example.com:8001")
a.IsTrue(ok) a.IsTrue(ok)