From 967ee9679ca78f1dffd250c6bd0cfcc2fc7d12b0 Mon Sep 17 00:00:00 2001 From: Matt Baer Date: Mon, 26 Apr 2021 11:18:51 -0400 Subject: [PATCH] Support international domain names This internally converts the configured host name into its Punycode ASCII representation, while showing users the correct Unicode domain name. --- collections.go | 5 ++++- config/config.go | 22 +++++++++++++++++++++- config/funcs.go | 24 ++++++++++++++++++++++-- go.mod | 2 +- 4 files changed, 48 insertions(+), 5 deletions(-) diff --git a/collections.go b/collections.go index 15938fc..5f7b608 100644 --- a/collections.go +++ b/collections.go @@ -33,6 +33,7 @@ import ( "github.com/writefreely/writefreely/author" "github.com/writefreely/writefreely/config" "github.com/writefreely/writefreely/page" + "golang.org/x/net/idna" ) type ( @@ -236,7 +237,9 @@ func (c *Collection) DisplayCanonicalURL() string { if p == "/" { p = "" } - return u.Hostname() + p + d := u.Hostname() + d, _ = idna.ToUnicode(d) + return d + p } func (c *Collection) RedirectingCanonicalURL(isRedir bool) string { diff --git a/config/config.go b/config/config.go index 8ee03ba..299b7b9 100644 --- a/config/config.go +++ b/config/config.go @@ -1,5 +1,5 @@ /* - * Copyright © 2018-2020 A Bunch Tell LLC. + * Copyright © 2018-2021 A Bunch Tell LLC. * * This file is part of WriteFreely. * @@ -12,8 +12,11 @@ package config import ( + "net/url" "strings" + "github.com/writeas/web-core/log" + "golang.org/x/net/idna" "gopkg.in/ini.v1" ) @@ -257,6 +260,23 @@ func Load(fname string) (*Config, error) { if err != nil { return nil, err } + + // Do any transformations + u, err := url.Parse(uc.App.Host) + if err != nil { + return nil, err + } + d, err := idna.ToASCII(u.Hostname()) + log.Error("Host: %s", uc.App.Host) + if err != nil { + log.Error("ToASCII: %s", err) + return nil, err + } + uc.App.Host = u.Scheme + "://" + d + if u.Port() != "" { + uc.App.Host += ":" + u.Port() + } + return uc, nil } diff --git a/config/funcs.go b/config/funcs.go index 9678df0..fc8c687 100644 --- a/config/funcs.go +++ b/config/funcs.go @@ -1,5 +1,5 @@ /* - * Copyright © 2018 A Bunch Tell LLC. + * Copyright © 2018, 2020-2021 A Bunch Tell LLC. * * This file is part of WriteFreely. * @@ -11,14 +11,34 @@ package config import ( + "github.com/writeas/web-core/log" + "golang.org/x/net/idna" "net/http" + "net/url" "strings" "time" ) // FriendlyHost returns the app's Host sans any schema func (ac AppCfg) FriendlyHost() string { - return ac.Host[strings.Index(ac.Host, "://")+len("://"):] + rawHost := ac.Host[strings.Index(ac.Host, "://")+len("://"):] + + u, err := url.Parse(ac.Host) + if err != nil { + log.Error("url.Parse failed on %s: %s", ac.Host, err) + return rawHost + } + d, err := idna.ToUnicode(u.Hostname()) + if err != nil { + log.Error("idna.ToUnicode failed on %s: %s", ac.Host, err) + return rawHost + } + + res := d + if u.Port() != "" { + res += ":" + u.Port() + } + return res } func (ac AppCfg) CanCreateBlogs(currentlyUsed uint64) bool { diff --git a/go.mod b/go.mod index 3006fa6..3822df3 100644 --- a/go.mod +++ b/go.mod @@ -42,7 +42,7 @@ require ( github.com/writeas/web-core v1.3.1-0.20210330164422-95a3a717ed8f github.com/writefreely/go-nodeinfo v1.2.0 golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 - golang.org/x/net v0.0.0-20200707034311-ab3426394381 // indirect + golang.org/x/net v0.0.0-20200707034311-ab3426394381 gopkg.in/ini.v1 v1.62.0 )