cmd/tailscaled, ipn/{ipnlocal,ipnserver}: let netstack get access to LocalBackend

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
pull/1493/head
Brad Fitzpatrick 3 years ago committed by Brad Fitzpatrick
parent 25d2dd868b
commit 2bc518dcb2

@ -29,6 +29,7 @@ import (
"time"
"github.com/go-multierror/multierror"
"tailscale.com/ipn/ipnlocal"
"tailscale.com/ipn/ipnserver"
"tailscale.com/logpolicy"
"tailscale.com/net/socks5"
@ -227,6 +228,8 @@ func run() error {
return err
}
localBEFuture := ipnlocal.NewLocalBackendFuture()
var ns *netstack.Impl
if useNetstack {
tunDev, magicConn := e.(wgengine.InternalsGetter).GetInternals()
@ -298,6 +301,7 @@ func run() error {
LegacyConfigPath: paths.LegacyConfigPath(),
SurviveDisconnects: true,
DebugMux: debugMux,
OnBackendCreated: localBEFuture.Set,
}
err = ipnserver.Run(ctx, logf, pol.PublicID.String(), ipnserver.FixedEngine(e), opts)
// Cancelation is not an error: it is the only way to stop ipnserver.

@ -0,0 +1,28 @@
// Copyright (c) 2021 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 ipnlocal
import "sync"
// LocalBackendFuture is a Future that returns a *LocalBackend.
type LocalBackendFuture struct {
getOnce sync.Once
ch chan *LocalBackend
v *LocalBackend
}
func (f *LocalBackendFuture) Get() *LocalBackend {
f.getOnce.Do(f.get)
return f.v
}
func (f *LocalBackendFuture) get() { f.v = <-f.ch }
func (f *LocalBackendFuture) Set(v *LocalBackend) { f.ch <- v }
func NewLocalBackendFuture() *LocalBackendFuture {
return &LocalBackendFuture{
ch: make(chan *LocalBackend, 1),
}
}

@ -92,6 +92,10 @@ type Options struct {
// DebugMux, if non-nil, specifies an HTTP ServeMux in which
// to register a debug handler.
DebugMux *http.ServeMux
// OnBackendCreated, if non-nil, is called once when the LocalBackend
// is created.
OnBackendCreated func(*ipnlocal.LocalBackend)
}
// server is an IPN backend and its set of 0 or more active connections
@ -739,6 +743,10 @@ func Run(ctx context.Context, logf logger.Logf, logid string, getEngine func() (
return smallzstd.NewDecoder(nil)
})
if opts.OnBackendCreated != nil {
opts.OnBackendCreated(b)
}
if opts.DebugMux != nil {
opts.DebugMux.HandleFunc("/debug/ipn", func(w http.ResponseWriter, r *http.Request) {
serveHTMLStatus(w, b)

Loading…
Cancel
Save