From 05f2e5babef35d3fa175b434fdb8a078dd377242 Mon Sep 17 00:00:00 2001 From: Siyuan Date: Wed, 8 Oct 2025 13:18:17 +0000 Subject: [PATCH] fix online state detection --- network.go | 36 ++++++++++++++++++++---------------- pkg/nmlite/interface.go | 17 ++++++++++------- pkg/nmlite/manager.go | 6 +++--- 3 files changed, 33 insertions(+), 26 deletions(-) diff --git a/network.go b/network.go index 70936a89..5e3496bb 100644 --- a/network.go +++ b/network.go @@ -47,31 +47,35 @@ func restartMdns() { }, true) } -func networkStateChanged(iface string, state *types.InterfaceState) { +func triggerTimeSyncOnNetworkStateChange() { + if timeSync == nil { + return + } + + // set the NTP servers from the network manager + if networkManager != nil { + timeSync.SetDhcpNtpAddresses(networkManager.NTPServerStrings()) + } + + // sync time + if err := timeSync.Sync(); err != nil { + networkLogger.Error().Err(err).Msg("failed to sync time after network state change") + } +} + +func networkStateChanged(_ string, state types.InterfaceState) { // do not block the main thread go waitCtrlAndRequestDisplayUpdate(true, "network_state_changed") - if timeSync != nil { - if networkManager != nil { - timeSync.SetDhcpNtpAddresses(networkManager.NTPServerStrings()) - } - - if err := timeSync.Sync(); err != nil { - networkLogger.Error().Err(err).Msg("failed to sync time after network state change") - } + if state.Online { + networkLogger.Info().Msg("network state changed to online, triggering time sync") + triggerTimeSyncOnNetworkStateChange() } // always restart mDNS when the network state changes if mDNS != nil { restartMdns() } - - // if the network is now online, trigger an NTP sync if still needed - if state.Up && timeSync != nil && (isTimeSyncNeeded() || !timeSync.IsSyncSuccess()) { - if err := timeSync.Sync(); err != nil { - logger.Warn().Str("error", err.Error()).Msg("unable to sync time on network state change") - } - } } func validateNetworkConfig() { diff --git a/pkg/nmlite/interface.go b/pkg/nmlite/interface.go index 42ede504..eea1cd59 100644 --- a/pkg/nmlite/interface.go +++ b/pkg/nmlite/interface.go @@ -36,7 +36,7 @@ type InterfaceManager struct { hostname *HostnameManager // Callbacks - onStateChange func(state *types.InterfaceState) + onStateChange func(state types.InterfaceState) onConfigChange func(config *types.NetworkConfig) onDHCPLeaseChange func(lease *types.DHCPLease) @@ -321,7 +321,7 @@ func (im *InterfaceManager) RenewDHCPLease() error { } // SetOnStateChange sets the callback for state changes -func (im *InterfaceManager) SetOnStateChange(callback func(state *types.InterfaceState)) { +func (im *InterfaceManager) SetOnStateChange(callback func(state types.InterfaceState)) { im.onStateChange = callback } @@ -693,13 +693,17 @@ func (im *InterfaceManager) updateInterfaceState() error { attrs := nl.Attrs() isUp := attrs.OperState == netlink.OperUp + // check if the interface has unicast addresses hasAddrs := false addrs, err := nl.AddrList(link.AfUnspec) if err != nil { return fmt.Errorf("failed to get addresses: %w", err) } - if len(addrs) > 0 { - hasAddrs = true + for _, addr := range addrs { + if addr.IP.IsGlobalUnicast() { + hasAddrs = true + break + } } // Check if state changed @@ -731,9 +735,8 @@ func (im *InterfaceManager) updateInterfaceState() error { // Notify callback if state changed if stateChanged && im.onStateChange != nil { - state := *im.state - im.logger.Debug().Interface("state", state).Msg("notifying state change") - im.onStateChange(&state) + im.logger.Debug().Interface("state", im.state).Msg("notifying state change") + im.onStateChange(*im.state) } return nil diff --git a/pkg/nmlite/manager.go b/pkg/nmlite/manager.go index b7ef6a64..dc04190f 100644 --- a/pkg/nmlite/manager.go +++ b/pkg/nmlite/manager.go @@ -24,7 +24,7 @@ type NetworkManager struct { cancel context.CancelFunc // Callback functions for state changes - onInterfaceStateChange func(iface string, state *types.InterfaceState) + onInterfaceStateChange func(iface string, state types.InterfaceState) onConfigChange func(iface string, config *types.NetworkConfig) onDHCPLeaseChange func(iface string, lease *types.DHCPLease) } @@ -63,7 +63,7 @@ func (nm *NetworkManager) AddInterface(iface string, config *types.NetworkConfig } // Set up callbacks - im.SetOnStateChange(func(state *types.InterfaceState) { + im.SetOnStateChange(func(state types.InterfaceState) { if nm.onInterfaceStateChange != nil { nm.onInterfaceStateChange(iface, state) } @@ -179,7 +179,7 @@ func (nm *NetworkManager) RenewDHCPLease(iface string) error { } // SetOnInterfaceStateChange sets the callback for interface state changes -func (nm *NetworkManager) SetOnInterfaceStateChange(callback func(iface string, state *types.InterfaceState)) { +func (nm *NetworkManager) SetOnInterfaceStateChange(callback func(iface string, state types.InterfaceState)) { nm.onInterfaceStateChange = callback }