Skip to content
23 changes: 23 additions & 0 deletions example.config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,29 @@ handshake = "10s"
# Only applies to Linux and Darwin; ignored on other platforms.
# tcp-not-sent-lowat = "128kib"

# Small-segment ServerHello (Linux only). Some DPI boxes recognise the FakeTLS
# ServerHello when it is sent in full-size TCP segments, but not when it is
# split into small ones. client-mss splits the beginning of the server
# response into segments as if the MSS were client-mss; the rest of the session
# uses client-mss-bulk, which is set as TCP_MAXSEG on the listening socket
# (Linux cannot raise the MSS of an established connection, so the order
# cannot be reversed).
# client-mss: 0 (default, disabled) or 48..1460.
# client-mss-bulk: 0 or 536..65495, 1400 by default when client-mss is set;
# 0 keeps the whole session at client-mss (client-mss must then be >= 88).
# client-mss-bytes: how many first bytes of the server response are split.
# 0 (default) splits the ServerHello record alone, about 130 bytes, which is
# 2 segments at client-mss = 92; ChangeCipherSpec and the 2.5-4.7 KB of
# ApplicationData after it go in full-size segments. Splitting the whole
# response (any value above its size, e.g. 65535) costs 35-60 segments:
# on a lossy path one of them is lost in most handshakes, and the response
# takes a few extra round trips of slow start. Ignored with
# client-mss-bulk = 0. 0..65535.
# Read at start only.
# client-mss = 92
# client-mss-bulk = 1400
# client-mss-bytes = 0

# this defines a configuration for TCP keep alives. Default values are taken
# from Golang default behavior.
[network.keep-alive]
Expand Down
46 changes: 46 additions & 0 deletions internal/cli/client_mss.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package cli

import (
"github.com/9seconds/mtg/v2/internal/config"
"github.com/9seconds/mtg/v2/mtglib"
"github.com/9seconds/mtg/v2/network"
)

// clientMSSPlan maps [network] client-mss / client-mss-bulk /
// client-mss-bytes onto two mechanisms:
// - listenerMSS is TCP_MAXSEG of the listening socket, it applies to the
// whole connection;
// - serverHelloMSS splits the beginning of the server response in user
// space: the first serverHelloBytes bytes, or the ServerHello record if it
// is 0.
//
// client-mss and bulk > 0: the socket gets bulk, the beginning of the
// response is split by client-mss. client-mss and bulk = 0: the whole
// connection runs at client-mss (like iptables TCPMSS). client-mss = 0:
// nothing. Outside Linux the options are ignored with a warning.
func clientMSSPlan(conf *config.Config, logger mtglib.Logger) (serverHelloMSS, serverHelloBytes, listenerMSS int) {
handshake, bulk := conf.GetClientMSS()
if handshake == 0 {
return 0, 0, 0
}

if !network.ListenerMSSSupported {
logger.Warning("network.client-mss is supported only on Linux; ignored")

return 0, 0, 0
}

if bulk == 0 {
serverHelloMSS, listenerMSS = 0, int(handshake)
} else {
serverHelloMSS, listenerMSS = int(handshake), int(bulk)
serverHelloBytes = int(conf.GetClientMSSBytes())
}

logger.BindInt("server_hello_mss", serverHelloMSS).
BindInt("server_hello_mss_bytes", serverHelloBytes).
BindInt("listener_mss", listenerMSS).
Info("client MSS shaping is enabled")

return serverHelloMSS, serverHelloBytes, listenerMSS
}
63 changes: 63 additions & 0 deletions internal/cli/client_mss_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package cli

