2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2020-02-05 22:16:58 +00:00
|
|
|
|
|
|
|
package ipn
|
|
|
|
|
|
|
|
import (
|
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 {
|
2022-11-09 05:58:10 +00:00
|
|
|
b.serverURL = opts.LegacyMigrationPrefs.ControlURLOrDefault()
|
2021-04-07 05:11:50 +00:00
|
|
|
if b.notify == nil {
|
|
|
|
panic("FakeBackend.Start: SetNotifyCallback not called")
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
nl := NeedsLogin
|
2021-04-07 05:11:50 +00:00
|
|
|
if b.notify != nil {
|
2022-11-09 05:58:10 +00:00
|
|
|
p := opts.LegacyMigrationPrefs.View()
|
2022-11-03 22:46:17 +00:00
|
|
|
b.notify(Notify{Prefs: &p})
|
2021-04-07 05:11:50 +00:00
|
|
|
b.notify(Notify{State: &nl})
|
|
|
|
}
|
2020-02-05 22:16:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-07 05:11:50 +00: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 05:11:50 +00: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 05:11:50 +00:00
|
|
|
if b.notify != nil {
|
|
|
|
b.notify(Notify{BrowseToURL: &u})
|
|
|
|
}
|
2020-07-13 20:13:11 +00:00
|
|
|
b.login()
|
|
|
|
}
|
|
|
|
|
2021-03-19 17:21:33 +00:00
|
|
|
func (b *FakeBackend) Login(token *tailcfg.Oauth2Token) {
|
2020-07-13 20:13:11 +00: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 05:11:50 +00: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 05:11:50 +00: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 05:11:50 +00:00
|
|
|
if b.notify != nil {
|
2022-11-03 22:46:17 +00:00
|
|
|
p := new.View()
|
|
|
|
b.notify(Notify{Prefs: &p})
|
2021-04-07 05:11:50 +00:00
|
|
|
}
|
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 05:11:50 +00:00
|
|
|
if b.notify != nil {
|
|
|
|
b.notify(Notify{Engine: &EngineStatus{}})
|
|
|
|
}
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|