Compare commits

...

3 Commits

Author SHA1 Message Date
Alex cbf0b1aaa4
Merge 140a803ccf into cf679978be 2025-09-16 18:18:34 +00:00
Marc Brooks cf679978be
fix(timesync): ensure that auto-update waits for time sync (#609)
- Added check to not attempt auto update if time sync is needed and not yet successful (delays 30 second to recheck).
- Added resync of time when DHCP or link state changes if online
- Added conditional* fallback from configured* NTP servers to the IP-named NTP servers, and then to the DNS named ones if that fails
- Added conditional* fallback from the configured* HTTP servers to the default DNS named ones.
- Uses the configuration* option for how many queries to run in parallel
- Added known static IPs for time servers (in case DNS resolution isn't up yet)
- Added time.cloudflare.com to fall-back NTP servers
- Added fallback to NTP via hostnames
- Logs the resultant time (and mode)
2025-09-16 15:37:02 +02:00
Marc Brooks 80a8b9e9e3
feat: Adds IPv6 disabling feature (#803)
* Allow disabling IPv6

Simply ignores any IPv6 addresses in the lease and doesn't offer them to the RPC
Also fixed display issue for IPv6 link local address.
Fixes https://github.com/orgs/jetkvm/projects/7/views/1?pane=issue&itemId=122761546&issue=jetkvm%7Ckvm%7C685

* Don't listen on disabled addresses in mDNS or web server.

* We have to set the IPv4 and IPv6 modes on the server.
2025-09-16 12:44:56 +02:00
11 changed files with 130 additions and 69 deletions

View File

@ -56,13 +56,12 @@ type NetworkConfig struct {
} }
func (c *NetworkConfig) GetMDNSMode() *mdns.MDNSListenOptions { func (c *NetworkConfig) GetMDNSMode() *mdns.MDNSListenOptions {
mode := c.MDNSMode.String
listenOptions := &mdns.MDNSListenOptions{ listenOptions := &mdns.MDNSListenOptions{
IPv4: true, IPv4: c.IPv4Mode.String != "disabled",
IPv6: true, IPv6: c.IPv6Mode.String != "disabled",
} }
switch mode { switch c.MDNSMode.String {
case "ipv4_only": case "ipv4_only":
listenOptions.IPv6 = false listenOptions.IPv6 = false
case "ipv6_only": case "ipv6_only":

View File

@ -48,7 +48,7 @@ type NetworkInterfaceOptions struct {
DefaultHostname string DefaultHostname string
OnStateChange func(state *NetworkInterfaceState) OnStateChange func(state *NetworkInterfaceState)
OnInitialCheck func(state *NetworkInterfaceState) OnInitialCheck func(state *NetworkInterfaceState)
OnDhcpLeaseChange func(lease *udhcpc.Lease) OnDhcpLeaseChange func(lease *udhcpc.Lease, state *NetworkInterfaceState)
OnConfigChange func(config *NetworkConfig) OnConfigChange func(config *NetworkConfig)
NetworkConfig *NetworkConfig NetworkConfig *NetworkConfig
} }
@ -94,7 +94,7 @@ func NewNetworkInterfaceState(opts *NetworkInterfaceOptions) (*NetworkInterfaceS
_ = s.updateNtpServersFromLease(lease) _ = s.updateNtpServersFromLease(lease)
_ = s.setHostnameIfNotSame() _ = s.setHostnameIfNotSame()
opts.OnDhcpLeaseChange(lease) opts.OnDhcpLeaseChange(lease, s)
}, },
}) })
@ -239,6 +239,10 @@ func (s *NetworkInterfaceState) update() (DhcpTargetState, error) {
ipv4Addresses = append(ipv4Addresses, addr.IP) ipv4Addresses = append(ipv4Addresses, addr.IP)
ipv4AddressesString = append(ipv4AddressesString, addr.IPNet.String()) ipv4AddressesString = append(ipv4AddressesString, addr.IPNet.String())
} else if addr.IP.To16() != nil { } else if addr.IP.To16() != nil {
if s.config.IPv6Mode.String == "disabled" {
continue
}
scopedLogger := s.l.With().Str("ipv6", addr.IP.String()).Logger() scopedLogger := s.l.With().Str("ipv6", addr.IP.String()).Logger()
// check if it's a link local address // check if it's a link local address
if addr.IP.IsLinkLocalUnicast() { if addr.IP.IsLinkLocalUnicast() {
@ -287,6 +291,7 @@ func (s *NetworkInterfaceState) update() (DhcpTargetState, error) {
} }
s.ipv4Addresses = ipv4AddressesString s.ipv4Addresses = ipv4AddressesString
if s.config.IPv6Mode.String != "disabled" {
if ipv6LinkLocal != nil { if ipv6LinkLocal != nil {
if s.ipv6LinkLocal == nil || s.ipv6LinkLocal.String() != ipv6LinkLocal.String() { if s.ipv6LinkLocal == nil || s.ipv6LinkLocal.String() != ipv6LinkLocal.String() {
scopedLogger := s.l.With().Str("ipv6", ipv6LinkLocal.String()).Logger() scopedLogger := s.l.With().Str("ipv6", ipv6LinkLocal.String()).Logger()
@ -318,6 +323,7 @@ func (s *NetworkInterfaceState) update() (DhcpTargetState, error) {
changed = true changed = true
} }
} }
}
// if it's the initial check, we'll set changed to false // if it's the initial check, we'll set changed to false
initialCheck := !s.checked initialCheck := !s.checked

View File

@ -65,7 +65,7 @@ func (s *NetworkInterfaceState) IPv6LinkLocalAddress() string {
func (s *NetworkInterfaceState) RpcGetNetworkState() RpcNetworkState { func (s *NetworkInterfaceState) RpcGetNetworkState() RpcNetworkState {
ipv6Addresses := make([]RpcIPv6Address, 0) ipv6Addresses := make([]RpcIPv6Address, 0)
if s.ipv6Addresses != nil { if s.ipv6Addresses != nil && s.config.IPv6Mode.String != "disabled" {
for _, addr := range s.ipv6Addresses { for _, addr := range s.ipv6Addresses {
ipv6Addresses = append(ipv6Addresses, RpcIPv6Address{ ipv6Addresses = append(ipv6Addresses, RpcIPv6Address{
Address: addr.Prefix.String(), Address: addr.Prefix.String(),

View File

@ -9,17 +9,32 @@ import (
"github.com/beevik/ntp" "github.com/beevik/ntp"
) )
var defaultNTPServers = []string{ var defaultNTPServerIPs = []string{
// These servers are known by static IP and as such don't need DNS lookups
// These are from Google and Cloudflare since if they're down, the internet
// is broken anyway
"162.159.200.1", // time.cloudflare.com IPv4
"162.159.200.123", // time.cloudflare.com IPv4
"2606:4700:f1::1", // time.cloudflare.com IPv6
"2606:4700:f1::123", // time.cloudflare.com IPv6
"216.239.35.0", // time.google.com IPv4
"216.239.35.4", // time.google.com IPv4
"216.239.35.8", // time.google.com IPv4
"216.239.35.12", // time.google.com IPv4
"2001:4860:4806::", // time.google.com IPv6
"2001:4860:4806:4::", // time.google.com IPv6
"2001:4860:4806:8::", // time.google.com IPv6
"2001:4860:4806:c::", // time.google.com IPv6
}
var defaultNTPServerHostnames = []string{
// should use something from https://github.com/jauderho/public-ntp-servers
"time.apple.com", "time.apple.com",
"time.aws.com", "time.aws.com",
"time.windows.com", "time.windows.com",
"time.google.com", "time.google.com",
"162.159.200.123", // time.cloudflare.com IPv4 "time.cloudflare.com",
"2606:4700:f1::123", // time.cloudflare.com IPv6 "pool.ntp.org",
"0.pool.ntp.org",
"1.pool.ntp.org",
"2.pool.ntp.org",
"3.pool.ntp.org",
} }
func (t *TimeSync) queryNetworkTime(ntpServers []string) (now *time.Time, offset *time.Duration) { func (t *TimeSync) queryNetworkTime(ntpServers []string) (now *time.Time, offset *time.Duration) {

View File

@ -158,6 +158,7 @@ func (t *TimeSync) Sync() error {
var ( var (
now *time.Time now *time.Time
offset *time.Duration offset *time.Duration
log zerolog.Logger
) )
metricTimeSyncCount.Inc() metricTimeSyncCount.Inc()
@ -166,54 +167,54 @@ func (t *TimeSync) Sync() error {
Orders: Orders:
for _, mode := range syncMode.Ordering { for _, mode := range syncMode.Ordering {
log = t.l.With().Str("mode", mode).Logger()
switch mode { switch mode {
case "ntp_user_provided": case "ntp_user_provided":
if syncMode.Ntp { if syncMode.Ntp {
t.l.Info().Msg("using NTP custom servers") log.Info().Msg("using NTP custom servers")
now, offset = t.queryNetworkTime(t.networkConfig.TimeSyncNTPServers) now, offset = t.queryNetworkTime(t.networkConfig.TimeSyncNTPServers)
if now != nil { if now != nil {
t.l.Info().Str("source", "NTP").Time("now", *now).Msg("time obtained")
break Orders break Orders
} }
} }
case "ntp_dhcp": case "ntp_dhcp":
if syncMode.Ntp { if syncMode.Ntp {
t.l.Info().Msg("using NTP servers from DHCP") log.Info().Msg("using NTP servers from DHCP")
now, offset = t.queryNetworkTime(t.dhcpNtpAddresses) now, offset = t.queryNetworkTime(t.dhcpNtpAddresses)
if now != nil { if now != nil {
t.l.Info().Str("source", "NTP DHCP").Time("now", *now).Msg("time obtained")
break Orders break Orders
} }
} }
case "ntp": case "ntp":
if syncMode.Ntp && syncMode.NtpUseFallback { if syncMode.Ntp && syncMode.NtpUseFallback {
t.l.Info().Msg("using NTP fallback") log.Info().Msg("using NTP fallback IPs")
now, offset = t.queryNetworkTime(defaultNTPServers) now, offset = t.queryNetworkTime(defaultNTPServerIPs)
if now == nil {
log.Info().Msg("using NTP fallback hostnames")
now, offset = t.queryNetworkTime(defaultNTPServerHostnames)
}
if now != nil { if now != nil {
t.l.Info().Str("source", "NTP fallback").Time("now", *now).Msg("time obtained")
break Orders break Orders
} }
} }
case "http_user_provided": case "http_user_provided":
if syncMode.Http { if syncMode.Http {
t.l.Info().Msg("using HTTP custom URLs") log.Info().Msg("using HTTP custom URLs")
now = t.queryAllHttpTime(t.networkConfig.TimeSyncHTTPUrls) now = t.queryAllHttpTime(t.networkConfig.TimeSyncHTTPUrls)
if now != nil { if now != nil {
t.l.Info().Str("source", "HTTP").Time("now", *now).Msg("time obtained")
break Orders break Orders
} }
} }
case "http": case "http":
if syncMode.Http && syncMode.HttpUseFallback { if syncMode.Http && syncMode.HttpUseFallback {
t.l.Info().Msg("using HTTP fallback") log.Info().Msg("using HTTP fallback")
now = t.queryAllHttpTime(defaultHTTPUrls) now = t.queryAllHttpTime(defaultHTTPUrls)
if now != nil { if now != nil {
t.l.Info().Str("source", "HTTP fallback").Time("now", *now).Msg("time obtained")
break Orders break Orders
} }
} }
default: default:
t.l.Warn().Str("mode", mode).Msg("unknown time sync mode, skipping") log.Warn().Msg("unknown time sync mode, skipping")
} }
} }
@ -226,6 +227,8 @@ Orders:
now = &newNow now = &newNow
} }
log.Info().Time("now", *now).Msg("time obtained")
err := t.setSystemTime(*now) err := t.setSystemTime(*now)
if err != nil { if err != nil {
return fmt.Errorf("failed to set system time: %w", err) return fmt.Errorf("failed to set system time: %w", err)

View File

@ -246,16 +246,25 @@ func Main(audioServer bool, audioInputServer bool) {
if !config.AutoUpdateEnabled { if !config.AutoUpdateEnabled {
return return
} }
if isTimeSyncNeeded() || !timeSync.IsSyncSuccess() {
logger.Debug().Msg("system time is not synced, will retry in 30 seconds")
time.Sleep(30 * time.Second)
continue
}
if currentSession != nil { if currentSession != nil {
logger.Debug().Msg("skipping update since a session is active") logger.Debug().Msg("skipping update since a session is active")
time.Sleep(1 * time.Minute) time.Sleep(1 * time.Minute)
continue continue
} }
includePreRelease := config.IncludePreRelease includePreRelease := config.IncludePreRelease
err = TryUpdate(context.Background(), GetDeviceID(), includePreRelease) err = TryUpdate(context.Background(), GetDeviceID(), includePreRelease)
if err != nil { if err != nil {
logger.Warn().Err(err).Msg("failed to auto update") logger.Warn().Err(err).Msg("failed to auto update")
} }
time.Sleep(1 * time.Hour) time.Sleep(1 * time.Hour)
} }
}() }()

View File

@ -13,10 +13,7 @@ func initMdns() error {
networkState.GetHostname(), networkState.GetHostname(),
networkState.GetFQDN(), networkState.GetFQDN(),
}, },
ListenOptions: &mdns.MDNSListenOptions{ ListenOptions: config.NetworkConfig.GetMDNSMode(),
IPv4: true,
IPv6: true,
},
}) })
if err != nil { if err != nil {
return err return err

View File

@ -15,7 +15,7 @@ var (
networkState *network.NetworkInterfaceState networkState *network.NetworkInterfaceState
) )
func networkStateChanged() { func networkStateChanged(isOnline bool) {
// do not block the main thread // do not block the main thread
go waitCtrlAndRequestDisplayUpdate(true) go waitCtrlAndRequestDisplayUpdate(true)
@ -37,6 +37,13 @@ func networkStateChanged() {
networkState.GetFQDN(), networkState.GetFQDN(),
}, true) }, true)
} }
// if the network is now online, trigger an NTP sync if still needed
if isOnline && 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 initNetwork() error { func initNetwork() error {
@ -48,13 +55,13 @@ func initNetwork() error {
NetworkConfig: config.NetworkConfig, NetworkConfig: config.NetworkConfig,
Logger: networkLogger, Logger: networkLogger,
OnStateChange: func(state *network.NetworkInterfaceState) { OnStateChange: func(state *network.NetworkInterfaceState) {
networkStateChanged() networkStateChanged(state.IsOnline())
}, },
OnInitialCheck: func(state *network.NetworkInterfaceState) { OnInitialCheck: func(state *network.NetworkInterfaceState) {
networkStateChanged() networkStateChanged(state.IsOnline())
}, },
OnDhcpLeaseChange: func(lease *udhcpc.Lease) { OnDhcpLeaseChange: func(lease *udhcpc.Lease, state *network.NetworkInterfaceState) {
networkStateChanged() networkStateChanged(state.IsOnline())
if currentSession == nil { if currentSession == nil {
return return
@ -64,7 +71,15 @@ func initNetwork() error {
}, },
OnConfigChange: func(networkConfig *network.NetworkConfig) { OnConfigChange: func(networkConfig *network.NetworkConfig) {
config.NetworkConfig = networkConfig config.NetworkConfig = networkConfig
networkStateChanged() networkStateChanged(false)
if mDNS != nil {
_ = mDNS.SetListenOptions(networkConfig.GetMDNSMode())
_ = mDNS.SetLocalNames([]string{
networkState.GetHostname(),
networkState.GetFQDN(),
}, true)
}
}, },
}) })

View File

@ -17,7 +17,7 @@ export default function Ipv6NetworkCard({
</h3> </h3>
<div className="grid grid-cols-2 gap-x-6 gap-y-2"> <div className="grid grid-cols-2 gap-x-6 gap-y-2">
{networkState?.dhcp_lease?.ip && ( {networkState?.ipv6_link_local && (
<div className="flex flex-col justify-between"> <div className="flex flex-col justify-between">
<span className="text-sm text-slate-600 dark:text-slate-400"> <span className="text-sm text-slate-600 dark:text-slate-400">
Link-local Link-local

View File

@ -166,11 +166,11 @@ export default function SettingsNetworkRoute() {
}, [getNetworkState, getNetworkSettings]); }, [getNetworkState, getNetworkSettings]);
const handleIpv4ModeChange = (value: IPv4Mode | string) => { const handleIpv4ModeChange = (value: IPv4Mode | string) => {
setNetworkSettings({ ...networkSettings, ipv4_mode: value as IPv4Mode }); setNetworkSettingsRemote({ ...networkSettings, ipv4_mode: value as IPv4Mode });
}; };
const handleIpv6ModeChange = (value: IPv6Mode | string) => { const handleIpv6ModeChange = (value: IPv6Mode | string) => {
setNetworkSettings({ ...networkSettings, ipv6_mode: value as IPv6Mode }); setNetworkSettingsRemote({ ...networkSettings, ipv6_mode: value as IPv6Mode });
}; };
const handleLldpModeChange = (value: LLDPMode | string) => { const handleLldpModeChange = (value: LLDPMode | string) => {
@ -419,7 +419,7 @@ export default function SettingsNetworkRoute() {
value={networkSettings.ipv6_mode} value={networkSettings.ipv6_mode}
onChange={e => handleIpv6ModeChange(e.target.value)} onChange={e => handleIpv6ModeChange(e.target.value)}
options={filterUnknown([ options={filterUnknown([
// { value: "disabled", label: "Disabled" }, { value: "disabled", label: "Disabled" },
{ value: "slaac", label: "SLAAC" }, { value: "slaac", label: "SLAAC" },
// { value: "dhcpv6", label: "DHCPv6" }, // { value: "dhcpv6", label: "DHCPv6" },
// { value: "slaac_and_dhcpv6", label: "SLAAC and DHCPv6" }, // { value: "slaac_and_dhcpv6", label: "SLAAC and DHCPv6" },

25
web.go
View File

@ -576,14 +576,31 @@ func RunWebServer() {
r := setupRouter() r := setupRouter()
// Determine the binding address based on the config // Determine the binding address based on the config
bindAddress := ":80" // Default to all interfaces var bindAddress string
listenPort := 80 // default port
useIPv4 := config.NetworkConfig.IPv4Mode.String != "disabled"
useIPv6 := config.NetworkConfig.IPv6Mode.String != "disabled"
if config.LocalLoopbackOnly { if config.LocalLoopbackOnly {
bindAddress = "localhost:80" // Loopback only (both IPv4 and IPv6) if useIPv4 && useIPv6 {
bindAddress = fmt.Sprintf("localhost:%d", listenPort)
} else if useIPv4 {
bindAddress = fmt.Sprintf("127.0.0.1:%d", listenPort)
} else if useIPv6 {
bindAddress = fmt.Sprintf("[::1]:%d", listenPort)
}
} else {
if useIPv4 && useIPv6 {
bindAddress = fmt.Sprintf(":%d", listenPort)
} else if useIPv4 {
bindAddress = fmt.Sprintf("0.0.0.0:%d", listenPort)
} else if useIPv6 {
bindAddress = fmt.Sprintf("[::]:%d", listenPort)
}
} }
logger.Info().Str("bindAddress", bindAddress).Bool("loopbackOnly", config.LocalLoopbackOnly).Msg("Starting web server") logger.Info().Str("bindAddress", bindAddress).Bool("loopbackOnly", config.LocalLoopbackOnly).Msg("Starting web server")
err := r.Run(bindAddress) if err := r.Run(bindAddress); err != nil {
if err != nil {
panic(err) panic(err)
} }
} }