Browse Source

Add config for short domains

master
Jan-Lukas Else 4 weeks ago
parent
commit
efce703f33
6 changed files with 50 additions and 16 deletions
  1. +2
    -2
      activityPub.go
  2. +23
    -11
      config.go
  3. +6
    -2
      http.go
  4. +3
    -0
      postsFuncs.go
  5. +15
    -0
      shortDomain.go
  6. +1
    -1
      webmention.go

+ 2
- 2
activityPub.go View File

@ -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",


+ 23
- 11
config.go View File

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


+ 6
- 2
http.go View File

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


+ 3
- 0
postsFuncs.go View File

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


+ 15
- 0
shortDomain.go 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)
})
}

+ 1
- 1
webmention.go View File

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


Loading…
Cancel
Save