2020-11-25 14:01:53 +03:00
|
|
|
package internet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
2024-07-09 19:19:31 +03:00
|
|
|
"strconv"
|
2020-11-25 14:01:53 +03:00
|
|
|
"syscall"
|
|
|
|
|
2024-06-29 21:32:57 +03:00
|
|
|
"github.com/xtls/xray-core/common/errors"
|
2020-11-25 14:01:53 +03:00
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
)
|
|
|
|
|
|
|
|
func bindAddr(fd uintptr, ip []byte, port uint32) error {
|
|
|
|
setReuseAddr(fd)
|
|
|
|
setReusePort(fd)
|
|
|
|
|
|
|
|
var sockaddr syscall.Sockaddr
|
|
|
|
|
|
|
|
switch len(ip) {
|
|
|
|
case net.IPv4len:
|
|
|
|
a4 := &syscall.SockaddrInet4{
|
|
|
|
Port: int(port),
|
|
|
|
}
|
|
|
|
copy(a4.Addr[:], ip)
|
|
|
|
sockaddr = a4
|
|
|
|
case net.IPv6len:
|
|
|
|
a6 := &syscall.SockaddrInet6{
|
|
|
|
Port: int(port),
|
|
|
|
}
|
|
|
|
copy(a6.Addr[:], ip)
|
|
|
|
sockaddr = a6
|
|
|
|
default:
|
2024-06-29 21:32:57 +03:00
|
|
|
return errors.New("unexpected length of ip")
|
2020-11-25 14:01:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return syscall.Bind(int(fd), sockaddr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func applyOutboundSocketOptions(network string, address string, fd uintptr, config *SocketConfig) error {
|
|
|
|
if config.Mark != 0 {
|
|
|
|
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_MARK, int(config.Mark)); err != nil {
|
2024-06-29 21:32:57 +03:00
|
|
|
return errors.New("failed to set SO_MARK").Base(err)
|
2020-11-25 14:01:53 +03:00
|
|
|
}
|
|
|
|
}
|
2023-03-09 16:51:16 +03:00
|
|
|
|
2023-03-17 08:17:01 +03:00
|
|
|
if config.Interface != "" {
|
|
|
|
if err := syscall.BindToDevice(int(fd), config.Interface); err != nil {
|
2024-06-29 21:32:57 +03:00
|
|
|
return errors.New("failed to set Interface").Base(err)
|
2023-03-17 08:17:01 +03:00
|
|
|
}
|
|
|
|
}
|
2020-11-25 14:01:53 +03:00
|
|
|
|
|
|
|
if isTCPSocket(network) {
|
2021-03-30 19:42:02 +03:00
|
|
|
tfo := config.ParseTFOValue()
|
2021-03-06 17:45:12 +03:00
|
|
|
if tfo > 0 {
|
|
|
|
tfo = 1
|
|
|
|
}
|
|
|
|
if tfo >= 0 {
|
2023-03-12 12:44:41 +03:00
|
|
|
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_TCP, unix.TCP_FASTOPEN_CONNECT, tfo); err != nil {
|
2024-06-29 21:32:57 +03:00
|
|
|
return errors.New("failed to set TCP_FASTOPEN_CONNECT", tfo).Base(err)
|
2020-11-25 14:01:53 +03:00
|
|
|
}
|
|
|
|
}
|
2021-10-12 18:39:08 +03:00
|
|
|
|
2022-07-31 16:55:40 +03:00
|
|
|
if config.TcpKeepAliveInterval > 0 || config.TcpKeepAliveIdle > 0 {
|
|
|
|
if config.TcpKeepAliveInterval > 0 {
|
|
|
|
if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, syscall.TCP_KEEPINTVL, int(config.TcpKeepAliveInterval)); err != nil {
|
2024-06-29 21:32:57 +03:00
|
|
|
return errors.New("failed to set TCP_KEEPINTVL", err)
|
2022-07-31 16:55:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if config.TcpKeepAliveIdle > 0 {
|
|
|
|
if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, syscall.TCP_KEEPIDLE, int(config.TcpKeepAliveIdle)); err != nil {
|
2024-06-29 21:32:57 +03:00
|
|
|
return errors.New("failed to set TCP_KEEPIDLE", err)
|
2022-07-31 16:55:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, 1); err != nil {
|
2024-06-29 21:32:57 +03:00
|
|
|
return errors.New("failed to set SO_KEEPALIVE", err)
|
2022-07-31 16:55:40 +03:00
|
|
|
}
|
|
|
|
} else if config.TcpKeepAliveInterval < 0 || config.TcpKeepAliveIdle < 0 {
|
|
|
|
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, 0); err != nil {
|
2024-06-29 21:32:57 +03:00
|
|
|
return errors.New("failed to unset SO_KEEPALIVE", err)
|
2021-10-12 18:39:08 +03:00
|
|
|
}
|
|
|
|
}
|
2022-10-10 20:13:50 +03:00
|
|
|
|
2022-12-26 03:37:35 +03:00
|
|
|
if config.TcpCongestion != "" {
|
|
|
|
if err := syscall.SetsockoptString(int(fd), syscall.SOL_TCP, syscall.TCP_CONGESTION, config.TcpCongestion); err != nil {
|
2024-06-29 21:32:57 +03:00
|
|
|
return errors.New("failed to set TCP_CONGESTION", err)
|
2022-12-26 03:37:35 +03:00
|
|
|
}
|
|
|
|
}
|
2023-03-09 16:51:16 +03:00
|
|
|
|
|
|
|
if config.TcpWindowClamp > 0 {
|
2023-03-17 08:17:01 +03:00
|
|
|
if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, syscall.TCP_WINDOW_CLAMP, int(config.TcpWindowClamp)); err != nil {
|
2024-06-29 21:32:57 +03:00
|
|
|
return errors.New("failed to set TCP_WINDOW_CLAMP", err)
|
2023-03-17 08:17:01 +03:00
|
|
|
}
|
|
|
|
}
|
2023-03-12 12:44:41 +03:00
|
|
|
|
|
|
|
if config.TcpUserTimeout > 0 {
|
|
|
|
if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, unix.TCP_USER_TIMEOUT, int(config.TcpUserTimeout)); err != nil {
|
2024-06-29 21:32:57 +03:00
|
|
|
return errors.New("failed to set TCP_USER_TIMEOUT", err)
|
2023-03-12 12:44:41 +03:00
|
|
|
}
|
|
|
|
}
|
2023-04-30 03:03:30 +03:00
|
|
|
|
|
|
|
if config.TcpMaxSeg > 0 {
|
|
|
|
if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, unix.TCP_MAXSEG, int(config.TcpMaxSeg)); err != nil {
|
2024-06-29 21:32:57 +03:00
|
|
|
return errors.New("failed to set TCP_MAXSEG", err)
|
2023-04-30 03:03:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-22 05:59:58 +03:00
|
|
|
if config.TcpNoDelay {
|
|
|
|
if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, unix.TCP_NODELAY, 1); err != nil {
|
2024-06-29 21:32:57 +03:00
|
|
|
return errors.New("failed to set TCP_NODELAY", err)
|
2023-05-22 05:59:58 +03:00
|
|
|
}
|
|
|
|
}
|
2024-07-09 19:19:31 +03:00
|
|
|
if len(config.CustomSockopt) > 0 {
|
|
|
|
for _, custom := range config.CustomSockopt {
|
|
|
|
var level = 0x6 // default TCP
|
|
|
|
var opt int
|
|
|
|
if len(custom.Opt) == 0 {
|
|
|
|
return errors.New("No opt!")
|
|
|
|
} else {
|
|
|
|
opt, _ = strconv.Atoi(custom.Opt)
|
|
|
|
}
|
|
|
|
if custom.Level != "" {
|
|
|
|
level, _ = strconv.Atoi(custom.Level)
|
|
|
|
}
|
|
|
|
if custom.Type == "int" {
|
|
|
|
value, _ := strconv.Atoi(custom.Value)
|
|
|
|
if err := syscall.SetsockoptInt(int(fd), level, opt, value); err != nil {
|
|
|
|
return errors.New("failed to set CustomSockoptInt", opt, value, err)
|
|
|
|
}
|
|
|
|
} else if custom.Type == "str" {
|
|
|
|
if err := syscall.SetsockoptString(int(fd), level, opt, custom.Value); err != nil {
|
|
|
|
return errors.New("failed to set CustomSockoptString", opt, custom.Value, err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return errors.New("unknown CustomSockopt type:", custom.Type)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-25 14:01:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if config.Tproxy.IsEnabled() {
|
|
|
|
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_IP, syscall.IP_TRANSPARENT, 1); err != nil {
|
2024-06-29 21:32:57 +03:00
|
|
|
return errors.New("failed to set IP_TRANSPARENT").Base(err)
|
2020-11-25 14:01:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig) error {
|
|
|
|
if config.Mark != 0 {
|
|
|
|
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_MARK, int(config.Mark)); err != nil {
|
2024-06-29 21:32:57 +03:00
|
|
|
return errors.New("failed to set SO_MARK").Base(err)
|
2020-11-25 14:01:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if isTCPSocket(network) {
|
2021-03-30 19:42:02 +03:00
|
|
|
tfo := config.ParseTFOValue()
|
|
|
|
if tfo >= 0 {
|
2023-03-12 12:44:41 +03:00
|
|
|
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_TCP, unix.TCP_FASTOPEN, tfo); err != nil {
|
2024-06-29 21:32:57 +03:00
|
|
|
return errors.New("failed to set TCP_FASTOPEN", tfo).Base(err)
|
2020-11-25 14:01:53 +03:00
|
|
|
}
|
|
|
|
}
|
2021-10-12 18:39:08 +03:00
|
|
|
|
2022-07-31 16:55:40 +03:00
|
|
|
if config.TcpKeepAliveInterval > 0 || config.TcpKeepAliveIdle > 0 {
|
|
|
|
if config.TcpKeepAliveInterval > 0 {
|
|
|
|
if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, syscall.TCP_KEEPINTVL, int(config.TcpKeepAliveInterval)); err != nil {
|
2024-06-29 21:32:57 +03:00
|
|
|
return errors.New("failed to set TCP_KEEPINTVL", err)
|
2022-07-31 16:55:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if config.TcpKeepAliveIdle > 0 {
|
|
|
|
if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, syscall.TCP_KEEPIDLE, int(config.TcpKeepAliveIdle)); err != nil {
|
2024-06-29 21:32:57 +03:00
|
|
|
return errors.New("failed to set TCP_KEEPIDLE", err)
|
2022-07-31 16:55:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, 1); err != nil {
|
2024-06-29 21:32:57 +03:00
|
|
|
return errors.New("failed to set SO_KEEPALIVE", err)
|
2022-07-31 16:55:40 +03:00
|
|
|
}
|
|
|
|
} else if config.TcpKeepAliveInterval < 0 || config.TcpKeepAliveIdle < 0 {
|
|
|
|
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, 0); err != nil {
|
2024-06-29 21:32:57 +03:00
|
|
|
return errors.New("failed to unset SO_KEEPALIVE", err)
|
2021-10-12 18:39:08 +03:00
|
|
|
}
|
|
|
|
}
|
2022-10-10 20:13:50 +03:00
|
|
|
|
2022-12-26 03:37:35 +03:00
|
|
|
if config.TcpCongestion != "" {
|
|
|
|
if err := syscall.SetsockoptString(int(fd), syscall.SOL_TCP, syscall.TCP_CONGESTION, config.TcpCongestion); err != nil {
|
2024-06-29 21:32:57 +03:00
|
|
|
return errors.New("failed to set TCP_CONGESTION", err)
|
2022-12-26 03:37:35 +03:00
|
|
|
}
|
|
|
|
}
|
2023-03-09 16:51:16 +03:00
|
|
|
|
|
|
|
if config.TcpWindowClamp > 0 {
|
|
|
|
if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, syscall.TCP_WINDOW_CLAMP, int(config.TcpWindowClamp)); err != nil {
|
2024-06-29 21:32:57 +03:00
|
|
|
return errors.New("failed to set TCP_WINDOW_CLAMP", err)
|
2023-03-17 08:17:01 +03:00
|
|
|
}
|
2023-03-09 16:51:16 +03:00
|
|
|
}
|
2023-03-12 12:44:41 +03:00
|
|
|
|
|
|
|
if config.TcpUserTimeout > 0 {
|
|
|
|
if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, unix.TCP_USER_TIMEOUT, int(config.TcpUserTimeout)); err != nil {
|
2024-06-29 21:32:57 +03:00
|
|
|
return errors.New("failed to set TCP_USER_TIMEOUT", err)
|
2023-03-12 12:44:41 +03:00
|
|
|
}
|
|
|
|
}
|
2023-04-30 03:03:30 +03:00
|
|
|
|
|
|
|
if config.TcpMaxSeg > 0 {
|
|
|
|
if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, unix.TCP_MAXSEG, int(config.TcpMaxSeg)); err != nil {
|
2024-06-29 21:32:57 +03:00
|
|
|
return errors.New("failed to set TCP_MAXSEG", err)
|
2023-04-30 03:03:30 +03:00
|
|
|
}
|
|
|
|
}
|
2024-07-09 19:19:31 +03:00
|
|
|
if len(config.CustomSockopt) > 0 {
|
|
|
|
for _, custom := range config.CustomSockopt {
|
|
|
|
var level = 0x6 // default TCP
|
|
|
|
var opt int
|
|
|
|
if len(custom.Opt) == 0 {
|
|
|
|
return errors.New("No opt!")
|
|
|
|
} else {
|
|
|
|
opt, _ = strconv.Atoi(custom.Opt)
|
|
|
|
}
|
|
|
|
if custom.Level != "" {
|
|
|
|
level, _ = strconv.Atoi(custom.Level)
|
|
|
|
}
|
|
|
|
if custom.Type == "int" {
|
|
|
|
value, _ := strconv.Atoi(custom.Value)
|
|
|
|
if err := syscall.SetsockoptInt(int(fd), level, opt, value); err != nil {
|
|
|
|
return errors.New("failed to set CustomSockoptInt", opt, value, err)
|
|
|
|
}
|
|
|
|
} else if custom.Type == "str" {
|
|
|
|
if err := syscall.SetsockoptString(int(fd), level, opt, custom.Value); err != nil {
|
|
|
|
return errors.New("failed to set CustomSockoptString", opt, custom.Value, err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return errors.New("unknown CustomSockopt type:", custom.Type)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-25 14:01:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if config.Tproxy.IsEnabled() {
|
|
|
|
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_IP, syscall.IP_TRANSPARENT, 1); err != nil {
|
2024-06-29 21:32:57 +03:00
|
|
|
return errors.New("failed to set IP_TRANSPARENT").Base(err)
|
2020-11-25 14:01:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.ReceiveOriginalDestAddress && isUDPSocket(network) {
|
|
|
|
err1 := syscall.SetsockoptInt(int(fd), syscall.SOL_IPV6, unix.IPV6_RECVORIGDSTADDR, 1)
|
|
|
|
err2 := syscall.SetsockoptInt(int(fd), syscall.SOL_IP, syscall.IP_RECVORIGDSTADDR, 1)
|
|
|
|
if err1 != nil && err2 != nil {
|
|
|
|
return err1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-24 19:54:40 +03:00
|
|
|
if config.V6Only {
|
|
|
|
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_IPV6, syscall.IPV6_V6ONLY, 1); err != nil {
|
2024-06-29 21:32:57 +03:00
|
|
|
return errors.New("failed to set IPV6_V6ONLY", err)
|
2023-02-24 19:54:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-25 14:01:53 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func setReuseAddr(fd uintptr) error {
|
|
|
|
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1); err != nil {
|
2024-06-29 21:32:57 +03:00
|
|
|
return errors.New("failed to set SO_REUSEADDR").Base(err).AtWarning()
|
2020-11-25 14:01:53 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func setReusePort(fd uintptr) error {
|
|
|
|
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, unix.SO_REUSEPORT, 1); err != nil {
|
2024-06-29 21:32:57 +03:00
|
|
|
return errors.New("failed to set SO_REUSEPORT").Base(err).AtWarning()
|
2020-11-25 14:01:53 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|