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 c0e5255..0f07329 100644 --- a/config/config.go +++ b/config/config.go @@ -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" ) @@ -258,6 +261,22 @@ 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()) + if err != nil { + log.Error("idna.ToASCII for %s: %s", u.Hostname(), 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 bb69895..b6cf12e 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 )