From 6799ef838f51c7b1fe2aa4ac721057296cd8f9ea Mon Sep 17 00:00:00 2001 From: Mihai Parparita Date: Thu, 9 Feb 2023 14:58:20 -0800 Subject: [PATCH] ipn/ipnlocal: add PeerAPI endpoint for doctor output Useful when debugging issues (e.g. to see the full routing table), and easier to refer to the output via a browser than trying to read it from the logs generated by `bugreport --diagnose`. Behind a canDebug() check, similar to the /magicsock and /interfaces endpoints. Updates #7184 Signed-off-by: Mihai Parparita --- ipn/ipnlocal/peerapi.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/ipn/ipnlocal/peerapi.go b/ipn/ipnlocal/peerapi.go index a699b8f02..e3e7401e7 100644 --- a/ipn/ipnlocal/peerapi.go +++ b/ipn/ipnlocal/peerapi.go @@ -707,6 +707,9 @@ func (h *peerAPIHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { case "/v0/interfaces": h.handleServeInterfaces(w, r) return + case "/v0/doctor": + h.handleServeDoctor(w, r) + return case "/v0/ingress": metricIngressCalls.Add(1) h.handleServeIngress(w, r) @@ -818,6 +821,24 @@ func (h *peerAPIHandler) handleServeInterfaces(w http.ResponseWriter, r *http.Re fmt.Fprintln(w, "") } +func (h *peerAPIHandler) handleServeDoctor(w http.ResponseWriter, r *http.Request) { + if !h.canDebug() { + http.Error(w, "denied; no debug access", http.StatusForbidden) + return + } + w.Header().Set("Content-Type", "text/html; charset=utf-8") + fmt.Fprintln(w, "

Doctor Output

") + + fmt.Fprintln(w, "
")
+
+	h.ps.b.Doctor(r.Context(), func(format string, args ...any) {
+		line := fmt.Sprintf(format, args)
+		fmt.Fprintln(w, html.EscapeString(line))
+	})
+
+	fmt.Fprintln(w, "
") +} + type incomingFile struct { name string // "foo.jpg" started time.Time