fix online state detection

This commit is contained in:
Siyuan 2025-10-08 13:18:17 +00:00
parent 8cc7ead032
commit 05f2e5babe
3 changed files with 33 additions and 26 deletions

View File

@ -47,31 +47,35 @@ func restartMdns() {
}, true) }, true)
} }
func networkStateChanged(iface string, state *types.InterfaceState) { func triggerTimeSyncOnNetworkStateChange() {
// do not block the main thread if timeSync == nil {
go waitCtrlAndRequestDisplayUpdate(true, "network_state_changed") return
}
if timeSync != nil { // set the NTP servers from the network manager
if networkManager != nil { if networkManager != nil {
timeSync.SetDhcpNtpAddresses(networkManager.NTPServerStrings()) timeSync.SetDhcpNtpAddresses(networkManager.NTPServerStrings())
} }
// sync time
if err := timeSync.Sync(); err != nil { if err := timeSync.Sync(); err != nil {
networkLogger.Error().Err(err).Msg("failed to sync time after network state change") 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 state.Online {
networkLogger.Info().Msg("network state changed to online, triggering time sync")
triggerTimeSyncOnNetworkStateChange()
}
// always restart mDNS when the network state changes // always restart mDNS when the network state changes
if mDNS != nil { if mDNS != nil {
restartMdns() 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() { func validateNetworkConfig() {

View File

@ -36,7 +36,7 @@ type InterfaceManager struct {
hostname *HostnameManager hostname *HostnameManager
// Callbacks // Callbacks
onStateChange func(state *types.InterfaceState) onStateChange func(state types.InterfaceState)
onConfigChange func(config *types.NetworkConfig) onConfigChange func(config *types.NetworkConfig)
onDHCPLeaseChange func(lease *types.DHCPLease) onDHCPLeaseChange func(lease *types.DHCPLease)
@ -321,7 +321,7 @@ func (im *InterfaceManager) RenewDHCPLease() error {
} }
// SetOnStateChange sets the callback for state changes // 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 im.onStateChange = callback
} }
@ -693,13 +693,17 @@ func (im *InterfaceManager) updateInterfaceState() error {
attrs := nl.Attrs() attrs := nl.Attrs()
isUp := attrs.OperState == netlink.OperUp isUp := attrs.OperState == netlink.OperUp
// check if the interface has unicast addresses
hasAddrs := false hasAddrs := false
addrs, err := nl.AddrList(link.AfUnspec) addrs, err := nl.AddrList(link.AfUnspec)
if err != nil { if err != nil {
return fmt.Errorf("failed to get addresses: %w", err) return fmt.Errorf("failed to get addresses: %w", err)
} }
if len(addrs) > 0 { for _, addr := range addrs {
if addr.IP.IsGlobalUnicast() {
hasAddrs = true hasAddrs = true
break
}
} }
// Check if state changed // Check if state changed
@ -731,9 +735,8 @@ func (im *InterfaceManager) updateInterfaceState() error {
// Notify callback if state changed // Notify callback if state changed
if stateChanged && im.onStateChange != nil { if stateChanged && im.onStateChange != nil {
state := *im.state im.logger.Debug().Interface("state", im.state).Msg("notifying state change")
im.logger.Debug().Interface("state", state).Msg("notifying state change") im.onStateChange(*im.state)
im.onStateChange(&state)
} }
return nil return nil

View File

@ -24,7 +24,7 @@ type NetworkManager struct {
cancel context.CancelFunc cancel context.CancelFunc
// Callback functions for state changes // 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) onConfigChange func(iface string, config *types.NetworkConfig)
onDHCPLeaseChange func(iface string, lease *types.DHCPLease) onDHCPLeaseChange func(iface string, lease *types.DHCPLease)
} }
@ -63,7 +63,7 @@ func (nm *NetworkManager) AddInterface(iface string, config *types.NetworkConfig
} }
// Set up callbacks // Set up callbacks
im.SetOnStateChange(func(state *types.InterfaceState) { im.SetOnStateChange(func(state types.InterfaceState) {
if nm.onInterfaceStateChange != nil { if nm.onInterfaceStateChange != nil {
nm.onInterfaceStateChange(iface, state) nm.onInterfaceStateChange(iface, state)
} }
@ -179,7 +179,7 @@ func (nm *NetworkManager) RenewDHCPLease(iface string) error {
} }
// SetOnInterfaceStateChange sets the callback for interface state changes // 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 nm.onInterfaceStateChange = callback
} }