From 46c8d6e61f5e5f5f8904a55dce926b3e7028b973 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Wed, 27 Dec 2023 17:33:07 +0800 Subject: [PATCH] Fix pprof URL path --- debug_http.go | 21 +++++++++++++++------ debug_stub.go | 2 +- debug_linux.go => debug_unix.go | 2 ++ 3 files changed, 18 insertions(+), 7 deletions(-) rename debug_linux.go => debug_unix.go (93%) diff --git a/debug_http.go b/debug_http.go index 7c675eba..218efc4f 100644 --- a/debug_http.go +++ b/debug_http.go @@ -5,6 +5,7 @@ import ( "net/http/pprof" "runtime" "runtime/debug" + "strings" "github.com/sagernet/sing-box/common/badjson" "github.com/sagernet/sing-box/common/humanize" @@ -47,12 +48,20 @@ func applyDebugListenOption(options option.DebugOptions) { encoder.SetIndent("", " ") encoder.Encode(memObject) }) - r.HandleFunc("/pprof", pprof.Index) - r.HandleFunc("/pprof/*", pprof.Index) - r.HandleFunc("/pprof/cmdline", pprof.Cmdline) - r.HandleFunc("/pprof/profile", pprof.Profile) - r.HandleFunc("/pprof/symbol", pprof.Symbol) - r.HandleFunc("/pprof/trace", pprof.Trace) + r.Route("/pprof", func(r chi.Router) { + r.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) { + if !strings.HasSuffix(request.URL.Path, "/") { + http.Redirect(writer, request, request.URL.Path+"/", http.StatusMovedPermanently) + } else { + pprof.Index(writer, request) + } + }) + r.HandleFunc("/*", pprof.Index) + r.HandleFunc("/cmdline", pprof.Cmdline) + r.HandleFunc("/profile", pprof.Profile) + r.HandleFunc("/symbol", pprof.Symbol) + r.HandleFunc("/trace", pprof.Trace) + }) }) debugHTTPServer = &http.Server{ Addr: options.Listen, diff --git a/debug_stub.go b/debug_stub.go index ea7e2c0b..a8988c20 100644 --- a/debug_stub.go +++ b/debug_stub.go @@ -1,4 +1,4 @@ -//go:build !linux +//go:build !(linux || darwin) package box diff --git a/debug_linux.go b/debug_unix.go similarity index 93% rename from debug_linux.go rename to debug_unix.go index 3296bdec..3be097e9 100644 --- a/debug_linux.go +++ b/debug_unix.go @@ -1,3 +1,5 @@ +//go:build linux || darwin + package box import (