From 3ae4014b393ea1dccb04f1f93c389cb1ab65b091 Mon Sep 17 00:00:00 2001
From: wwqgtxx <wwqgtxx@gmail.com>
Date: Sun, 12 May 2024 20:44:12 +0800
Subject: [PATCH] chore: disable tfo when lower than Windows 10.0.14393

---
 component/dialer/dialer.go      |  2 +-
 component/dialer/tfo.go         |  2 ++
 component/dialer/tfo_windows.go | 11 +++++++++++
 3 files changed, 14 insertions(+), 1 deletion(-)
 create mode 100644 component/dialer/tfo_windows.go

diff --git a/component/dialer/dialer.go b/component/dialer/dialer.go
index 8fb5a0a6..c21e638e 100644
--- a/component/dialer/dialer.go
+++ b/component/dialer/dialer.go
@@ -164,7 +164,7 @@ func dialContext(ctx context.Context, network string, destination netip.Addr, po
 	if opt.mpTcp {
 		setMultiPathTCP(dialer)
 	}
-	if opt.tfo {
+	if opt.tfo && !DisableTFO {
 		return dialTFO(ctx, *dialer, network, address)
 	}
 	return dialer.DialContext(ctx, network, address)
diff --git a/component/dialer/tfo.go b/component/dialer/tfo.go
index 228e9689..76fe94d0 100644
--- a/component/dialer/tfo.go
+++ b/component/dialer/tfo.go
@@ -9,6 +9,8 @@ import (
 	"github.com/metacubex/tfo-go"
 )
 
+var DisableTFO = false
+
 type tfoConn struct {
 	net.Conn
 	closed bool
diff --git a/component/dialer/tfo_windows.go b/component/dialer/tfo_windows.go
new file mode 100644
index 00000000..63266118
--- /dev/null
+++ b/component/dialer/tfo_windows.go
@@ -0,0 +1,11 @@
+package dialer
+
+import "github.com/metacubex/mihomo/constant/features"
+
+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
+	}
+}