Flush DNS cache on macOS

This commit is contained in:
世界 2023-03-26 10:37:44 +08:00
parent ca53ccf346
commit 980d4bf9a3
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
2 changed files with 14 additions and 6 deletions

View file

@ -13,6 +13,7 @@ import (
"github.com/sagernet/sing/common/bufio" "github.com/sagernet/sing/common/bufio"
E "github.com/sagernet/sing/common/exceptions" E "github.com/sagernet/sing/common/exceptions"
N "github.com/sagernet/sing/common/network" N "github.com/sagernet/sing/common/network"
"github.com/sagernet/sing/common/shell"
"golang.org/x/net/route" "golang.org/x/net/route"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
@ -101,6 +102,7 @@ func (t *NativeTun) Write(p []byte) (n int, err error) {
} }
func (t *NativeTun) Close() error { func (t *NativeTun) Close() error {
flushDNSCache()
return t.tunFile.Close() return t.tunFile.Close()
} }
@ -285,6 +287,7 @@ func configure(tunFd int, ifIndex int, name string, options Options) error {
} }
} }
} }
flushDNSCache()
} }
return nil return nil
} }
@ -326,3 +329,7 @@ func addRoute(destination netip.Prefix, gateway netip.Addr) error {
return common.Error(unix.Write(socketFd, request)) return common.Error(unix.Write(socketFd, request))
}) })
} }
func flushDNSCache() {
shell.Exec("dscacheutil", "-flushcache").Start()
}

View file

@ -116,6 +116,10 @@ func (t *NativeTun) configure() error {
} }
} }
} }
err := windnsapi.FlushResolverCache()
if err != nil {
return err
}
} }
if len(t.options.Inet4Address) > 0 { if len(t.options.Inet4Address) > 0 {
inetIf, err := luid.IPInterface(winipcfg.AddressFamily(windows.AF_INET)) inetIf, err := luid.IPInterface(winipcfg.AddressFamily(windows.AF_INET))
@ -331,11 +335,6 @@ func (t *NativeTun) configure() error {
if err != nil { if err != nil {
return os.NewSyscallError("FwpmFilterAdd0", err) return os.NewSyscallError("FwpmFilterAdd0", err)
} }
err = windnsapi.FlushResolverCache()
if err != nil {
return err
}
} }
return nil return nil
@ -479,7 +478,9 @@ func (t *NativeTun) Close() error {
if t.fwpmSession != 0 { if t.fwpmSession != 0 {
winsys.FwpmEngineClose0(t.fwpmSession) winsys.FwpmEngineClose0(t.fwpmSession)
} }
windnsapi.FlushResolverCache() if t.options.AutoRoute {
windnsapi.FlushResolverCache()
}
}) })
return err return err
} }