From dfa10d4ebe54da4b8185c48c7c0af59b8ae3a9da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Fri, 5 Aug 2022 16:55:46 +0800 Subject: [PATCH] Add tun support for macOS --- common/dialer/default.go | 2 +- docs/configuration/inbound/tun.md | 4 ++-- docs/configuration/route/index.md | 4 ++-- go.mod | 4 ++-- go.sum | 8 ++++---- inbound/tun.go | 2 +- inbound/tun_stub.go | 2 +- route/router.go | 2 +- route/router_dns.go | 2 +- test/go.mod | 4 ++-- test/go.sum | 8 ++++---- 11 files changed, 21 insertions(+), 21 deletions(-) diff --git a/common/dialer/default.go b/common/dialer/default.go index 5c0fc772..837abe4c 100644 --- a/common/dialer/default.go +++ b/common/dialer/default.go @@ -18,7 +18,7 @@ import ( var warnBindInterfaceOnUnsupportedPlatform = warning.New( func() bool { - return !(C.IsLinux || C.IsWindows) + return !(C.IsLinux || C.IsWindows || C.IsDarwin) }, "outbound option `bind_interface` is only supported on Linux and Windows", ) diff --git a/docs/configuration/inbound/tun.md b/docs/configuration/inbound/tun.md index 5046b8c7..6de56ab5 100644 --- a/docs/configuration/inbound/tun.md +++ b/docs/configuration/inbound/tun.md @@ -1,6 +1,6 @@ !!! error "" - Linux and Windows only + Only supported on Linux, Windows and macOS. ### Structure @@ -52,7 +52,7 @@ Set the default route to the Tun. #### endpoint_independent_nat -Enabled endpoint-independent NAT. +Enable endpoint-independent NAT. Performance may degrade slightly, so it is not recommended to enable on when it is not needed. diff --git a/docs/configuration/route/index.md b/docs/configuration/route/index.md index 5db96fd0..ebdd638a 100644 --- a/docs/configuration/route/index.md +++ b/docs/configuration/route/index.md @@ -30,7 +30,7 @@ Default outbound tag. the first outbound will be used if empty. !!! error "" - Only supported on Linux and Windows. + Only supported on Linux, Windows and macOS. Bind outbound connections to the default NIC by default to prevent routing loops under Tun. @@ -40,7 +40,7 @@ Takes no effect if `outbound.bind_interface` is set. !!! error "" - Only supported on Linux and Windows. + Only supported on Linux, Windows and macOS. Bind outbound connections to the specified NIC by default to prevent routing loops under Tun. diff --git a/go.mod b/go.mod index d5a8968a..74aa032d 100644 --- a/go.mod +++ b/go.mod @@ -13,10 +13,10 @@ require ( github.com/hashicorp/yamux v0.1.1 github.com/logrusorgru/aurora v2.0.3+incompatible github.com/oschwald/maxminddb-golang v1.9.0 - github.com/sagernet/sing v0.0.0-20220804023557-9c64b40e7050 + github.com/sagernet/sing v0.0.0-20220805082239-f616c4f12600 github.com/sagernet/sing-dns v0.0.0-20220803121532-9e1ffb850d91 github.com/sagernet/sing-shadowsocks v0.0.0-20220801112336-a91eacdd01e1 - github.com/sagernet/sing-tun v0.0.0-20220804154459-7ee0d19103d2 + github.com/sagernet/sing-tun v0.0.0-20220805085423-4a83493b40e2 github.com/sagernet/sing-vmess v0.0.0-20220804023624-e829b41c84c2 github.com/spf13/cobra v1.5.0 github.com/stretchr/testify v1.8.0 diff --git a/go.sum b/go.sum index 905c66cc..5ceb8bbf 100644 --- a/go.sum +++ b/go.sum @@ -149,14 +149,14 @@ github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/sagernet/netlink v0.0.0-20220803045538-bdac49abf805 h1:hE+vtsjBCCPmxkRz9jZA+CicHgVkDT6H+Av5ZzskVxs= github.com/sagernet/netlink v0.0.0-20220803045538-bdac49abf805/go.mod h1:xLnfdiJbSp8rNqYEdIW/6eDO4mVoogml14Bh2hSiFpM= -github.com/sagernet/sing v0.0.0-20220804023557-9c64b40e7050 h1:Q6PV3tlP7htm9jgebFryXT4HFvEHvb5QHEcuWrf4E2o= -github.com/sagernet/sing v0.0.0-20220804023557-9c64b40e7050/go.mod h1:QVsS5L/ZA2Q5UhQwLrn0Trw+msNd/NPGEhBKR/ioWiY= +github.com/sagernet/sing v0.0.0-20220805082239-f616c4f12600 h1:ZyNW19K2vuRxEfjS7fFp4Y2lDFh7sfxzivGalMQdTjw= +github.com/sagernet/sing v0.0.0-20220805082239-f616c4f12600/go.mod h1:QVsS5L/ZA2Q5UhQwLrn0Trw+msNd/NPGEhBKR/ioWiY= github.com/sagernet/sing-dns v0.0.0-20220803121532-9e1ffb850d91 h1:jxt2PYixIkK2i7nUGW3f+PzJagEZcbNyQddBWGuqNnw= github.com/sagernet/sing-dns v0.0.0-20220803121532-9e1ffb850d91/go.mod h1:T77zZdE2Cm6VqnFumrpwsq+kxYsbq+vWDhmjtdSl/oM= github.com/sagernet/sing-shadowsocks v0.0.0-20220801112336-a91eacdd01e1 h1:RYvOc69eSNMN0dwVugrDts41Nn7Ar/C/n/fvytvFcp4= github.com/sagernet/sing-shadowsocks v0.0.0-20220801112336-a91eacdd01e1/go.mod h1:NqZjiXszgVCMQ4gVDa2V+drhS8NMfGqUqDF86EacEFc= -github.com/sagernet/sing-tun v0.0.0-20220804154459-7ee0d19103d2 h1:har8hmVNhGxp14zLNAoGrgfzgxZQn0KTYJDfJudj0RU= -github.com/sagernet/sing-tun v0.0.0-20220804154459-7ee0d19103d2/go.mod h1:K1Hfxaa/1zsxZix3ats3k1TJftVwK0l4OoRnUjjhi0g= +github.com/sagernet/sing-tun v0.0.0-20220805085423-4a83493b40e2 h1:mdYyKefLtjM/5P/poakkBXvqpVOeOOxjIq2eiH3qol8= +github.com/sagernet/sing-tun v0.0.0-20220805085423-4a83493b40e2/go.mod h1:K1Hfxaa/1zsxZix3ats3k1TJftVwK0l4OoRnUjjhi0g= github.com/sagernet/sing-vmess v0.0.0-20220804023624-e829b41c84c2 h1:C8sc2MYiNx0O7uQ0nieJWq5qYeIHj20XHFWPlcgoQeY= github.com/sagernet/sing-vmess v0.0.0-20220804023624-e829b41c84c2/go.mod h1:bNXBqSWYaG3ePl6u0xQY5zneE+ZKa3683ZpuE8S1M1w= github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= diff --git a/inbound/tun.go b/inbound/tun.go index 643bf5bd..e0a370fe 100644 --- a/inbound/tun.go +++ b/inbound/tun.go @@ -1,4 +1,4 @@ -//go:build (linux || windows) && !no_gvisor +//go:build (linux || windows || darwin) && !no_gvisor package inbound diff --git a/inbound/tun_stub.go b/inbound/tun_stub.go index 76ff9727..41b1073b 100644 --- a/inbound/tun_stub.go +++ b/inbound/tun_stub.go @@ -1,4 +1,4 @@ -//go:build !(linux || windows) || no_gvisor +//go:build !(linux || windows || darwin) || no_gvisor package inbound diff --git a/route/router.go b/route/router.go index ce791616..041061b0 100644 --- a/route/router.go +++ b/route/router.go @@ -39,7 +39,7 @@ import ( var warnDefaultInterfaceOnUnsupportedPlatform = warning.New( func() bool { - return !(C.IsLinux || C.IsWindows) + return !(C.IsLinux || C.IsWindows || C.IsDarwin) }, "route option `default_mark` is only supported on Linux and Windows", ) diff --git a/route/router_dns.go b/route/router_dns.go index 63a35e77..cbdb3997 100644 --- a/route/router_dns.go +++ b/route/router_dns.go @@ -25,7 +25,7 @@ func (r *Router) Exchange(ctx context.Context, message *dnsmessage.Message) (*dn if err != nil && len(message.Questions) > 0 { r.dnsLogger.ErrorContext(ctx, E.Cause(err, "exchange failed for ", message.Questions[0].Name.String())) } - if response != nil { + if len(message.Questions) > 0 && response != nil { LogDNSAnswers(r.dnsLogger, ctx, message.Questions[0].Name.String(), response.Answers) } return response, err diff --git a/test/go.mod b/test/go.mod index a59711e6..46b05e51 100644 --- a/test/go.mod +++ b/test/go.mod @@ -10,7 +10,7 @@ require ( github.com/docker/docker v20.10.17+incompatible github.com/docker/go-connections v0.4.0 github.com/gofrs/uuid v4.2.0+incompatible - github.com/sagernet/sing v0.0.0-20220804023557-9c64b40e7050 + github.com/sagernet/sing v0.0.0-20220805082239-f616c4f12600 github.com/sagernet/sing-shadowsocks v0.0.0-20220801112336-a91eacdd01e1 github.com/spyzhov/ajson v0.7.1 github.com/stretchr/testify v1.8.0 @@ -54,7 +54,7 @@ require ( github.com/pmezard/go-difflib v1.0.0 // indirect github.com/sagernet/netlink v0.0.0-20220803045538-bdac49abf805 // indirect github.com/sagernet/sing-dns v0.0.0-20220803121532-9e1ffb850d91 // indirect - github.com/sagernet/sing-tun v0.0.0-20220804154459-7ee0d19103d2 // indirect + github.com/sagernet/sing-tun v0.0.0-20220805085423-4a83493b40e2 // indirect github.com/sagernet/sing-vmess v0.0.0-20220804023624-e829b41c84c2 // indirect github.com/sirupsen/logrus v1.8.1 // indirect github.com/vishvananda/netns v0.0.0-20211101163701-50045581ed74 // indirect diff --git a/test/go.sum b/test/go.sum index c66afcca..7d13319c 100644 --- a/test/go.sum +++ b/test/go.sum @@ -174,14 +174,14 @@ github.com/prometheus/procfs v0.0.0-20180725123919-05ee40e3a273/go.mod h1:c3At6R github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= github.com/sagernet/netlink v0.0.0-20220803045538-bdac49abf805 h1:hE+vtsjBCCPmxkRz9jZA+CicHgVkDT6H+Av5ZzskVxs= github.com/sagernet/netlink v0.0.0-20220803045538-bdac49abf805/go.mod h1:xLnfdiJbSp8rNqYEdIW/6eDO4mVoogml14Bh2hSiFpM= -github.com/sagernet/sing v0.0.0-20220804023557-9c64b40e7050 h1:Q6PV3tlP7htm9jgebFryXT4HFvEHvb5QHEcuWrf4E2o= -github.com/sagernet/sing v0.0.0-20220804023557-9c64b40e7050/go.mod h1:QVsS5L/ZA2Q5UhQwLrn0Trw+msNd/NPGEhBKR/ioWiY= +github.com/sagernet/sing v0.0.0-20220805082239-f616c4f12600 h1:ZyNW19K2vuRxEfjS7fFp4Y2lDFh7sfxzivGalMQdTjw= +github.com/sagernet/sing v0.0.0-20220805082239-f616c4f12600/go.mod h1:QVsS5L/ZA2Q5UhQwLrn0Trw+msNd/NPGEhBKR/ioWiY= github.com/sagernet/sing-dns v0.0.0-20220803121532-9e1ffb850d91 h1:jxt2PYixIkK2i7nUGW3f+PzJagEZcbNyQddBWGuqNnw= github.com/sagernet/sing-dns v0.0.0-20220803121532-9e1ffb850d91/go.mod h1:T77zZdE2Cm6VqnFumrpwsq+kxYsbq+vWDhmjtdSl/oM= github.com/sagernet/sing-shadowsocks v0.0.0-20220801112336-a91eacdd01e1 h1:RYvOc69eSNMN0dwVugrDts41Nn7Ar/C/n/fvytvFcp4= github.com/sagernet/sing-shadowsocks v0.0.0-20220801112336-a91eacdd01e1/go.mod h1:NqZjiXszgVCMQ4gVDa2V+drhS8NMfGqUqDF86EacEFc= -github.com/sagernet/sing-tun v0.0.0-20220804154459-7ee0d19103d2 h1:har8hmVNhGxp14zLNAoGrgfzgxZQn0KTYJDfJudj0RU= -github.com/sagernet/sing-tun v0.0.0-20220804154459-7ee0d19103d2/go.mod h1:K1Hfxaa/1zsxZix3ats3k1TJftVwK0l4OoRnUjjhi0g= +github.com/sagernet/sing-tun v0.0.0-20220805085423-4a83493b40e2 h1:mdYyKefLtjM/5P/poakkBXvqpVOeOOxjIq2eiH3qol8= +github.com/sagernet/sing-tun v0.0.0-20220805085423-4a83493b40e2/go.mod h1:K1Hfxaa/1zsxZix3ats3k1TJftVwK0l4OoRnUjjhi0g= github.com/sagernet/sing-vmess v0.0.0-20220804023624-e829b41c84c2 h1:C8sc2MYiNx0O7uQ0nieJWq5qYeIHj20XHFWPlcgoQeY= github.com/sagernet/sing-vmess v0.0.0-20220804023624-e829b41c84c2/go.mod h1:bNXBqSWYaG3ePl6u0xQY5zneE+ZKa3683ZpuE8S1M1w= github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=