From c4660e1aad5cf8f5a89874dc191e8c110c15eb58 Mon Sep 17 00:00:00 2001 From: wwqgtxx Date: Wed, 21 Aug 2024 10:51:49 +0800 Subject: [PATCH] chore: reopen tfo support on windows for golang1.23 maybe broken again when golang1.24 release --- adapter/inbound/listen.go | 14 +++++++++++++- adapter/inbound/listen_unix.go | 23 ----------------------- adapter/inbound/listen_windows.go | 15 --------------- component/dialer/tfo.go | 17 +++++++++++++++++ component/dialer/tfo_unix.go | 25 ------------------------- component/dialer/tfo_windows.go | 15 +++++++-------- go.mod | 2 +- go.sum | 4 ++-- 8 files changed, 40 insertions(+), 75 deletions(-) delete mode 100644 adapter/inbound/listen_unix.go delete mode 100644 adapter/inbound/listen_windows.go delete mode 100644 component/dialer/tfo_unix.go diff --git a/adapter/inbound/listen.go b/adapter/inbound/listen.go index edbccea70..18dc1bc24 100644 --- a/adapter/inbound/listen.go +++ b/adapter/inbound/listen.go @@ -3,10 +3,22 @@ package inbound import ( "context" "net" + + "github.com/metacubex/tfo-go" ) +var ( + lc = tfo.ListenConfig{ + DisableTFO: true, + } +) + +func SetTfo(open bool) { + lc.DisableTFO = !open +} + func SetMPTCP(open bool) { - setMultiPathTCP(getListenConfig(), open) + setMultiPathTCP(&lc.ListenConfig, open) } func ListenContext(ctx context.Context, network, address string) (net.Listener, error) { diff --git a/adapter/inbound/listen_unix.go b/adapter/inbound/listen_unix.go deleted file mode 100644 index bb78adb22..000000000 --- a/adapter/inbound/listen_unix.go +++ /dev/null @@ -1,23 +0,0 @@ -//go:build unix - -package inbound - -import ( - "net" - - "github.com/metacubex/tfo-go" -) - -var ( - lc = tfo.ListenConfig{ - DisableTFO: true, - } -) - -func SetTfo(open bool) { - lc.DisableTFO = !open -} - -func getListenConfig() *net.ListenConfig { - return &lc.ListenConfig -} diff --git a/adapter/inbound/listen_windows.go b/adapter/inbound/listen_windows.go deleted file mode 100644 index a4223e2b5..000000000 --- a/adapter/inbound/listen_windows.go +++ /dev/null @@ -1,15 +0,0 @@ -package inbound - -import ( - "net" -) - -var ( - lc = net.ListenConfig{} -) - -func SetTfo(open bool) {} - -func getListenConfig() *net.ListenConfig { - return &lc -} diff --git a/component/dialer/tfo.go b/component/dialer/tfo.go index bc32b38a7..76fe94d02 100644 --- a/component/dialer/tfo.go +++ b/component/dialer/tfo.go @@ -5,8 +5,12 @@ import ( "io" "net" "time" + + "github.com/metacubex/tfo-go" ) +var DisableTFO = false + type tfoConn struct { net.Conn closed bool @@ -120,3 +124,16 @@ func (c *tfoConn) ReaderReplaceable() bool { func (c *tfoConn) WriterReplaceable() bool { return c.Conn != nil } + +func dialTFO(ctx context.Context, netDialer net.Dialer, network, address string) (net.Conn, error) { + ctx, cancel := context.WithTimeout(context.Background(), DefaultTCPTimeout) + dialer := tfo.Dialer{Dialer: netDialer, DisableTFO: false} + return &tfoConn{ + dialed: make(chan bool, 1), + cancel: cancel, + ctx: ctx, + dialFn: func(ctx context.Context, earlyData []byte) (net.Conn, error) { + return dialer.DialContext(ctx, network, address, earlyData) + }, + }, nil +} diff --git a/component/dialer/tfo_unix.go b/component/dialer/tfo_unix.go deleted file mode 100644 index b8908849e..000000000 --- a/component/dialer/tfo_unix.go +++ /dev/null @@ -1,25 +0,0 @@ -//go:build unix - -package dialer - -import ( - "context" - "net" - - "github.com/metacubex/tfo-go" -) - -const DisableTFO = false - -func dialTFO(ctx context.Context, netDialer net.Dialer, network, address string) (net.Conn, error) { - ctx, cancel := context.WithTimeout(context.Background(), DefaultTCPTimeout) - dialer := tfo.Dialer{Dialer: netDialer, DisableTFO: false} - return &tfoConn{ - dialed: make(chan bool, 1), - cancel: cancel, - ctx: ctx, - dialFn: func(ctx context.Context, earlyData []byte) (net.Conn, error) { - return dialer.DialContext(ctx, network, address, earlyData) - }, - }, nil -} diff --git a/component/dialer/tfo_windows.go b/component/dialer/tfo_windows.go index f1dddcf44..632661186 100644 --- a/component/dialer/tfo_windows.go +++ b/component/dialer/tfo_windows.go @@ -1,12 +1,11 @@ package dialer -import ( - "context" - "net" -) +import "github.com/metacubex/mihomo/constant/features" -const DisableTFO = true - -func dialTFO(ctx context.Context, netDialer net.Dialer, network, address string) (net.Conn, error) { - return netDialer.DialContext(ctx, network, address) +func init() { + // According to MSDN, this option is available since Windows 10, 1607 + // https://msdn.microsoft.com/en-us/library/windows/desktop/ms738596(v=vs.85).aspx + if features.WindowsMajorVersion < 10 || (features.WindowsMajorVersion == 10 && features.WindowsBuildNumber < 14393) { + DisableTFO = true + } } diff --git a/go.mod b/go.mod index 796e09628..cc1c8b635 100644 --- a/go.mod +++ b/go.mod @@ -27,7 +27,7 @@ require ( github.com/metacubex/sing-tun v0.2.7-0.20240729131039-ed03f557dee1 github.com/metacubex/sing-vmess v0.1.9-0.20240719134745-1df6fb20bbf9 github.com/metacubex/sing-wireguard v0.0.0-20240618022557-a6efaa37127a - github.com/metacubex/tfo-go v0.0.0-20240228025757-be1269474a66 + github.com/metacubex/tfo-go v0.0.0-20240821025650-e9be0afd5e7d github.com/metacubex/utls v1.6.6 github.com/miekg/dns v1.1.61 github.com/mroth/weightedrand/v2 v2.1.0 diff --git a/go.sum b/go.sum index a34788609..9e63c337e 100644 --- a/go.sum +++ b/go.sum @@ -120,8 +120,8 @@ github.com/metacubex/sing-vmess v0.1.9-0.20240719134745-1df6fb20bbf9 h1:OAXiCosq github.com/metacubex/sing-vmess v0.1.9-0.20240719134745-1df6fb20bbf9/go.mod h1:olVkD4FChQ5gKMHG4ZzuD7+fMkJY1G8vwOKpRehjrmY= github.com/metacubex/sing-wireguard v0.0.0-20240618022557-a6efaa37127a h1:NpSGclHJUYndUwBmyIpFBSoBVg8PoVX7QQKhYg0DjM0= github.com/metacubex/sing-wireguard v0.0.0-20240618022557-a6efaa37127a/go.mod h1:uY+BYb0UEknLrqvbGcwi9i++KgrKxsurysgI6G1Pveo= -github.com/metacubex/tfo-go v0.0.0-20240228025757-be1269474a66 h1:as/aO/fM8nv4W4pOr9EETP6kV/Oaujk3fUNyQSJK61c= -github.com/metacubex/tfo-go v0.0.0-20240228025757-be1269474a66/go.mod h1:c7bVFM9f5+VzeZ/6Kg77T/jrg1Xp8QpqlSHvG/aXVts= +github.com/metacubex/tfo-go v0.0.0-20240821025650-e9be0afd5e7d h1:j9LtzkYstLFoNvXW824QQeN7Y26uPL5249kzWKbzO9U= +github.com/metacubex/tfo-go v0.0.0-20240821025650-e9be0afd5e7d/go.mod h1:c7bVFM9f5+VzeZ/6Kg77T/jrg1Xp8QpqlSHvG/aXVts= github.com/metacubex/utls v1.6.6 h1:3D12YKHTf2Z41UPhQU2dWerNWJ5TVQD9gKoQ+H+iLC8= github.com/metacubex/utls v1.6.6/go.mod h1:+WLFUnXjcpdxXCnyX25nggw8C6YonZ8zOK2Zm/oRvdo= github.com/miekg/dns v1.1.61 h1:nLxbwF3XxhwVSm8g9Dghm9MHPaUZuqhPiGL+675ZmEs=