ipn/ipnserver: add New, Server.Server

Change-Id: I1dc85a5131b9a628d7fc780fde7103492cd3f1f8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
pull/3211/head
Brad Fitzpatrick 3 years ago committed by Brad Fitzpatrick
parent d381bc2b6c
commit 22dbaa0894

@ -101,6 +101,7 @@ type Server struct {
// connection (such as on Windows by default). Even if this // connection (such as on Windows by default). Even if this
// is true, the ForceDaemon pref can override this. // is true, the ForceDaemon pref can override this.
resetOnZero bool resetOnZero bool
opts Options
bsMu sync.Mutex // lock order: bsMu, then mu bsMu sync.Mutex // lock order: bsMu, then mu
bs *ipn.BackendServer bs *ipn.BackendServer
@ -725,11 +726,24 @@ func Run(ctx context.Context, logf logger.Logf, logid string, getEngine func() (
} }
} }
server, err := New(logf, logid, store, eng, serverModeUser, opts)
if err != nil {
return err
}
serverMu.Lock()
serverOrNil = server
serverMu.Unlock()
return server.Serve(ctx, listen)
}
// New returns a new Server.
//
// The opts.StatePath option is ignored; it's only used by Run.
func New(logf logger.Logf, logid string, store ipn.StateStore, eng wgengine.Engine, serverModeUser *user.User, opts Options) (*Server, error) {
b, err := ipnlocal.NewLocalBackend(logf, logid, store, eng) b, err := ipnlocal.NewLocalBackend(logf, logid, store, eng)
if err != nil { if err != nil {
return fmt.Errorf("NewLocalBackend: %v", err) return nil, fmt.Errorf("NewLocalBackend: %v", err)
} }
defer b.Shutdown()
b.SetDecompressor(func() (controlclient.Decompressor, error) { b.SetDecompressor(func() (controlclient.Decompressor, error) {
return smallzstd.NewDecoder(nil) return smallzstd.NewDecoder(nil)
}) })
@ -746,35 +760,45 @@ func Run(ctx context.Context, logf logger.Logf, logid string, getEngine func() (
logf: logf, logf: logf,
resetOnZero: !opts.SurviveDisconnects, resetOnZero: !opts.SurviveDisconnects,
serverModeUser: serverModeUser, serverModeUser: serverModeUser,
opts: opts,
} }
server.bs = ipn.NewBackendServer(logf, b, server.writeToClients) server.bs = ipn.NewBackendServer(logf, b, server.writeToClients)
return server, nil
}
serverMu.Lock() // Serve accepts connections from ln forever.
serverOrNil = server //
serverMu.Unlock() // The context is only used to suppress errors
func (s *Server) Serve(ctx context.Context, ln net.Listener) error {
if opts.AutostartStateKey != "" { defer s.b.Shutdown()
server.bs.GotCommand(context.TODO(), &ipn.Command{ if s.opts.AutostartStateKey != "" {
s.bs.GotCommand(ctx, &ipn.Command{
Version: version.Long, Version: version.Long,
Start: &ipn.StartArgs{ Start: &ipn.StartArgs{
Opts: ipn.Options{StateKey: opts.AutostartStateKey}, Opts: ipn.Options{StateKey: s.opts.AutostartStateKey},
}, },
}) })
} }
systemd.Ready() systemd.Ready()
for i := 1; ctx.Err() == nil; i++ { bo := backoff.NewBackoff("ipnserver", s.logf, 30*time.Second)
c, err := listen.Accept() var connNum int
for {
if ctx.Err() != nil {
return ctx.Err()
}
c, err := ln.Accept()
if err != nil { if err != nil {
if ctx.Err() == nil { if ctx.Err() != nil {
logf("ipnserver: Accept: %v", err) return ctx.Err()
bo.BackOff(ctx, err)
} }
s.logf("ipnserver: Accept: %v", err)
bo.BackOff(ctx, err)
continue continue
} }
go server.serveConn(ctx, c, logger.WithPrefix(logf, fmt.Sprintf("ipnserver: conn%d: ", i))) connNum++
go s.serveConn(ctx, c, logger.WithPrefix(s.logf, fmt.Sprintf("ipnserver: conn%d: ", connNum)))
} }
return ctx.Err()
} }
// BabysitProc runs the current executable as a child process with the // BabysitProc runs the current executable as a child process with the

Loading…
Cancel
Save