import (
"testing"

"github.com/9seconds/mtg/v2/internal/config"
"github.com/9seconds/mtg/v2/logger"
"github.com/9seconds/mtg/v2/network"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestClientMSSPlan(t *testing.T) {
cases := []struct {
name string
network string
wantHello int
wantBytes int
wantListener int
}{
{name: "disabled", network: ""},
{name: "ServerHello at 92, session at 1400", network: "client-mss = 92\n", wantHello: 92, wantListener: 1400},
{name: "custom bulk", network: "client-mss = 92\nclient-mss-bulk = 1200\n", wantHello: 92, wantListener: 1200},
{name: "whole session at 92", network: "client-mss = 92\nclient-mss-bulk = 0\n", wantListener: 92},
{
name: "first 500 bytes at 92",
network: "client-mss = 92\nclient-mss-bytes = 500\n",
wantHello: 92, wantBytes: 500, wantListener: 1400,
},
{
name: "bytes are ignored for the whole session at 92",
network: "client-mss = 92\nclient-mss-bulk = 0\nclient-mss-bytes = 500\n",
wantListener: 92,
},
{name: "bytes without client-mss", network: "client-mss-bytes = 500\n"},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
conf, err := config.Parse([]byte(`bind-to = "0.0.0.0:443"
secret = "7mqFMMq3P2Tvvt_rPx5qhmFnb29nbGUuY29t"

[network]
` + tc.network))
require.NoError(t, err)
require.NoError(t, conf.Validate())

hello, helloBytes, listener := clientMSSPlan(conf, logger.NewNoopLogger())

if !network.ListenerMSSSupported {
assert.Zero(t, hello)
assert.Zero(t, helloBytes)
assert.Zero(t, listener)

return
}

assert.Equal(t, tc.wantHello, hello)
assert.Equal(t, tc.wantBytes, helloBytes)
assert.Equal(t, tc.wantListener, listener)
})
}
}
10 changes: 10 additions & 0 deletions internal/cli/run_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,8 @@ func runProxy(conf *config.Config, version string) error { //nolint: funlen, cyc
doppelGangerURLs[i] = v.String()
}

serverHelloMSS, serverHelloMSSBytes, listenerMSS := clientMSSPlan(conf, logger)

