From d0703b78fa9af1a160d05881f9b319af06c1cb9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Fri, 26 Aug 2022 20:59:22 +0800 Subject: [PATCH] Fix dns hijack on android iproute2 on android does not support port rules --- common/dialer/bind.go | 19 ------------------- common/dialer/default.go | 16 ++++++++-------- common/settings/proxy_darwin.go | 3 ++- go.mod | 4 ++-- go.sum | 8 ++++---- route/router.go | 2 +- 6 files changed, 17 insertions(+), 35 deletions(-) delete mode 100644 common/dialer/bind.go diff --git a/common/dialer/bind.go b/common/dialer/bind.go deleted file mode 100644 index 14922471..00000000 --- a/common/dialer/bind.go +++ /dev/null @@ -1,19 +0,0 @@ -package dialer - -import ( - "syscall" - - "github.com/sagernet/sing/common/control" - M "github.com/sagernet/sing/common/metadata" - N "github.com/sagernet/sing/common/network" -) - -func skipIfPrivate(next control.Func) control.Func { - return func(network, address string, conn syscall.RawConn) error { - destination := M.ParseSocksaddr(address) - if !N.IsPublicAddr(destination.Addr) { - return nil - } - return next(network, address, conn) - } -} diff --git a/common/dialer/default.go b/common/dialer/default.go index b21980fb..d88b21cb 100644 --- a/common/dialer/default.go +++ b/common/dialer/default.go @@ -64,25 +64,25 @@ func NewDefault(router adapter.Router, options option.DialerOptions) *DefaultDia var listener net.ListenConfig if options.BindInterface != "" { warnBindInterfaceOnUnsupportedPlatform.Check() - bindFunc := skipIfPrivate(control.BindToInterface(router.InterfaceBindManager(), options.BindInterface)) + bindFunc := control.BindToInterface(router.InterfaceBindManager(), options.BindInterface) dialer.Control = control.Append(dialer.Control, bindFunc) listener.Control = control.Append(listener.Control, bindFunc) } else if router.AutoDetectInterface() { if C.IsWindows { - bindFunc := skipIfPrivate(control.BindToInterfaceIndexFunc(func() int { - return router.InterfaceMonitor().DefaultInterfaceIndex() - })) + bindFunc := control.BindToInterfaceIndexFunc(func(network, address string) int { + return router.InterfaceMonitor().DefaultInterfaceIndex(M.ParseSocksaddr(address).Addr) + }) dialer.Control = control.Append(dialer.Control, bindFunc) listener.Control = control.Append(listener.Control, bindFunc) } else { - bindFunc := skipIfPrivate(control.BindToInterfaceFunc(router.InterfaceBindManager(), func() string { - return router.InterfaceMonitor().DefaultInterfaceName() - })) + bindFunc := control.BindToInterfaceFunc(router.InterfaceBindManager(), func(network, address string) string { + return router.InterfaceMonitor().DefaultInterfaceName(M.ParseSocksaddr(address).Addr) + }) dialer.Control = control.Append(dialer.Control, bindFunc) listener.Control = control.Append(listener.Control, bindFunc) } } else if router.DefaultInterface() != "" { - bindFunc := skipIfPrivate(control.BindToInterface(router.InterfaceBindManager(), router.DefaultInterface())) + bindFunc := control.BindToInterface(router.InterfaceBindManager(), router.DefaultInterface()) dialer.Control = control.Append(dialer.Control, bindFunc) listener.Control = control.Append(listener.Control, bindFunc) } diff --git a/common/settings/proxy_darwin.go b/common/settings/proxy_darwin.go index 22d3b7c5..ce8e9cd7 100644 --- a/common/settings/proxy_darwin.go +++ b/common/settings/proxy_darwin.go @@ -1,6 +1,7 @@ package settings import ( + "net/netip" "strings" "github.com/sagernet/sing-box/adapter" @@ -20,7 +21,7 @@ type systemProxy struct { } func (p *systemProxy) update() error { - newInterfaceName := p.monitor.DefaultInterfaceName() + newInterfaceName := p.monitor.DefaultInterfaceName(netip.IPv4Unspecified()) if p.interfaceName == newInterfaceName { return nil } diff --git a/go.mod b/go.mod index 3b39b066..f6e9900f 100644 --- a/go.mod +++ b/go.mod @@ -20,10 +20,10 @@ require ( github.com/pires/go-proxyproto v0.6.2 github.com/sagernet/certmagic v0.0.0-20220819042630-4a57f8b6853a github.com/sagernet/quic-go v0.0.0-20220818150011-de611ab3e2bb - github.com/sagernet/sing v0.0.0-20220825093630-185d87918290 + github.com/sagernet/sing v0.0.0-20220826124916-d4ba8fdfac88 github.com/sagernet/sing-dns v0.0.0-20220822023312-3e086b06d666 github.com/sagernet/sing-shadowsocks v0.0.0-20220819002358-7461bb09a8f6 - github.com/sagernet/sing-tun v0.0.0-20220826042514-409136e64474 + github.com/sagernet/sing-tun v0.0.0-20220826130451-f85debfedb76 github.com/sagernet/sing-vmess v0.0.0-20220811135656-4f3f07acf9c4 github.com/sagernet/smux v0.0.0-20220812084127-e2d085ee3939 github.com/spf13/cobra v1.5.0 diff --git a/go.sum b/go.sum index 8befac08..782a7965 100644 --- a/go.sum +++ b/go.sum @@ -142,14 +142,14 @@ github.com/sagernet/quic-go v0.0.0-20220818150011-de611ab3e2bb h1:wc0yQ+SBn4TaTY github.com/sagernet/quic-go v0.0.0-20220818150011-de611ab3e2bb/go.mod h1:MIccjRKnPTjWwAOpl+AUGWOkzyTd9tERytudxu+1ra4= github.com/sagernet/sing v0.0.0-20220812082120-05f9836bff8f/go.mod h1:QVsS5L/ZA2Q5UhQwLrn0Trw+msNd/NPGEhBKR/ioWiY= github.com/sagernet/sing v0.0.0-20220817130738-ce854cda8522/go.mod h1:QVsS5L/ZA2Q5UhQwLrn0Trw+msNd/NPGEhBKR/ioWiY= -github.com/sagernet/sing v0.0.0-20220825093630-185d87918290 h1:OAt6dFNrGOpgaPgM3uvAdQE0NkGC7AAygqpo8MwryY8= -github.com/sagernet/sing v0.0.0-20220825093630-185d87918290/go.mod h1:kZvzh1VDa/Dg/Bt5WaYKU0jl5ept8KKDpl3Ay4gRtRQ= +github.com/sagernet/sing v0.0.0-20220826124916-d4ba8fdfac88 h1:wxUQfVBqiUtAemytzP9mNjAkSiI0nVsRZBQvCLP8r5g= +github.com/sagernet/sing v0.0.0-20220826124916-d4ba8fdfac88/go.mod h1:kZvzh1VDa/Dg/Bt5WaYKU0jl5ept8KKDpl3Ay4gRtRQ= github.com/sagernet/sing-dns v0.0.0-20220822023312-3e086b06d666 h1:XUTocA/Ek0dFxUX+xJCWMPPFZCn2GC/uLrBjTSr1vHY= github.com/sagernet/sing-dns v0.0.0-20220822023312-3e086b06d666/go.mod h1:eDyH7AJmqBGjZQdQmpZIzlbTREudZuWDExMuGKgjRVM= github.com/sagernet/sing-shadowsocks v0.0.0-20220819002358-7461bb09a8f6 h1:JJfDeYYhWunvtxsU/mOVNTmFQmnzGx9dY034qG6G3g4= github.com/sagernet/sing-shadowsocks v0.0.0-20220819002358-7461bb09a8f6/go.mod h1:EX3RbZvrwAkPI2nuGa78T2iQXmrkT+/VQtskjou42xM= -github.com/sagernet/sing-tun v0.0.0-20220826042514-409136e64474 h1:dy9dLOSTDtig/s5se7cRqIypUlqtcp4+Zw0+XMpORPE= -github.com/sagernet/sing-tun v0.0.0-20220826042514-409136e64474/go.mod h1:zMKRFCEoO6Jp5Yxb2NUTqc+SvAtNVAmzfwArAheJy5g= +github.com/sagernet/sing-tun v0.0.0-20220826130451-f85debfedb76 h1:wwcNrhbcvBPrgD7pENWx5TNnAN+oEd+j/HTnBHV6oFY= +github.com/sagernet/sing-tun v0.0.0-20220826130451-f85debfedb76/go.mod h1:zMKRFCEoO6Jp5Yxb2NUTqc+SvAtNVAmzfwArAheJy5g= github.com/sagernet/sing-vmess v0.0.0-20220811135656-4f3f07acf9c4 h1:2hLETh97+S4WnfMR27XyC7QVU1SH7FTNoCznP229YJU= github.com/sagernet/sing-vmess v0.0.0-20220811135656-4f3f07acf9c4/go.mod h1:82O6gzbxLha/W/jxSVQbsqf2lVdRTjMIgyLug0lpJps= github.com/sagernet/smux v0.0.0-20220812084127-e2d085ee3939 h1:pB1Dh1NbwVrLhQhotr4O4Hs3yhiBzmg3AvnUyYjL4x4= diff --git a/route/router.go b/route/router.go index 162f9b5b..9903af5e 100644 --- a/route/router.go +++ b/route/router.go @@ -262,7 +262,7 @@ func NewRouter(ctx context.Context, logger log.ContextLogger, dnsLogger log.Cont return nil, E.New("auto_detect_interface unsupported on current platform") } interfaceMonitor.RegisterCallback(func() error { - router.logger.Info("updated default interface ", router.interfaceMonitor.DefaultInterfaceName(), ", index ", router.interfaceMonitor.DefaultInterfaceIndex()) + router.logger.Info("updated default interface ", router.interfaceMonitor.DefaultInterfaceName(netip.IPv4Unspecified()), ", index ", router.interfaceMonitor.DefaultInterfaceIndex(netip.IPv4Unspecified())) return nil }) router.interfaceMonitor = interfaceMonitor