1
Fork 0

Add new configuration directive proxyURL.

The strategy of computing the base URL from the request doesn't
necessarily work if we're behind a reverse proxy.  proxyURL
can be set in cases where our guess is incorrect.

Thanks to Dianne Skoll.
This commit is contained in:
Juliusz Chroboczek 2022-10-21 13:28:11 +02:00
parent 31ed146a95
commit 16e2888d56
3 changed files with 35 additions and 6 deletions

8
README
View File

@ -47,15 +47,19 @@ The server may be configured in the JSON file `data/config.json`. This
file may look as follows: file may look as follows:
{ {
"canonicalHost": "galene.example.org",
"admin":[{"username":"root","password":"secret"}] "admin":[{"username":"root","password":"secret"}]
"canonicalHost": "galene.example.org",
} }
The fields are as follows: The fields are as follows:
- `canonicalHost`: the canonical name of the host running the server; - `proxyURL`: if running behind a reverse proxy, this specifies the
address of the proxy.
- `admin` defines the users allowed to look at the `/stats.html` file; it - `admin` defines the users allowed to look at the `/stats.html` file; it
has the same syntax as user definitions in groups (see below). has the same syntax as user definitions in groups (see below).
- `canonicalHost`: the canonical name of the host running the server; this
will cause clients to be redirected if they use a different hostname to
access the server.
# Group definitions # Group definitions

View File

@ -857,6 +857,7 @@ type Configuration struct {
fileSize int64 `json:"-"` fileSize int64 `json:"-"`
CanonicalHost string `json:"canonicalHost"` CanonicalHost string `json:"canonicalHost"`
ProxyURL string `json:"proxyURL"`
Admin []ClientPattern `json:"admin"` Admin []ClientPattern `json:"admin"`
} }

View File

@ -332,7 +332,18 @@ func groupHandler(w http.ResponseWriter, r *http.Request) {
serveFile(w, r, filepath.Join(StaticRoot, "galene.html")) serveFile(w, r, filepath.Join(StaticRoot, "galene.html"))
} }
func groupBase(r *http.Request) string { func groupBase(r *http.Request) (string, error) {
conf, err := group.GetConfiguration()
if err != nil {
return "", nil
}
if conf.ProxyURL != "" {
u, err := url.Parse(conf.ProxyURL)
if err != nil {
return "", err
}
return u.JoinPath("/group/").String(), nil
}
scheme := "https" scheme := "https"
if r.TLS == nil { if r.TLS == nil {
scheme = "http" scheme = "http"
@ -342,7 +353,7 @@ func groupBase(r *http.Request) string {
Host: r.Host, Host: r.Host,
Path: "/group/", Path: "/group/",
} }
return base.String() return base.String(), nil
} }
func groupStatusHandler(w http.ResponseWriter, r *http.Request) { func groupStatusHandler(w http.ResponseWriter, r *http.Request) {
@ -364,7 +375,13 @@ func groupStatusHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
d := g.Status(false, groupBase(r)) base, err := groupBase(r)
if err != nil {
http.Error(w, "Internal server error",
http.StatusInternalServerError)
return
}
d := g.Status(false, base)
w.Header().Set("content-type", "application/json") w.Header().Set("content-type", "application/json")
w.Header().Set("cache-control", "no-cache") w.Header().Set("cache-control", "no-cache")
@ -377,6 +394,13 @@ func groupStatusHandler(w http.ResponseWriter, r *http.Request) {
} }
func publicHandler(w http.ResponseWriter, r *http.Request) { func publicHandler(w http.ResponseWriter, r *http.Request) {
base, err := groupBase(r)
if err != nil {
log.Println("couldn't determine group base: %v", err)
http.Error(w, "Internal server error",
http.StatusInternalServerError)
return
}
w.Header().Set("content-type", "application/json") w.Header().Set("content-type", "application/json")
w.Header().Set("cache-control", "no-cache") w.Header().Set("cache-control", "no-cache")
@ -384,7 +408,7 @@ func publicHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
g := group.GetPublic(groupBase(r)) g := group.GetPublic(base)
e := json.NewEncoder(w) e := json.NewEncoder(w)
e.Encode(g) e.Encode(g)
} }