Files
EdgeCommon/pkg/configutils/domain.go

94 lines
2.0 KiB
Go
Raw Permalink Normal View History

2020-09-13 19:27:47 +08:00
package configutils
import (
2024-07-27 13:29:26 +08:00
"strings"
2020-09-13 19:27:47 +08:00
"github.com/iwind/TeaGo/logs"
"github.com/iwind/TeaGo/utils/string"
)
// MatchDomains 从一组规则中匹配域名
2020-09-13 19:27:47 +08:00
// 支持的格式example.com, www.example.com, .example.com, *.example.com, ~(\d+).example.com
// 更多参考http://nginx.org/en/docs/http/ngx_http_core_module.html#server_name
func MatchDomains(patterns []string, domain string) (isMatched bool) {
if len(patterns) == 0 {
return
}
for _, pattern := range patterns {
if MatchDomain(pattern, domain) {
2020-09-13 19:27:47 +08:00
return true
}
}
return
}
// MatchDomain 匹配单个域名规则
func MatchDomain(pattern string, domain string) (isMatched bool) {
2020-09-13 19:27:47 +08:00
if len(pattern) == 0 {
return
}
if pattern == domain {
return true
}
if pattern == "*" {
return true
}
2020-09-13 19:27:47 +08:00
// 正则表达式
if pattern[0] == '~' {
reg, err := stringutil.RegexpCompile(strings.TrimSpace(pattern[1:]))
if err != nil {
logs.Error(err)
return false
}
return reg.MatchString(domain)
}
if pattern[0] == '.' {
return strings.HasSuffix(domain, pattern)
}
// 其他匹配
2022-11-09 18:21:05 +08:00
var patternPieces = strings.Split(pattern, ".")
var domainPieces = strings.Split(domain, ".")
2020-09-13 19:27:47 +08:00
if len(patternPieces) != len(domainPieces) {
return
}
isMatched = true
for index, patternPiece := range patternPieces {
if patternPiece == "" || patternPiece == "*" || patternPiece == domainPieces[index] {
continue
}
2022-11-09 18:21:05 +08:00
if strings.HasSuffix(patternPiece, ":*") {
var portIndex = strings.LastIndex(patternPiece, ":*")
if portIndex >= 0 {
var prefix = patternPiece[:portIndex]
if strings.HasPrefix(domainPieces[index], prefix+":") || domainPieces[index] == prefix {
2022-11-09 18:21:05 +08:00
continue
}
}
}
2020-09-13 19:27:47 +08:00
isMatched = false
break
}
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
}