mirror of https://github.com/v2ray/v2ray-core
simplify tcp network detection
parent
682b28cbda
commit
1e4547e262
|
@ -1 +1,10 @@
|
||||||
package internet
|
package internet
|
||||||
|
|
||||||
|
func isTCPSocket(network string) bool {
|
||||||
|
switch network {
|
||||||
|
case "tcp", "tcp4", "tcp6":
|
||||||
|
return true
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package internet
|
package internet
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strings"
|
|
||||||
"syscall"
|
"syscall"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -12,7 +11,7 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
func applyOutboundSocketOptions(network string, address string, fd uintptr, config *SocketConfig) error {
|
func applyOutboundSocketOptions(network string, address string, fd uintptr, config *SocketConfig) error {
|
||||||
if strings.HasPrefix(network, "tcp") {
|
if isTCPSocket(network) {
|
||||||
switch config.Tfo {
|
switch config.Tfo {
|
||||||
case SocketConfig_Enable:
|
case SocketConfig_Enable:
|
||||||
if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, TCP_FASTOPEN, TCP_FASTOPEN_CLIENT); err != nil {
|
if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, TCP_FASTOPEN, TCP_FASTOPEN_CLIENT); err != nil {
|
||||||
|
@ -29,7 +28,7 @@ func applyOutboundSocketOptions(network string, address string, fd uintptr, conf
|
||||||
}
|
}
|
||||||
|
|
||||||
func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig) error {
|
func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig) error {
|
||||||
if strings.HasPrefix(network, "tcp") {
|
if isTCPSocket(network) {
|
||||||
switch config.Tfo {
|
switch config.Tfo {
|
||||||
case SocketConfig_Enable:
|
case SocketConfig_Enable:
|
||||||
if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, TCP_FASTOPEN, TCP_FASTOPEN_SERVER); err != nil {
|
if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, TCP_FASTOPEN, TCP_FASTOPEN_SERVER); err != nil {
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package internet
|
package internet
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strings"
|
|
||||||
"syscall"
|
"syscall"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -19,7 +18,7 @@ func applyOutboundSocketOptions(network string, address string, fd uintptr, conf
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if strings.HasPrefix(network, "tcp") {
|
if isTCPSocket(network) {
|
||||||
switch config.Tfo {
|
switch config.Tfo {
|
||||||
case SocketConfig_Enable:
|
case SocketConfig_Enable:
|
||||||
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_TCP, TCP_FASTOPEN_CONNECT, 1); err != nil {
|
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_TCP, TCP_FASTOPEN_CONNECT, 1); err != nil {
|
||||||
|
@ -36,7 +35,7 @@ func applyOutboundSocketOptions(network string, address string, fd uintptr, conf
|
||||||
}
|
}
|
||||||
|
|
||||||
func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig) error {
|
func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig) error {
|
||||||
if strings.HasPrefix(network, "tcp") {
|
if isTCPSocket(network) {
|
||||||
switch config.Tfo {
|
switch config.Tfo {
|
||||||
case SocketConfig_Enable:
|
case SocketConfig_Enable:
|
||||||
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_TCP, TCP_FASTOPEN, 1); err != nil {
|
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_TCP, TCP_FASTOPEN, 1); err != nil {
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package internet
|
package internet
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strings"
|
|
||||||
"syscall"
|
"syscall"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -9,34 +8,35 @@ const (
|
||||||
TCP_FASTOPEN = 15
|
TCP_FASTOPEN = 15
|
||||||
)
|
)
|
||||||
|
|
||||||
func applyOutboundSocketOptions(network string, address string, fd uintptr, config *SocketConfig) error {
|
func setTFO(fd syscall.Handle, settings SocketConfig_TCPFastOpenState) error {
|
||||||
if strings.HasPrefix(network, "tcp") {
|
switch settings {
|
||||||
switch config.Tfo {
|
case SocketConfig_Enable:
|
||||||
case SocketConfig_Enable:
|
if err := syscall.SetsockoptInt(fd, syscall.IPPROTO_TCP, TCP_FASTOPEN, 1); err != nil {
|
||||||
if err := syscall.SetsockoptInt(syscall.Handle(fd), syscall.IPPROTO_TCP, TCP_FASTOPEN, 1); err != nil {
|
return err
|
||||||
return err
|
|
||||||
}
|
|
||||||
case SocketConfig_Disable:
|
|
||||||
if err := syscall.SetsockoptInt(syscall.Handle(fd), syscall.IPPROTO_TCP, TCP_FASTOPEN, 0); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
case SocketConfig_Disable:
|
||||||
|
if err := syscall.SetsockoptInt(fd, syscall.IPPROTO_TCP, TCP_FASTOPEN, 0); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func applyOutboundSocketOptions(network string, address string, fd uintptr, config *SocketConfig) error {
|
||||||
|
if isTCPSocket(network) {
|
||||||
|
if err := setTFO(syscall.Hanle(fd), config.Tfo); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig) error {
|
func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig) error {
|
||||||
if strings.HasPrefix(network, "tcp") {
|
if isTCPSocket(network) {
|
||||||
switch config.Tfo {
|
if err := setTFO(syscall.Hanle(fd), config.Tfo); err != nil {
|
||||||
case SocketConfig_Enable:
|
return err
|
||||||
if err := syscall.SetsockoptInt(syscall.Handle(fd), syscall.IPPROTO_TCP, TCP_FASTOPEN, 1); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
case SocketConfig_Disable:
|
|
||||||
if err := syscall.SetsockoptInt(syscall.Handle(fd), syscall.IPPROTO_TCP, TCP_FASTOPEN, 0); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue