mirror of
https://github.com/TeaOSLab/EdgeCommon.git
synced 2026-04-08 08:25:21 +08:00
反向代理源站实现使用域名分组
This commit is contained in:
@@ -6,7 +6,7 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// 从一组规则中匹配域名
|
||||
// MatchDomains 从一组规则中匹配域名
|
||||
// 支持的格式: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) {
|
||||
@@ -14,19 +14,23 @@ func MatchDomains(patterns []string, domain string) (isMatched bool) {
|
||||
return
|
||||
}
|
||||
for _, pattern := range patterns {
|
||||
if matchDomain(pattern, domain) {
|
||||
if MatchDomain(pattern, domain) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// 匹配单个域名规则
|
||||
func matchDomain(pattern string, domain string) (isMatched bool) {
|
||||
// MatchDomain 匹配单个域名规则
|
||||
func MatchDomain(pattern string, domain string) (isMatched bool) {
|
||||
if len(pattern) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
if pattern == "*" {
|
||||
return true
|
||||
}
|
||||
|
||||
// 正则表达式
|
||||
if pattern[0] == '~' {
|
||||
reg, err := stringutil.RegexpCompile(strings.TrimSpace(pattern[1:]))
|
||||
|
||||
@@ -76,4 +76,8 @@ func TestMatchDomain(t *testing.T) {
|
||||
ok := MatchDomains([]string{"~^\\d+.example.com$"}, "123.example.com")
|
||||
a.IsTrue(ok)
|
||||
}
|
||||
{
|
||||
ok := MatchDomains([]string{"*"}, "example.com")
|
||||
a.IsTrue(ok)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user