diff --git a/cmd/cli/dns_proxy.go b/cmd/cli/dns_proxy.go index 1a790a0..799dc58 100644 --- a/cmd/cli/dns_proxy.go +++ b/cmd/cli/dns_proxy.go @@ -1250,8 +1250,16 @@ func (p *prog) monitorNetworkChanges(ctx context.Context) error { } } + // if the default route changed, set changed to true + if delta.New.DefaultRouteInterface != delta.Old.DefaultRouteInterface { + changed = true + mainLog.Load().Debug().Msgf("Default route changed from %s to %s", delta.Old.DefaultRouteInterface, delta.New.DefaultRouteInterface) + } + if !changed { mainLog.Load().Debug().Msg("Ignoring interface change - no valid interfaces affected") + // check if the default IPs are still on an interface that is up + ValidateDefaultLocalIPsFromDelta(delta.New) return } @@ -1581,3 +1589,32 @@ func (p *prog) buildRecoveryUpstreams(reason RecoveryReason) map[string]*ctrld.U } return upstreams } + +// ValidateDefaultLocalIPsFromDelta checks if the default local IPv4 and IPv6 stored +// are still present in the new network state (provided by delta.New). +// If a stored default IP is no longer active, it resets that default (sets it to nil) +// so that it won't be used in subsequent custom dialer contexts. +func ValidateDefaultLocalIPsFromDelta(newState *netmon.State) { + currentIPv4 := ctrld.GetDefaultLocalIPv4() + currentIPv6 := ctrld.GetDefaultLocalIPv6() + + // Build a map of active IP addresses from the new state. + activeIPs := make(map[string]bool) + for _, prefixes := range newState.InterfaceIPs { + for _, prefix := range prefixes { + activeIPs[prefix.Addr().String()] = true + } + } + + // Check if the default IPv4 is still active. + if currentIPv4 != nil && !activeIPs[currentIPv4.String()] { + mainLog.Load().Debug().Msgf("DefaultLocalIPv4 %s is no longer active in the new state. Resetting.", currentIPv4) + ctrld.SetDefaultLocalIPv4(nil) + } + + // Check if the default IPv6 is still active. + if currentIPv6 != nil && !activeIPs[currentIPv6.String()] { + mainLog.Load().Debug().Msgf("DefaultLocalIPv6 %s is no longer active in the new state. Resetting.", currentIPv6) + ctrld.SetDefaultLocalIPv6(nil) + } +} diff --git a/resolver.go b/resolver.go index b61aef0..e5abef2 100644 --- a/resolver.go +++ b/resolver.go @@ -289,8 +289,11 @@ func (o *osResolver) Resolve(ctx context.Context, msg *dns.Msg) (*dns.Msg, error numServers-- } } - - Log(ctx, ProxyLogger.Load().Debug(), "os resolver query with nameservers: %v public: %v", nss, publicServers) + question := "" + if msg != nil && len(msg.Question) > 0 { + question = msg.Question[0].Name + } + Log(ctx, ProxyLogger.Load().Debug(), "os resolver query for %s with nameservers: %v public: %v", question, nss, publicServers) // New check: If no resolvers are available, return an error. if numServers == 0 {