mirror of
https://github.com/XTLS/Xray-core.git
synced 2025-04-30 09:18:34 +00:00
Sockopt: Fix Windows UDP interface
bind; Allow customSockopt
work for UDP (#4504)
This commit is contained in:
parent
1067171e6a
commit
b585b26f29
6 changed files with 169 additions and 137 deletions
|
@ -3,6 +3,7 @@ package internet
|
|||
import (
|
||||
"net"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
|
||||
"github.com/xtls/xray-core/common/errors"
|
||||
|
@ -35,6 +36,8 @@ func bindAddr(fd uintptr, ip []byte, port uint32) error {
|
|||
return syscall.Bind(int(fd), sockaddr)
|
||||
}
|
||||
|
||||
// applyOutboundSocketOptions applies socket options for outbound connection.
|
||||
// note that unlike other part of Xray, this function needs network with speified network stack(tcp4/tcp6/udp4/udp6)
|
||||
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 {
|
||||
|
@ -103,30 +106,38 @@ func applyOutboundSocketOptions(network string, address string, fd uintptr, conf
|
|||
}
|
||||
}
|
||||
|
||||
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 len(config.CustomSockopt) > 0 {
|
||||
for _, custom := range config.CustomSockopt {
|
||||
// Skip unwanted network type
|
||||
// network might be tcp4 or tcp6
|
||||
// use HasPrefix so that "tcp" can match tcp4/6 with "tcp" if user want to control all tcp (udp is also the same)
|
||||
// if it is empty, strings.HasPrefix will always return true to make it apply for all networks
|
||||
if !strings.HasPrefix(network, custom.Network) {
|
||||
continue
|
||||
}
|
||||
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)
|
||||
}
|
||||
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)
|
||||
} 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -140,6 +151,8 @@ func applyOutboundSocketOptions(network string, address string, fd uintptr, conf
|
|||
return nil
|
||||
}
|
||||
|
||||
// applyInboundSocketOptions applies socket options for inbound listener.
|
||||
// note that unlike other part of Xray, this function needs network with speified network stack(tcp4/tcp6/udp4/udp6)
|
||||
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 {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue