2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2020-02-25 16:46:26 +00:00
|
|
|
|
|
|
|
// Package safesocket creates either a Unix socket, if possible, or
|
|
|
|
// otherwise a localhost TCP connection.
|
|
|
|
package safesocket
|
|
|
|
|
|
|
|
import (
|
2021-01-29 22:32:56 +00:00
|
|
|
"errors"
|
2020-02-25 16:46:26 +00:00
|
|
|
"net"
|
2021-01-29 22:32:56 +00:00
|
|
|
"runtime"
|
2021-08-31 21:36:10 +00:00
|
|
|
"time"
|
2020-02-25 16:46:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type closeable interface {
|
|
|
|
CloseRead() error
|
|
|
|
CloseWrite() error
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConnCloseRead calls c's CloseRead method. c is expected to be
|
|
|
|
// either a UnixConn or TCPConn as returned from this package.
|
|
|
|
func ConnCloseRead(c net.Conn) error {
|
|
|
|
return c.(closeable).CloseRead()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConnCloseWrite calls c's CloseWrite method. c is expected to be
|
|
|
|
// either a UnixConn or TCPConn as returned from this package.
|
|
|
|
func ConnCloseWrite(c net.Conn) error {
|
|
|
|
return c.(closeable).CloseWrite()
|
|
|
|
}
|
|
|
|
|
2021-08-31 21:36:10 +00:00
|
|
|
var processStartTime = time.Now()
|
|
|
|
var tailscaledProcExists = func() bool { return false } // set by safesocket_ps.go
|
|
|
|
|
|
|
|
// tailscaledStillStarting reports whether tailscaled is probably
|
|
|
|
// still starting up. That is, it reports whether the caller should
|
|
|
|
// keep retrying to connect.
|
|
|
|
func tailscaledStillStarting() bool {
|
|
|
|
d := time.Since(processStartTime)
|
|
|
|
if d < 2*time.Second {
|
|
|
|
// Without even checking the process table, assume
|
|
|
|
// that for the first two seconds that tailscaled is
|
|
|
|
// probably still starting. That is, assume they're
|
|
|
|
// running "tailscaled & tailscale up ...." and make
|
|
|
|
// the tailscale client block for a bit for tailscaled
|
|
|
|
// to start accepting on the socket.
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if d > 5*time.Second {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return tailscaledProcExists()
|
|
|
|
}
|
|
|
|
|
2023-12-21 20:55:14 +00:00
|
|
|
// Connect connects to tailscaled using a unix socket or named pipe.
|
|
|
|
func Connect(path string) (net.Conn, error) {
|
2021-08-31 21:36:10 +00:00
|
|
|
for {
|
2023-12-21 20:55:14 +00:00
|
|
|
c, err := connect(path)
|
2021-08-31 21:36:10 +00:00
|
|
|
if err != nil && tailscaledStillStarting() {
|
|
|
|
time.Sleep(250 * time.Millisecond)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return c, err
|
|
|
|
}
|
2020-02-25 16:46:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Listen returns a listener either on Unix socket path (on Unix), or
|
2023-01-30 17:34:51 +00:00
|
|
|
// the NamedPipe path (on Windows).
|
|
|
|
func Listen(path string) (net.Listener, error) {
|
|
|
|
return listen(path)
|
2020-02-25 16:46:26 +00:00
|
|
|
}
|
2021-01-29 22:32:56 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
ErrTokenNotFound = errors.New("no token found")
|
|
|
|
ErrNoTokenOnOS = errors.New("no token on " + runtime.GOOS)
|
|
|
|
)
|
|
|
|
|
|
|
|
var localTCPPortAndToken func() (port int, token string, err error)
|
|
|
|
|
|
|
|
// LocalTCPPortAndToken returns the port number and auth token to connect to
|
|
|
|
// the local Tailscale daemon. It's currently only applicable on macOS
|
|
|
|
// when tailscaled is being run in the Mac Sandbox from the App Store version
|
|
|
|
// of Tailscale.
|
|
|
|
func LocalTCPPortAndToken() (port int, token string, err error) {
|
|
|
|
if localTCPPortAndToken == nil {
|
|
|
|
return 0, "", ErrNoTokenOnOS
|
|
|
|
}
|
|
|
|
return localTCPPortAndToken()
|
|
|
|
}
|
2021-03-02 19:12:14 +00:00
|
|
|
|
|
|
|
// PlatformUsesPeerCreds reports whether the current platform uses peer credentials
|
|
|
|
// to authenticate connections.
|
2021-05-03 16:23:01 +00:00
|
|
|
func PlatformUsesPeerCreds() bool { return GOOSUsesPeerCreds(runtime.GOOS) }
|
|
|
|
|
|
|
|
// GOOSUsesPeerCreds is like PlatformUsesPeerCreds but takes a
|
|
|
|
// runtime.GOOS value instead of using the current one.
|
|
|
|
func GOOSUsesPeerCreds(goos string) bool {
|
|
|
|
switch goos {
|
2021-03-03 19:41:32 +00:00
|
|
|
case "linux", "darwin", "freebsd":
|
2021-03-02 19:12:14 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|