mirror of https://github.com/jetkvm/kvm.git
fix online state detection
This commit is contained in:
parent
8cc7ead032
commit
05f2e5babe
36
network.go
36
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() {
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue