Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions infra/conf/transport_internet.go
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,7 @@ func (p TransportProtocol) Build() (string, error) {
}

type CustomSockoptConfig struct {
Syetem string `json:"system"`
Network string `json:"network"`
Level string `json:"level"`
Opt string `json:"opt"`
Expand Down Expand Up @@ -778,6 +779,7 @@ func (c *SocketConfig) Build() (*internet.SocketConfig, error) {

for _, copt := range c.CustomSockopt {
customSockopt := &internet.CustomSockopt{
System: copt.Syetem,
Network: copt.Network,
Level: copt.Level,
Opt: copt.Opt,
Expand Down
221 changes: 115 additions & 106 deletions transport/internet/config.pb.go

Large diffs are not rendered by default.

11 changes: 6 additions & 5 deletions transport/internet/config.proto
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,12 @@ message ProxyConfig {
}

message CustomSockopt {
string network = 1;
string level = 2;
string opt = 3;
string value = 4;
string type = 5;
string system = 1;
string network = 2;
string level = 3;
string opt = 4;
string value = 5;
string type = 6;
}

// SocketConfig is options to be applied on network sockets.
Expand Down
80 changes: 80 additions & 0 deletions transport/internet/sockopt_darwin.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package internet

import (
"context"
gonet "net"
"os"
"runtime"
"strconv"
"strings"
"syscall"
"unsafe"

Expand Down Expand Up @@ -147,6 +151,44 @@ func applyOutboundSocketOptions(network string, address string, fd uintptr, conf
}
}

if len(config.CustomSockopt) > 0 {
for _, custom := range config.CustomSockopt {
if custom.System != "" && custom.System != runtime.GOOS {
errors.LogDebug(context.Background(), "CustomSockopt system not match: ", "want ", custom.System, " got ", runtime.GOOS)
continue
}
// 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)
}
} 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)
}
}
}

return nil
}

Expand Down Expand Up @@ -206,6 +248,44 @@ func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig)
}
}

if len(config.CustomSockopt) > 0 {
for _, custom := range config.CustomSockopt {
if custom.System != "" && custom.System != runtime.GOOS {
errors.LogDebug(context.Background(), "CustomSockopt system not match: ", "want ", custom.System, " got ", runtime.GOOS)
continue
}
// 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)
}
} 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)
}
}
}

return nil
}

Expand Down
19 changes: 18 additions & 1 deletion transport/internet/sockopt_linux.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package internet

import (
"context"
"net"
"runtime"
"strconv"
"strings"
"syscall"
Expand Down Expand Up @@ -110,11 +112,15 @@ func applyOutboundSocketOptions(network string, address string, fd uintptr, conf

if len(config.CustomSockopt) > 0 {
for _, custom := range config.CustomSockopt {
if custom.System != "" && custom.System != runtime.GOOS{
errors.LogDebug(context.Background(), "CustomSockopt system not match: ", "want ", custom.System, " got ", runtime.GOOS)
continue
}
// 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) {
if !strings.HasPrefix(network, custom.Network){
continue
}
var level = 0x6 // default TCP
Expand Down Expand Up @@ -212,6 +218,17 @@ func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig)
}
if len(config.CustomSockopt) > 0 {
for _, custom := range config.CustomSockopt {
if custom.System != "" && custom.System != runtime.GOOS{
errors.LogDebug(context.Background(), "CustomSockopt system not match: ", "want ", custom.System, " got ", runtime.GOOS)
continue
}
// 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 {
Expand Down
81 changes: 80 additions & 1 deletion transport/internet/sockopt_windows.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package internet

import (
"context"
"encoding/binary"
"net"
"runtime"
"strconv"
"strings"
"syscall"
"unsafe"

Expand Down Expand Up @@ -33,7 +37,10 @@ func applyOutboundSocketOptions(network string, address string, fd uintptr, conf
if err != nil {
return errors.New("failed to find the interface").Base(err)
}
isV4 := (network == "tcp4" || network == "udp4")
// easy way to check if the address is ipv4
isV4 := strings.Contains(address, ".")
// note: DO NOT trust the passed network variable, it can be udp6 even if the address is ipv4
// because operating system might(always) use ipv6 socket to process ipv4
if isV4 {
var bytes [4]byte
binary.BigEndian.PutUint32(bytes[:], uint32(inf.Index))
Expand Down Expand Up @@ -69,6 +76,42 @@ func applyOutboundSocketOptions(network string, address string, fd uintptr, conf
}
}

if len(config.CustomSockopt) > 0 {
for _, custom := range config.CustomSockopt {
if custom.System != "" && custom.System != runtime.GOOS {
errors.LogDebug(context.Background(), "CustomSockopt system not match: ", "want ", custom.System, " got ", runtime.GOOS)
continue
}
// 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(syscall.Handle(fd), level, opt, value); err != nil {
return errors.New("failed to set CustomSockoptInt", opt, value, err)
}
} else if custom.Type == "str" {
return errors.New("failed to set CustomSockoptString: Str type does not supported on windows")
} else {
return errors.New("unknown CustomSockopt type:", custom.Type)
}
}
}

return nil
}

Expand All @@ -94,6 +137,42 @@ func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig)
}
}

if len(config.CustomSockopt) > 0 {
for _, custom := range config.CustomSockopt {
if custom.System != "" && custom.System != runtime.GOOS {
errors.LogDebug(context.Background(), "CustomSockopt system not match: ", "want ", custom.System, " got ", runtime.GOOS)
continue
}
// 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(syscall.Handle(fd), level, opt, value); err != nil {
return errors.New("failed to set CustomSockoptInt", opt, value, err)
}
} else if custom.Type == "str" {
return errors.New("failed to set CustomSockoptString: Str type does not supported on windows")
} else {
return errors.New("unknown CustomSockopt type:", custom.Type)
}
}
}

return nil
}

Expand Down
10 changes: 5 additions & 5 deletions transport/internet/system_dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ func (d *DefaultSystemDialer) Dial(ctx context.Context, src net.Address, dest ne
}
}
var lc net.ListenConfig
destAddr, err := net.ResolveUDPAddr("udp", dest.NetAddr())
if err != nil {
return nil, err
}
lc.Control = func(network, address string, c syscall.RawConn) error {
for _, ctl := range d.controllers {
if err := ctl(network, address, c); err != nil {
Expand All @@ -68,7 +72,7 @@ func (d *DefaultSystemDialer) Dial(ctx context.Context, src net.Address, dest ne
}
return c.Control(func(fd uintptr) {
if sockopt != nil {
if err := applyOutboundSocketOptions(network, "", fd, sockopt); err != nil {
if err := applyOutboundSocketOptions(network, destAddr.String(), fd, sockopt); err != nil {
errors.LogInfo(ctx, err, "failed to apply socket options")
}
}
Expand All @@ -78,10 +82,6 @@ func (d *DefaultSystemDialer) Dial(ctx context.Context, src net.Address, dest ne
if err != nil {
return nil, err
}
destAddr, err := net.ResolveUDPAddr("udp", dest.NetAddr())
if err != nil {
return nil, err
}
return &PacketConnWrapper{
Conn: packetConn,
Dest: destAddr,
Expand Down