cmd/tailscale: Check App Store tailscaled dialable before selecting. (#9234)

PR #9217 attempted to fix the same issue, but suffered from not letting the
user connect to non-oss tailscaled if something was listening on the socket, as
the --socket flag doesn't let you select the mac apps.

Rather than leave the user unable to choose, we keep the mac/socket preference
order the same and check a bit harder whether the macsys version really is
running. Now, we prefer the App Store Tailscale (even if it's Stopped) and you
can use --socket to sswitch. But if you quit the App Store Tailscale, we'll try
the socket without needing the flag.

Fixes #5761
Signed-off-by: Paul Scott <408401+icio@users.noreply.github.com>
This commit is contained in:
Paul Scott 2023-09-06 12:43:10 +01:00 committed by GitHub
parent 86b0fc5295
commit 9a56184bef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -8,12 +8,14 @@
"bytes" "bytes"
"errors" "errors"
"fmt" "fmt"
"net"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
"time"
) )
func init() { func init() {
@ -46,6 +48,17 @@ func localTCPPortAndTokenMacsys() (port int, token string, err error) {
if auth == "" { if auth == "" {
return 0, "", errors.New("empty auth token in sameuserproof file") 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 return port, auth, nil
} }