From e8d2fc7f7f047ed19498a4d80abb81997f1e147a Mon Sep 17 00:00:00 2001 From: Andrew Dunham Date: Thu, 22 Feb 2024 19:06:46 -0500 Subject: [PATCH] net/tshttpproxy: log when we're using a proxy Updates #11196 Signed-off-by: Andrew Dunham Change-Id: Id6334c10f52f4cfbda9f03dc8096ab7a6c54a088 --- net/tshttpproxy/tshttpproxy.go | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/net/tshttpproxy/tshttpproxy.go b/net/tshttpproxy/tshttpproxy.go index 24b0050e9..2ca440b57 100644 --- a/net/tshttpproxy/tshttpproxy.go +++ b/net/tshttpproxy/tshttpproxy.go @@ -19,6 +19,7 @@ "time" "golang.org/x/net/http/httpproxy" + "tailscale.com/util/mak" ) // InvalidateCache invalidates the package-level cache for ProxyFromEnvironment. @@ -117,10 +118,33 @@ func SetSelfProxy(addrs ...string) { // For example, WPAD PAC files on Windows. var sysProxyFromEnv func(*http.Request) (*url.URL, error) +// These variables track whether we've printed a log message for a given proxy +// URL; we only print them once to avoid log spam. +var ( + logMessageMu sync.Mutex + logMessagePrinted map[string]bool +) + // ProxyFromEnvironment is like the standard library's http.ProxyFromEnvironment // but additionally does OS-specific proxy lookups if the environment variables // alone don't specify a proxy. -func ProxyFromEnvironment(req *http.Request) (*url.URL, error) { +func ProxyFromEnvironment(req *http.Request) (ret *url.URL, _ error) { + defer func() { + if ret == nil { + return + } + + ss := ret.String() + + logMessageMu.Lock() + defer logMessageMu.Unlock() + if logMessagePrinted[ss] { + return + } + log.Printf("tshttpproxy: using proxy %q for URL: %q", ss, req.URL.String()) + mak.Set(&logMessagePrinted, ss, true) + }() + localProxyFunc := getProxyFunc() u, err := localProxyFunc(req.URL) if u != nil && err == nil {