From 4bc8dc7f279d2860ec4923b9ac263b86c7ce3a55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Mon, 12 Jun 2023 14:44:17 +0800 Subject: [PATCH] Configure `default-route` for systemd-resolved Even though the documentation says this parameter doesn't matter, some people have reported that not configuring it can cause problems. --- tun_linux.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/tun_linux.go b/tun_linux.go index e460aa8..1751737 100644 --- a/tun_linux.go +++ b/tun_linux.go @@ -167,7 +167,7 @@ func (t *NativeTun) configure(tunLink netlink.Link) error { return err } - setSearchDomainForSystemdResolved(t.options.Name) + t.setSearchDomainForSystemdResolved() if t.options.AutoRoute && runtime.GOOS == "android" { t.interfaceCallback = t.options.InterfaceMonitor.RegisterCallback(t.routeUpdate) @@ -599,10 +599,21 @@ func (t *NativeTun) routeUpdate(event int) error { return nil } -func setSearchDomainForSystemdResolved(interfaceName string) { +func (t *NativeTun) setSearchDomainForSystemdResolved() { ctlPath, err := exec.LookPath("resolvectl") if err != nil { return } - shell.Exec(ctlPath, "domain", interfaceName, "~.").Run() + var dnsServer []netip.Addr + if len(t.options.Inet4Address) > 0 { + dnsServer = append(dnsServer, t.options.Inet4Address[0].Addr().Next()) + } + if len(t.options.Inet6Address) > 0 { + dnsServer = append(dnsServer, t.options.Inet6Address[0].Addr().Next()) + } + shell.Exec(ctlPath, "domain", t.options.Name, "~.").Start() + if t.options.AutoRoute { + shell.Exec(ctlPath, "default-route", t.options.Name, "true").Start() + shell.Exec(ctlPath, append([]string{"dns", t.options.Name}, common.Map(dnsServer, netip.Addr.String)...)...).Start() + } }