diff --git a/cmd/derper/derper.go b/cmd/derper/derper.go index 02736b6be..373faa8b5 100644 --- a/cmd/derper/derper.go +++ b/cmd/derper/derper.go @@ -12,15 +12,19 @@ import ( "expvar" "flag" "fmt" + "html" "io" "log" "math" "net" "net/http" + "net/http/pprof" "net/netip" "os" "path/filepath" "regexp" + "runtime" + "strconv" "strings" "time" @@ -33,6 +37,7 @@ import ( "tailscale.com/net/stun" "tailscale.com/tsweb" "tailscale.com/types/key" + "tailscale.com/version" ) var ( @@ -55,6 +60,8 @@ var ( acceptConnLimit = flag.Float64("accept-connection-limit", math.Inf(+1), "rate limit for accepting new connection") acceptConnBurst = flag.Int("accept-connection-burst", math.MaxInt, "burst limit for accepting new connection") + + debugAddr = flag.String("debug_addr", "", "listening address for debug server") ) var ( @@ -136,6 +143,7 @@ func main() { if *dev { *addr = ":3340" // above the keys DERP + *debugAddr = "localhost:8383" log.Printf("Running in dev mode.") tsweb.DevMode = true } @@ -222,6 +230,11 @@ func main() { go serveSTUN(listenHost, *stunPort) } + if *debugAddr != "" { + log.Printf("running debug server on %s", *debugAddr) + go runDebugServer(*debugAddr, s) + } + quietLogger := log.New(logFilter{}, "", 0) httpsrv := &http.Server{ Addr: *addr, @@ -513,3 +526,79 @@ func (logFilter) Write(p []byte) (int, error) { log.Printf("%s", p) return len(p), nil } + +func runDebugServer(addr string, s *derp.Server) { + mux := http.NewServeMux() + mux.Handle("/debug/vars", http.DefaultServeMux) // serve out expvar + mux.HandleFunc("/debug/varz", tsweb.VarzHandler) // expvar but in prometheus format + mux.HandleFunc("/debug/pprof/", pprof.Index) + mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline) + mux.HandleFunc("/debug/pprof/profile", pprof.Profile) + mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol) + mux.HandleFunc("/debug/pprof/trace", pprof.Trace) + mux.Handle("/debug/pprofrate", http.HandlerFunc(handlePprofRate)) + + mux.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/html; charset=utf-8") + w.WriteHeader(200) + fmt.Fprintf(w, `
++ This is a Tailscale DERP debug server. +
+%s+