Skip to content

Commit

Permalink
client/tailscale, cmd/tailscale/cli: plumb --socket through
Browse files Browse the repository at this point in the history
Without this, `tailscale status` ignores the --socket flag on macOS and
always talks to the IPNExtension, even if you wanted it to inspect a
userspace tailscaled.

Signed-off-by: David Crawshaw <crawshaw@tailscale.com>
  • Loading branch information
crawshaw committed Mar 30, 2021
1 parent f01091b commit e67f1b5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
20 changes: 13 additions & 7 deletions client/tailscale/tailscale.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,31 @@ import (
"strconv"

"tailscale.com/ipn/ipnstate"
"tailscale.com/paths"
"tailscale.com/safesocket"
"tailscale.com/tailcfg"
)

// TailscaledSocket is the tailscaled Unix socket.
var TailscaledSocket = paths.DefaultTailscaledSocket()

// tsClient does HTTP requests to the local Tailscale daemon.
var tsClient = &http.Client{
Transport: &http.Transport{
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
if addr != "local-tailscaled.sock:80" {
return nil, fmt.Errorf("unexpected URL address %q", addr)
}
// On macOS, when dialing from non-sandboxed program to sandboxed GUI running
// a TCP server on a random port, find the random port. For HTTP connections,
// we don't send the token. It gets added in an HTTP Basic-Auth header.
if port, _, err := safesocket.LocalTCPPortAndToken(); err == nil {
var d net.Dialer
return d.DialContext(ctx, "tcp", "localhost:"+strconv.Itoa(port))
if TailscaledSocket == paths.DefaultTailscaledSocket() {
// On macOS, when dialing from non-sandboxed program to sandboxed GUI running
// a TCP server on a random port, find the random port. For HTTP connections,
// we don't send the token. It gets added in an HTTP Basic-Auth header.
if port, _, err := safesocket.LocalTCPPortAndToken(); err == nil {
var d net.Dialer
return d.DialContext(ctx, "tcp", "localhost:"+strconv.Itoa(port))
}
}
return safesocket.ConnectDefault()
return safesocket.Connect(TailscaledSocket, 41112)
},
},
}
Expand Down
3 changes: 3 additions & 0 deletions cmd/tailscale/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"text/tabwriter"

"github.com/peterbourgon/ff/v2/ffcli"
"tailscale.com/client/tailscale"
"tailscale.com/ipn"
"tailscale.com/paths"
"tailscale.com/safesocket"
Expand Down Expand Up @@ -88,6 +89,8 @@ change in the future.
return err
}

tailscale.TailscaledSocket = rootArgs.socket

err := rootCmd.Run(context.Background())
if err == flag.ErrHelp {
return nil
Expand Down
7 changes: 0 additions & 7 deletions safesocket/safesocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
"errors"
"net"
"runtime"

"tailscale.com/paths"
)

type closeable interface {
Expand All @@ -31,11 +29,6 @@ func ConnCloseWrite(c net.Conn) error {
return c.(closeable).CloseWrite()
}

// ConnectDefault connects to the local Tailscale daemon.
func ConnectDefault() (net.Conn, error) {
return Connect(paths.DefaultTailscaledSocket(), 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)
Expand Down

0 comments on commit e67f1b5

Please sign in to comment.