From efce703f3367a2903cf2f05bf65bc0259af5dc28 Mon Sep 17 00:00:00 2001 From: Jan-Lukas Else Date: Thu, 24 Dec 2020 11:00:16 +0100 Subject: [PATCH] Add config for short domains --- activityPub.go | 4 ++-- config.go | 34 +++++++++++++++++++++++----------- http.go | 8 ++++++-- postsFuncs.go | 3 +++ shortDomain.go | 15 +++++++++++++++ webmention.go | 2 +- 6 files changed, 50 insertions(+), 16 deletions(-) create mode 100644 shortDomain.go diff --git a/activityPub.go b/activityPub.go index ba709ba..ec31c88 100644 --- a/activityPub.go +++ b/activityPub.go @@ -72,7 +72,7 @@ func initActivityPub() error { } func apHandleWebfinger(w http.ResponseWriter, r *http.Request) { - re, err := regexp.Compile(`^acct:(.*)@` + regexp.QuoteMeta(appConfig.Server.Domain) + `$`) + re, err := regexp.Compile(`^acct:(.*)@` + regexp.QuoteMeta(appConfig.Server.publicHostname) + `$`) if err != nil { serveError(w, r, err.Error(), http.StatusInternalServerError) return @@ -85,7 +85,7 @@ func apHandleWebfinger(w http.ResponseWriter, r *http.Request) { } w.Header().Set(contentType, "application/jrd+json"+charsetUtf8Suffix) _ = json.NewEncoder(w).Encode(map[string]interface{}{ - "subject": "acct:" + name + "@" + appConfig.Server.Domain, + "subject": "acct:" + name + "@" + appConfig.Server.publicHostname, "links": []map[string]string{ { "rel": "self", diff --git a/config.go b/config.go index c77a65a..90e9315 100644 --- a/config.go +++ b/config.go @@ -2,6 +2,7 @@ package main import ( "errors" + "net/url" "strings" "github.com/spf13/viper" @@ -23,15 +24,17 @@ type config struct { } type configServer struct { - Logging bool `mapstructure:"logging"` - LogFile string `mapstructure:"logFile"` - Debug bool `mapstructure:"Debug"` - Port int `mapstructure:"port"` - Domain string `mapstructure:"domain"` - PublicAddress string `mapstructure:"publicAddress"` - PublicHTTPS bool `mapstructure:"publicHttps"` - LetsEncryptMail string `mapstructure:"letsEncryptMail"` - JWTSecret string `mapstructure:"jwtSecret"` + Logging bool `mapstructure:"logging"` + LogFile string `mapstructure:"logFile"` + Debug bool `mapstructure:"Debug"` + Port int `mapstructure:"port"` + PublicAddress string `mapstructure:"publicAddress"` + ShortPublicAddress string `mapstructure:"shortPublicAddress"` + PublicHTTPS bool `mapstructure:"publicHttps"` + LetsEncryptMail string `mapstructure:"letsEncryptMail"` + JWTSecret string `mapstructure:"jwtSecret"` + publicHostname string + shortPublicHostname string } type configDb struct { @@ -211,8 +214,17 @@ func initConfig() error { return err } // Check config - if appConfig.Server.Domain == "" { - return errors.New("no domain configured") + publicURL, err := url.Parse(appConfig.Server.PublicAddress) + if err != nil { + return err + } + appConfig.Server.publicHostname = publicURL.Hostname() + if appConfig.Server.ShortPublicAddress != "" { + shortPublicURL, err := url.Parse(appConfig.Server.ShortPublicAddress) + if err != nil { + return err + } + appConfig.Server.shortPublicHostname = shortPublicURL.Hostname() } if appConfig.Server.JWTSecret == "" { return errors.New("no JWT secret configured") diff --git a/http.go b/http.go index 083f82a..18d25af 100644 --- a/http.go +++ b/http.go @@ -49,7 +49,11 @@ func startServer() (err error) { certmagic.DefaultACME.Agreed = true certmagic.DefaultACME.Email = appConfig.Server.LetsEncryptMail certmagic.DefaultACME.CA = certmagic.LetsEncryptProductionCA - err = certmagic.HTTPS([]string{appConfig.Server.Domain}, securityHeaders(d)) + hosts := []string{appConfig.Server.publicHostname} + if appConfig.Server.shortPublicHostname != "" { + hosts = append(hosts, appConfig.Server.shortPublicHostname) + } + err = certmagic.HTTPS(hosts, securityHeaders(d)) } else { err = http.ListenAndServe(localAddress, d) } @@ -72,9 +76,9 @@ func buildHandler() (http.Handler, error) { if appConfig.Server.Logging { r.Use(logMiddleware) } - // r.Use(middleware.Logger) r.Use(middleware.Recoverer) r.Use(middleware.Compress(flate.DefaultCompression)) + r.Use(redirectShortDomain) r.Use(middleware.RedirectSlashes) r.Use(middleware.CleanPath) r.Use(middleware.GetHead) diff --git a/postsFuncs.go b/postsFuncs.go index f2f58bb..d5e8337 100644 --- a/postsFuncs.go +++ b/postsFuncs.go @@ -17,6 +17,9 @@ func (p *post) shortURL() string { if err != nil { return "" } + if appConfig.Server.ShortPublicAddress != "" { + return appConfig.Server.ShortPublicAddress + s + } return appConfig.Server.PublicAddress + s } diff --git a/shortDomain.go b/shortDomain.go new file mode 100644 index 0000000..1f5136a --- /dev/null +++ b/shortDomain.go @@ -0,0 +1,15 @@ +package main + +import ( + "net/http" +) + +func redirectShortDomain(next http.Handler) http.Handler { + return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { + if appConfig.Server.shortPublicHostname != "" && r.Host == appConfig.Server.shortPublicHostname { + http.Redirect(rw, r, appConfig.Server.PublicAddress+r.RequestURI, http.StatusMovedPermanently) + return + } + next.ServeHTTP(rw, r) + }) +} diff --git a/webmention.go b/webmention.go index ca40b44..362cf7e 100644 --- a/webmention.go +++ b/webmention.go @@ -48,7 +48,7 @@ func handleWebmention(w http.ResponseWriter, r *http.Request) { serveError(w, r, err.Error(), http.StatusBadRequest) return } - if !isAllowedHost(httptest.NewRequest(http.MethodGet, m.Target, nil), r.URL.Host, appConfig.Server.Domain) { + if !isAllowedHost(httptest.NewRequest(http.MethodGet, m.Target, nil), appConfig.Server.publicHostname) { serveError(w, r, "target not allowed", http.StatusBadRequest) return }