From 21280ca2d10cfa6ce347f5991fd4452fc9b31fb1 Mon Sep 17 00:00:00 2001 From: David Anderson Date: Mon, 3 Feb 2020 15:58:40 -0800 Subject: [PATCH] Make ipn.Options.Prefs a pointer. This is a prelude to making it truly optional, once state management has moved into the backend. For now though, it's still required. This change is just isolating the bubbling-up of the pointerification into other layers. Signed-Off-By: David Anderson --- cmd/tailscale/ipn.go | 2 +- ipn/backend.go | 2 +- ipn/e2e_test.go | 2 +- ipn/fake.go | 2 +- ipn/handle.go | 4 +++- ipn/local.go | 9 +++++++-- ipn/message_test.go | 1 + ipn/prefs.go | 4 ++-- 8 files changed, 17 insertions(+), 9 deletions(-) diff --git a/cmd/tailscale/ipn.go b/cmd/tailscale/ipn.go index de8209ee5..a957edb8f 100644 --- a/cmd/tailscale/ipn.go +++ b/cmd/tailscale/ipn.go @@ -90,7 +90,7 @@ func main() { bc := ipn.NewBackendClient(log.Printf, clientToServer) opts := ipn.Options{ - Prefs: prefs, + Prefs: &prefs, ServerURL: *server, Notify: func(n ipn.Notify) { log.Printf("Notify: %v\n", n) diff --git a/ipn/backend.go b/ipn/backend.go index 245b70a6b..b69871477 100644 --- a/ipn/backend.go +++ b/ipn/backend.go @@ -53,7 +53,7 @@ type Notify struct { type Options struct { FrontendLogID string // public logtail id used by frontend ServerURL string - Prefs Prefs + Prefs *Prefs Notify func(n Notify) `json:"-"` } diff --git a/ipn/e2e_test.go b/ipn/e2e_test.go index dedc0c051..9b9b4de1b 100644 --- a/ipn/e2e_test.go +++ b/ipn/e2e_test.go @@ -170,7 +170,7 @@ func newNode(t *testing.T, prefix string, https *httptest.Server) testNode { n.Start(Options{ FrontendLogID: prefix + "-f", ServerURL: https.URL, - Prefs: Prefs{ + Prefs: &Prefs{ RouteAll: true, AllowSingleHosts: true, CorpDNS: true, diff --git a/ipn/fake.go b/ipn/fake.go index 3e885d1af..89a5d58fa 100644 --- a/ipn/fake.go +++ b/ipn/fake.go @@ -21,7 +21,7 @@ func (b *FakeBackend) Start(opts Options) error { log.Fatalf("FakeBackend.Start: opts.Notify is nil\n") } b.notify = opts.Notify - b.notify(Notify{Prefs: &opts.Prefs}) + b.notify(Notify{Prefs: opts.Prefs}) nl := NeedsLogin b.notify(Notify{State: &nl}) return nil diff --git a/ipn/handle.go b/ipn/handle.go index 4d5f9020c..8b4794daa 100644 --- a/ipn/handle.go +++ b/ipn/handle.go @@ -49,7 +49,9 @@ func (h *Handle) Start(opts Options) error { h.netmapCache = nil h.engineStatusCache = EngineStatus{} h.stateCache = NoState - h.prefsCache = opts.Prefs + if opts.Prefs != nil { + h.prefsCache = *opts.Prefs + } xopts := opts xopts.Notify = h.notify return h.b.Start(xopts) diff --git a/ipn/local.go b/ipn/local.go index 92dcf5e85..4fbfe1a35 100644 --- a/ipn/local.go +++ b/ipn/local.go @@ -53,7 +53,6 @@ type LocalBackend struct { } func NewLocalBackend(logf logger.Logf, logid string, e wgengine.Engine) (*LocalBackend, error) { - if e == nil { panic("ipn.NewLocalBackend: wgengine must not be nil") } @@ -114,6 +113,10 @@ func (b *LocalBackend) SetCmpDiff(cmpDiff func(x, y interface{}) string) { } func (b *LocalBackend) Start(opts Options) error { + if opts.Prefs == nil { + panic("Prefs can't be nil yet") + } + if b.c != nil { // TODO(apenwarr): avoid the need to reinit controlclient. // This will trigger a full relogin/reconfigure cycle every @@ -136,7 +139,9 @@ func (b *LocalBackend) Start(opts Options) error { b.hiCache = hi b.state = NoState b.serverURL = opts.ServerURL - b.prefs = opts.Prefs + if opts.Prefs != nil { + b.prefs = *opts.Prefs + } b.notify = opts.Notify b.netMapCache = nil b.mu.Unlock() diff --git a/ipn/message_test.go b/ipn/message_test.go index cecafa9f9..b4f9cd6be 100644 --- a/ipn/message_test.go +++ b/ipn/message_test.go @@ -96,6 +96,7 @@ func TestClientServer(t *testing.T) { ch := make(chan Notify, 256) h, err := NewHandle(bc, clogf, Options{ ServerURL: "http://example.com/fake", + Prefs: &Prefs{}, Notify: func(n Notify) { ch <- n }, diff --git a/ipn/prefs.go b/ipn/prefs.go index 922f30729..9fca69185 100644 --- a/ipn/prefs.go +++ b/ipn/prefs.go @@ -103,7 +103,7 @@ func (uc *Prefs) Copy() *Prefs { return &uc2 } -func LoadPrefs(filename string, enforceDefaults bool) Prefs { +func LoadPrefs(filename string, enforceDefaults bool) *Prefs { log.Printf("Loading prefs %v\n", filename) data, err := ioutil.ReadFile(filename) uc := NewPrefs() @@ -136,7 +136,7 @@ post: uc.WantRunning = true } log.Printf("Loaded prefs %v %v\n", filename, uc.Pretty()) - return uc + return &uc } func SavePrefs(filename string, uc *Prefs) {