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 (
|
|
|
|
"time"
|
2020-03-27 20:26:35 +00:00
|
|
|
|
|
|
|
"tailscale.com/ipn/ipnstate"
|
2021-03-19 17:21:33 +00:00
|
|
|
"tailscale.com/tailcfg"
|
2021-02-05 23:44:46 +00:00
|
|
|
"tailscale.com/types/netmap"
|
2020-02-05 22:16:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type FakeBackend struct {
|
|
|
|
serverURL string
|
|
|
|
notify func(n Notify)
|
|
|
|
live bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *FakeBackend) Start(opts Options) error {
|
ipn{,/ipnlocal}, cmd/tailscale/cli: don't check pref reverts on initial up
The ipn.NewPrefs func returns a populated ipn.Prefs for historical
reasons. It's not used or as important as it once was, but it hasn't
yet been removed. Meanwhile, it contains some default values that are
used on some platforms. Notably, for this bug (#1725), Windows/Mac use
its Prefs.RouteAll true value (to accept subnets), but Linux users
have always gotten a "false" value for that, because that's what
cmd/tailscale's CLI default flag is _for all operating systems_. That
meant that "tailscale up" was rightfully reporting that the user was
changing an implicit setting: RouteAll was changing from true with
false with the user explicitly saying so.
An obvious fix might be to change ipn.NewPrefs to return
Prefs.RouteAll == false on some platforms, but the logic is
complicated by darwin: we want RouteAll true on windows, android, ios,
and the GUI mac app, but not the CLI tailscaled-on-macOS mode. But
even if we used build tags (e.g. the "redo" build tag) to determine
what the default is, that then means we have duplicated and differing
"defaults" between both the CLI up flags and ipn.NewPrefs. Furthering
that complication didn't seem like a good idea.
So, changing the NewPrefs defaults is too invasive at this stage of
the release, as is removing the NewPrefs func entirely.
Instead, tweak slightly the semantics of the ipn.Prefs.ControlURL
field. This now defines that a ControlURL of the empty string means
both "we're uninitialized" and also "just use the default".
Then, once we have the "empty-string-means-unintialized" semantics,
use that to suppress "tailscale up"'s recent implicit-setting-revert
checking safety net, if we've never initialized Tailscale yet.
And update/add tests.
Fixes #1725
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2021-04-18 06:50:58 +01:00
|
|
|
b.serverURL = opts.Prefs.ControlURLOrDefault()
|
2021-04-07 06:11:50 +01:00
|
|
|
if b.notify == nil {
|
|
|
|
panic("FakeBackend.Start: SetNotifyCallback not called")
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
nl := NeedsLogin
|
2021-04-07 06:11:50 +01:00
|
|
|
if b.notify != nil {
|
|
|
|
b.notify(Notify{Prefs: opts.Prefs})
|
|
|
|
b.notify(Notify{State: &nl})
|
|
|
|
}
|
2020-02-05 22:16:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-07 06:11:50 +01:00
|
|
|
func (b *FakeBackend) SetNotifyCallback(notify func(Notify)) {
|
|
|
|
if notify == nil {
|
|
|
|
panic("FakeBackend.SetNotifyCallback: notify is nil")
|
|
|
|
}
|
|
|
|
b.notify = notify
|
|
|
|
}
|
|
|
|
|
2020-02-05 22:16:58 +00:00
|
|
|
func (b *FakeBackend) newState(s State) {
|
2021-04-07 06:11:50 +01:00
|
|
|
if b.notify != nil {
|
|
|
|
b.notify(Notify{State: &s})
|
|
|
|
}
|
2020-02-05 22:16:58 +00:00
|
|
|
if s == Running {
|
|
|
|
b.live = true
|
|
|
|
} else {
|
|
|
|
b.live = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *FakeBackend) StartLoginInteractive() {
|
|
|
|
u := b.serverURL + "/this/is/fake"
|
2021-04-07 06:11:50 +01:00
|
|
|
if b.notify != nil {
|
|
|
|
b.notify(Notify{BrowseToURL: &u})
|
|
|
|
}
|
2020-07-13 21:13:11 +01:00
|
|
|
b.login()
|
|
|
|
}
|
|
|
|
|
2021-03-19 17:21:33 +00:00
|
|
|
func (b *FakeBackend) Login(token *tailcfg.Oauth2Token) {
|
2020-07-13 21:13:11 +01:00
|
|
|
b.login()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *FakeBackend) login() {
|
2020-02-05 22:16:58 +00:00
|
|
|
b.newState(NeedsMachineAuth)
|
|
|
|
b.newState(Stopped)
|
|
|
|
// TODO(apenwarr): Fill in a more interesting netmap here.
|
2021-04-07 06:11:50 +01:00
|
|
|
if b.notify != nil {
|
|
|
|
b.notify(Notify{NetMap: &netmap.NetworkMap{}})
|
|
|
|
}
|
2020-02-05 22:16:58 +00:00
|
|
|
b.newState(Starting)
|
|
|
|
// TODO(apenwarr): Fill in a more interesting status.
|
2021-04-07 06:11:50 +01:00
|
|
|
if b.notify != nil {
|
|
|
|
b.notify(Notify{Engine: &EngineStatus{}})
|
|
|
|
}
|
2020-02-05 22:16:58 +00:00
|
|
|
b.newState(Running)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *FakeBackend) Logout() {
|
|
|
|
b.newState(NeedsLogin)
|
|
|
|
}
|
|
|
|
|
2020-02-20 19:07:00 +00:00
|
|
|
func (b *FakeBackend) SetPrefs(new *Prefs) {
|
|
|
|
if new == nil {
|
|
|
|
panic("FakeBackend.SetPrefs got nil prefs")
|
|
|
|
}
|
|
|
|
|
2021-04-07 06:11:50 +01:00
|
|
|
if b.notify != nil {
|
|
|
|
b.notify(Notify{Prefs: new.Clone()})
|
|
|
|
}
|
2020-02-05 22:16:58 +00:00
|
|
|
if new.WantRunning && !b.live {
|
|
|
|
b.newState(Starting)
|
|
|
|
b.newState(Running)
|
|
|
|
} else if !new.WantRunning && b.live {
|
|
|
|
b.newState(Stopped)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *FakeBackend) RequestEngineStatus() {
|
2021-04-07 06:11:50 +01:00
|
|
|
if b.notify != nil {
|
|
|
|
b.notify(Notify{Engine: &EngineStatus{}})
|
|
|
|
}
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *FakeBackend) FakeExpireAfter(x time.Duration) {
|
2021-04-07 06:11:50 +01:00
|
|
|
if b.notify != nil {
|
|
|
|
b.notify(Notify{NetMap: &netmap.NetworkMap{}})
|
|
|
|
}
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
2020-08-09 22:49:42 +01:00
|
|
|
|
2021-03-23 22:16:15 +00:00
|
|
|
func (b *FakeBackend) Ping(ip string, useTSMP bool) {
|
2021-04-07 06:11:50 +01:00
|
|
|
if b.notify != nil {
|
|
|
|
b.notify(Notify{PingResult: &ipnstate.PingResult{}})
|
|
|
|
}
|
2020-08-09 22:49:42 +01:00
|
|
|
}
|