From c7bff35feec3123b8ee0a4b80ee80d15ae7932c4 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Wed, 3 Nov 2021 11:17:56 -0700 Subject: [PATCH] ipn/ipnlocal: add owner-only debug handler to get process env For debugging Synology. Like the existing goroutines handler, in that it's owner-only. Change-Id: I852f0626be8e1c0b6794c1e062111d14adc3e6ac Signed-off-by: Brad Fitzpatrick --- ipn/ipnlocal/peerapi.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/ipn/ipnlocal/peerapi.go b/ipn/ipnlocal/peerapi.go index 2e180afbf..4ccf231ec 100644 --- a/ipn/ipnlocal/peerapi.go +++ b/ipn/ipnlocal/peerapi.go @@ -6,6 +6,7 @@ package ipnlocal import ( "context" + "encoding/json" "errors" "fmt" "hash/crc32" @@ -29,6 +30,7 @@ import ( "inet.af/netaddr" "tailscale.com/client/tailscale/apitype" + "tailscale.com/hostinfo" "tailscale.com/ipn" "tailscale.com/logtail/backoff" "tailscale.com/net/interfaces" @@ -504,6 +506,10 @@ func (h *peerAPIHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { h.handleServeGoroutines(w, r) return } + if r.URL.Path == "/v0/env" { + h.handleServeEnv(w, r) + return + } who := h.peerUser.DisplayName fmt.Fprintf(w, ` @@ -710,3 +716,23 @@ func (h *peerAPIHandler) handleServeGoroutines(w http.ResponseWriter, r *http.Re } w.Write(buf) } + +func (h *peerAPIHandler) handleServeEnv(w http.ResponseWriter, r *http.Request) { + if !h.isSelf { + http.Error(w, "not owner", http.StatusForbidden) + return + } + var data struct { + Hostinfo *tailcfg.Hostinfo + Uid int + Args []string + Env []string + } + data.Hostinfo = hostinfo.New() + data.Uid = os.Getuid() + data.Args = os.Args + data.Env = os.Environ() + + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(data) +}