Files
mqtt-5-test/cmd/ui-proxy/main.go
T
Ryan 1539f067f9
Test and deploy MQTT backend / test (push) Successful in 1m50s
Test and deploy MQTT backend / deploy (push) Successful in 25s
Add private Docker UI relay for Nginx Proxy Manager
2026-10-10 09:13:50 +03:30

47 lines
1.4 KiB
Go

// ui-proxy exposes the read-only loopback viewer to the private Docker gateway.
package main
import (
"flag"
"log"
"net"
"net/http"
"net/http/httputil"
"net/url"
"time"
)
func handler(target *url.URL) http.Handler {
proxy := httputil.NewSingleHostReverseProxy(target)
original := proxy.Director
proxy.Director = func(r *http.Request) {
original(r)
r.Host = target.Host
// The viewer is read-only behind NPM; do not forward the public browser origin
// into the loopback-only origin guard. Mutating methods are rejected below.
r.Header.Del("Origin")
}
proxy.FlushInterval = -1 // stream SSE immediately
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet && r.Method != http.MethodHead {
w.Header().Set("Allow", "GET, HEAD")
http.Error(w, "read-only viewer", http.StatusMethodNotAllowed)
return
}
proxy.ServeHTTP(w, r)
})
}
func main() {
address := flag.String("listen", "172.20.0.1:18082", "Private Docker gateway listener")
flag.Parse()
host, _, err := net.SplitHostPort(*address)
ip := net.ParseIP(host)
if err != nil || ip == nil || !ip.IsPrivate() {
log.Fatal("listen must use an explicit private IP and port")
}
target, _ := url.Parse("http://127.0.0.1:18080")
server := &http.Server{Addr: *address, Handler: handler(target), ReadHeaderTimeout: 5 * time.Second, IdleTimeout: 60 * time.Second, MaxHeaderBytes: 8 << 10}
log.Fatal(server.ListenAndServe())
}