From ebfaf77c8f7857e10b9edb9954ce88e81a2594a8 Mon Sep 17 00:00:00 2001 From: zhengkunwang <31820853+zhengkunwang223@users.noreply.github.com> Date: Thu, 28 Dec 2023 16:49:38 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=BD=91=E7=AB=99=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E4=B8=AD=E6=96=87=E5=9F=9F=E5=90=8D=20(#3472)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Refs https://github.com/1Panel-dev/1Panel/issues/1522 --- backend/app/service/website.go | 13 ++++++++--- backend/app/service/website_utils.go | 31 ++++++++++++++++++------ backend/utils/common/common.go | 35 ++++++++++++++++------------ 3 files changed, 54 insertions(+), 25 deletions(-) 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 +}