2021-03-01 19:00:37 -08:00
|
|
|
|
// Copyright (c) 2021 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
2022-08-02 11:34:03 -07:00
|
|
|
|
//go:build go1.19
|
2022-03-18 07:44:05 -07:00
|
|
|
|
|
2021-03-01 19:00:37 -08:00
|
|
|
|
package tailscale
|
|
|
|
|
|
|
|
|
|
import (
|
2021-04-11 16:10:31 -07:00
|
|
|
|
"bytes"
|
2021-03-01 19:00:37 -08:00
|
|
|
|
"context"
|
2021-08-17 15:03:28 -07:00
|
|
|
|
"crypto/tls"
|
2021-03-01 19:00:37 -08:00
|
|
|
|
"encoding/json"
|
2021-03-31 11:55:21 -07:00
|
|
|
|
"errors"
|
2021-03-01 19:00:37 -08:00
|
|
|
|
"fmt"
|
2021-03-30 12:56:00 -07:00
|
|
|
|
"io"
|
2021-03-01 19:00:37 -08:00
|
|
|
|
"net"
|
|
|
|
|
"net/http"
|
2022-03-24 09:04:01 -07:00
|
|
|
|
"net/http/httptrace"
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-25 21:14:09 -07:00
|
|
|
|
"net/netip"
|
2021-03-01 19:00:37 -08:00
|
|
|
|
"net/url"
|
2021-09-13 17:35:55 -07:00
|
|
|
|
"os/exec"
|
|
|
|
|
"runtime"
|
2021-03-01 19:00:37 -08:00
|
|
|
|
"strconv"
|
2021-03-30 15:59:44 -07:00
|
|
|
|
"strings"
|
2021-10-05 21:40:19 -07:00
|
|
|
|
"sync"
|
2021-08-17 15:03:28 -07:00
|
|
|
|
"time"
|
2021-03-01 19:00:37 -08:00
|
|
|
|
|
2021-08-17 15:03:28 -07:00
|
|
|
|
"go4.org/mem"
|
2021-04-13 08:13:46 -07:00
|
|
|
|
"tailscale.com/client/tailscale/apitype"
|
2021-04-07 08:27:35 -07:00
|
|
|
|
"tailscale.com/ipn"
|
2021-03-18 19:34:59 -07:00
|
|
|
|
"tailscale.com/ipn/ipnstate"
|
2022-03-24 09:04:01 -07:00
|
|
|
|
"tailscale.com/net/netutil"
|
2021-03-30 09:21:22 -07:00
|
|
|
|
"tailscale.com/paths"
|
2021-03-01 19:00:37 -08:00
|
|
|
|
"tailscale.com/safesocket"
|
2021-06-25 11:44:40 -07:00
|
|
|
|
"tailscale.com/tailcfg"
|
2022-08-11 10:43:09 -07:00
|
|
|
|
"tailscale.com/tka"
|
2022-10-31 16:47:51 -07:00
|
|
|
|
"tailscale.com/types/key"
|
2021-03-01 19:00:37 -08:00
|
|
|
|
)
|
|
|
|
|
|
2022-04-29 11:20:11 -07:00
|
|
|
|
// defaultLocalClient is the default LocalClient when using the legacy
|
|
|
|
|
// package-level functions.
|
|
|
|
|
var defaultLocalClient LocalClient
|
|
|
|
|
|
2022-11-16 23:04:07 +05:00
|
|
|
|
// LocalClient is a client to Tailscale's "LocalAPI", communicating with the
|
2022-04-29 11:20:11 -07:00
|
|
|
|
// Tailscale daemon on the local machine. Its API is not necessarily stable and
|
|
|
|
|
// subject to changes between releases. Some API calls have stricter
|
|
|
|
|
// compatibility guarantees, once they've been widely adopted. See method docs
|
|
|
|
|
// for details.
|
|
|
|
|
//
|
|
|
|
|
// Its zero value is valid to use.
|
|
|
|
|
//
|
|
|
|
|
// Any exported fields should be set before using methods on the type
|
|
|
|
|
// and not changed thereafter.
|
|
|
|
|
type LocalClient struct {
|
|
|
|
|
// Dial optionally specifies an alternate func that connects to the local
|
|
|
|
|
// machine's tailscaled or equivalent. If nil, a default is used.
|
|
|
|
|
Dial func(ctx context.Context, network, addr string) (net.Conn, error)
|
|
|
|
|
|
|
|
|
|
// Socket specifies an alternate path to the local Tailscale socket.
|
|
|
|
|
// If empty, a platform-specific default is used.
|
|
|
|
|
Socket string
|
|
|
|
|
|
|
|
|
|
// UseSocketOnly, if true, tries to only connect to tailscaled via the
|
|
|
|
|
// Unix socket and not via fallback mechanisms as done on macOS when
|
|
|
|
|
// connecting to the GUI client variants.
|
|
|
|
|
UseSocketOnly bool
|
|
|
|
|
|
|
|
|
|
// tsClient does HTTP requests to the local Tailscale daemon.
|
|
|
|
|
// It's lazily initialized on first use.
|
|
|
|
|
tsClient *http.Client
|
|
|
|
|
tsClientOnce sync.Once
|
|
|
|
|
}
|
2021-03-30 09:21:22 -07:00
|
|
|
|
|
2022-04-29 11:20:11 -07:00
|
|
|
|
func (lc *LocalClient) socket() string {
|
|
|
|
|
if lc.Socket != "" {
|
|
|
|
|
return lc.Socket
|
|
|
|
|
}
|
|
|
|
|
return paths.DefaultTailscaledSocket()
|
|
|
|
|
}
|
safesocket: add ConnectionStrategy, provide control over fallbacks
fee2d9fad added support for cmd/tailscale to connect to IPNExtension.
It came in two parts: If no socket was provided, dial IPNExtension first,
and also, if dialing the socket failed, fall back to IPNExtension.
The second half of that support caused the integration tests to fail
when run on a machine that was also running IPNExtension.
The integration tests want to wait until the tailscaled instances
that they spun up are listening. They do that by dialing the new
instance. But when that dial failed, it was falling back to IPNExtension,
so it appeared (incorrectly) that tailscaled was running.
Hilarity predictably ensued.
If a user (or a test) explicitly provides a socket to dial,
it is a reasonable assumption that they have a specific tailscaled
in mind and don't want to fall back to IPNExtension.
It is certainly true of the integration tests.
Instead of adding a bool to Connect, split out the notion of a
connection strategy. For now, the implementation remains the same,
but with the details hidden a bit. Later, we can improve that.
Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
2021-12-08 13:55:55 -08:00
|
|
|
|
|
2022-04-29 11:20:11 -07:00
|
|
|
|
func (lc *LocalClient) dialer() func(ctx context.Context, network, addr string) (net.Conn, error) {
|
|
|
|
|
if lc.Dial != nil {
|
|
|
|
|
return lc.Dial
|
|
|
|
|
}
|
|
|
|
|
return lc.defaultDialer
|
|
|
|
|
}
|
2021-10-05 21:40:19 -07:00
|
|
|
|
|
2022-04-29 11:20:11 -07:00
|
|
|
|
func (lc *LocalClient) defaultDialer(ctx context.Context, network, addr string) (net.Conn, error) {
|
2021-10-05 21:40:19 -07:00
|
|
|
|
if addr != "local-tailscaled.sock:80" {
|
|
|
|
|
return nil, fmt.Errorf("unexpected URL address %q", addr)
|
|
|
|
|
}
|
2022-04-29 11:20:11 -07:00
|
|
|
|
if !lc.UseSocketOnly {
|
2021-10-05 21:40:19 -07:00
|
|
|
|
// 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))
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-04-29 11:20:11 -07:00
|
|
|
|
s := safesocket.DefaultConnectionStrategy(lc.socket())
|
safesocket: add ConnectionStrategy, provide control over fallbacks
fee2d9fad added support for cmd/tailscale to connect to IPNExtension.
It came in two parts: If no socket was provided, dial IPNExtension first,
and also, if dialing the socket failed, fall back to IPNExtension.
The second half of that support caused the integration tests to fail
when run on a machine that was also running IPNExtension.
The integration tests want to wait until the tailscaled instances
that they spun up are listening. They do that by dialing the new
instance. But when that dial failed, it was falling back to IPNExtension,
so it appeared (incorrectly) that tailscaled was running.
Hilarity predictably ensued.
If a user (or a test) explicitly provides a socket to dial,
it is a reasonable assumption that they have a specific tailscaled
in mind and don't want to fall back to IPNExtension.
It is certainly true of the integration tests.
Instead of adding a bool to Connect, split out the notion of a
connection strategy. For now, the implementation remains the same,
but with the details hidden a bit. Later, we can improve that.
Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
2021-12-08 13:55:55 -08:00
|
|
|
|
// The user provided a non-default tailscaled socket address.
|
|
|
|
|
// Connect only to exactly what they provided.
|
|
|
|
|
s.UseFallback(false)
|
|
|
|
|
return safesocket.Connect(s)
|
2021-03-01 19:00:37 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DoLocalRequest makes an HTTP request to the local machine's Tailscale daemon.
|
|
|
|
|
//
|
|
|
|
|
// URLs are of the form http://local-tailscaled.sock/localapi/v0/whois?ip=1.2.3.4.
|
|
|
|
|
//
|
|
|
|
|
// The hostname must be "local-tailscaled.sock", even though it
|
|
|
|
|
// doesn't actually do any DNS lookup. The actual means of connecting to and
|
|
|
|
|
// authenticating to the local Tailscale daemon vary by platform.
|
|
|
|
|
//
|
|
|
|
|
// DoLocalRequest may mutate the request to add Authorization headers.
|
2022-04-29 11:20:11 -07:00
|
|
|
|
func (lc *LocalClient) DoLocalRequest(req *http.Request) (*http.Response, error) {
|
|
|
|
|
lc.tsClientOnce.Do(func() {
|
|
|
|
|
lc.tsClient = &http.Client{
|
2021-10-05 21:40:19 -07:00
|
|
|
|
Transport: &http.Transport{
|
2022-04-29 11:20:11 -07:00
|
|
|
|
DialContext: lc.dialer(),
|
2021-10-05 21:40:19 -07:00
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
})
|
2021-03-01 19:00:37 -08:00
|
|
|
|
if _, token, err := safesocket.LocalTCPPortAndToken(); err == nil {
|
|
|
|
|
req.SetBasicAuth("", token)
|
|
|
|
|
}
|
2022-04-29 11:20:11 -07:00
|
|
|
|
return lc.tsClient.Do(req)
|
2021-03-01 19:00:37 -08:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-29 11:20:11 -07:00
|
|
|
|
func (lc *LocalClient) doLocalRequestNiceError(req *http.Request) (*http.Response, error) {
|
|
|
|
|
res, err := lc.DoLocalRequest(req)
|
2021-11-30 08:47:52 -08:00
|
|
|
|
if err == nil {
|
2022-04-25 20:30:41 -07:00
|
|
|
|
if server := res.Header.Get("Tailscale-Version"); server != "" && server != ipn.IPCVersion() && onVersionMismatch != nil {
|
|
|
|
|
onVersionMismatch(ipn.IPCVersion(), server)
|
2021-11-30 08:47:52 -08:00
|
|
|
|
}
|
2022-01-25 09:58:21 -08:00
|
|
|
|
if res.StatusCode == 403 {
|
2022-09-15 20:06:59 +08:00
|
|
|
|
all, _ := io.ReadAll(res.Body)
|
2022-01-25 09:58:21 -08:00
|
|
|
|
return nil, &AccessDeniedError{errors.New(errorMessageFromBody(all))}
|
|
|
|
|
}
|
2021-11-30 08:47:52 -08:00
|
|
|
|
return res, nil
|
|
|
|
|
}
|
|
|
|
|
if ue, ok := err.(*url.Error); ok {
|
|
|
|
|
if oe, ok := ue.Err.(*net.OpError); ok && oe.Op == "dial" {
|
|
|
|
|
path := req.URL.Path
|
2022-03-19 12:42:46 -07:00
|
|
|
|
pathPrefix, _, _ := strings.Cut(path, "?")
|
2021-11-30 08:47:52 -08:00
|
|
|
|
return nil, fmt.Errorf("Failed to connect to local Tailscale daemon for %s; %s Error: %w", pathPrefix, tailscaledConnectHint(), oe)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-16 10:57:46 -07:00
|
|
|
|
type errorJSON struct {
|
|
|
|
|
Error string
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-01 09:48:18 -07:00
|
|
|
|
// AccessDeniedError is an error due to permissions.
|
|
|
|
|
type AccessDeniedError struct {
|
|
|
|
|
err error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *AccessDeniedError) Error() string { return fmt.Sprintf("Access denied: %v", e.err) }
|
|
|
|
|
func (e *AccessDeniedError) Unwrap() error { return e.err }
|
|
|
|
|
|
|
|
|
|
// IsAccessDeniedError reports whether err is or wraps an AccessDeniedError.
|
|
|
|
|
func IsAccessDeniedError(err error) bool {
|
|
|
|
|
var ae *AccessDeniedError
|
|
|
|
|
return errors.As(err, &ae)
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-16 10:57:46 -07:00
|
|
|
|
// bestError returns either err, or if body contains a valid JSON
|
|
|
|
|
// object of type errorJSON, its non-empty error body.
|
|
|
|
|
func bestError(err error, body []byte) error {
|
|
|
|
|
var j errorJSON
|
|
|
|
|
if err := json.Unmarshal(body, &j); err == nil && j.Error != "" {
|
|
|
|
|
return errors.New(j.Error)
|
|
|
|
|
}
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-01 09:48:18 -07:00
|
|
|
|
func errorMessageFromBody(body []byte) string {
|
|
|
|
|
var j errorJSON
|
|
|
|
|
if err := json.Unmarshal(body, &j); err == nil && j.Error != "" {
|
|
|
|
|
return j.Error
|
|
|
|
|
}
|
|
|
|
|
return strings.TrimSpace(string(body))
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-31 15:21:27 -07:00
|
|
|
|
var onVersionMismatch func(clientVer, serverVer string)
|
|
|
|
|
|
|
|
|
|
// SetVersionMismatchHandler sets f as the version mismatch handler
|
|
|
|
|
// to be called when the client (the current process) has a version
|
|
|
|
|
// number that doesn't match the server's declared version.
|
|
|
|
|
func SetVersionMismatchHandler(f func(clientVer, serverVer string)) {
|
|
|
|
|
onVersionMismatch = f
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-29 11:20:11 -07:00
|
|
|
|
func (lc *LocalClient) send(ctx context.Context, method, path string, wantStatus int, body io.Reader) ([]byte, error) {
|
2022-11-10 20:36:06 -08:00
|
|
|
|
if jr, ok := body.(jsonReader); ok && jr.err != nil {
|
|
|
|
|
return nil, jr.err // fail early if there was a JSON marshaling error
|
|
|
|
|
}
|
2022-11-16 07:19:07 -08:00
|
|
|
|
req, err := http.NewRequestWithContext(ctx, method, "http://"+apitype.LocalAPIHost+path, body)
|
2021-03-01 19:00:37 -08:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2022-04-29 11:20:11 -07:00
|
|
|
|
res, err := lc.doLocalRequestNiceError(req)
|
2021-03-01 19:00:37 -08:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
defer res.Body.Close()
|
2022-09-15 20:06:59 +08:00
|
|
|
|
slurp, err := io.ReadAll(res.Body)
|
2021-04-07 08:19:36 -07:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if res.StatusCode != wantStatus {
|
2022-03-23 13:52:29 -07:00
|
|
|
|
err = fmt.Errorf("%v: %s", res.Status, bytes.TrimSpace(slurp))
|
2021-04-16 10:57:46 -07:00
|
|
|
|
return nil, bestError(err, slurp)
|
2021-04-07 08:19:36 -07:00
|
|
|
|
}
|
|
|
|
|
return slurp, nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-29 11:20:11 -07:00
|
|
|
|
func (lc *LocalClient) get200(ctx context.Context, path string) ([]byte, error) {
|
|
|
|
|
return lc.send(ctx, "GET", path, 200, nil)
|
2021-04-07 08:19:36 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WhoIs returns the owner of the remoteAddr, which must be an IP or IP:port.
|
2022-04-29 11:20:11 -07:00
|
|
|
|
//
|
|
|
|
|
// Deprecated: use LocalClient.WhoIs.
|
2021-04-13 08:13:46 -07:00
|
|
|
|
func WhoIs(ctx context.Context, remoteAddr string) (*apitype.WhoIsResponse, error) {
|
2022-04-29 11:20:11 -07:00
|
|
|
|
return defaultLocalClient.WhoIs(ctx, remoteAddr)
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-10 21:43:26 -08:00
|
|
|
|
func decodeJSON[T any](b []byte) (ret T, err error) {
|
|
|
|
|
if err := json.Unmarshal(b, &ret); err != nil {
|
|
|
|
|
var zero T
|
|
|
|
|
return zero, fmt.Errorf("failed to unmarshal JSON into %T: %w", ret, err)
|
|
|
|
|
}
|
|
|
|
|
return ret, nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-29 11:20:11 -07:00
|
|
|
|
// WhoIs returns the owner of the remoteAddr, which must be an IP or IP:port.
|
|
|
|
|
func (lc *LocalClient) WhoIs(ctx context.Context, remoteAddr string) (*apitype.WhoIsResponse, error) {
|
|
|
|
|
body, err := lc.get200(ctx, "/localapi/v0/whois?addr="+url.QueryEscape(remoteAddr))
|
2021-04-07 08:19:36 -07:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
2021-03-01 19:00:37 -08:00
|
|
|
|
}
|
2022-11-10 21:43:26 -08:00
|
|
|
|
return decodeJSON[*apitype.WhoIsResponse](body)
|
2021-03-01 19:00:37 -08:00
|
|
|
|
}
|
2021-03-05 12:07:00 -08:00
|
|
|
|
|
2021-03-18 19:34:59 -07:00
|
|
|
|
// Goroutines returns a dump of the Tailscale daemon's current goroutines.
|
2022-04-29 11:20:11 -07:00
|
|
|
|
func (lc *LocalClient) Goroutines(ctx context.Context) ([]byte, error) {
|
|
|
|
|
return lc.get200(ctx, "/localapi/v0/goroutines")
|
2021-03-05 12:07:00 -08:00
|
|
|
|
}
|
2021-03-18 19:34:59 -07:00
|
|
|
|
|
2021-11-15 15:06:37 -08:00
|
|
|
|
// DaemonMetrics returns the Tailscale daemon's metrics in
|
|
|
|
|
// the Prometheus text exposition format.
|
2022-04-29 11:20:11 -07:00
|
|
|
|
func (lc *LocalClient) DaemonMetrics(ctx context.Context) ([]byte, error) {
|
|
|
|
|
return lc.get200(ctx, "/localapi/v0/metrics")
|
2021-11-15 15:06:37 -08:00
|
|
|
|
}
|
|
|
|
|
|
2022-11-10 11:41:04 -08:00
|
|
|
|
// Pprof returns a pprof profile of the Tailscale daemon.
|
|
|
|
|
func (lc *LocalClient) Pprof(ctx context.Context, pprofType string, sec int) ([]byte, error) {
|
2021-09-23 09:20:14 -07:00
|
|
|
|
var secArg string
|
|
|
|
|
if sec < 0 || sec > 300 {
|
|
|
|
|
return nil, errors.New("duration out of range")
|
|
|
|
|
}
|
|
|
|
|
if sec != 0 || pprofType == "profile" {
|
|
|
|
|
secArg = fmt.Sprint(sec)
|
|
|
|
|
}
|
2022-11-10 11:41:04 -08:00
|
|
|
|
return lc.get200(ctx, fmt.Sprintf("/localapi/v0/pprof?name=%s&seconds=%v", url.QueryEscape(pprofType), secArg))
|
2021-09-23 09:20:14 -07:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-26 13:07:28 -04:00
|
|
|
|
// BugReportOpts contains options to pass to the Tailscale daemon when
|
|
|
|
|
// generating a bug report.
|
|
|
|
|
type BugReportOpts struct {
|
|
|
|
|
// Note contains an optional user-provided note to add to the logs.
|
|
|
|
|
Note string
|
|
|
|
|
|
|
|
|
|
// Diagnose specifies whether to print additional diagnostic information to
|
|
|
|
|
// the logs when generating this bugreport.
|
|
|
|
|
Diagnose bool
|
2022-10-15 19:31:35 +02:00
|
|
|
|
|
|
|
|
|
// Record specifies, if non-nil, whether to perform a bugreport
|
|
|
|
|
// "recording"–generating an initial log marker, then waiting for
|
|
|
|
|
// this channel to be closed before finishing the request, which
|
|
|
|
|
// generates another log marker.
|
|
|
|
|
Record <-chan struct{}
|
2022-09-26 13:07:28 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// BugReportWithOpts logs and returns a log marker that can be shared by the
|
|
|
|
|
// user with support.
|
|
|
|
|
//
|
|
|
|
|
// The opts type specifies options to pass to the Tailscale daemon when
|
|
|
|
|
// generating this bug report.
|
|
|
|
|
func (lc *LocalClient) BugReportWithOpts(ctx context.Context, opts BugReportOpts) (string, error) {
|
2022-10-15 19:31:35 +02:00
|
|
|
|
qparams := make(url.Values)
|
2022-09-26 13:07:28 -04:00
|
|
|
|
if opts.Note != "" {
|
|
|
|
|
qparams.Set("note", opts.Note)
|
|
|
|
|
}
|
|
|
|
|
if opts.Diagnose {
|
|
|
|
|
qparams.Set("diagnose", "true")
|
|
|
|
|
}
|
2022-10-15 19:31:35 +02:00
|
|
|
|
if opts.Record != nil {
|
|
|
|
|
qparams.Set("record", "true")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
|
|
var requestBody io.Reader
|
|
|
|
|
if opts.Record != nil {
|
|
|
|
|
pr, pw := io.Pipe()
|
|
|
|
|
requestBody = pr
|
|
|
|
|
|
|
|
|
|
// This goroutine waits for the 'Record' channel to be closed,
|
|
|
|
|
// and then closes the write end of our pipe to unblock the
|
|
|
|
|
// reader.
|
|
|
|
|
go func() {
|
|
|
|
|
defer pw.Close()
|
|
|
|
|
select {
|
|
|
|
|
case <-opts.Record:
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
}
|
2022-09-26 13:07:28 -04:00
|
|
|
|
|
2022-10-15 19:31:35 +02:00
|
|
|
|
// lc.send might block if opts.Record != nil; see above.
|
2022-09-26 13:07:28 -04:00
|
|
|
|
uri := fmt.Sprintf("/localapi/v0/bugreport?%s", qparams.Encode())
|
2022-10-15 19:31:35 +02:00
|
|
|
|
body, err := lc.send(ctx, "POST", uri, 200, requestBody)
|
2021-03-30 15:59:44 -07:00
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
return strings.TrimSpace(string(body)), nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-26 13:07:28 -04:00
|
|
|
|
// BugReport logs and returns a log marker that can be shared by the user with support.
|
|
|
|
|
//
|
|
|
|
|
// This is the same as calling BugReportWithOpts and only specifying the Note
|
|
|
|
|
// field.
|
|
|
|
|
func (lc *LocalClient) BugReport(ctx context.Context, note string) (string, error) {
|
|
|
|
|
return lc.BugReportWithOpts(ctx, BugReportOpts{Note: note})
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-28 19:41:41 -08:00
|
|
|
|
// DebugAction invokes a debug action, such as "rebind" or "restun".
|
|
|
|
|
// These are development tools and subject to change or removal over time.
|
2022-04-29 11:20:11 -07:00
|
|
|
|
func (lc *LocalClient) DebugAction(ctx context.Context, action string) error {
|
|
|
|
|
body, err := lc.send(ctx, "POST", "/localapi/v0/debug?action="+url.QueryEscape(action), 200, nil)
|
2021-12-28 19:41:41 -08:00
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("error %w: %s", err, body)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-07 13:04:10 -08:00
|
|
|
|
// SetDevStoreKeyValue set a statestore key/value. It's only meant for development.
|
|
|
|
|
// The schema (including when keys are re-read) is not a stable interface.
|
|
|
|
|
func (lc *LocalClient) SetDevStoreKeyValue(ctx context.Context, key, value string) error {
|
|
|
|
|
body, err := lc.send(ctx, "POST", "/localapi/v0/dev-set-state-store?"+(url.Values{
|
|
|
|
|
"key": {key},
|
|
|
|
|
"value": {value},
|
|
|
|
|
}).Encode(), 200, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("error %w: %s", err, body)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-03 20:39:45 -07:00
|
|
|
|
// SetComponentDebugLogging sets component's debug logging enabled for
|
|
|
|
|
// the provided duration. If the duration is in the past, the debug logging
|
|
|
|
|
// is disabled.
|
|
|
|
|
func (lc *LocalClient) SetComponentDebugLogging(ctx context.Context, component string, d time.Duration) error {
|
|
|
|
|
body, err := lc.send(ctx, "POST",
|
|
|
|
|
fmt.Sprintf("/localapi/v0/component-debug-logging?component=%s&secs=%d",
|
|
|
|
|
url.QueryEscape(component), int64(d.Seconds())), 200, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("error %w: %s", err, body)
|
|
|
|
|
}
|
|
|
|
|
var res struct {
|
|
|
|
|
Error string
|
|
|
|
|
}
|
|
|
|
|
if err := json.Unmarshal(body, &res); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if res.Error != "" {
|
|
|
|
|
return errors.New(res.Error)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-18 19:34:59 -07:00
|
|
|
|
// Status returns the Tailscale daemon's status.
|
|
|
|
|
func Status(ctx context.Context) (*ipnstate.Status, error) {
|
2022-04-29 11:20:11 -07:00
|
|
|
|
return defaultLocalClient.Status(ctx)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Status returns the Tailscale daemon's status.
|
|
|
|
|
func (lc *LocalClient) Status(ctx context.Context) (*ipnstate.Status, error) {
|
|
|
|
|
return lc.status(ctx, "")
|
2021-03-18 21:07:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-01 00:15:47 -06:00
|
|
|
|
// StatusWithoutPeers returns the Tailscale daemon's status, without the peer info.
|
2021-03-18 21:07:58 -07:00
|
|
|
|
func StatusWithoutPeers(ctx context.Context) (*ipnstate.Status, error) {
|
2022-04-29 11:20:11 -07:00
|
|
|
|
return defaultLocalClient.StatusWithoutPeers(ctx)
|
2021-03-18 21:07:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-29 11:20:11 -07:00
|
|
|
|
// StatusWithoutPeers returns the Tailscale daemon's status, without the peer info.
|
|
|
|
|
func (lc *LocalClient) StatusWithoutPeers(ctx context.Context) (*ipnstate.Status, error) {
|
|
|
|
|
return lc.status(ctx, "?peers=false")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (lc *LocalClient) status(ctx context.Context, queryString string) (*ipnstate.Status, error) {
|
|
|
|
|
body, err := lc.get200(ctx, "/localapi/v0/status"+queryString)
|
2021-03-18 19:34:59 -07:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2022-11-10 21:43:26 -08:00
|
|
|
|
return decodeJSON[*ipnstate.Status](body)
|
2021-03-18 19:34:59 -07:00
|
|
|
|
}
|
2021-03-30 12:56:00 -07:00
|
|
|
|
|
2022-04-12 11:57:46 -07:00
|
|
|
|
// IDToken is a request to get an OIDC ID token for an audience.
|
|
|
|
|
// The token can be presented to any resource provider which offers OIDC
|
|
|
|
|
// Federation.
|
2022-04-29 11:20:11 -07:00
|
|
|
|
func (lc *LocalClient) IDToken(ctx context.Context, aud string) (*tailcfg.TokenResponse, error) {
|
|
|
|
|
body, err := lc.get200(ctx, "/localapi/v0/id-token?aud="+url.QueryEscape(aud))
|
2022-04-12 11:57:46 -07:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2022-11-10 21:43:26 -08:00
|
|
|
|
return decodeJSON[*tailcfg.TokenResponse](body)
|
2022-04-12 11:57:46 -07:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-29 11:20:11 -07:00
|
|
|
|
func (lc *LocalClient) WaitingFiles(ctx context.Context) ([]apitype.WaitingFile, error) {
|
|
|
|
|
body, err := lc.get200(ctx, "/localapi/v0/files/")
|
2021-03-30 12:56:00 -07:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2022-11-10 21:43:26 -08:00
|
|
|
|
return decodeJSON[[]apitype.WaitingFile](body)
|
2021-03-30 12:56:00 -07:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-29 11:20:11 -07:00
|
|
|
|
func (lc *LocalClient) DeleteWaitingFile(ctx context.Context, baseName string) error {
|
|
|
|
|
_, err := lc.send(ctx, "DELETE", "/localapi/v0/files/"+url.PathEscape(baseName), http.StatusNoContent, nil)
|
2021-04-07 08:19:36 -07:00
|
|
|
|
return err
|
2021-03-30 12:56:00 -07:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-29 11:20:11 -07:00
|
|
|
|
func (lc *LocalClient) GetWaitingFile(ctx context.Context, baseName string) (rc io.ReadCloser, size int64, err error) {
|
2022-11-16 07:19:07 -08:00
|
|
|
|
req, err := http.NewRequestWithContext(ctx, "GET", "http://"+apitype.LocalAPIHost+"/localapi/v0/files/"+url.PathEscape(baseName), nil)
|
2021-03-30 12:56:00 -07:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, 0, err
|
|
|
|
|
}
|
2022-04-29 11:20:11 -07:00
|
|
|
|
res, err := lc.doLocalRequestNiceError(req)
|
2021-03-30 12:56:00 -07:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, 0, err
|
|
|
|
|
}
|
|
|
|
|
if res.ContentLength == -1 {
|
|
|
|
|
res.Body.Close()
|
|
|
|
|
return nil, 0, fmt.Errorf("unexpected chunking")
|
|
|
|
|
}
|
|
|
|
|
if res.StatusCode != 200 {
|
2022-09-15 20:06:59 +08:00
|
|
|
|
body, _ := io.ReadAll(res.Body)
|
2021-03-30 12:56:00 -07:00
|
|
|
|
res.Body.Close()
|
2021-03-31 11:55:21 -07:00
|
|
|
|
return nil, 0, fmt.Errorf("HTTP %s: %s", res.Status, body)
|
2021-03-30 12:56:00 -07:00
|
|
|
|
}
|
|
|
|
|
return res.Body, res.ContentLength, nil
|
|
|
|
|
}
|
2021-03-31 11:55:21 -07:00
|
|
|
|
|
2022-04-29 11:20:11 -07:00
|
|
|
|
func (lc *LocalClient) FileTargets(ctx context.Context) ([]apitype.FileTarget, error) {
|
|
|
|
|
body, err := lc.get200(ctx, "/localapi/v0/file-targets")
|
2021-04-13 08:13:46 -07:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2022-11-10 21:43:26 -08:00
|
|
|
|
return decodeJSON[[]apitype.FileTarget](body)
|
2021-04-13 08:13:46 -07:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-30 08:47:52 -08:00
|
|
|
|
// PushFile sends Taildrop file r to target.
|
|
|
|
|
//
|
|
|
|
|
// A size of -1 means unknown.
|
|
|
|
|
// The name parameter is the original filename, not escaped.
|
2022-04-29 11:20:11 -07:00
|
|
|
|
func (lc *LocalClient) PushFile(ctx context.Context, target tailcfg.StableNodeID, size int64, name string, r io.Reader) error {
|
2022-11-16 07:19:07 -08:00
|
|
|
|
req, err := http.NewRequestWithContext(ctx, "PUT", "http://"+apitype.LocalAPIHost+"/localapi/v0/file-put/"+string(target)+"/"+url.PathEscape(name), r)
|
2021-11-30 08:47:52 -08:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if size != -1 {
|
|
|
|
|
req.ContentLength = size
|
|
|
|
|
}
|
2022-04-29 11:20:11 -07:00
|
|
|
|
res, err := lc.doLocalRequestNiceError(req)
|
2021-11-30 08:47:52 -08:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if res.StatusCode == 200 {
|
|
|
|
|
io.Copy(io.Discard, res.Body)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
all, _ := io.ReadAll(res.Body)
|
2022-01-25 09:58:21 -08:00
|
|
|
|
return bestError(fmt.Errorf("%s: %s", res.Status, all), all)
|
2021-11-30 08:47:52 -08:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-29 11:20:11 -07:00
|
|
|
|
// CheckIPForwarding asks the local Tailscale daemon whether it looks like the
|
|
|
|
|
// machine is properly configured to forward IP packets as a subnet router
|
|
|
|
|
// or exit node.
|
|
|
|
|
func (lc *LocalClient) CheckIPForwarding(ctx context.Context) error {
|
|
|
|
|
body, err := lc.get200(ctx, "/localapi/v0/check-ip-forwarding")
|
2021-03-31 11:55:21 -07:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
var jres struct {
|
|
|
|
|
Warning string
|
|
|
|
|
}
|
2021-04-07 08:19:36 -07:00
|
|
|
|
if err := json.Unmarshal(body, &jres); err != nil {
|
2021-03-31 11:55:21 -07:00
|
|
|
|
return fmt.Errorf("invalid JSON from check-ip-forwarding: %w", err)
|
|
|
|
|
}
|
|
|
|
|
if jres.Warning != "" {
|
|
|
|
|
return errors.New(jres.Warning)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2021-04-07 08:27:35 -07:00
|
|
|
|
|
2022-04-18 09:37:23 -07:00
|
|
|
|
// CheckPrefs validates the provided preferences, without making any changes.
|
|
|
|
|
//
|
|
|
|
|
// The CLI uses this before a Start call to fail fast if the preferences won't
|
|
|
|
|
// work. Currently (2022-04-18) this only checks for SSH server compatibility.
|
|
|
|
|
// Note that EditPrefs does the same validation as this, so call CheckPrefs before
|
|
|
|
|
// EditPrefs is not necessary.
|
2022-04-29 11:20:11 -07:00
|
|
|
|
func (lc *LocalClient) CheckPrefs(ctx context.Context, p *ipn.Prefs) error {
|
2022-11-10 20:36:06 -08:00
|
|
|
|
_, err := lc.send(ctx, "POST", "/localapi/v0/check-prefs", http.StatusOK, jsonBody(p))
|
2022-04-18 09:37:23 -07:00
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-29 11:20:11 -07:00
|
|
|
|
func (lc *LocalClient) GetPrefs(ctx context.Context) (*ipn.Prefs, error) {
|
|
|
|
|
body, err := lc.get200(ctx, "/localapi/v0/prefs")
|
2021-04-07 08:27:35 -07:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
var p ipn.Prefs
|
|
|
|
|
if err := json.Unmarshal(body, &p); err != nil {
|
2021-04-11 16:10:31 -07:00
|
|
|
|
return nil, fmt.Errorf("invalid prefs JSON: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return &p, nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-29 11:20:11 -07:00
|
|
|
|
func (lc *LocalClient) EditPrefs(ctx context.Context, mp *ipn.MaskedPrefs) (*ipn.Prefs, error) {
|
2022-11-10 20:36:06 -08:00
|
|
|
|
body, err := lc.send(ctx, "PATCH", "/localapi/v0/prefs", http.StatusOK, jsonBody(mp))
|
2021-04-11 16:10:31 -07:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2022-11-10 21:43:26 -08:00
|
|
|
|
return decodeJSON[*ipn.Prefs](body)
|
2021-04-07 08:27:35 -07:00
|
|
|
|
}
|
2021-04-07 21:06:31 -07:00
|
|
|
|
|
2022-04-29 11:20:11 -07:00
|
|
|
|
func (lc *LocalClient) Logout(ctx context.Context) error {
|
|
|
|
|
_, err := lc.send(ctx, "POST", "/localapi/v0/logout", http.StatusNoContent, nil)
|
2021-04-07 21:06:31 -07:00
|
|
|
|
return err
|
|
|
|
|
}
|
2021-06-08 14:49:13 -07:00
|
|
|
|
|
2021-06-08 15:24:53 -07:00
|
|
|
|
// SetDNS adds a DNS TXT record for the given domain name, containing
|
|
|
|
|
// the provided TXT value. The intended use case is answering
|
|
|
|
|
// LetsEncrypt/ACME dns-01 challenges.
|
|
|
|
|
//
|
|
|
|
|
// The control plane will only permit SetDNS requests with very
|
|
|
|
|
// specific names and values. The name should be
|
|
|
|
|
// "_acme-challenge." + your node's MagicDNS name. It's expected that
|
|
|
|
|
// clients cache the certs from LetsEncrypt (or whichever CA is
|
|
|
|
|
// providing them) and only request new ones as needed; the control plane
|
|
|
|
|
// rate limits SetDNS requests.
|
|
|
|
|
//
|
|
|
|
|
// This is a low-level interface; it's expected that most Tailscale
|
|
|
|
|
// users use a higher level interface to getting/using TLS
|
|
|
|
|
// certificates.
|
2022-04-29 11:20:11 -07:00
|
|
|
|
func (lc *LocalClient) SetDNS(ctx context.Context, name, value string) error {
|
2021-06-08 14:49:13 -07:00
|
|
|
|
v := url.Values{}
|
|
|
|
|
v.Set("name", name)
|
|
|
|
|
v.Set("value", value)
|
2022-04-29 11:20:11 -07:00
|
|
|
|
_, err := lc.send(ctx, "POST", "/localapi/v0/set-dns?"+v.Encode(), 200, nil)
|
2021-06-08 14:49:13 -07:00
|
|
|
|
return err
|
|
|
|
|
}
|
2021-06-25 11:44:40 -07:00
|
|
|
|
|
2022-03-24 09:04:01 -07:00
|
|
|
|
// DialTCP connects to the host's port via Tailscale.
|
|
|
|
|
//
|
|
|
|
|
// The host may be a base DNS name (resolved from the netmap inside
|
|
|
|
|
// tailscaled), a FQDN, or an IP address.
|
|
|
|
|
//
|
|
|
|
|
// The ctx is only used for the duration of the call, not the lifetime of the net.Conn.
|
2022-04-29 11:20:11 -07:00
|
|
|
|
func (lc *LocalClient) DialTCP(ctx context.Context, host string, port uint16) (net.Conn, error) {
|
2022-03-24 09:04:01 -07:00
|
|
|
|
connCh := make(chan net.Conn, 1)
|
|
|
|
|
trace := httptrace.ClientTrace{
|
|
|
|
|
GotConn: func(info httptrace.GotConnInfo) {
|
|
|
|
|
connCh <- info.Conn
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
ctx = httptrace.WithClientTrace(ctx, &trace)
|
2022-11-16 07:19:07 -08:00
|
|
|
|
req, err := http.NewRequestWithContext(ctx, "POST", "http://"+apitype.LocalAPIHost+"/localapi/v0/dial", nil)
|
2022-03-24 09:04:01 -07:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
req.Header = http.Header{
|
|
|
|
|
"Upgrade": []string{"ts-dial"},
|
|
|
|
|
"Connection": []string{"upgrade"},
|
|
|
|
|
"Dial-Host": []string{host},
|
|
|
|
|
"Dial-Port": []string{fmt.Sprint(port)},
|
|
|
|
|
}
|
2022-04-29 11:20:11 -07:00
|
|
|
|
res, err := lc.DoLocalRequest(req)
|
2022-03-24 09:04:01 -07:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if res.StatusCode != http.StatusSwitchingProtocols {
|
|
|
|
|
body, _ := io.ReadAll(res.Body)
|
|
|
|
|
res.Body.Close()
|
|
|
|
|
return nil, fmt.Errorf("unexpected HTTP response: %s, %s", res.Status, body)
|
|
|
|
|
}
|
|
|
|
|
// From here on, the underlying net.Conn is ours to use, but there
|
|
|
|
|
// is still a read buffer attached to it within resp.Body. So, we
|
|
|
|
|
// must direct I/O through resp.Body, but we can still use the
|
|
|
|
|
// underlying net.Conn for stuff like deadlines.
|
|
|
|
|
var switchedConn net.Conn
|
|
|
|
|
select {
|
|
|
|
|
case switchedConn = <-connCh:
|
|
|
|
|
default:
|
|
|
|
|
}
|
|
|
|
|
if switchedConn == nil {
|
|
|
|
|
res.Body.Close()
|
|
|
|
|
return nil, fmt.Errorf("httptrace didn't provide a connection")
|
|
|
|
|
}
|
|
|
|
|
rwc, ok := res.Body.(io.ReadWriteCloser)
|
|
|
|
|
if !ok {
|
|
|
|
|
res.Body.Close()
|
|
|
|
|
return nil, errors.New("http Transport did not provide a writable body")
|
|
|
|
|
}
|
|
|
|
|
return netutil.NewAltReadWriteCloserConn(rwc, switchedConn), nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-25 11:44:40 -07:00
|
|
|
|
// CurrentDERPMap returns the current DERPMap that is being used by the local tailscaled.
|
|
|
|
|
// It is intended to be used with netcheck to see availability of DERPs.
|
2022-04-29 11:20:11 -07:00
|
|
|
|
func (lc *LocalClient) CurrentDERPMap(ctx context.Context) (*tailcfg.DERPMap, error) {
|
2021-06-25 11:44:40 -07:00
|
|
|
|
var derpMap tailcfg.DERPMap
|
2022-04-29 11:20:11 -07:00
|
|
|
|
res, err := lc.send(ctx, "GET", "/localapi/v0/derpmap", 200, nil)
|
2021-06-25 11:44:40 -07:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if err = json.Unmarshal(res, &derpMap); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("invalid derp map json: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return &derpMap, nil
|
|
|
|
|
}
|
2021-08-17 15:03:28 -07:00
|
|
|
|
|
|
|
|
|
// CertPair returns a cert and private key for the provided DNS domain.
|
|
|
|
|
//
|
|
|
|
|
// It returns a cached certificate from disk if it's still valid.
|
2022-04-29 11:20:11 -07:00
|
|
|
|
//
|
|
|
|
|
// Deprecated: use LocalClient.CertPair.
|
2021-08-17 15:03:28 -07:00
|
|
|
|
func CertPair(ctx context.Context, domain string) (certPEM, keyPEM []byte, err error) {
|
2022-04-29 11:20:11 -07:00
|
|
|
|
return defaultLocalClient.CertPair(ctx, domain)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CertPair returns a cert and private key for the provided DNS domain.
|
|
|
|
|
//
|
|
|
|
|
// It returns a cached certificate from disk if it's still valid.
|
|
|
|
|
//
|
|
|
|
|
// API maturity: this is considered a stable API.
|
|
|
|
|
func (lc *LocalClient) CertPair(ctx context.Context, domain string) (certPEM, keyPEM []byte, err error) {
|
|
|
|
|
res, err := lc.send(ctx, "GET", "/localapi/v0/cert/"+domain+"?type=pair", 200, nil)
|
2021-08-17 15:03:28 -07:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, nil, err
|
|
|
|
|
}
|
|
|
|
|
// with ?type=pair, the response PEM is first the one private
|
|
|
|
|
// key PEM block, then the cert PEM blocks.
|
|
|
|
|
i := mem.Index(mem.B(res), mem.S("--\n--"))
|
|
|
|
|
if i == -1 {
|
|
|
|
|
return nil, nil, fmt.Errorf("unexpected output: no delimiter")
|
|
|
|
|
}
|
|
|
|
|
i += len("--\n")
|
|
|
|
|
keyPEM, certPEM = res[:i], res[i:]
|
|
|
|
|
if mem.Contains(mem.B(certPEM), mem.S(" PRIVATE KEY-----")) {
|
|
|
|
|
return nil, nil, fmt.Errorf("unexpected output: key in cert")
|
|
|
|
|
}
|
|
|
|
|
return certPEM, keyPEM, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetCertificate fetches a TLS certificate for the TLS ClientHello in hi.
|
|
|
|
|
//
|
|
|
|
|
// It returns a cached certificate from disk if it's still valid.
|
|
|
|
|
//
|
|
|
|
|
// It's the right signature to use as the value of
|
|
|
|
|
// tls.Config.GetCertificate.
|
2022-04-29 11:20:11 -07:00
|
|
|
|
//
|
|
|
|
|
// Deprecated: use LocalClient.GetCertificate.
|
2021-08-17 15:03:28 -07:00
|
|
|
|
func GetCertificate(hi *tls.ClientHelloInfo) (*tls.Certificate, error) {
|
2022-04-29 11:20:11 -07:00
|
|
|
|
return defaultLocalClient.GetCertificate(hi)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetCertificate fetches a TLS certificate for the TLS ClientHello in hi.
|
|
|
|
|
//
|
|
|
|
|
// It returns a cached certificate from disk if it's still valid.
|
|
|
|
|
//
|
|
|
|
|
// It's the right signature to use as the value of
|
|
|
|
|
// tls.Config.GetCertificate.
|
|
|
|
|
//
|
|
|
|
|
// API maturity: this is considered a stable API.
|
|
|
|
|
func (lc *LocalClient) GetCertificate(hi *tls.ClientHelloInfo) (*tls.Certificate, error) {
|
2021-08-17 15:03:28 -07:00
|
|
|
|
if hi == nil || hi.ServerName == "" {
|
|
|
|
|
return nil, errors.New("no SNI ServerName")
|
|
|
|
|
}
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
|
|
|
|
|
defer cancel()
|
2021-08-18 10:05:05 -07:00
|
|
|
|
|
|
|
|
|
name := hi.ServerName
|
|
|
|
|
if !strings.Contains(name, ".") {
|
2022-04-29 11:20:11 -07:00
|
|
|
|
if v, ok := lc.ExpandSNIName(ctx, name); ok {
|
2021-08-18 10:05:05 -07:00
|
|
|
|
name = v
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-04-29 11:20:11 -07:00
|
|
|
|
certPEM, keyPEM, err := lc.CertPair(ctx, name)
|
2021-08-17 15:03:28 -07:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
cert, err := tls.X509KeyPair(certPEM, keyPEM)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return &cert, nil
|
|
|
|
|
}
|
2021-08-18 10:05:05 -07:00
|
|
|
|
|
2022-09-25 14:29:55 -04:00
|
|
|
|
// ExpandSNIName expands bare label name into the most likely actual TLS cert name.
|
2022-04-29 11:20:11 -07:00
|
|
|
|
//
|
|
|
|
|
// Deprecated: use LocalClient.ExpandSNIName.
|
2021-08-18 10:05:05 -07:00
|
|
|
|
func ExpandSNIName(ctx context.Context, name string) (fqdn string, ok bool) {
|
2022-04-29 11:20:11 -07:00
|
|
|
|
return defaultLocalClient.ExpandSNIName(ctx, name)
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-25 14:29:55 -04:00
|
|
|
|
// ExpandSNIName expands bare label name into the most likely actual TLS cert name.
|
2022-04-29 11:20:11 -07:00
|
|
|
|
func (lc *LocalClient) ExpandSNIName(ctx context.Context, name string) (fqdn string, ok bool) {
|
2022-05-05 12:15:59 -07:00
|
|
|
|
st, err := lc.StatusWithoutPeers(ctx)
|
2021-08-18 10:05:05 -07:00
|
|
|
|
if err != nil {
|
|
|
|
|
return "", false
|
|
|
|
|
}
|
|
|
|
|
for _, d := range st.CertDomains {
|
|
|
|
|
if len(d) > len(name)+1 && strings.HasPrefix(d, name) && d[len(name)] == '.' {
|
|
|
|
|
return d, true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return "", false
|
|
|
|
|
}
|
2021-09-13 17:35:55 -07:00
|
|
|
|
|
2022-05-03 14:16:34 -07:00
|
|
|
|
// Ping sends a ping of the provided type to the provided IP and waits
|
|
|
|
|
// for its response.
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-25 21:14:09 -07:00
|
|
|
|
func (lc *LocalClient) Ping(ctx context.Context, ip netip.Addr, pingtype tailcfg.PingType) (*ipnstate.PingResult, error) {
|
2022-05-03 14:16:34 -07:00
|
|
|
|
v := url.Values{}
|
|
|
|
|
v.Set("ip", ip.String())
|
|
|
|
|
v.Set("type", string(pingtype))
|
|
|
|
|
body, err := lc.send(ctx, "POST", "/localapi/v0/ping?"+v.Encode(), 200, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("error %w: %s", err, body)
|
|
|
|
|
}
|
2022-11-10 21:43:26 -08:00
|
|
|
|
return decodeJSON[*ipnstate.PingResult](body)
|
2022-05-03 14:16:34 -07:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-11 10:43:09 -07:00
|
|
|
|
// NetworkLockStatus fetches information about the tailnet key authority, if one is configured.
|
|
|
|
|
func (lc *LocalClient) NetworkLockStatus(ctx context.Context) (*ipnstate.NetworkLockStatus, error) {
|
|
|
|
|
body, err := lc.send(ctx, "GET", "/localapi/v0/tka/status", 200, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("error: %w", err)
|
|
|
|
|
}
|
2022-11-10 21:43:26 -08:00
|
|
|
|
return decodeJSON[*ipnstate.NetworkLockStatus](body)
|
2022-08-11 10:43:09 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NetworkLockInit initializes the tailnet key authority.
|
2022-10-27 13:40:31 -07:00
|
|
|
|
//
|
|
|
|
|
// TODO(tom): Plumb through disablement secrets.
|
|
|
|
|
func (lc *LocalClient) NetworkLockInit(ctx context.Context, keys []tka.Key, disablementValues [][]byte) (*ipnstate.NetworkLockStatus, error) {
|
2022-08-11 10:43:09 -07:00
|
|
|
|
var b bytes.Buffer
|
|
|
|
|
type initRequest struct {
|
2022-10-27 13:40:31 -07:00
|
|
|
|
Keys []tka.Key
|
|
|
|
|
DisablementValues [][]byte
|
2022-08-11 10:43:09 -07:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-27 13:40:31 -07:00
|
|
|
|
if err := json.NewEncoder(&b).Encode(initRequest{Keys: keys, DisablementValues: disablementValues}); err != nil {
|
2022-08-11 10:43:09 -07:00
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
body, err := lc.send(ctx, "POST", "/localapi/v0/tka/init", 200, &b)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("error: %w", err)
|
|
|
|
|
}
|
2022-11-10 21:43:26 -08:00
|
|
|
|
return decodeJSON[*ipnstate.NetworkLockStatus](body)
|
2022-09-15 13:51:23 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NetworkLockModify adds and/or removes key(s) to the tailnet key authority.
|
|
|
|
|
func (lc *LocalClient) NetworkLockModify(ctx context.Context, addKeys, removeKeys []tka.Key) (*ipnstate.NetworkLockStatus, error) {
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
|
type modifyRequest struct {
|
|
|
|
|
AddKeys []tka.Key
|
|
|
|
|
RemoveKeys []tka.Key
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := json.NewEncoder(&b).Encode(modifyRequest{AddKeys: addKeys, RemoveKeys: removeKeys}); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
body, err := lc.send(ctx, "POST", "/localapi/v0/tka/modify", 200, &b)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("error: %w", err)
|
|
|
|
|
}
|
2022-11-10 21:43:26 -08:00
|
|
|
|
return decodeJSON[*ipnstate.NetworkLockStatus](body)
|
2022-08-11 10:43:09 -07:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-31 16:47:51 -07:00
|
|
|
|
// NetworkLockSign signs the specified node-key and transmits that signature to the control plane.
|
|
|
|
|
// rotationPublic, if specified, must be an ed25519 public key.
|
|
|
|
|
func (lc *LocalClient) NetworkLockSign(ctx context.Context, nodeKey key.NodePublic, rotationPublic []byte) error {
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
|
type signRequest struct {
|
|
|
|
|
NodeKey key.NodePublic
|
|
|
|
|
RotationPublic []byte
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := json.NewEncoder(&b).Encode(signRequest{NodeKey: nodeKey, RotationPublic: rotationPublic}); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if _, err := lc.send(ctx, "POST", "/localapi/v0/tka/sign", 200, &b); err != nil {
|
|
|
|
|
return fmt.Errorf("error: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-10 22:58:40 -05:00
|
|
|
|
// SetServeConfig sets or replaces the serving settings.
|
|
|
|
|
// If config is nil, settings are cleared and serving is disabled.
|
|
|
|
|
func (lc *LocalClient) SetServeConfig(ctx context.Context, config *ipn.ServeConfig) error {
|
2022-11-10 20:36:06 -08:00
|
|
|
|
_, err := lc.send(ctx, "POST", "/localapi/v0/serve-config", 200, jsonBody(config))
|
2022-11-10 22:58:40 -05:00
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("sending serve config: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-04 12:12:51 -07:00
|
|
|
|
// NetworkLockDisable shuts down network-lock across the tailnet.
|
|
|
|
|
func (lc *LocalClient) NetworkLockDisable(ctx context.Context, secret []byte) error {
|
|
|
|
|
if _, err := lc.send(ctx, "POST", "/localapi/v0/tka/disable", 200, bytes.NewReader(secret)); err != nil {
|
|
|
|
|
return fmt.Errorf("error: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-10 22:58:40 -05:00
|
|
|
|
// GetServeConfig return the current serve config.
|
2022-11-10 20:24:31 -08:00
|
|
|
|
//
|
|
|
|
|
// If the serve config is empty, it returns (nil, nil).
|
2022-11-10 22:58:40 -05:00
|
|
|
|
func (lc *LocalClient) GetServeConfig(ctx context.Context) (*ipn.ServeConfig, error) {
|
|
|
|
|
body, err := lc.send(ctx, "GET", "/localapi/v0/serve-config", 200, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("getting serve config: %w", err)
|
|
|
|
|
}
|
2022-11-10 20:24:31 -08:00
|
|
|
|
return getServeConfigFromJSON(body)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getServeConfigFromJSON(body []byte) (sc *ipn.ServeConfig, err error) {
|
|
|
|
|
if err := json.Unmarshal(body, &sc); err != nil {
|
2022-11-10 22:58:40 -05:00
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return sc, nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-13 17:35:55 -07:00
|
|
|
|
// tailscaledConnectHint gives a little thing about why tailscaled (or
|
|
|
|
|
// platform equivalent) is not answering localapi connections.
|
|
|
|
|
//
|
|
|
|
|
// It ends in a punctuation. See caller.
|
|
|
|
|
func tailscaledConnectHint() string {
|
|
|
|
|
if runtime.GOOS != "linux" {
|
|
|
|
|
// TODO(bradfitz): flesh this out
|
|
|
|
|
return "not running?"
|
|
|
|
|
}
|
|
|
|
|
out, err := exec.Command("systemctl", "show", "tailscaled.service", "--no-page", "--property", "LoadState,ActiveState,SubState").Output()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "not running?"
|
|
|
|
|
}
|
|
|
|
|
// Parse:
|
|
|
|
|
// LoadState=loaded
|
|
|
|
|
// ActiveState=inactive
|
|
|
|
|
// SubState=dead
|
|
|
|
|
st := map[string]string{}
|
|
|
|
|
for _, line := range strings.Split(string(out), "\n") {
|
2022-03-19 12:42:46 -07:00
|
|
|
|
if k, v, ok := strings.Cut(line, "="); ok {
|
|
|
|
|
st[k] = strings.TrimSpace(v)
|
2021-09-13 17:35:55 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if st["LoadState"] == "loaded" &&
|
|
|
|
|
(st["SubState"] != "running" || st["ActiveState"] != "active") {
|
|
|
|
|
return "systemd tailscaled.service not running."
|
|
|
|
|
}
|
|
|
|
|
return "not running?"
|
|
|
|
|
}
|
2022-11-10 20:36:06 -08:00
|
|
|
|
|
|
|
|
|
type jsonReader struct {
|
|
|
|
|
b *bytes.Reader
|
|
|
|
|
err error // sticky JSON marshal error, if any
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// jsonBody returns an io.Reader that marshals v as JSON and then reads it.
|
|
|
|
|
func jsonBody(v any) jsonReader {
|
|
|
|
|
b, err := json.Marshal(v)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return jsonReader{err: err}
|
|
|
|
|
}
|
|
|
|
|
return jsonReader{b: bytes.NewReader(b)}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r jsonReader) Read(p []byte) (n int, err error) {
|
|
|
|
|
if r.err != nil {
|
|
|
|
|
return 0, r.err
|
|
|
|
|
}
|
|
|
|
|
return r.b.Read(p)
|
|
|
|
|
}
|