2020-02-05 22:16:58 +00:00
|
|
|
// Copyright (c) 2020 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.
|
|
|
|
|
2020-02-14 17:28:29 +00:00
|
|
|
// Package controlclient implements the client for the Tailscale
|
|
|
|
// control plane.
|
2020-02-05 22:16:58 +00:00
|
|
|
//
|
|
|
|
// It handles authentication, port picking, and collects the local
|
|
|
|
// network configuration.
|
|
|
|
package controlclient
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2020-05-27 18:46:09 +00:00
|
|
|
"github.com/tailscale/wireguard-go/wgcfg"
|
2020-02-05 22:16:58 +00:00
|
|
|
"golang.org/x/oauth2"
|
|
|
|
"tailscale.com/logtail/backoff"
|
|
|
|
"tailscale.com/tailcfg"
|
2020-02-14 21:09:19 +00:00
|
|
|
"tailscale.com/types/empty"
|
2020-02-15 03:23:16 +00:00
|
|
|
"tailscale.com/types/logger"
|
2020-05-03 20:58:39 +00:00
|
|
|
"tailscale.com/types/structs"
|
2020-02-05 22:16:58 +00:00
|
|
|
)
|
|
|
|
|
2020-05-27 18:46:09 +00:00
|
|
|
// State is the high-level state of the client. It is used only in
|
|
|
|
// unit tests for proper sequencing, don't depend on it anywhere else.
|
|
|
|
// TODO(apenwarr): eliminate 'state', as it's now obsolete.
|
|
|
|
type State int
|
2020-02-05 22:16:58 +00:00
|
|
|
|
|
|
|
const (
|
2020-05-27 18:46:09 +00:00
|
|
|
StateNew = State(iota)
|
|
|
|
StateNotAuthenticated
|
|
|
|
StateAuthenticating
|
|
|
|
StateURLVisitRequired
|
|
|
|
StateAuthenticated
|
|
|
|
StateSynchronized // connected and received map update
|
2020-02-05 22:16:58 +00:00
|
|
|
)
|
|
|
|
|
2020-05-27 18:46:09 +00:00
|
|
|
func (s State) MarshalText() ([]byte, error) {
|
2020-02-05 22:16:58 +00:00
|
|
|
return []byte(s.String()), nil
|
|
|
|
}
|
|
|
|
|
2020-05-27 18:46:09 +00:00
|
|
|
func (s State) String() string {
|
2020-02-05 22:16:58 +00:00
|
|
|
switch s {
|
2020-05-27 18:46:09 +00:00
|
|
|
case StateNew:
|
2020-02-05 22:16:58 +00:00
|
|
|
return "state:new"
|
2020-05-27 18:46:09 +00:00
|
|
|
case StateNotAuthenticated:
|
2020-02-05 22:16:58 +00:00
|
|
|
return "state:not-authenticated"
|
2020-05-27 18:46:09 +00:00
|
|
|
case StateAuthenticating:
|
2020-02-05 22:16:58 +00:00
|
|
|
return "state:authenticating"
|
2020-05-27 18:46:09 +00:00
|
|
|
case StateURLVisitRequired:
|
2020-02-05 22:16:58 +00:00
|
|
|
return "state:url-visit-required"
|
2020-05-27 18:46:09 +00:00
|
|
|
case StateAuthenticated:
|
2020-02-05 22:16:58 +00:00
|
|
|
return "state:authenticated"
|
2020-05-27 18:46:09 +00:00
|
|
|
case StateSynchronized:
|
2020-02-05 22:16:58 +00:00
|
|
|
return "state:synchronized"
|
|
|
|
default:
|
|
|
|
return fmt.Sprintf("state:unknown:%d", int(s))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Status struct {
|
2020-05-03 20:58:39 +00:00
|
|
|
_ structs.Incomparable
|
2020-02-14 21:09:19 +00:00
|
|
|
LoginFinished *empty.Message
|
2020-02-05 22:16:58 +00:00
|
|
|
Err string
|
|
|
|
URL string
|
2020-02-25 18:04:20 +00:00
|
|
|
Persist *Persist // locally persisted configuration
|
|
|
|
NetMap *NetworkMap // server-pushed configuration
|
|
|
|
Hostinfo *tailcfg.Hostinfo // current Hostinfo data
|
2020-05-27 18:46:09 +00:00
|
|
|
State State
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Equal reports whether s and s2 are equal.
|
|
|
|
func (s *Status) Equal(s2 *Status) bool {
|
|
|
|
if s == nil && s2 == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return s != nil && s2 != nil &&
|
|
|
|
(s.LoginFinished == nil) == (s2.LoginFinished == nil) &&
|
|
|
|
s.Err == s2.Err &&
|
|
|
|
s.URL == s2.URL &&
|
|
|
|
reflect.DeepEqual(s.Persist, s2.Persist) &&
|
|
|
|
reflect.DeepEqual(s.NetMap, s2.NetMap) &&
|
|
|
|
reflect.DeepEqual(s.Hostinfo, s2.Hostinfo) &&
|
2020-05-27 18:46:09 +00:00
|
|
|
s.State == s2.State
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s Status) String() string {
|
|
|
|
b, err := json.MarshalIndent(s, "", "\t")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-05-27 18:46:09 +00:00
|
|
|
return s.State.String() + " " + string(b)
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type LoginGoal struct {
|
2020-05-03 20:58:39 +00:00
|
|
|
_ structs.Incomparable
|
2020-02-05 22:16:58 +00:00
|
|
|
wantLoggedIn bool // true if we *want* to be logged in
|
|
|
|
token *oauth2.Token // oauth token to use when logging in
|
|
|
|
flags LoginFlags // flags to use when logging in
|
|
|
|
url string // auth url that needs to be visited
|
|
|
|
}
|
|
|
|
|
|
|
|
// Client connects to a tailcontrol server for a node.
|
|
|
|
type Client struct {
|
|
|
|
direct *Direct // our interface to the server APIs
|
|
|
|
timeNow func() time.Time
|
|
|
|
logf logger.Logf
|
|
|
|
expiry *time.Time
|
|
|
|
closed bool
|
|
|
|
newMapCh chan struct{} // readable when we must restart a map request
|
|
|
|
|
|
|
|
mu sync.Mutex // mutex guards the following fields
|
|
|
|
statusFunc func(Status) // called to update Client status
|
|
|
|
|
|
|
|
loggedIn bool // true if currently logged in
|
|
|
|
loginGoal *LoginGoal // non-nil if some login activity is desired
|
|
|
|
synced bool // true if our netmap is up-to-date
|
2020-02-25 18:04:20 +00:00
|
|
|
hostinfo *tailcfg.Hostinfo
|
2020-02-05 22:16:58 +00:00
|
|
|
inPollNetMap bool // true if currently running a PollNetMap
|
|
|
|
inSendStatus int // number of sendStatus calls currently in progress
|
2020-05-27 18:46:09 +00:00
|
|
|
state State
|
2020-02-05 22:16:58 +00:00
|
|
|
|
|
|
|
authCtx context.Context // context used for auth requests
|
|
|
|
mapCtx context.Context // context used for netmap requests
|
|
|
|
authCancel func() // cancel the auth context
|
|
|
|
mapCancel func() // cancel the netmap context
|
|
|
|
quit chan struct{} // when closed, goroutines should all exit
|
|
|
|
authDone chan struct{} // when closed, auth goroutine is done
|
|
|
|
mapDone chan struct{} // when closed, map goroutine is done
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates and starts a new Client.
|
|
|
|
func New(opts Options) (*Client, error) {
|
|
|
|
c, err := NewNoStart(opts)
|
|
|
|
if c != nil {
|
|
|
|
c.Start()
|
|
|
|
}
|
|
|
|
return c, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewNoStart creates a new Client, but without calling Start on it.
|
|
|
|
func NewNoStart(opts Options) (*Client, error) {
|
|
|
|
direct, err := NewDirect(opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-02-11 20:08:07 +00:00
|
|
|
if opts.Logf == nil {
|
|
|
|
opts.Logf = func(fmt string, args ...interface{}) {}
|
|
|
|
}
|
2020-03-08 12:40:56 +00:00
|
|
|
if opts.TimeNow == nil {
|
|
|
|
opts.TimeNow = time.Now
|
|
|
|
}
|
2020-02-05 22:16:58 +00:00
|
|
|
c := &Client{
|
|
|
|
direct: direct,
|
|
|
|
timeNow: opts.TimeNow,
|
|
|
|
logf: opts.Logf,
|
|
|
|
newMapCh: make(chan struct{}, 1),
|
|
|
|
quit: make(chan struct{}),
|
|
|
|
authDone: make(chan struct{}),
|
|
|
|
mapDone: make(chan struct{}),
|
|
|
|
}
|
|
|
|
c.authCtx, c.authCancel = context.WithCancel(context.Background())
|
|
|
|
c.mapCtx, c.mapCancel = context.WithCancel(context.Background())
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start starts the client's goroutines.
|
|
|
|
//
|
|
|
|
// It should only be called for clients created by NewNoStart.
|
|
|
|
func (c *Client) Start() {
|
|
|
|
go c.authRoutine()
|
|
|
|
go c.mapRoutine()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) cancelAuth() {
|
|
|
|
c.mu.Lock()
|
|
|
|
if c.authCancel != nil {
|
|
|
|
c.authCancel()
|
|
|
|
}
|
|
|
|
if !c.closed {
|
|
|
|
c.authCtx, c.authCancel = context.WithCancel(context.Background())
|
|
|
|
}
|
|
|
|
c.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) cancelMapLocked() {
|
|
|
|
if c.mapCancel != nil {
|
|
|
|
c.mapCancel()
|
|
|
|
}
|
|
|
|
if !c.closed {
|
|
|
|
c.mapCtx, c.mapCancel = context.WithCancel(context.Background())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) cancelMapUnsafely() {
|
|
|
|
c.mu.Lock()
|
|
|
|
c.cancelMapLocked()
|
|
|
|
c.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) cancelMapSafely() {
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
|
2020-04-11 15:35:34 +00:00
|
|
|
c.logf("cancelMapSafely: synced=%v", c.synced)
|
2020-02-05 22:16:58 +00:00
|
|
|
|
2020-02-11 07:14:38 +00:00
|
|
|
if c.inPollNetMap {
|
2020-02-05 22:16:58 +00:00
|
|
|
// received at least one netmap since the last
|
|
|
|
// interruption. That means the server has already
|
|
|
|
// fully processed our last request, which might
|
|
|
|
// include UpdateEndpoints(). Interrupt it and try
|
|
|
|
// again.
|
|
|
|
c.cancelMapLocked()
|
|
|
|
} else {
|
|
|
|
// !synced means we either haven't done a netmap
|
|
|
|
// request yet, or it hasn't answered yet. So the
|
|
|
|
// server is in an undefined state. If we send
|
|
|
|
// another netmap request too soon, it might race
|
|
|
|
// with the last one, and if we're very unlucky,
|
|
|
|
// the new request will be applied before the old one,
|
|
|
|
// and the wrong endpoints will get registered. We
|
|
|
|
// have to tell the client to abort politely, only
|
|
|
|
// after it receives a response to its existing netmap
|
|
|
|
// request.
|
|
|
|
select {
|
|
|
|
case c.newMapCh <- struct{}{}:
|
2020-04-11 15:35:34 +00:00
|
|
|
c.logf("cancelMapSafely: wrote to channel")
|
2020-02-05 22:16:58 +00:00
|
|
|
default:
|
|
|
|
// if channel write failed, then there was already
|
|
|
|
// an outstanding newMapCh request. One is enough,
|
|
|
|
// since it'll always use the latest endpoints.
|
2020-04-11 15:35:34 +00:00
|
|
|
c.logf("cancelMapSafely: channel was full")
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) authRoutine() {
|
|
|
|
defer close(c.authDone)
|
Add tstest.PanicOnLog(), and fix various problems detected by this.
If a test calls log.Printf, 'go test' horrifyingly rearranges the
output to no longer be in chronological order, which makes debugging
virtually impossible. Let's stop that from happening by making
log.Printf panic if called from any module, no matter how deep, during
tests.
This required us to change the default error handler in at least one
http.Server, as well as plumbing a bunch of logf functions around,
especially in magicsock and wgengine, but also in logtail and backoff.
To add insult to injury, 'go test' also rearranges the output when a
parent test has multiple sub-tests (all the sub-test's t.Logf is always
printed after all the parent tests t.Logf), so we need to screw around
with a special Logf that can point at the "current" t (current_t.Logf)
in some places. Probably our entire way of using subtests is wrong,
since 'go test' would probably like to run them all in parallel if you
called t.Parallel(), but it definitely can't because the're all
manipulating the shared state created by the parent test. They should
probably all be separate toplevel tests instead, with common
setup/teardown logic. But that's a job for another time.
Signed-off-by: Avery Pennarun <apenwarr@tailscale.com>
2020-05-14 02:59:54 +00:00
|
|
|
bo := backoff.NewBackoff("authRoutine", c.logf)
|
2020-02-05 22:16:58 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
c.mu.Lock()
|
2020-04-11 15:35:34 +00:00
|
|
|
c.logf("authRoutine: %s", c.state)
|
2020-02-05 22:16:58 +00:00
|
|
|
expiry := c.expiry
|
|
|
|
goal := c.loginGoal
|
|
|
|
ctx := c.authCtx
|
|
|
|
synced := c.synced
|
|
|
|
c.mu.Unlock()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-c.quit:
|
2020-04-11 15:35:34 +00:00
|
|
|
c.logf("authRoutine: quit")
|
2020-02-05 22:16:58 +00:00
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
report := func(err error, msg string) {
|
2020-04-11 15:35:34 +00:00
|
|
|
c.logf("%s: %v", msg, err)
|
2020-02-05 22:16:58 +00:00
|
|
|
err = fmt.Errorf("%s: %v", msg, err)
|
|
|
|
// don't send status updates for context errors,
|
|
|
|
// since context cancelation is always on purpose.
|
|
|
|
if ctx.Err() == nil {
|
|
|
|
c.sendStatus("authRoutine1", err, "", nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if goal == nil {
|
|
|
|
// Wait for something interesting to happen
|
|
|
|
var exp <-chan time.Time
|
|
|
|
if expiry != nil && !expiry.IsZero() {
|
|
|
|
// if expiry is in the future, don't delay
|
|
|
|
// past that time.
|
|
|
|
// If it's in the past, then it's already
|
|
|
|
// being handled by someone, so no need to
|
|
|
|
// wake ourselves up again.
|
|
|
|
now := c.timeNow()
|
|
|
|
if expiry.Before(now) {
|
|
|
|
delay := expiry.Sub(now)
|
|
|
|
if delay > 5*time.Second {
|
|
|
|
delay = time.Second
|
|
|
|
}
|
|
|
|
exp = time.After(delay)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2020-04-11 15:35:34 +00:00
|
|
|
c.logf("authRoutine: context done.")
|
2020-02-05 22:16:58 +00:00
|
|
|
case <-exp:
|
|
|
|
// Unfortunately the key expiry isn't provided
|
|
|
|
// by the control server until mapRequest.
|
|
|
|
// So we have to do some hackery with c.expiry
|
|
|
|
// in here.
|
|
|
|
// TODO(apenwarr): add a key expiry field in RegisterResponse.
|
2020-04-11 15:35:34 +00:00
|
|
|
c.logf("authRoutine: key expiration check.")
|
2020-02-05 22:16:58 +00:00
|
|
|
if synced && expiry != nil && !expiry.IsZero() && expiry.Before(c.timeNow()) {
|
|
|
|
c.logf("Key expired; setting loggedIn=false.")
|
|
|
|
|
|
|
|
c.mu.Lock()
|
|
|
|
c.loginGoal = &LoginGoal{
|
|
|
|
wantLoggedIn: c.loggedIn,
|
|
|
|
}
|
|
|
|
c.loggedIn = false
|
|
|
|
c.expiry = nil
|
|
|
|
c.mu.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if !goal.wantLoggedIn {
|
|
|
|
err := c.direct.TryLogout(c.authCtx)
|
|
|
|
if err != nil {
|
|
|
|
report(err, "TryLogout")
|
|
|
|
bo.BackOff(ctx, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// success
|
|
|
|
c.mu.Lock()
|
|
|
|
c.loggedIn = false
|
|
|
|
c.loginGoal = nil
|
2020-05-27 18:46:09 +00:00
|
|
|
c.state = StateNotAuthenticated
|
2020-02-05 22:16:58 +00:00
|
|
|
c.synced = false
|
|
|
|
c.mu.Unlock()
|
|
|
|
|
|
|
|
c.sendStatus("authRoutine2", nil, "", nil)
|
|
|
|
bo.BackOff(ctx, nil)
|
|
|
|
} else { // ie. goal.wantLoggedIn
|
|
|
|
c.mu.Lock()
|
|
|
|
if goal.url != "" {
|
2020-05-27 18:46:09 +00:00
|
|
|
c.state = StateURLVisitRequired
|
2020-02-05 22:16:58 +00:00
|
|
|
} else {
|
2020-05-27 18:46:09 +00:00
|
|
|
c.state = StateAuthenticating
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
c.mu.Unlock()
|
|
|
|
|
|
|
|
var url string
|
|
|
|
var err error
|
|
|
|
var f string
|
|
|
|
if goal.url != "" {
|
|
|
|
url, err = c.direct.WaitLoginURL(ctx, goal.url)
|
|
|
|
f = "WaitLoginURL"
|
|
|
|
} else {
|
|
|
|
url, err = c.direct.TryLogin(ctx, goal.token, goal.flags)
|
|
|
|
f = "TryLogin"
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
report(err, f)
|
|
|
|
bo.BackOff(ctx, err)
|
|
|
|
continue
|
|
|
|
} else if url != "" {
|
|
|
|
if goal.url != "" {
|
|
|
|
err = fmt.Errorf("weird: server required a new url?")
|
|
|
|
report(err, "WaitLoginURL")
|
|
|
|
}
|
|
|
|
|
|
|
|
c.mu.Lock()
|
2020-07-15 17:00:20 +00:00
|
|
|
c.loginGoal = &LoginGoal{
|
|
|
|
wantLoggedIn: true,
|
|
|
|
flags: LoginDefault,
|
|
|
|
url: url,
|
|
|
|
}
|
2020-05-27 18:46:09 +00:00
|
|
|
c.state = StateURLVisitRequired
|
2020-02-05 22:16:58 +00:00
|
|
|
c.synced = false
|
|
|
|
c.mu.Unlock()
|
|
|
|
|
|
|
|
c.sendStatus("authRoutine3", err, url, nil)
|
|
|
|
bo.BackOff(ctx, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// success
|
|
|
|
c.mu.Lock()
|
|
|
|
c.loggedIn = true
|
|
|
|
c.loginGoal = nil
|
2020-05-27 18:46:09 +00:00
|
|
|
c.state = StateAuthenticated
|
2020-02-05 22:16:58 +00:00
|
|
|
c.mu.Unlock()
|
|
|
|
|
|
|
|
c.sendStatus("authRoutine4", nil, "", nil)
|
|
|
|
c.cancelMapSafely()
|
|
|
|
bo.BackOff(ctx, nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-27 18:46:09 +00:00
|
|
|
// Expiry returns the credential expiration time, or the zero time if
|
|
|
|
// the expiration time isn't known. Used in tests only.
|
|
|
|
func (c *Client) Expiry() *time.Time {
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
return c.expiry
|
|
|
|
}
|
|
|
|
|
|
|
|
// Direct returns the underlying direct client object. Used in tests
|
|
|
|
// only.
|
|
|
|
func (c *Client) Direct() *Direct {
|
|
|
|
return c.direct
|
|
|
|
}
|
|
|
|
|
2020-02-05 22:16:58 +00:00
|
|
|
func (c *Client) mapRoutine() {
|
|
|
|
defer close(c.mapDone)
|
Add tstest.PanicOnLog(), and fix various problems detected by this.
If a test calls log.Printf, 'go test' horrifyingly rearranges the
output to no longer be in chronological order, which makes debugging
virtually impossible. Let's stop that from happening by making
log.Printf panic if called from any module, no matter how deep, during
tests.
This required us to change the default error handler in at least one
http.Server, as well as plumbing a bunch of logf functions around,
especially in magicsock and wgengine, but also in logtail and backoff.
To add insult to injury, 'go test' also rearranges the output when a
parent test has multiple sub-tests (all the sub-test's t.Logf is always
printed after all the parent tests t.Logf), so we need to screw around
with a special Logf that can point at the "current" t (current_t.Logf)
in some places. Probably our entire way of using subtests is wrong,
since 'go test' would probably like to run them all in parallel if you
called t.Parallel(), but it definitely can't because the're all
manipulating the shared state created by the parent test. They should
probably all be separate toplevel tests instead, with common
setup/teardown logic. But that's a job for another time.
Signed-off-by: Avery Pennarun <apenwarr@tailscale.com>
2020-05-14 02:59:54 +00:00
|
|
|
bo := backoff.NewBackoff("mapRoutine", c.logf)
|
2020-02-05 22:16:58 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
c.mu.Lock()
|
2020-04-11 15:35:34 +00:00
|
|
|
c.logf("mapRoutine: %s", c.state)
|
2020-02-05 22:16:58 +00:00
|
|
|
loggedIn := c.loggedIn
|
|
|
|
ctx := c.mapCtx
|
|
|
|
c.mu.Unlock()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-c.quit:
|
2020-04-11 15:35:34 +00:00
|
|
|
c.logf("mapRoutine: quit")
|
2020-02-05 22:16:58 +00:00
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
report := func(err error, msg string) {
|
2020-04-11 15:35:34 +00:00
|
|
|
c.logf("%s: %v", msg, err)
|
2020-02-05 22:16:58 +00:00
|
|
|
err = fmt.Errorf("%s: %v", msg, err)
|
|
|
|
// don't send status updates for context errors,
|
|
|
|
// since context cancelation is always on purpose.
|
|
|
|
if ctx.Err() == nil {
|
|
|
|
c.sendStatus("mapRoutine1", err, "", nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !loggedIn {
|
|
|
|
// Wait for something interesting to happen
|
|
|
|
c.mu.Lock()
|
|
|
|
c.synced = false
|
|
|
|
// c.state is set by authRoutine()
|
|
|
|
c.mu.Unlock()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2020-04-11 15:35:34 +00:00
|
|
|
c.logf("mapRoutine: context done.")
|
2020-02-05 22:16:58 +00:00
|
|
|
case <-c.newMapCh:
|
2020-04-11 15:35:34 +00:00
|
|
|
c.logf("mapRoutine: new map needed while idle.")
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Be sure this is false when we're not inside
|
|
|
|
// PollNetMap, so that cancelMapSafely() can notify
|
|
|
|
// us correctly.
|
|
|
|
c.mu.Lock()
|
|
|
|
c.inPollNetMap = false
|
|
|
|
c.mu.Unlock()
|
|
|
|
|
|
|
|
err := c.direct.PollNetMap(ctx, -1, func(nm *NetworkMap) {
|
|
|
|
c.mu.Lock()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-c.newMapCh:
|
2020-04-11 15:35:34 +00:00
|
|
|
c.logf("mapRoutine: new map request during PollNetMap. canceling.")
|
2020-02-05 22:16:58 +00:00
|
|
|
c.cancelMapLocked()
|
|
|
|
|
|
|
|
// Don't emit this netmap; we're
|
|
|
|
// about to request a fresh one.
|
|
|
|
c.mu.Unlock()
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
c.synced = true
|
|
|
|
c.inPollNetMap = true
|
|
|
|
if c.loggedIn {
|
2020-05-27 18:46:09 +00:00
|
|
|
c.state = StateSynchronized
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
exp := nm.Expiry
|
|
|
|
c.expiry = &exp
|
|
|
|
stillAuthed := c.loggedIn
|
|
|
|
state := c.state
|
|
|
|
|
|
|
|
c.mu.Unlock()
|
|
|
|
|
2020-04-11 15:35:34 +00:00
|
|
|
c.logf("mapRoutine: netmap received: %s", state)
|
2020-02-05 22:16:58 +00:00
|
|
|
if stillAuthed {
|
|
|
|
c.sendStatus("mapRoutine2", nil, "", nm)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
c.mu.Lock()
|
|
|
|
c.synced = false
|
|
|
|
c.inPollNetMap = false
|
2020-05-27 18:46:09 +00:00
|
|
|
if c.state == StateSynchronized {
|
|
|
|
c.state = StateAuthenticated
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
c.mu.Unlock()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
report(err, "PollNetMap")
|
|
|
|
bo.BackOff(ctx, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
bo.BackOff(ctx, nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) AuthCantContinue() bool {
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
|
|
|
|
return !c.loggedIn && (c.loginGoal == nil || c.loginGoal.url != "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) SetStatusFunc(fn func(Status)) {
|
|
|
|
c.mu.Lock()
|
|
|
|
c.statusFunc = fn
|
|
|
|
c.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2020-02-25 18:04:20 +00:00
|
|
|
func (c *Client) SetHostinfo(hi *tailcfg.Hostinfo) {
|
|
|
|
if hi == nil {
|
|
|
|
panic("nil Hostinfo")
|
|
|
|
}
|
2020-04-02 00:18:39 +00:00
|
|
|
if !c.direct.SetHostinfo(hi) {
|
|
|
|
c.logf("[unexpected] duplicate Hostinfo: %v", hi)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.logf("Hostinfo: %v", hi)
|
|
|
|
|
2020-02-05 22:16:58 +00:00
|
|
|
// Send new Hostinfo to server
|
|
|
|
c.cancelMapSafely()
|
|
|
|
}
|
|
|
|
|
2020-03-04 06:21:56 +00:00
|
|
|
func (c *Client) SetNetInfo(ni *tailcfg.NetInfo) {
|
|
|
|
if ni == nil {
|
|
|
|
panic("nil NetInfo")
|
|
|
|
}
|
2020-04-02 00:18:39 +00:00
|
|
|
if !c.direct.SetNetInfo(ni) {
|
|
|
|
c.logf("[unexpected] duplicate NetInfo: %v", ni)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.logf("NetInfo: %v", ni)
|
|
|
|
|
2020-03-04 06:21:56 +00:00
|
|
|
// Send new Hostinfo (which includes NetInfo) to server
|
|
|
|
c.cancelMapSafely()
|
|
|
|
}
|
|
|
|
|
2020-02-05 22:16:58 +00:00
|
|
|
func (c *Client) sendStatus(who string, err error, url string, nm *NetworkMap) {
|
|
|
|
c.mu.Lock()
|
|
|
|
state := c.state
|
|
|
|
loggedIn := c.loggedIn
|
|
|
|
synced := c.synced
|
|
|
|
statusFunc := c.statusFunc
|
|
|
|
hi := c.hostinfo
|
|
|
|
c.inSendStatus++
|
|
|
|
c.mu.Unlock()
|
|
|
|
|
2020-04-11 15:35:34 +00:00
|
|
|
c.logf("sendStatus: %s: %v", who, state)
|
2020-02-05 22:16:58 +00:00
|
|
|
|
|
|
|
var p *Persist
|
2020-02-14 21:09:19 +00:00
|
|
|
var fin *empty.Message
|
2020-05-27 18:46:09 +00:00
|
|
|
if state == StateAuthenticated {
|
2020-02-14 21:09:19 +00:00
|
|
|
fin = new(empty.Message)
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
if nm != nil && loggedIn && synced {
|
|
|
|
pp := c.direct.GetPersist()
|
|
|
|
p = &pp
|
|
|
|
} else {
|
|
|
|
// don't send netmap status, as it's misleading when we're
|
|
|
|
// not logged in.
|
|
|
|
nm = nil
|
|
|
|
}
|
|
|
|
new := Status{
|
|
|
|
LoginFinished: fin,
|
|
|
|
URL: url,
|
|
|
|
Persist: p,
|
|
|
|
NetMap: nm,
|
|
|
|
Hostinfo: hi,
|
2020-05-27 18:46:09 +00:00
|
|
|
State: state,
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
new.Err = err.Error()
|
|
|
|
}
|
|
|
|
if statusFunc != nil {
|
|
|
|
statusFunc(new)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.mu.Lock()
|
|
|
|
c.inSendStatus--
|
|
|
|
c.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) Login(t *oauth2.Token, flags LoginFlags) {
|
2020-04-11 15:35:34 +00:00
|
|
|
c.logf("client.Login(%v, %v)", t != nil, flags)
|
2020-02-05 22:16:58 +00:00
|
|
|
|
|
|
|
c.mu.Lock()
|
|
|
|
c.loginGoal = &LoginGoal{
|
|
|
|
wantLoggedIn: true,
|
|
|
|
token: t,
|
|
|
|
flags: flags,
|
|
|
|
}
|
|
|
|
c.mu.Unlock()
|
|
|
|
|
|
|
|
c.cancelAuth()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) Logout() {
|
2020-04-11 15:35:34 +00:00
|
|
|
c.logf("client.Logout()")
|
2020-02-05 22:16:58 +00:00
|
|
|
|
|
|
|
c.mu.Lock()
|
|
|
|
c.loginGoal = &LoginGoal{
|
|
|
|
wantLoggedIn: false,
|
|
|
|
}
|
|
|
|
c.mu.Unlock()
|
|
|
|
|
|
|
|
c.cancelAuth()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) UpdateEndpoints(localPort uint16, endpoints []string) {
|
2020-02-14 17:28:29 +00:00
|
|
|
changed := c.direct.SetEndpoints(localPort, endpoints)
|
|
|
|
if changed {
|
2020-02-05 22:16:58 +00:00
|
|
|
c.cancelMapSafely()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) Shutdown() {
|
2020-04-11 15:35:34 +00:00
|
|
|
c.logf("client.Shutdown()")
|
2020-02-05 22:16:58 +00:00
|
|
|
|
|
|
|
c.mu.Lock()
|
|
|
|
inSendStatus := c.inSendStatus
|
|
|
|
closed := c.closed
|
|
|
|
if !closed {
|
|
|
|
c.closed = true
|
|
|
|
c.statusFunc = nil
|
|
|
|
}
|
|
|
|
c.mu.Unlock()
|
|
|
|
|
2020-04-11 15:35:34 +00:00
|
|
|
c.logf("client.Shutdown: inSendStatus=%v", inSendStatus)
|
2020-02-05 22:16:58 +00:00
|
|
|
if !closed {
|
|
|
|
close(c.quit)
|
|
|
|
c.cancelAuth()
|
|
|
|
<-c.authDone
|
|
|
|
c.cancelMapUnsafely()
|
|
|
|
<-c.mapDone
|
2020-04-11 15:35:34 +00:00
|
|
|
c.logf("Client.Shutdown done.")
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-27 18:46:09 +00:00
|
|
|
|
|
|
|
// NodePublicKey returns the node public key currently in use. This is
|
|
|
|
// used exclusively in tests.
|
|
|
|
func (c *Client) TestOnlyNodePublicKey() wgcfg.Key {
|
|
|
|
priv := c.direct.GetPersist()
|
|
|
|
return priv.PrivateNodeKey.Public()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) TestOnlySetAuthKey(authkey string) {
|
|
|
|
c.direct.mu.Lock()
|
|
|
|
defer c.direct.mu.Unlock()
|
|
|
|
c.direct.authKey = authkey
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) TestOnlyTimeNow() time.Time {
|
|
|
|
return c.timeNow()
|
|
|
|
}
|