mirror of
https://github.com/XTLS/Xray-core.git
synced 2025-04-30 09:18:34 +00:00
Add custom Sockopt support (#3517)
* Add custom sockopt * Add custom level * Change field * Sth left
This commit is contained in:
parent
ce637c0c23
commit
308f0c64c3
4 changed files with 321 additions and 135 deletions
|
@ -2,6 +2,7 @@ package internet
|
|||
|
||||
import (
|
||||
"net"
|
||||
"strconv"
|
||||
"syscall"
|
||||
|
||||
"github.com/xtls/xray-core/common/errors"
|
||||
|
@ -107,7 +108,32 @@ func applyOutboundSocketOptions(network string, address string, fd uintptr, conf
|
|||
return errors.New("failed to set TCP_NODELAY", err)
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if config.Tproxy.IsEnabled() {
|
||||
|
@ -176,6 +202,32 @@ func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig)
|
|||
return errors.New("failed to set TCP_MAXSEG", err)
|
||||
}
|
||||
}
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if config.Tproxy.IsEnabled() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue