Flush systemd-resolved cache on DNS changes

This commit is contained in:
世界
2026-10-23 19:57:04 +08:00
parent b6fc3a4863
commit a130b482d4
8 changed files with 46 additions and 5 deletions
+5
View File
@@ -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
+1
View File
@@ -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()
}
@@ -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
}
+1 -1
View File
@@ -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
+2 -2
View File
@@ -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=
+14
View File
@@ -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) {
+15
View File
@@ -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()
+5 -2
View File
@@ -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 {