diff --git a/backend/app/service/website.go b/backend/app/service/website.go index e4bdfdb96..5c130e8a5 100644 --- a/backend/app/service/website.go +++ b/backend/app/service/website.go @@ -9,6 +9,7 @@ import ( "encoding/pem" "errors" "fmt" + "github.com/1Panel-dev/1Panel/backend/utils/common" "os" "path" "reflect" @@ -172,8 +173,14 @@ func (w WebsiteService) GetWebsites() ([]response.WebsiteDTO, error) { func (w WebsiteService) CreateWebsite(create request.WebsiteCreate) (err error) { primaryDomainArray := strings.Split(create.PrimaryDomain, ":") primaryDomain := primaryDomainArray[0] - - if exist, _ := websiteRepo.GetBy(websiteRepo.WithAlias(create.Alias)); len(exist) > 0 { + alias := create.Alias + if common.ContainsChinese(alias) { + alias, err = common.PunycodeEncode(alias) + if err != nil { + return + } + } + if exist, _ := websiteRepo.GetBy(websiteRepo.WithAlias(alias)); len(exist) > 0 { return buserr.New(constant.ErrAliasIsExist) } @@ -201,7 +208,7 @@ func (w WebsiteService) CreateWebsite(create request.WebsiteCreate) (err error) website := &model.Website{ PrimaryDomain: primaryDomain, Type: create.Type, - Alias: create.Alias, + Alias: alias, Remark: create.Remark, Status: constant.WebRunning, ExpireDate: defaultDate, diff --git a/backend/app/service/website_utils.go b/backend/app/service/website_utils.go index 83106f902..07231fbf9 100644 --- a/backend/app/service/website_utils.go +++ b/backend/app/service/website_utils.go @@ -30,27 +30,44 @@ import ( ) func getDomain(domainStr string, defaultPort int) (model.WebsiteDomain, error) { - domain := model.WebsiteDomain{} + var ( + err error + domain = model.WebsiteDomain{} + portN int + ) domainArray := strings.Split(domainStr, ":") if len(domainArray) == 1 { - domain.Domain = domainArray[0] + domain.Domain, err = handleChineseDomain(domainArray[0]) + if err != nil { + return domain, err + } domain.Port = defaultPort return domain, nil } if len(domainArray) > 1 { - domain.Domain = domainArray[0] + domain.Domain, err = handleChineseDomain(domainArray[0]) + if err != nil { + return domain, err + } portStr := domainArray[1] - portN, err := strconv.Atoi(portStr) + portN, err = strconv.Atoi(portStr) if err != nil { - return model.WebsiteDomain{}, buserr.WithName("ErrTypePort", portStr) + return domain, buserr.WithName("ErrTypePort", portStr) } if portN <= 0 || portN > 65535 { - return model.WebsiteDomain{}, buserr.New("ErrTypePortRange") + return domain, buserr.New("ErrTypePortRange") } domain.Port = portN return domain, nil } - return model.WebsiteDomain{}, nil + return domain, nil +} + +func handleChineseDomain(domain string) (string, error) { + if common.ContainsChinese(domain) { + return common.PunycodeEncode(domain) + } + return domain, nil } func createIndexFile(website *model.Website, runtime *model.Runtime) error { diff --git a/backend/utils/common/common.go b/backend/utils/common/common.go index f8a0a7ce6..7b4423791 100644 --- a/backend/utils/common/common.go +++ b/backend/utils/common/common.go @@ -3,6 +3,7 @@ package common import ( "crypto/rand" "fmt" + "golang.org/x/net/idna" "io" mathRand "math/rand" "net" @@ -12,9 +13,9 @@ import ( "strconv" "strings" "time" + "unicode" "github.com/1Panel-dev/1Panel/backend/utils/cmd" - "github.com/mozillazg/go-pinyin" ) func CompareVersion(version1, version2 string) bool { @@ -216,20 +217,6 @@ func LoadTimeZoneByCmd() string { return fields[2] } -func ConvertToPinyin(text string) string { - args := pinyin.NewArgs() - args.Fallback = func(r rune, a pinyin.Args) []string { - return []string{string(r)} - } - p := pinyin.Pinyin(text, args) - var strArr []string - for i := 0; i < len(p); i++ { - strArr = append(strArr, strings.Join(p[i], "")) - } - - return strings.Join(strArr, "") -} - func IsValidDomain(domain string) bool { pattern := `^([\w\p{Han}\-\*]{1,100}\.){1,10}([\w\p{Han}\-]{1,24}|[\w\p{Han}\-]{1,24}\.[\w\p{Han}\-]{1,24})(:\d{1,5})?$` match, err := regexp.MatchString(pattern, domain) @@ -238,3 +225,21 @@ func IsValidDomain(domain string) bool { } return match } + +func ContainsChinese(text string) bool { + for _, char := range text { + if unicode.Is(unicode.Han, char) { + return true + } + } + return false +} + +func PunycodeEncode(text string) (string, error) { + encoder := idna.New() + ascii, err := encoder.ToASCII(text) + if err != nil { + return "", err + } + return ascii, nil +}