diff --git a/safesocket/safesocket_darwin.go b/safesocket/safesocket_darwin.go index f72570890..36fc7c438 100644 --- a/safesocket/safesocket_darwin.go +++ b/safesocket/safesocket_darwin.go @@ -8,12 +8,14 @@ import ( "bytes" "errors" "fmt" + "net" "os" "os/exec" "path/filepath" "strconv" "strings" "sync" + "time" ) func init() { @@ -46,6 +48,17 @@ func localTCPPortAndTokenMacsys() (port int, token string, err error) { if auth == "" { return 0, "", errors.New("empty auth token in sameuserproof file") } + + // The above files exist forever after the first run of + // /Applications/Tailscale.app, so check we can connect to avoid returning a + // port nothing is listening on. Connect to "127.0.0.1" rather than + // "localhost" due to #7851. + conn, err := net.DialTimeout("tcp", "127.0.0.1:"+portStr, time.Second) + if err != nil { + return 0, "", err + } + conn.Close() + return port, auth, nil }