反向代理源站实现使用域名分组

This commit is contained in:
刘祥超
2021-09-20 11:54:21 +08:00
parent bf57bc5a1c
commit 1f6be53d3d
18 changed files with 431 additions and 186 deletions

View File

@@ -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:]))

View File

@@ -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)
}
}