-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
183 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package net | ||
|
||
import ( | ||
"net" | ||
"runtime" | ||
"time" | ||
) | ||
|
||
var ( | ||
KeepAliveIdle = 0 * time.Second | ||
KeepAliveInterval = 0 * time.Second | ||
DisableKeepAlive = false | ||
) | ||
|
||
func TCPKeepAlive(c net.Conn) { | ||
if tcp, ok := c.(*net.TCPConn); ok { | ||
if runtime.GOOS == "android" || DisableKeepAlive { | ||
_ = tcp.SetKeepAlive(false) | ||
} else { | ||
tcpKeepAlive(tcp) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
//go:build go1.23 && unix | ||
|
||
package net | ||
|
||
func SupportTCPKeepAliveIdle() bool { | ||
return true | ||
} | ||
|
||
func SupportTCPKeepAliveInterval() bool { | ||
return true | ||
} | ||
|
||
func SupportTCPKeepAliveCount() bool { | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
//go:build go1.23 && windows | ||
|
||
// copy and modify from golang1.23's internal/syscall/windows/version_windows.go | ||
|
||
package net | ||
|
||
import ( | ||
"errors" | ||
"sync" | ||
"syscall" | ||
|
||
"github.com/metacubex/mihomo/constant/features" | ||
|
||
"golang.org/x/sys/windows" | ||
) | ||
|
||
var ( | ||
supportTCPKeepAliveIdle bool | ||
supportTCPKeepAliveInterval bool | ||
supportTCPKeepAliveCount bool | ||
) | ||
|
||
var initTCPKeepAlive = sync.OnceFunc(func() { | ||
s, err := windows.WSASocket(syscall.AF_INET, syscall.SOCK_STREAM, syscall.IPPROTO_TCP, nil, 0, windows.WSA_FLAG_NO_HANDLE_INHERIT) | ||
if err != nil { | ||
// Fallback to checking the Windows version. | ||
major, build := features.WindowsMajorVersion, features.WindowsBuildNumber | ||
supportTCPKeepAliveIdle = major >= 10 && build >= 16299 | ||
supportTCPKeepAliveInterval = major >= 10 && build >= 16299 | ||
supportTCPKeepAliveCount = major >= 10 && build >= 15063 | ||
return | ||
} | ||
defer windows.Closesocket(s) | ||
var optSupported = func(opt int) bool { | ||
err := windows.SetsockoptInt(s, syscall.IPPROTO_TCP, opt, 1) | ||
return !errors.Is(err, syscall.WSAENOPROTOOPT) | ||
} | ||
supportTCPKeepAliveIdle = optSupported(windows.TCP_KEEPIDLE) | ||
supportTCPKeepAliveInterval = optSupported(windows.TCP_KEEPINTVL) | ||
supportTCPKeepAliveCount = optSupported(windows.TCP_KEEPCNT) | ||
}) | ||
|
||
// SupportTCPKeepAliveIdle indicates whether TCP_KEEPIDLE is supported. | ||
// The minimal requirement is Windows 10.0.16299. | ||
func SupportTCPKeepAliveIdle() bool { | ||
initTCPKeepAlive() | ||
return supportTCPKeepAliveIdle | ||
} | ||
|
||
// SupportTCPKeepAliveInterval indicates whether TCP_KEEPINTVL is supported. | ||
// The minimal requirement is Windows 10.0.16299. | ||
func SupportTCPKeepAliveInterval() bool { | ||
initTCPKeepAlive() | ||
return supportTCPKeepAliveInterval | ||
} | ||
|
||
// SupportTCPKeepAliveCount indicates whether TCP_KEEPCNT is supported. | ||
// supports TCP_KEEPCNT. | ||
// The minimal requirement is Windows 10.0.15063. | ||
func SupportTCPKeepAliveCount() bool { | ||
initTCPKeepAlive() | ||
return supportTCPKeepAliveCount | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package dialer | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"syscall" | ||
) | ||
|
||
// SocketControl | ||
// never change type traits because it's used in CFMA | ||
type SocketControl func(network, address string, conn syscall.RawConn) error | ||
|
||
// DefaultSocketHook | ||
// never change type traits because it's used in CFMA | ||
var DefaultSocketHook SocketControl | ||
|
||
func socketHookToToDialer(dialer *net.Dialer) { | ||
addControlToDialer(dialer, func(ctx context.Context, network, address string, c syscall.RawConn) error { | ||
return DefaultSocketHook(network, address, c) | ||
}) | ||
} | ||
|
||
func socketHookToListenConfig(lc *net.ListenConfig) { | ||
addControlToListenConfig(lc, func(ctx context.Context, network, address string, c syscall.RawConn) error { | ||
return DefaultSocketHook(network, address, c) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters