diff --git a/adapter/network.go b/adapter/network.go index c3ae515a..2ef2c5d2 100644 --- a/adapter/network.go +++ b/adapter/network.go @@ -35,6 +35,7 @@ type NetworkManager interface { WIFIState() WIFIState UpdateWIFIState(ctx context.Context) ResetNetwork(ctx context.Context) + FlushSystemDNSCache() } type NetworkOptions struct { @@ -52,6 +53,10 @@ type InterfaceUpdateListener interface { InterfaceUpdated(ctx context.Context) } +type SystemDNSCacheFlusher interface { + FlushSystemDNSCache() +} + type WIFIState struct { SSID string BSSID string diff --git a/dns/router.go b/dns/router.go index ec8ef4e1..40107d9e 100644 --- a/dns/router.go +++ b/dns/router.go @@ -1376,6 +1376,7 @@ func (r *Router) ClearCache() { if r.platformInterface != nil { r.platformInterface.ClearDNSCache() } + service.FromContext[adapter.NetworkManager](r.ctx).FlushSystemDNSCache() if r.dnsReverseMapping != nil { r.dnsReverseMapping.Purge() } diff --git a/dns/transport/local/local_resolved_linux.go b/dns/transport/local/local_resolved_linux.go index 06a7a702..5707843c 100644 --- a/dns/transport/local/local_resolved_linux.go +++ b/dns/transport/local/local_resolved_linux.go @@ -34,6 +34,7 @@ import ( type DBusResolvedResolver struct { ctx context.Context logger logger.ContextLogger + networkManager adapter.NetworkManager interfaceFinder control.InterfaceFinder interfaceMonitor tun.DefaultInterfaceMonitor interfaceCallback *list.Element[tun.DefaultInterfaceUpdateCallback] @@ -142,6 +143,7 @@ func NewResolvedResolver(ctx context.Context, logger logger.ContextLogger) (Reso return &DBusResolvedResolver{ ctx: ctx, logger: logger, + networkManager: networkManager, interfaceFinder: networkManager.InterfaceFinder(), interfaceMonitor: interfaceMonitor, networkMonitor: networkManager.NetworkMonitor(), @@ -441,6 +443,7 @@ func (t *DBusResolvedResolver) updateStatus(ctx context.Context) error { } else if oldServerSet == nil { t.logger.Debug("systemd-resolved service is available") } + t.networkManager.FlushSystemDNSCache() return nil } diff --git a/go.mod b/go.mod index 91761b66..3ac5768b 100644 --- a/go.mod +++ b/go.mod @@ -55,7 +55,7 @@ require ( github.com/sagernet/sing-shadowsocks2 v0.2.2-0.20260929152114-a69d1086332b github.com/sagernet/sing-shadowtls v0.2.2-0.20260928201441-a9c0127d5c99 github.com/sagernet/sing-snell v0.0.0-20260904135315-bc5a12ac736f - github.com/sagernet/sing-tun v0.9.7-0.20261023115432-4faef97726ed + github.com/sagernet/sing-tun v0.9.7-0.20261023115432-fba659785148 github.com/sagernet/sing-usbip v0.0.0-20260817040617-28bd42667eca github.com/sagernet/sing-vmess v0.2.9-0.20260929152519-9b95ab8c9478 github.com/sagernet/smux v1.5.50-sing-box-mod.1 diff --git a/go.sum b/go.sum index 44a2f270..c1ade40c 100644 --- a/go.sum +++ b/go.sum @@ -338,8 +338,8 @@ github.com/sagernet/sing-shadowtls v0.2.2-0.20260928201441-a9c0127d5c99 h1:l/tdZ github.com/sagernet/sing-shadowtls v0.2.2-0.20260928201441-a9c0127d5c99/go.mod h1:sWqKnGlMipCHaGsw1sTTlimyUpgzP4WP3pjhCsYt9oA= github.com/sagernet/sing-snell v0.0.0-20260904135315-bc5a12ac736f h1:tbTCI+iis5sTmua7a6Q7vebcSRURok/LGgnEafnQAsc= github.com/sagernet/sing-snell v0.0.0-20260904135315-bc5a12ac736f/go.mod h1:et8Lws4f5QbOrY65DmjevHGup3mijJkhswkto6cwciM= -github.com/sagernet/sing-tun v0.9.7-0.20261023115432-4faef97726ed h1:4etIDY7Bn3c9X9qQjRXWllMP+bcdW7uw/jfBBysGMxw= -github.com/sagernet/sing-tun v0.9.7-0.20261023115432-4faef97726ed/go.mod h1:3EgPst7agntRO7D6GOsiZ1l9FoqdLeuWmKT5TnWkmf0= +github.com/sagernet/sing-tun v0.9.7-0.20261023115432-fba659785148 h1:zUiB60/5d4pZucVDQ7vwq7Tq9+SdXyVBP4MCzy5sVDE= +github.com/sagernet/sing-tun v0.9.7-0.20261023115432-fba659785148/go.mod h1:3EgPst7agntRO7D6GOsiZ1l9FoqdLeuWmKT5TnWkmf0= github.com/sagernet/sing-usbip v0.0.0-20260817040617-28bd42667eca h1:5wA+IE0Fq1CGVLOgSpm0gKKZ03HzugcJ2JyhmDqeX6A= github.com/sagernet/sing-usbip v0.0.0-20260817040617-28bd42667eca/go.mod h1:ADAZZU85MnM91XNhd2TdZRTaIbA7RjTZuQ1KCHcUbNg= github.com/sagernet/sing-vmess v0.2.9-0.20260929152519-9b95ab8c9478 h1:q2eQn4nq8oWGxRm9zXesXSlPr9GEgPrOGzf4iPtzG9A= diff --git a/protocol/tun/inbound.go b/protocol/tun/inbound.go index 9caf731b..231e88fe 100644 --- a/protocol/tun/inbound.go +++ b/protocol/tun/inbound.go @@ -527,6 +527,20 @@ func (t *Inbound) InterfaceUpdated(ctx context.Context) { } } +func (t *Inbound) FlushSystemDNSCache() { + linuxTUN, isLinuxTUN := t.tunIf.(tun.LinuxTUN) + if !isLinuxTUN { + return + } + serviceManager := service.FromContext[adapter.ServiceManager](t.ctx) + if slices.ContainsFunc(serviceManager.Services(), func(it adapter.Service) bool { + return it.Type() == C.TypeResolved + }) { + return + } + linuxTUN.FlushDNSCache() +} + func (t *Inbound) JudgeFlow(network uint8, source netip.AddrPort, destination netip.AddrPort, firstPacket []byte) tun.FlowVerdict { if slices.Contains(t.dnsHijackAddress, destination.Addr()) { if network == uint8(header.UDPProtocolNumber) { diff --git a/route/network.go b/route/network.go index 53a1be8d..1ff6e49e 100644 --- a/route/network.go +++ b/route/network.go @@ -481,6 +481,21 @@ func (r *NetworkManager) ResetNetwork(ctx context.Context) { r.router.ResetNetwork() } +func (r *NetworkManager) FlushSystemDNSCache() { + r.interfaceUpdateAccess.Lock() + started := r.startedCtx != nil + r.interfaceUpdateAccess.Unlock() + if !started { + return + } + for _, inbound := range r.inbound.Inbounds() { + flusher, isFlusher := inbound.(adapter.SystemDNSCacheFlusher) + if isFlusher { + flusher.FlushSystemDNSCache() + } + } +} + func (r *NetworkManager) notifyInterfaceUpdate(_ *control.Interface, _ int) { r.interfaceUpdateAccess.Lock() defer r.interfaceUpdateAccess.Unlock() diff --git a/route/network_environment.go b/route/network_environment.go index a3c6d2da..ac10b306 100644 --- a/route/network_environment.go +++ b/route/network_environment.go @@ -86,10 +86,13 @@ func (r *NetworkManager) updateNetworkEnvironment() { changed := environmentHash != r.networkEnvironment r.networkEnvironment = environmentHash r.stateAccess.Unlock() - if !changed || len(options) == 0 { + if !changed { return } - r.logger.Info("updated network environment: ", strings.Join(options, ", ")) + if len(options) > 0 { + r.logger.Info("updated network environment: ", strings.Join(options, ", ")) + } + r.FlushSystemDNSCache() } func formatEnvironmentValues(values []string) string {