From d722f56000b6f71865b73a57f2932118dab2db18 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Wed, 17 Nov 2021 16:15:27 +0100 Subject: [PATCH] Add pprof HTTP server This enables production debugging of the bouncer. Closes: https://todo.sr.ht/~emersion/soju/155 --- cmd/soju/main.go | 21 +++++++++++++++++++++ doc/soju.1.scd | 3 +++ 2 files changed, 24 insertions(+) diff --git a/cmd/soju/main.go b/cmd/soju/main.go index 214c0cc..eb3ab68 100644 --- a/cmd/soju/main.go +++ b/cmd/soju/main.go @@ -9,6 +9,7 @@ import ( "log" "net" "net/http" + _ "net/http/pprof" "net/url" "os" "os/signal" @@ -286,6 +287,26 @@ func main() { log.Fatalf("serving %q: %v", listen, err) } }() + case "http+pprof": + // Only allow localhost as listening host for security reasons. + // Users can always explicitly setup reverse proxies if desirable. + hostname, _, err := net.SplitHostPort(u.Host) + if err != nil { + log.Fatalf("invalid host in URI %q: %v", listen, err) + } else if hostname != "localhost" { + log.Fatalf("pprof listening host must be localhost") + } + + // net/http/pprof registers its handlers in http.DefaultServeMux + httpSrv := http.Server{ + Addr: u.Host, + Handler: http.DefaultServeMux, + } + go func() { + if err := httpSrv.ListenAndServe(); err != nil { + log.Fatalf("serving %q: %v", listen, err) + } + }() default: log.Fatalf("failed to listen on %q: unsupported scheme", listen) } diff --git a/doc/soju.1.scd b/doc/soju.1.scd index d838f90..489f53e 100644 --- a/doc/soju.1.scd +++ b/doc/soju.1.scd @@ -100,6 +100,9 @@ The following directives are supported: port: 113) - _http+prometheus://localhost:_ listens for plain-text HTTP connections and serves Prometheus metrics (host must be "localhost") + - _http+pprof://localhost:_ listens for plain-text HTTP connections + and serves pprof runtime profiling data (host must be "localhost"). For + more information, see: . If the scheme is omitted, "ircs" is assumed. If multiple *listen* directives are specified, soju will listen on each of them.