Add config for short domains

This commit is contained in:
Jan-Lukas Else 2020-12-24 11:00:16 +01:00
parent 9e4a03549f
commit efce703f33
6 changed files with 50 additions and 16 deletions

View File

@ -72,7 +72,7 @@ func initActivityPub() error {
} }
func apHandleWebfinger(w http.ResponseWriter, r *http.Request) { 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 { if err != nil {
serveError(w, r, err.Error(), http.StatusInternalServerError) serveError(w, r, err.Error(), http.StatusInternalServerError)
return return
@ -85,7 +85,7 @@ func apHandleWebfinger(w http.ResponseWriter, r *http.Request) {
} }
w.Header().Set(contentType, "application/jrd+json"+charsetUtf8Suffix) w.Header().Set(contentType, "application/jrd+json"+charsetUtf8Suffix)
_ = json.NewEncoder(w).Encode(map[string]interface{}{ _ = json.NewEncoder(w).Encode(map[string]interface{}{
"subject": "acct:" + name + "@" + appConfig.Server.Domain, "subject": "acct:" + name + "@" + appConfig.Server.publicHostname,
"links": []map[string]string{ "links": []map[string]string{
{ {
"rel": "self", "rel": "self",

View File

@ -2,6 +2,7 @@ package main
import ( import (
"errors" "errors"
"net/url"
"strings" "strings"
"github.com/spf13/viper" "github.com/spf13/viper"
@ -23,15 +24,17 @@ type config struct {
} }
type configServer struct { type configServer struct {
Logging bool `mapstructure:"logging"` Logging bool `mapstructure:"logging"`
LogFile string `mapstructure:"logFile"` LogFile string `mapstructure:"logFile"`
Debug bool `mapstructure:"Debug"` Debug bool `mapstructure:"Debug"`
Port int `mapstructure:"port"` Port int `mapstructure:"port"`
Domain string `mapstructure:"domain"` PublicAddress string `mapstructure:"publicAddress"`
PublicAddress string `mapstructure:"publicAddress"` ShortPublicAddress string `mapstructure:"shortPublicAddress"`
PublicHTTPS bool `mapstructure:"publicHttps"` PublicHTTPS bool `mapstructure:"publicHttps"`
LetsEncryptMail string `mapstructure:"letsEncryptMail"` LetsEncryptMail string `mapstructure:"letsEncryptMail"`
JWTSecret string `mapstructure:"jwtSecret"` JWTSecret string `mapstructure:"jwtSecret"`
publicHostname string
shortPublicHostname string
} }
type configDb struct { type configDb struct {
@ -211,8 +214,17 @@ func initConfig() error {
return err return err
} }
// Check config // Check config
if appConfig.Server.Domain == "" { publicURL, err := url.Parse(appConfig.Server.PublicAddress)
return errors.New("no domain configured") 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 == "" { if appConfig.Server.JWTSecret == "" {
return errors.New("no JWT secret configured") return errors.New("no JWT secret configured")

View File

@ -49,7 +49,11 @@ func startServer() (err error) {
certmagic.DefaultACME.Agreed = true certmagic.DefaultACME.Agreed = true
certmagic.DefaultACME.Email = appConfig.Server.LetsEncryptMail certmagic.DefaultACME.Email = appConfig.Server.LetsEncryptMail
certmagic.DefaultACME.CA = certmagic.LetsEncryptProductionCA 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 { } else {
err = http.ListenAndServe(localAddress, d) err = http.ListenAndServe(localAddress, d)
} }
@ -72,9 +76,9 @@ func buildHandler() (http.Handler, error) {
if appConfig.Server.Logging { if appConfig.Server.Logging {
r.Use(logMiddleware) r.Use(logMiddleware)
} }
// r.Use(middleware.Logger)
r.Use(middleware.Recoverer) r.Use(middleware.Recoverer)
r.Use(middleware.Compress(flate.DefaultCompression)) r.Use(middleware.Compress(flate.DefaultCompression))
r.Use(redirectShortDomain)
r.Use(middleware.RedirectSlashes) r.Use(middleware.RedirectSlashes)
r.Use(middleware.CleanPath) r.Use(middleware.CleanPath)
r.Use(middleware.GetHead) r.Use(middleware.GetHead)

View File

@ -17,6 +17,9 @@ func (p *post) shortURL() string {
if err != nil { if err != nil {
return "" return ""
} }
if appConfig.Server.ShortPublicAddress != "" {
return appConfig.Server.ShortPublicAddress + s
}
return appConfig.Server.PublicAddress + s return appConfig.Server.PublicAddress + s
} }

15
shortDomain.go Normal file
View File

@ -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)
})
}

View File

@ -48,7 +48,7 @@ func handleWebmention(w http.ResponseWriter, r *http.Request) {
serveError(w, r, err.Error(), http.StatusBadRequest) serveError(w, r, err.Error(), http.StatusBadRequest)
return 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) serveError(w, r, "target not allowed", http.StatusBadRequest)
return return
} }