From 60e189f6999497203d55c5c80b6e454d334a5e7b Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Fri, 29 Jan 2021 13:49:17 -0800 Subject: [PATCH] cmd/hello: use safesocket client to connect --- cmd/hello/hello.go | 10 ++++------ safesocket/safesocket.go | 5 +++++ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/cmd/hello/hello.go b/cmd/hello/hello.go index b88a93244..bd4145144 100644 --- a/cmd/hello/hello.go +++ b/cmd/hello/hello.go @@ -18,6 +18,7 @@ import ( "net/url" "strings" + "tailscale.com/safesocket" "tailscale.com/tailcfg" ) @@ -102,15 +103,12 @@ func root(w http.ResponseWriter, r *http.Request) { }) } -// tsSockClient does HTTP requests to the local tailscaled by dialing -// its Unix socket or whatever type of connection is required on the local -// system. -// TODO(bradfitz): do the macOS dial-the-sandbox dance like cmd/tailscale does. +// tsSockClient does HTTP requests to the local Tailscale daemon. +// The hostname in the HTTP request is ignored. var tsSockClient = &http.Client{ Transport: &http.Transport{ DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { - var d net.Dialer - return d.DialContext(ctx, "unix", "/var/run/tailscale/tailscaled.sock") + return safesocket.ConnectDefault() }, }, } diff --git a/safesocket/safesocket.go b/safesocket/safesocket.go index ba23a1d65..3fbacc4a2 100644 --- a/safesocket/safesocket.go +++ b/safesocket/safesocket.go @@ -27,6 +27,11 @@ func ConnCloseWrite(c net.Conn) error { return c.(closeable).CloseWrite() } +// ConnectDefault connects to the local Tailscale daemon. +func ConnectDefault() (net.Conn, error) { + return Connect("/var/run/tailscale/tailscaled.sock", 41112) +} + // Connect connects to either path (on Unix) or the provided localhost port (on Windows). func Connect(path string, port uint16) (net.Conn, error) { return connect(path, port)