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.
|
|
|
|
|
|
|
|
package ipn
|
|
|
|
|
|
|
|
import (
|
2020-02-03 18:57:34 +00:00
|
|
|
"time"
|
|
|
|
|
2020-02-05 22:16:58 +00:00
|
|
|
"tailscale.com/control/controlclient"
|
|
|
|
"tailscale.com/tailcfg"
|
2020-02-14 21:09:19 +00:00
|
|
|
"tailscale.com/types/empty"
|
2020-02-05 22:16:58 +00:00
|
|
|
"tailscale.com/wgengine"
|
|
|
|
)
|
|
|
|
|
|
|
|
type State int
|
|
|
|
|
|
|
|
const (
|
|
|
|
NoState = State(iota)
|
|
|
|
NeedsLogin
|
|
|
|
NeedsMachineAuth
|
|
|
|
Stopped
|
|
|
|
Starting
|
|
|
|
Running
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s State) String() string {
|
|
|
|
return [...]string{"NoState", "NeedsLogin", "NeedsMachineAuth",
|
|
|
|
"Stopped", "Starting", "Running"}[s]
|
|
|
|
}
|
|
|
|
|
2020-02-25 20:30:28 +00:00
|
|
|
// EngineStatus contains WireGuard engine stats.
|
2020-02-05 22:16:58 +00:00
|
|
|
type EngineStatus struct {
|
|
|
|
RBytes, WBytes wgengine.ByteCount
|
|
|
|
NumLive int
|
2020-03-19 06:55:14 +00:00
|
|
|
LiveDERPs int // number of active DERP connections
|
2020-02-05 22:16:58 +00:00
|
|
|
LivePeers map[tailcfg.NodeKey]wgengine.PeerStatus
|
|
|
|
}
|
|
|
|
|
|
|
|
type NetworkMap = controlclient.NetworkMap
|
|
|
|
|
2020-02-25 18:04:20 +00:00
|
|
|
// Notify is a communication from a backend (e.g. tailscaled) to a frontend
|
|
|
|
// (cmd/tailscale, iOS, macOS, Win Tasktray).
|
2020-02-05 22:16:58 +00:00
|
|
|
// In any given notification, any or all of these may be nil, meaning
|
|
|
|
// that they have not changed.
|
2020-02-25 18:04:20 +00:00
|
|
|
// They are JSON-encoded on the wire, despite the lack of struct tags.
|
2020-02-05 22:16:58 +00:00
|
|
|
type Notify struct {
|
2020-02-14 21:09:19 +00:00
|
|
|
Version string // version number of IPN backend
|
|
|
|
ErrMessage *string // critical error message, if any
|
|
|
|
LoginFinished *empty.Message // event: non-nil when login process succeeded
|
|
|
|
State *State // current IPN state has changed
|
|
|
|
Prefs *Prefs // preferences were changed
|
|
|
|
NetMap *NetworkMap // new netmap received
|
|
|
|
Engine *EngineStatus // wireguard engine stats
|
|
|
|
BrowseToURL *string // UI should open a browser right now
|
|
|
|
BackendLogID *string // public logtail id used by backend
|
2020-02-25 18:04:20 +00:00
|
|
|
|
|
|
|
// type is mirrored in xcode/Shared/IPN.swift
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
|
2020-02-03 18:35:52 +00:00
|
|
|
// StateKey is an opaque identifier for a set of LocalBackend state
|
|
|
|
// (preferences, private keys, etc.).
|
|
|
|
//
|
|
|
|
// The reason we need this is that the Tailscale agent may be running
|
|
|
|
// on a multi-user machine, in a context where a single daemon is
|
|
|
|
// shared by several consecutive users. Ideally we would just use the
|
|
|
|
// username of the connected frontend as the StateKey.
|
|
|
|
//
|
|
|
|
// However, on Windows, there seems to be no safe way to figure out
|
|
|
|
// the owning user of a process connected over IPC mechanisms
|
|
|
|
// (sockets, named pipes). So instead, on Windows, we use a
|
|
|
|
// capability-oriented system where the frontend generates a random
|
|
|
|
// identifier for itself, and uses that as the StateKey when talking
|
|
|
|
// to the backend. That way, while we can't identify an OS user by
|
|
|
|
// name, we can tell two different users apart, because they'll have
|
|
|
|
// different opaque state keys (and no access to each others's keys).
|
|
|
|
type StateKey string
|
|
|
|
|
2020-02-05 22:16:58 +00:00
|
|
|
type Options struct {
|
2020-02-14 00:07:50 +00:00
|
|
|
// FrontendLogID is the public logtail id used by the frontend.
|
2020-02-03 18:35:52 +00:00
|
|
|
FrontendLogID string
|
|
|
|
// StateKey and Prefs together define the state the backend should
|
|
|
|
// use:
|
|
|
|
// - StateKey=="" && Prefs!=nil: use Prefs for internal state,
|
|
|
|
// don't persist changes in the backend.
|
|
|
|
// - StateKey!="" && Prefs==nil: load the given backend-side
|
|
|
|
// state and use/update that.
|
|
|
|
// - StateKey!="" && Prefs!=nil: like the previous case, but do
|
|
|
|
// an initial overwrite of backend state with Prefs.
|
|
|
|
StateKey StateKey
|
|
|
|
Prefs *Prefs
|
2020-02-20 07:23:34 +00:00
|
|
|
// LegacyConfigPath optionally specifies the old-style relaynode
|
|
|
|
// relay.conf location. If both LegacyConfigPath and StateKey are
|
|
|
|
// specified and the requested state doesn't exist in the backend
|
|
|
|
// store, the backend migrates the config from LegacyConfigPath.
|
|
|
|
//
|
|
|
|
// TODO(danderson): remove some time after the transition to
|
|
|
|
// tailscaled is done.
|
|
|
|
LegacyConfigPath string
|
2020-02-14 00:07:50 +00:00
|
|
|
// Notify is called when backend events happen.
|
2020-02-03 18:35:52 +00:00
|
|
|
Notify func(Notify) `json:"-"`
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
|
2020-02-25 20:30:28 +00:00
|
|
|
// Backend is the interface between Tailscale frontends
|
|
|
|
// (e.g. cmd/tailscale, iOS/MacOS/Windows GUIs) and the tailscale
|
|
|
|
// backend (e.g. cmd/tailscaled) running on the same machine.
|
|
|
|
// (It has nothing to do with the interface between the backends
|
|
|
|
// and the cloud control plane.)
|
2020-02-05 22:16:58 +00:00
|
|
|
type Backend interface {
|
2020-02-25 20:30:28 +00:00
|
|
|
// Start starts or restarts the backend, typically when a
|
|
|
|
// frontend client connects.
|
|
|
|
Start(Options) error
|
2020-02-14 00:07:50 +00:00
|
|
|
// StartLoginInteractive requests to start a new interactive login
|
|
|
|
// flow. This should trigger a new BrowseToURL notification
|
|
|
|
// eventually.
|
2020-02-05 22:16:58 +00:00
|
|
|
StartLoginInteractive()
|
2020-02-25 20:30:28 +00:00
|
|
|
// Logout terminates the current login session and stops the
|
2020-02-14 00:07:50 +00:00
|
|
|
// wireguard engine.
|
2020-02-05 22:16:58 +00:00
|
|
|
Logout()
|
2020-02-25 20:30:28 +00:00
|
|
|
// SetPrefs installs a new set of user preferences, including
|
|
|
|
// WantRunning. This may cause the wireguard engine to
|
2020-02-14 00:07:50 +00:00
|
|
|
// reconfigure or stop.
|
2020-03-03 16:05:11 +00:00
|
|
|
SetPrefs(*Prefs)
|
2020-02-14 00:07:50 +00:00
|
|
|
// RequestEngineStatus polls for an update from the wireguard
|
|
|
|
// engine. Only needed if you want to display byte
|
|
|
|
// counts. Connection events are emitted automatically without
|
|
|
|
// polling.
|
2020-02-05 22:16:58 +00:00
|
|
|
RequestEngineStatus()
|
2020-02-14 00:07:50 +00:00
|
|
|
// FakeExpireAfter pretends that the current key is going to
|
|
|
|
// expire after duration x. This is useful for testing GUIs to
|
|
|
|
// make sure they react properly with keys that are going to
|
|
|
|
// expire.
|
2020-02-05 22:16:58 +00:00
|
|
|
FakeExpireAfter(x time.Duration)
|
|
|
|
}
|