支持中文域名(转换punycode后)访问

This commit is contained in:
刘祥超
2022-09-07 17:08:59 +08:00
parent f725f3bb07
commit f796dc5921

View File

@@ -6,8 +6,12 @@ import (
"github.com/TeaOSLab/EdgeCommon/pkg/configutils" "github.com/TeaOSLab/EdgeCommon/pkg/configutils"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/sslconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/sslconfigs"
"golang.org/x/net/idna"
"regexp"
) )
var normalServerNameReg = regexp.MustCompile(`^[a-zA-Z0-9.-]+$`)
type ServerConfig struct { type ServerConfig struct {
Id int64 `yaml:"id" json:"id"` // ID Id int64 `yaml:"id" json:"id"` // ID
ClusterId int64 `yaml:"clusterId" json:"clusterId"` // 集群ID ClusterId int64 `yaml:"clusterId" json:"clusterId"` // 集群ID
@@ -341,6 +345,14 @@ func (this *ServerConfig) AllStrictNames() []string {
if len(name) > 0 { if len(name) > 0 {
if !configutils.IsFuzzyDomain(name) { if !configutils.IsFuzzyDomain(name) {
result = append(result, name) result = append(result, name)
// unicode domain
if !normalServerNameReg.MatchString(name) {
asciiName, err := idna.ToASCII(name)
if err == nil && len(asciiName) > 0 {
result = append(result, asciiName)
}
}
} }
} }
} }
@@ -349,12 +361,28 @@ func (this *ServerConfig) AllStrictNames() []string {
if len(name) > 0 { if len(name) > 0 {
if !configutils.IsFuzzyDomain(name) { if !configutils.IsFuzzyDomain(name) {
result = append(result, name) result = append(result, name)
// unicode domain
if !normalServerNameReg.MatchString(name) {
asciiName, err := idna.ToASCII(name)
if err == nil && len(asciiName) > 0 {
result = append(result, asciiName)
}
}
} }
} }
for _, name := range serverName.SubNames { for _, name := range serverName.SubNames {
if len(name) > 0 { if len(name) > 0 {
if !configutils.IsFuzzyDomain(name) { if !configutils.IsFuzzyDomain(name) {
result = append(result, name) result = append(result, name)
// unicode domain
if !normalServerNameReg.MatchString(name) {
asciiName, err := idna.ToASCII(name)
if err == nil && len(asciiName) > 0 {
result = append(result, asciiName)
}
}
} }
} }
} }