Skip to content

Commit 711f987

Browse files
author
null
committed
brutal
1 parent 455f6bc commit 711f987

7 files changed

Lines changed: 235 additions & 3 deletions

File tree

infra/conf/transport_internet.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package conf
33
import (
44
"context"
55
"encoding/base64"
6+
"encoding/binary"
67
"encoding/hex"
78
"encoding/json"
89
"math"
@@ -22,6 +23,7 @@ import (
2223
"github.com/xtls/xray-core/common/platform/filesystem"
2324
"github.com/xtls/xray-core/common/serial"
2425
"github.com/xtls/xray-core/transport/internet"
26+
"github.com/xtls/xray-core/transport/internet/finalmask/brutal"
2527
"github.com/xtls/xray-core/transport/internet/finalmask/fragment"
2628
"github.com/xtls/xray-core/transport/internet/finalmask/header/custom"
2729
"github.com/xtls/xray-core/transport/internet/finalmask/mkcp/aes128gcm"
@@ -1230,6 +1232,7 @@ var (
12301232
customVarNamePattern = regexp.MustCompile(`^[A-Za-z_][A-Za-z0-9_]*$`)
12311233

12321234
tcpmaskLoader = NewJSONConfigLoader(ConfigCreatorCache{
1235+
"brutal": func() interface{} { return new(Brutal) },
12331236
"header-custom": func() interface{} { return new(HeaderCustomTCP) },
12341237
"fragment": func() interface{} { return new(FragmentMask) },
12351238
"sudoku": func() interface{} { return new(Sudoku) },
@@ -1247,6 +1250,34 @@ var (
12471250
}, "type", "settings")
12481251
)
12491252

1253+
type Brutal struct {
1254+
Rate uint64 `json:"rate"`
1255+
Cwnd uint64 `json:"cwnd"`
1256+
}
1257+
1258+
func (c *Brutal) Build() (proto.Message, error) {
1259+
rate := c.Rate
1260+
cwnd := c.Cwnd
1261+
if rate == 0 {
1262+
rate = 125000
1263+
}
1264+
if cwnd == 0 {
1265+
cwnd = 20
1266+
}
1267+
if rate < 62500 {
1268+
return nil, errors.New("invalid rate ", rate)
1269+
}
1270+
if cwnd < 5 || cwnd > 80 {
1271+
return nil, errors.New("invalid cwnd ", cwnd)
1272+
}
1273+
params := make([]byte, 16)
1274+
binary.NativeEndian.PutUint64(params, rate)
1275+
binary.NativeEndian.PutUint64(params[8:], cwnd)
1276+
return &brutal.Config{
1277+
Params: params,
1278+
}, nil
1279+
}
1280+
12501281
type TCPItem struct {
12511282
Delay Int32Range `json:"delay"`
12521283
Rand int32 `json:"rand"`
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package brutal
2+
3+
import "net"
4+
5+
func (c *Config) TCP() {}
6+
7+
func (c *Config) WrapConnClient(raw net.Conn) (net.Conn, error) {
8+
return NewConn(c, raw)
9+
}
10+
11+
func (c *Config) WrapConnServer(raw net.Conn) (net.Conn, error) {
12+
return NewConn(c, raw)
13+
}

transport/internet/finalmask/brutal/config.pb.go

Lines changed: 123 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
syntax = "proto3";
2+
3+
package xray.transport.internet.finalmask.brutal;
4+
option csharp_namespace = "Xray.Transport.Internet.Finalmask.Brutal";
5+
option go_package = "github.com/xtls/xray-core/transport/internet/finalmask/brutal";
6+
option java_package = "com.xray.transport.internet.finalmask.brutal";
7+
option java_multiple_files = true;
8+
9+
message Config {
10+
bytes params = 1;
11+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//go:build !linux
2+
3+
package brutal
4+
5+
import (
6+
"context"
7+
"net"
8+
9+
"github.com/xtls/xray-core/common/errors"
10+
)
11+
12+
func NewConn(c *Config, raw net.Conn) (net.Conn, error) {
13+
errors.LogError(context.Background(), "unsupported system")
14+
return raw, nil
15+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//go:build linux
2+
3+
package brutal
4+
5+
import (
6+
"context"
7+
"net"
8+
"reflect"
9+
"syscall"
10+
11+
"github.com/pires/go-proxyproto"
12+
"github.com/xtls/xray-core/common"
13+
"github.com/xtls/xray-core/common/errors"
14+
"golang.org/x/sys/unix"
15+
)
16+
17+
func NewConn(c *Config, raw net.Conn) (net.Conn, error) {
18+
conn := raw
19+
if pc, ok := conn.(*proxyproto.Conn); ok {
20+
conn = pc.Raw()
21+
}
22+
if _, ok := conn.(*net.TCPConn); !ok {
23+
errors.LogError(context.Background(), "unsupported conn ", reflect.TypeOf(conn))
24+
}
25+
sysConn := common.Must2(conn.(*net.TCPConn).SyscallConn())
26+
err := sysConn.Control(func(fd uintptr) {
27+
if err := unix.SetsockoptString(int(fd), unix.IPPROTO_TCP, unix.TCP_CONGESTION, "brutal"); err != nil {
28+
errors.LogErrorInner(context.Background(), err, "failed to set congestion")
29+
return
30+
}
31+
if err := syscall.SetsockoptString(int(fd), unix.IPPROTO_TCP, 23301, string(c.Params)); err != nil {
32+
errors.LogErrorInner(context.Background(), err, "failed to set params")
33+
return
34+
}
35+
})
36+
if err != nil {
37+
errors.LogErrorInner(context.Background(), err, "failed to control connection")
38+
}
39+
return raw, nil
40+
}

transport/internet/finalmask/finalmask.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,8 @@ func (l *tcpListener) Accept() (net.Conn, error) {
256256

257257
newConn, err := l.m.WrapConnServer(conn)
258258
if err != nil {
259-
errors.LogDebugInner(context.Background(), err, "mask err")
260-
_ = conn.Close()
261-
return nil, err
259+
errors.LogErrorInner(context.Background(), err, "[mask] failed to wrap connection, falling back to the original connection")
260+
return conn, nil
262261
}
263262

264263
return newConn, nil

0 commit comments

Comments
 (0)