Skip to content
Closed
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
4 changes: 4 additions & 0 deletions infra/conf/transport_internet.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ func (c *KCPConfig) Build() (proto.Message, error) {
type TCPConfig struct {
HeaderConfig json.RawMessage `json:"header"`
AcceptProxyProtocol bool `json:"acceptProxyProtocol"`
Rate uint64 `json:"rate"`
Cwnd uint32 `json:"cwnd"`
}

// Build implements Buildable.
Expand All @@ -131,6 +133,8 @@ func (c *TCPConfig) Build() (proto.Message, error) {
if c.AcceptProxyProtocol {
config.AcceptProxyProtocol = c.AcceptProxyProtocol
}
config.Rate = c.Rate
config.Cwnd = c.Cwnd
return config, nil
}

Expand Down
22 changes: 20 additions & 2 deletions transport/internet/tcp/config.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions transport/internet/tcp/config.proto
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ message Config {
reserved 1;
xray.common.serial.TypedMessage header_settings = 2;
bool accept_proxy_protocol = 3;
uint64 rate = 4;
uint32 cwnd = 5;
}
6 changes: 6 additions & 0 deletions transport/internet/tcp/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ func (v *Listener) keepAccepting() {
}
continue
}
if v.config.Rate > 0 {
err := SetBrutalRate(conn, v.config.Rate, v.config.Cwnd)
if err != nil {
errors.LogDebugInner(context.Background(), err, "failed to apply socket options to incoming connection")
}
}
go func() {
if v.tlsConfig != nil {
conn = tls.Server(conn, v.tlsConfig)
Expand Down
4 changes: 4 additions & 0 deletions transport/internet/tcp/sockopt_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@ func GetOriginalDestination(conn stat.Connection) (net.Destination, error) {
}
return dest, nil
}

func SetBrutalRate(conn stat.Connection, rate uint64, cwnd uint32) error {
return nil
}
4 changes: 4 additions & 0 deletions transport/internet/tcp/sockopt_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@ func GetOriginalDestination(conn stat.Connection) (net.Destination, error) {
}
return dest, nil
}

func SetBrutalRate(conn stat.Connection, rate uint64, cwnd uint32) error {
return nil
}
34 changes: 34 additions & 0 deletions transport/internet/tcp/sockopt_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package tcp

import (
"context"
"encoding/binary"
"syscall"
"unsafe"

Expand Down Expand Up @@ -50,3 +51,36 @@ func GetOriginalDestination(conn stat.Connection) (net.Destination, error) {
}
return dest, nil
}

func SetBrutalRate(conn stat.Connection, rate uint64, cwnd uint32) error {
sys, ok := conn.(syscall.Conn)
if !ok {
return errors.New("unable to get syscall.Conn")
}
sysConn, err := sys.SyscallConn()
if err != nil {
return errors.New("failed to get sys fd").Base(err)
}
err = sysConn.Control(func(fd uintptr) {
if err := syscall.SetsockoptString(int(fd), syscall.IPPROTO_TCP, syscall.TCP_CONGESTION, "brutal"); err != nil {
errors.LogDebugInner(context.Background(), err, "failed to set CustomSockoptString ", syscall.TCP_CONGESTION, " ", "brutal")
return
}

if cwnd == 0 {
cwnd = 15
}

buf := make([]byte, 16)
binary.LittleEndian.PutUint64(buf[0:], rate)
binary.LittleEndian.PutUint32(buf[8:], cwnd)
if err := syscall.SetsockoptString(int(fd), syscall.IPPROTO_TCP, 23301, string(buf)); err != nil {
errors.LogDebugInner(context.Background(), err, "failed to set CustomSockoptString 23301 ", buf)
return
}
})
if err != nil {
return errors.New("failed to control connection").Base(err)
}
return nil
}
32 changes: 32 additions & 0 deletions transport/internet/tcp/sockopt_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
package tcp_test

import (
"bytes"
"context"
"encoding/binary"
"fmt"
"strings"
"testing"
unsafe "unsafe"

"github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/testing/servers/tcp"
Expand All @@ -31,3 +35,31 @@ func TestGetOriginalDestination(t *testing.T) {
t.Error("unexpected state")
}
}

func TestSockoptParams(t *testing.T) {
type BrutalParams struct {
rate uint64
cwnd_gain uint32
}

params := BrutalParams{
rate: 15 * 1024 * 1024 / 8,
cwnd_gain: 15,
}

raw := unsafe.Slice(
(*byte)(unsafe.Pointer(&params)),
unsafe.Sizeof(params),
)

buf := make([]byte, 16)
binary.LittleEndian.PutUint64(buf, 15*1024*1024/8)
binary.LittleEndian.PutUint32(buf[8:], 15)

fmt.Println(len(raw), raw)
fmt.Println(len(buf), buf)

if !bytes.Equal(raw, buf) {
t.Fatal("!bytes.Equal(raw, buf)")
}
}
4 changes: 4 additions & 0 deletions transport/internet/tcp/sockopt_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ import (
func GetOriginalDestination(conn stat.Connection) (net.Destination, error) {
return net.Destination{}, nil
}

func SetBrutalRate(conn stat.Connection, rate uint64, cwnd uint32) error {
return nil
}