cmd/tailscale: avoid creating multiple netmon instances

This is a quick fix, we should come back and try to reorganize this
later.

Updates tailscale/tailscale#9374
pull/137/head
James Tucker 1 year ago committed by James Tucker
parent 6a142b2f50
commit 727e7e2b50

@ -46,6 +46,7 @@ type backend struct {
settings settingsFunc settings settingsFunc
lastCfg *router.Config lastCfg *router.Config
lastDNSCfg *dns.OSConfig lastDNSCfg *dns.OSConfig
netMon *netmon.Monitor
logIDPublic string logIDPublic string
logger *logtail.Logger logger *logtail.Logger
@ -124,7 +125,13 @@ func newBackend(dataDir string, jvm *jni.JVM, appCtx jni.Object, store *stateSto
} else { } else {
logID.UnmarshalText([]byte(storedLogID)) logID.UnmarshalText([]byte(storedLogID))
} }
b.SetupLogs(dataDir, logID)
netMon, err := netmon.New(logf)
if err != nil {
log.Printf("netmon.New: %w", err)
}
b.netMon = netMon
b.SetupLogs(dataDir, logID, logf)
dialer := new(tsdial.Dialer) dialer := new(tsdial.Dialer)
cb := &router.CallbackRouter{ cb := &router.CallbackRouter{
SetBoth: b.setCfg, SetBoth: b.setCfg,
@ -137,6 +144,7 @@ func newBackend(dataDir string, jvm *jni.JVM, appCtx jni.Object, store *stateSto
DNS: cb, DNS: cb,
Dialer: dialer, Dialer: dialer,
SetSubsystem: sys.Set, SetSubsystem: sys.Set,
NetMon: b.netMon,
}) })
if err != nil { if err != nil {
return nil, fmt.Errorf("runBackend: NewUserspaceEngine: %v", err) return nil, fmt.Errorf("runBackend: NewUserspaceEngine: %v", err)
@ -330,15 +338,11 @@ func (b *backend) CloseTUNs() {
} }
// SetupLogs sets up remote logging. // SetupLogs sets up remote logging.
func (b *backend) SetupLogs(logDir string, logID logid.PrivateID) { func (b *backend) SetupLogs(logDir string, logID logid.PrivateID, logf logger.Logf) {
logf := logger.RusagePrefixLog(log.Printf) if b.netMon == nil {
netMon, err := netmon.New(func(format string, args ...any) { panic("netMon must be created prior to SetupLogs")
logf(format, args...)
})
if err != nil {
log.Printf("netmon.New: %w", err)
} }
transport := logpolicy.NewLogtailTransport(logtail.DefaultHost, netMon, log.Printf) transport := logpolicy.NewLogtailTransport(logtail.DefaultHost, b.netMon, log.Printf)
logcfg := logtail.Config{ logcfg := logtail.Config{
Collection: logtail.CollectionNode, Collection: logtail.CollectionNode,

Loading…
Cancel
Save