1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-10 18:00:43 +00:00

simplify tcp network detection

This commit is contained in:
Darien Raymond 2018-09-11 10:03:26 +02:00
parent 682b28cbda
commit 1e4547e262
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
4 changed files with 35 additions and 28 deletions

View File

@ -1 +1,10 @@
package internet
func isTCPSocket(network string) bool {
switch network {
case "tcp", "tcp4", "tcp6":
return true
default:
return false
}
}

View File

@ -1,7 +1,6 @@
package internet
import (
"strings"
"syscall"
)
@ -12,7 +11,7 @@ const (
)
func applyOutboundSocketOptions(network string, address string, fd uintptr, config *SocketConfig) error {
if strings.HasPrefix(network, "tcp") {
if isTCPSocket(network) {
switch config.Tfo {
case SocketConfig_Enable:
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 {
if strings.HasPrefix(network, "tcp") {
if isTCPSocket(network) {
switch config.Tfo {
case SocketConfig_Enable:
if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, TCP_FASTOPEN, TCP_FASTOPEN_SERVER); err != nil {

View File

@ -1,7 +1,6 @@
package internet
import (
"strings"
"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 {
case SocketConfig_Enable:
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 {
if strings.HasPrefix(network, "tcp") {
if isTCPSocket(network) {
switch config.Tfo {
case SocketConfig_Enable:
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_TCP, TCP_FASTOPEN, 1); err != nil {

View File

@ -1,7 +1,6 @@
package internet
import (
"strings"
"syscall"
)
@ -9,34 +8,35 @@ const (
TCP_FASTOPEN = 15
)
func applyOutboundSocketOptions(network string, address string, fd uintptr, config *SocketConfig) error {
if strings.HasPrefix(network, "tcp") {
switch config.Tfo {
case SocketConfig_Enable:
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
}
func setTFO(fd syscall.Handle, settings SocketConfig_TCPFastOpenState) error {
switch settings {
case SocketConfig_Enable:
if err := syscall.SetsockoptInt(fd, syscall.IPPROTO_TCP, TCP_FASTOPEN, 1); 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
}
func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig) error {
if strings.HasPrefix(network, "tcp") {
switch config.Tfo {
case SocketConfig_Enable:
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
}
if isTCPSocket(network) {
if err := setTFO(syscall.Hanle(fd), config.Tfo); err != nil {
return err
}
}