g: set x-forwarded-proto based on upstream TLS Status

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
Jens Langhammer 2021-05-21 00:03:02 +02:00
parent bf4cbb25fe
commit a9519a4a68
1 changed files with 14 additions and 1 deletions

View File

@ -9,7 +9,20 @@ import (
func (ws *WebServer) configureProxy() { func (ws *WebServer) configureProxy() {
// Reverse proxy to the application server // Reverse proxy to the application server
u, _ := url.Parse("http://localhost:8000") u, _ := url.Parse("http://localhost:8000")
rp := httputil.NewSingleHostReverseProxy(u) director := func(req *http.Request) {
req.URL.Scheme = u.Scheme
req.URL.Host = u.Host
if _, ok := req.Header["User-Agent"]; !ok {
// explicitly disable User-Agent so it's not set to default value
req.Header.Set("User-Agent", "")
}
if req.TLS != nil {
req.Header.Set("X-Forwarded-Proto", "https")
} else {
req.Header.Set("X-Forwarded-Proto", "http")
}
}
rp := &httputil.ReverseProxy{Director: director}
rp.ErrorHandler = ws.proxyErrorHandler rp.ErrorHandler = ws.proxyErrorHandler
rp.ModifyResponse = ws.proxyModifyResponse rp.ModifyResponse = ws.proxyModifyResponse
ws.m.PathPrefix("/").Handler(rp) ws.m.PathPrefix("/").Handler(rp)