From 8a449c4dcdaa384e9874b6155f934ce50736781c Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Thu, 22 Apr 2021 15:47:28 -0700 Subject: [PATCH] ipn: define NewBackendServer nil as not affecting Backend's NotifyCallback Updates tailscale/corp#1646 Signed-off-by: Brad Fitzpatrick --- ipn/message.go | 12 +++++++++++- ipn/message_test.go | 2 ++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/ipn/message.go b/ipn/message.go index 8eeac8b57..7f8d2be21 100644 --- a/ipn/message.go +++ b/ipn/message.go @@ -93,17 +93,27 @@ type BackendServer struct { GotQuit bool // a Quit command was received } +// NewBackendServer creates a new BackendServer using b. +// +// If sendNotifyMsg is non-nil, it additionally sets the Backend's +// notification callback to call the func with ipn.Notify messages in +// JSON form. If nil, it does not change the notification callback. func NewBackendServer(logf logger.Logf, b Backend, sendNotifyMsg func(b []byte)) *BackendServer { bs := &BackendServer{ logf: logf, b: b, sendNotifyMsg: sendNotifyMsg, } - b.SetNotifyCallback(bs.send) + if sendNotifyMsg != nil { + b.SetNotifyCallback(bs.send) + } return bs } func (bs *BackendServer) send(n Notify) { + if bs.sendNotifyMsg == nil { + return + } n.Version = version.Long b, err := json.Marshal(n) if err != nil { diff --git a/ipn/message_test.go b/ipn/message_test.go index 1cc2d6fc2..3e2f1b8d3 100644 --- a/ipn/message_test.go +++ b/ipn/message_test.go @@ -87,6 +87,8 @@ func TestClientServer(t *testing.T) { t.Logf("c: "+fmt, args...) } bs = NewBackendServer(slogf, b, serverToClient) + // Verify that this doesn't break bs's callback: + NewBackendServer(slogf, b, nil) bc = NewBackendClient(clogf, clientToServer) ch := make(chan Notify, 256)