From 4a05fb6b289ce6115ecc912560cc60b3b8f168ea Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 6 Feb 2025 15:30:54 -0500 Subject: [PATCH] use the changed iface if no default route is set yet --- cmd/cli/dns_proxy.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/cmd/cli/dns_proxy.go b/cmd/cli/dns_proxy.go index 779b29a..b00a355 100644 --- a/cmd/cli/dns_proxy.go +++ b/cmd/cli/dns_proxy.go @@ -1316,7 +1316,7 @@ func (p *prog) monitorNetworkChanges(ctx context.Context) error { changed := false activeInterfaceExists := false - + changeIPs := []netip.Prefix{} // Check each valid interface for changes for ifaceName := range validIfaces { oldIface := delta.Old.Interface[ifaceName] @@ -1336,6 +1336,7 @@ func (p *prog) monitorNetworkChanges(ctx context.Context) error { if !interfaceStatesEqual(&oldIface, &newIface) || !interfaceIPsEqual(oldIPs, newIPs) { if newIface.IsUp() && len(newIPs) > 0 { changed = true + changeIPs = newIPs mainLog.Load().Debug(). Str("interface", ifaceName). Interface("old_ips", oldIPs). @@ -1372,6 +1373,18 @@ func (p *prog) monitorNetworkChanges(ctx context.Context) error { ipv6 = addr.String() } } + } else { + // If no default route interface is set yet, use the changed IPs + for _, ip := range changeIPs { + ipAddr, _ := netip.ParsePrefix(ip.String()) + addr := ipAddr.Addr() + if addr.Is4() && selfIP == "" && !addr.IsLoopback() && !addr.IsLinkLocalUnicast() { + selfIP = addr.String() + } + if addr.Is6() && !addr.IsLoopback() && !addr.IsLinkLocalUnicast() { + ipv6 = addr.String() + } + } } if ip := net.ParseIP(selfIP); ip != nil {