From fc82a32a483cc9bd2bc46ad6dee688b830c8db14 Mon Sep 17 00:00:00 2001 From: wwqgtxx Date: Sun, 12 May 2024 15:52:10 +0800 Subject: [PATCH] fix: `system` tun stack not working in win7 --- go.mod | 2 +- go.sum | 4 ++-- listener/sing_tun/server.go | 4 ++++ listener/sing_tun/server_windows.go | 8 ++++++++ 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 0769a2123..904a9274f 100644 --- a/go.mod +++ b/go.mod @@ -23,7 +23,7 @@ require ( github.com/metacubex/sing-quic v0.0.0-20240501013754-2a2b0f262f9f github.com/metacubex/sing-shadowsocks v0.2.6 github.com/metacubex/sing-shadowsocks2 v0.2.0 - github.com/metacubex/sing-tun v0.2.6 + github.com/metacubex/sing-tun v0.2.7-0.20240512075008-89e7c6208eec github.com/metacubex/sing-vmess v0.1.9-0.20231207122118-72303677451f github.com/metacubex/sing-wireguard v0.0.0-20240321042214-224f96122a63 github.com/metacubex/tfo-go v0.0.0-20240228025757-be1269474a66 diff --git a/go.sum b/go.sum index a4d90759f..ae1999635 100644 --- a/go.sum +++ b/go.sum @@ -114,8 +114,8 @@ github.com/metacubex/sing-shadowsocks v0.2.6 h1:6oEB3QcsFYnNiFeoevcXrCwJ3sAablwV github.com/metacubex/sing-shadowsocks v0.2.6/go.mod h1:zIkMeSnb8Mbf4hdqhw0pjzkn1d99YJ3JQm/VBg5WMTg= github.com/metacubex/sing-shadowsocks2 v0.2.0 h1:hqwT/AfI5d5UdPefIzR6onGHJfDXs5zgOM5QSgaM/9A= github.com/metacubex/sing-shadowsocks2 v0.2.0/go.mod h1:LCKF6j1P94zN8ZS+LXRK1gmYTVGB3squivBSXAFnOg8= -github.com/metacubex/sing-tun v0.2.6 h1:frc58BqnIClqcC9KcYBfVAn5bgO6WW1ANKvZW3/HYAQ= -github.com/metacubex/sing-tun v0.2.6/go.mod h1:4VsMwZH1IlgPGFK1ZbBomZ/B2MYkTgs2+gnBAr5GOIo= +github.com/metacubex/sing-tun v0.2.7-0.20240512075008-89e7c6208eec h1:K4Wq3GOdLZ/xcqwyzAt4kmYQrjokyKQ3u/Xh5Yft14U= +github.com/metacubex/sing-tun v0.2.7-0.20240512075008-89e7c6208eec/go.mod h1:4VsMwZH1IlgPGFK1ZbBomZ/B2MYkTgs2+gnBAr5GOIo= github.com/metacubex/sing-vmess v0.1.9-0.20231207122118-72303677451f h1:QjXrHKbTMBip/C+R79bvbfr42xH1gZl3uFb0RELdZiQ= github.com/metacubex/sing-vmess v0.1.9-0.20231207122118-72303677451f/go.mod h1:olVkD4FChQ5gKMHG4ZzuD7+fMkJY1G8vwOKpRehjrmY= github.com/metacubex/sing-wireguard v0.0.0-20240321042214-224f96122a63 h1:AGyIB55UfQm/0ZH0HtQO9u3l//yjtHUpjeRjjPGfGRI= diff --git a/listener/sing_tun/server.go b/listener/sing_tun/server.go index 7cd9fc723..2dc6524ee 100644 --- a/listener/sing_tun/server.go +++ b/listener/sing_tun/server.go @@ -20,12 +20,14 @@ import ( tun "github.com/metacubex/sing-tun" "github.com/sagernet/sing/common" + "github.com/sagernet/sing/common/control" E "github.com/sagernet/sing/common/exceptions" F "github.com/sagernet/sing/common/format" "github.com/sagernet/sing/common/ranges" ) var InterfaceName = "Meta" +var EnforceBindInterface = false type Listener struct { closed bool @@ -263,6 +265,8 @@ func New(options LC.Tun, tunnel C.Tunnel, additions ...inbound.Addition) (l *Lis UDPTimeout: udpTimeout, Handler: handler, Logger: log.SingLogger, + InterfaceFinder: control.DefaultInterfaceFinder(), + EnforceBindInterface: EnforceBindInterface, } if options.FileDescriptor > 0 { diff --git a/listener/sing_tun/server_windows.go b/listener/sing_tun/server_windows.go index 8da212879..4bc048351 100644 --- a/listener/sing_tun/server_windows.go +++ b/listener/sing_tun/server_windows.go @@ -6,6 +6,8 @@ import ( "github.com/metacubex/mihomo/log" tun "github.com/metacubex/sing-tun" + + "golang.org/x/sys/windows" ) func tunNew(options tun.Options) (tunIf tun.Tun, err error) { @@ -27,4 +29,10 @@ func tunNew(options tun.Options) (tunIf tun.Tun, err error) { func init() { tun.TunnelType = InterfaceName + + majorVersion, _, _ := windows.RtlGetNtVersionNumbers() + if majorVersion < 10 { + // to resolve "bind: The requested address is not valid in its context" + EnforceBindInterface = true + } }