opts := mtglib.ProxyOpts{
Logger: logger,
Network: ntw,
Expand All @@ -326,6 +328,8 @@ func runProxy(conf *config.Config, version string) error { //nolint: funlen, cyc
DoppelGangerPerRaid: conf.Defense.Doppelganger.Repeats.Get(mtglib.DoppelGangerPerRaid),
DoppelGangerEach: conf.Defense.Doppelganger.UpdateEach.Get(mtglib.DoppelGangerEach),
DoppelGangerDRS: conf.Defense.Doppelganger.DRS.Get(false),
ServerHelloMSS: serverHelloMSS,
ServerHelloMSSBytes: serverHelloMSSBytes,
}

proxy, err := mtglib.NewProxy(opts)
Expand All @@ -338,6 +342,12 @@ func runProxy(conf *config.Config, version string) error { //nolint: funlen, cyc
return fmt.Errorf("cannot start proxy: %w", err)
}

if err := utils.SetListenerMSS(listener, listenerMSS); err != nil {
listener.Close() //nolint: errcheck

return fmt.Errorf("cannot start proxy: %w", err)
}

if conf.ProxyProtocolListener.Get(false) {
listener = &proxyprotocol.ListenerAdapter{
Listener: proxyproto.Listener{
Expand Down
118 changes: 118 additions & 0 deletions internal/config/client_mss_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package config_test

import (
"testing"

"github.com/9seconds/mtg/v2/internal/config"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func parseClientMSS(t *testing.T, network string) (*config.Config, error) {
t.Helper()

conf, err := config.Parse([]byte(`bind-to = "0.0.0.0:443"
secret = "7mqFMMq3P2Tvvt_rPx5qhmFnb29nbGUuY29t"

[network]
` + network))
require.NoError(t, err)

return conf, conf.Validate()
}

func TestClientMSS(t *testing.T) {
cases := []struct {
name string
network string
wantHandshake uint
wantBulk uint
}{
{name: "not set", network: "", wantHandshake: 0, wantBulk: 0},
{name: "explicit zero", network: "client-mss = 0", wantHandshake: 0, wantBulk: 0},
{name: "default bulk 1400", network: "client-mss = 92", wantHandshake: 92, wantBulk: 1400},
{name: "custom bulk", network: "client-mss = 92\nclient-mss-bulk = 1200", wantHandshake: 92, wantBulk: 1200},
{name: "bulk 0 keeps the whole session at client-mss", network: "client-mss = 92\nclient-mss-bulk = 0", wantHandshake: 92, wantBulk: 0},
{name: "bulk without client-mss has no effect", network: "client-mss-bulk = 1400", wantHandshake: 0, wantBulk: 0},
{name: "lower bound", network: "client-mss = 48", wantHandshake: 48, wantBulk: 1400},
{name: "upper bounds", network: "client-mss = 1460\nclient-mss-bulk = 65495", wantHandshake: 1460, wantBulk: 65495},
{name: "bulk lower bound", network: "client-mss = 92\nclient-mss-bulk = 536", wantHandshake: 92, wantBulk: 536},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
conf, err := parseClientMSS(t, tc.network)
require.NoError(t, err)

handshake, bulk := conf.GetClientMSS()
assert.Equal(t, tc.wantHandshake, handshake)
assert.Equal(t, tc.wantBulk, bulk)
})
}
}

func TestClientMSSValidation(t *testing.T) {
cases := []struct {
name string
network string
wantErr string
}{
{name: "client-mss below 48", network: "client-mss = 47", wantErr: "network.client-mss must be 0 or 48..1460"},
{name: "client-mss above 1460", network: "client-mss = 1461", wantErr: "network.client-mss must be 0 or 48..1460"},
{name: "bulk below 536", network: "client-mss = 92\nclient-mss-bulk = 535", wantErr: "network.client-mss-bulk must be 0 or 536..65495"},
{name: "bulk above 65495", network: "client-mss = 92\nclient-mss-bulk = 65496", wantErr: "network.client-mss-bulk must be 0 or 536..65495"},
{name: "bulk not greater than client-mss", network: "client-mss = 1400\nclient-mss-bulk = 1400", wantErr: "must be greater than network.client-mss"},
{name: "bulk 0 and client-mss below the kernel minimum", network: "client-mss = 60\nclient-mss-bulk = 0", wantErr: "at least 88"},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
_, err := parseClientMSS(t, tc.network)
require.Error(t, err)
assert.Contains(t, err.Error(), tc.wantErr)
})
}
}

func TestClientMSSNotANumber(t *testing.T) {
_, err := config.Parse([]byte(`bind-to = "0.0.0.0:443"
secret = "7mqFMMq3P2Tvvt_rPx5qhmFnb29nbGUuY29t"

[network]
client-mss = "small"
`))
assert.Error(t, err)
}

func TestClientMSSBytes(t *testing.T) {
cases := []struct {
name string
network string
want uint
}{
{name: "not set is the ServerHello record", network: "client-mss = 92", want: 0},
{name: "explicit", network: "client-mss = 92\nclient-mss-bytes = 500", want: 500},
{name: "upper bound", network: "client-mss = 92\nclient-mss-bytes = 65535", want: 65535},
{name: "no effect without client-mss", network: "client-mss-bytes = 500", want: 0},
{name: "no effect with bulk 0", network: "client-mss = 92\nclient-mss-bulk = 0\nclient-mss-bytes = 500", want: 0},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
conf, err := parseClientMSS(t, tc.network)
require.NoError(t, err)
assert.Equal(t, tc.want, conf.GetClientMSSBytes())
})
}
}

func TestClientMSSBytesAboveUint16(t *testing.T) {
_, err := config.Parse([]byte(`bind-to = "0.0.0.0:443"
secret = "7mqFMMq3P2Tvvt_rPx5qhmFnb29nbGUuY29t"

[network]
client-mss = 92
client-mss-bytes = 65536
`))
assert.Error(t, err)
}
Loading
Loading