diff --git a/infra/conf/transport_finalmask.go b/infra/conf/transport_finalmask.go index c553b3427..50a4b01b3 100644 --- a/infra/conf/transport_finalmask.go +++ b/infra/conf/transport_finalmask.go @@ -941,7 +941,8 @@ type XMC struct { type XMCPaddingTurn struct { Length string `json:"length"` Direction string `json:"direction"` - Delay int64 `json:"delay,omitempty"` + // Delay is an optional millisecond value or inclusive min-max range. + Delay string `json:"delay,omitempty"` } type XMCProfile struct { @@ -984,11 +985,18 @@ func (c *XMC) Build() (proto.Message, error) { if c.Password == "" { return nil, fmt.Errorf("empty password") } + if len(c.Password) > xmc.MaxLegacyPasswordLength { + return nil, fmt.Errorf("minecraft password is longer than %d bytes", xmc.MaxLegacyPasswordLength) + } config := &xmc.Config{Password: c.Password, Hostname: c.Hostname} for i, turn := range c.Paddings { minimum, maximum, err := ParseRangeString(turn.Length) if err != nil { - return nil, fmt.Errorf("minecraft padding turn %d: %w", i, err) + return nil, fmt.Errorf("minecraft padding turn %d length: %w", i, err) + } + delayMinimum, delayMaximum, err := ParseRangeString(turn.Delay) + if err != nil { + return nil, fmt.Errorf("minecraft padding turn %d delay: %w", i, err) } var direction int32 @@ -1005,7 +1013,8 @@ func (c *XMC) Build() (proto.Message, error) { LengthMin: int64(minimum), LengthMax: int64(maximum), Direction: direction, - Delay: turn.Delay, + DelayMin: int64(delayMinimum), + DelayMax: int64(delayMaximum), }) } if err := config.ValidatePadding(); err != nil { diff --git a/infra/conf/transport_finalmask_xmc_test.go b/infra/conf/transport_finalmask_xmc_test.go index db1a31a30..aae1d0f65 100644 --- a/infra/conf/transport_finalmask_xmc_test.go +++ b/infra/conf/transport_finalmask_xmc_test.go @@ -12,9 +12,9 @@ func TestXMCBuildProfile(t *testing.T) { built, err := (&XMC{ Password: "test-password", Paddings: []XMCPaddingTurn{ - {Length: "3", Direction: "c2s", Delay: 0}, - {Length: "127-129", Direction: "s2c", Delay: 100}, - {Length: "8388608", Direction: "c2s", Delay: 50}, + {Length: "3", Direction: "c2s"}, + {Length: "127-129", Direction: "s2c", Delay: "100"}, + {Length: "8388608", Direction: "c2s", Delay: "50-150"}, }, Profiles: []XMCProfile{ { @@ -40,8 +40,10 @@ func TestXMCBuildProfile(t *testing.T) { if config.Paddings[0].Direction != 1 || config.Paddings[1].Direction != 2 || config.Paddings[2].Direction != 1 { t.Fatalf("unexpected padding directions: %v, %v, %v", config.Paddings[0].Direction, config.Paddings[1].Direction, config.Paddings[2].Direction) } - if config.Paddings[0].Delay != 0 || config.Paddings[1].Delay != 100 || config.Paddings[2].Delay != 50 { - t.Fatalf("unexpected padding delays: %v, %v, %v", config.Paddings[0].Delay, config.Paddings[1].Delay, config.Paddings[2].Delay) + if config.Paddings[0].DelayMin != 0 || config.Paddings[0].DelayMax != 0 || + config.Paddings[1].DelayMin != 100 || config.Paddings[1].DelayMax != 100 || + config.Paddings[2].DelayMin != 50 || config.Paddings[2].DelayMax != 150 { + t.Fatalf("unexpected padding delays: %v, %v, %v", config.Paddings[0].DelayMin, config.Paddings[1].DelayMin, config.Paddings[2].DelayMin) } } @@ -67,7 +69,10 @@ func TestXMCBuildRejectsPadding(t *testing.T) { "invalid_direction": `[{"length": "3", "direction": "invalid"}]`, "missing_direction": `[{"length": "3"}]`, "numeric_direction": `[{"length": "3", "direction": "1"}]`, - "negative_delay": `[{"length": "3", "direction": "c2s", "delay": -1}]`, + "negative_delay": `[{"length": "3", "direction": "c2s", "delay": "-1"}]`, + "reversed_delay": `[{"length": "3", "direction": "c2s", "delay": "10-5"}]`, + "invalid_delay_format": `[{"length": "3", "direction": "c2s", "delay": "abc"}]`, + "huge_delay": `[{"length": "3", "direction": "c2s", "delay": "9223372036854775807"}]`, } { t.Run(name, func(t *testing.T) { var config XMC @@ -87,3 +92,23 @@ func TestXMCBuildRejectsPadding(t *testing.T) { }) } } + +func TestXMCBuildRejectsLongPassword(t *testing.T) { + profile := XMCProfile{ + Username: "TestUser", UUID: "00112233-4455-6677-8899-aabbccddeeff", + TexturesValue: "textures-value", TexturesSignature: "textures-signature", + } + + // The verify token and the password share one RSA-1024 PKCS#1 v1.5 block, + // so the password cannot exceed xmc.MaxLegacyPasswordLength bytes. + valid := strings.Repeat("p", xmc.MaxLegacyPasswordLength) + if _, err := (&XMC{Password: valid, Profiles: []XMCProfile{profile}}).Build(); err != nil { + t.Fatalf("password of %d bytes was rejected: %v", len(valid), err) + } + + tooLong := valid + "p" + _, err := (&XMC{Password: tooLong, Profiles: []XMCProfile{profile}}).Build() + if err == nil || !strings.Contains(err.Error(), "longer than") { + t.Fatalf("expected a password length error, got %v", err) + } +} diff --git a/transport/internet/finalmask/xmc/client.go b/transport/internet/finalmask/xmc/client.go index eecb2a09b..5798a5423 100644 --- a/transport/internet/finalmask/xmc/client.go +++ b/transport/internet/finalmask/xmc/client.go @@ -48,6 +48,9 @@ func newClientConn(c net.Conn, profiles []loginProfile, password string, rsaPubl if len(profiles) == 0 { return nil, fmt.Errorf("empty profiles") } + if len(password) > MaxLegacyPasswordLength { + return nil, fmt.Errorf("password is longer than %d bytes", MaxLegacyPasswordLength) + } paddingSchedule, err := newPaddingSchedule(padding, true) if err != nil { return nil, fmt.Errorf("select padding profile: %w", err) @@ -195,6 +198,22 @@ func (c *clientConn) handshake() error { if err != nil { return fmt.Errorf("read login finished: %w", err) } + // Set Compression is not compressed itself and must be handled before the + // packet that follows it, which already uses the compressed framing. + if pkt.packetID == 0x03 { + var threshold Varint + if thresholdErr := pkt.readFields(&threshold); thresholdErr != nil { + return fmt.Errorf("read login compression threshold: %w", thresholdErr) + } + if threshold >= 0 { + c.reader = newCompressionReader(c.reader) + c.writer = newCompressionWriter(c.writer, int32(threshold)) + } + pkt, err = readPacket(c.reader) + if err != nil { + return fmt.Errorf("read login finished: %w", err) + } + } if pkt.packetID == 0x00 { var reason String if readErr := pkt.readFields(&reason); readErr != nil { @@ -217,7 +236,7 @@ func (c *clientConn) handshake() error { if err != nil { return fmt.Errorf("write login acknowledged: %w", err) } - if err = runPaddingSchedule(c.reader, c.writer, true, loginAcknowledgedLength, c.paddingSchedule); err != nil { + if err = runPaddingSchedule(c.writer, true, loginAcknowledgedLength, c.paddingSchedule); err != nil { return fmt.Errorf("run startup padding: %w", err) } diff --git a/transport/internet/finalmask/xmc/compression.go b/transport/internet/finalmask/xmc/compression.go new file mode 100644 index 000000000..cf24675f2 --- /dev/null +++ b/transport/internet/finalmask/xmc/compression.go @@ -0,0 +1,194 @@ +// Minecraft compression framing, negotiated by the login Set Compression packet. +package xmc + +import ( + "bytes" + "compress/zlib" + "fmt" + "io" +) + +const ( + // Packet Length is a VarInt, so one frame cannot exceed 2^21-1 bytes. + maxCompressedFrameLength = (1 << 21) - 1 + // Vanilla caps serverbound uncompressed packet bodies at 2^23 bytes. + maxUncompressedFrameBody = 1 << 23 +) + +// compressionReader converts the post-Set Compression framing +// (Packet Length, Data Length, optional zlib body) back into the plain +// (VarInt body length, body) frame that readPacket expects. +// +// A zero Data Length means the body was sent uncompressed, which vanilla +// servers accept even when the body exceeds the negotiated threshold. +type compressionReader struct { + r io.Reader + frame bytes.Buffer +} + +func newCompressionReader(r io.Reader) io.Reader { + return &compressionReader{r: r} +} + +func (c *compressionReader) Read(p []byte) (int, error) { + if c.frame.Len() == 0 { + if err := c.readFrame(); err != nil { + return 0, err + } + } + return c.frame.Read(p) +} + +func (c *compressionReader) readFrame() error { + packetLength, _, err := readVarintWithLength(c.r) + if err != nil { + return fmt.Errorf("read compressed packet length: %w", err) + } + if packetLength < 1 || int(packetLength) > maxCompressedFrameLength { + return fmt.Errorf("read compressed packet: bad length: %d", packetLength) + } + + dataLength, dataLengthSize, err := readVarintWithLength(c.r) + if err != nil { + return fmt.Errorf("read compressed data length: %w", err) + } + if dataLength < 0 || int(dataLength) > maxUncompressedFrameBody { + return fmt.Errorf("read compressed packet: bad uncompressed length: %d", dataLength) + } + + payloadLength := int(packetLength) - dataLengthSize + if payloadLength < 0 { + return fmt.Errorf("read compressed packet: bad length: %d", packetLength) + } + payload := make([]byte, payloadLength) + if _, err := io.ReadFull(c.r, payload); err != nil { + return fmt.Errorf("read compressed packet data: %w", err) + } + + body := payload + if dataLength > 0 { + zr, err := zlib.NewReader(bytes.NewReader(payload)) + if err != nil { + return fmt.Errorf("open compressed packet: %w", err) + } + body = make([]byte, int(dataLength)) + _, readErr := io.ReadFull(zr, body) + closeErr := zr.Close() + if readErr != nil { + return fmt.Errorf("decompress packet: %w", readErr) + } + if closeErr != nil { + return fmt.Errorf("close decompressor: %w", closeErr) + } + } + if len(body) < 1 { + return fmt.Errorf("read compressed packet: empty body") + } + + var frame bytes.Buffer + frame.Grow(varintSize(Varint(len(body))) + len(body)) + bodyLength := Varint(len(body)) + if err := bodyLength.writeTo(&frame); err != nil { + return err + } + frame.Write(body) + c.frame = frame + return nil +} + +// compressionWriter converts a plain frame (VarInt body length, body) into the +// post-Set Compression framing. Frames are buffered until complete, because a +// caller may split one frame across several Write calls. +type compressionWriter struct { + w io.Writer + threshold int32 + pending []byte +} + +func newCompressionWriter(w io.Writer, threshold int32) io.Writer { + return &compressionWriter{w: w, threshold: threshold} +} + +func (c *compressionWriter) Write(p []byte) (int, error) { + c.pending = append(c.pending, p...) + for { + body, ok, err := c.nextFrame() + if err != nil { + return 0, err + } + if !ok { + return len(p), nil + } + if err := c.writeFrame(body); err != nil { + return 0, err + } + } +} + +// nextFrame returns one complete frame body from the pending bytes. It only +// waits for more bytes while the length prefix or the body is still +// incomplete; a complete but invalid prefix is an error. +func (c *compressionWriter) nextFrame() ([]byte, bool, error) { + limit := len(c.pending) + if limit > 5 { + limit = 5 + } + bodyLength, prefixLength, err := readVarintWithLength(bytes.NewReader(c.pending[:limit])) + if err != nil { + if len(c.pending) < 5 { + return nil, false, nil + } + return nil, false, fmt.Errorf("read frame length: %w", err) + } + if bodyLength < 1 { + return nil, false, fmt.Errorf("read frame: bad length: %d", bodyLength) + } + end := prefixLength + int(bodyLength) + if end > len(c.pending) { + return nil, false, nil + } + + // The body stays valid after the reslice: appends only write past the + // remaining pending length and writeFrame consumes it immediately. + body := c.pending[prefixLength:end] + c.pending = c.pending[end:] + return body, true, nil +} + +func (c *compressionWriter) writeFrame(body []byte) error { + if len(body) >= int(c.threshold) { + var compressed bytes.Buffer + zw := zlib.NewWriter(&compressed) + if _, err := zw.Write(body); err != nil { + _ = zw.Close() + return fmt.Errorf("compress packet: %w", err) + } + if err := zw.Close(); err != nil { + return fmt.Errorf("close compressor: %w", err) + } + + var frame bytes.Buffer + packetLength := varintSize(Varint(len(body))) + compressed.Len() + frame.Grow(varintSize(Varint(packetLength)) + packetLength) + packetLengthVarint := Varint(packetLength) + if err := packetLengthVarint.writeTo(&frame); err != nil { + return err + } + bodyLength := Varint(len(body)) + if err := bodyLength.writeTo(&frame); err != nil { + return err + } + frame.Write(compressed.Bytes()) + return writeFull(c.w, frame.Bytes()) + } + + var frame bytes.Buffer + frame.Grow(varintSize(Varint(1+len(body))) + 1 + len(body)) + frameLength := Varint(1 + len(body)) + if err := frameLength.writeTo(&frame); err != nil { + return err + } + frame.WriteByte(0) + frame.Write(body) + return writeFull(c.w, frame.Bytes()) +} diff --git a/transport/internet/finalmask/xmc/compression_test.go b/transport/internet/finalmask/xmc/compression_test.go new file mode 100644 index 000000000..e3a52b7c0 --- /dev/null +++ b/transport/internet/finalmask/xmc/compression_test.go @@ -0,0 +1,220 @@ +package xmc + +import ( + "bytes" + "crypto/rand" + "errors" + "io" + "testing" +) + +func plainFrame(body []byte) []byte { + frame := make([]byte, 0, 5+len(body)) + length := Varint(len(body)) + var buf bytes.Buffer + _ = length.writeTo(&buf) + frame = append(frame, buf.Bytes()...) + return append(frame, body...) +} + +type wireFrame struct { + packetLength int + dataLength int + payload []byte +} + +func readWireFrame(t *testing.T, r *bytes.Reader) wireFrame { + t.Helper() + packetLength, _, err := readVarintWithLength(r) + if err != nil { + t.Fatalf("read packet length: %v", err) + } + dataLength, dataLengthSize, err := readVarintWithLength(r) + if err != nil { + t.Fatalf("read data length: %v", err) + } + payload := make([]byte, int(packetLength)-dataLengthSize) + if _, err := io.ReadFull(r, payload); err != nil { + t.Fatalf("read payload: %v", err) + } + return wireFrame{packetLength: int(packetLength), dataLength: int(dataLength), payload: payload} +} + +func randomBody(t *testing.T, size int) []byte { + t.Helper() + body := make([]byte, size) + if _, err := rand.Read(body); err != nil { + t.Fatalf("random body: %v", err) + } + return body +} + +func TestCompressionWriterThreshold(t *testing.T) { + for _, threshold := range []int32{0, 1, 256, 65536} { + for _, size := range []int{1, 10, 255, 256, 257, 65535, 65536} { + body := bytes.Repeat([]byte{0}, size) + var wire bytes.Buffer + w := newCompressionWriter(&wire, threshold) + if _, err := w.Write(plainFrame(body)); err != nil { + t.Fatalf("threshold %d size %d: write: %v", threshold, size, err) + } + + frame := readWireFrame(t, bytes.NewReader(wire.Bytes())) + wantCompressed := int32(size) >= threshold + if wantCompressed && frame.dataLength != size { + t.Fatalf("threshold %d size %d: want compressed data length %d, got %d", threshold, size, size, frame.dataLength) + } + if !wantCompressed { + if frame.dataLength != 0 { + t.Fatalf("threshold %d size %d: want uncompressed frame, got data length %d", threshold, size, frame.dataLength) + } + if !bytes.Equal(frame.payload, body) { + t.Fatalf("threshold %d size %d: uncompressed payload mismatch", threshold, size) + } + if frame.packetLength != 1+size { + t.Fatalf("threshold %d size %d: packet length %d", threshold, size, frame.packetLength) + } + } + + decoded, _, err := readFrame(newCompressionReader(bytes.NewReader(wire.Bytes())), maxUncompressedFrameBody) + if err != nil { + t.Fatalf("threshold %d size %d: decode: %v", threshold, size, err) + } + if !bytes.Equal(decoded, body) { + t.Fatalf("threshold %d size %d: round trip mismatch", threshold, size) + } + } + } +} + +func TestCompressionRoundTripChunked(t *testing.T) { + cases := [][]byte{ + []byte("hello"), + randomBody(t, 255), + randomBody(t, 24*1024), + randomBody(t, 64*1024), + bytes.Repeat([]byte("xmc"), 8*1024), + } + + for _, threshold := range []int32{0, 256, 65536} { + var wire bytes.Buffer + w := newCompressionWriter(&wire, threshold) + // One Write per frame, split into awkward chunk sizes. + for _, body := range cases { + frame := plainFrame(body) + for len(frame) > 0 { + n := 7 + if n > len(frame) { + n = len(frame) + } + if _, err := w.Write(frame[:n]); err != nil { + t.Fatalf("threshold %d: chunked write: %v", threshold, err) + } + frame = frame[n:] + } + } + + r := newCompressionReader(bytes.NewReader(wire.Bytes())) + for i, want := range cases { + got, _, err := readFrame(r, maxUncompressedFrameBody) + if err != nil { + t.Fatalf("threshold %d frame %d: decode: %v", threshold, i, err) + } + if !bytes.Equal(got, want) { + t.Fatalf("threshold %d frame %d: mismatch (%d vs %d bytes)", threshold, i, len(got), len(want)) + } + } + if _, _, err := readFrame(r, maxUncompressedFrameBody); !errors.Is(err, io.EOF) { + t.Fatalf("threshold %d: expected EOF after the last frame, got %v", threshold, err) + } + } +} + +func TestCompressionMultipleFramesOneWrite(t *testing.T) { + var plain []byte + bodies := [][]byte{[]byte("a"), randomBody(t, 1024), randomBody(t, 300)} + for _, body := range bodies { + plain = append(plain, plainFrame(body)...) + } + + var wire bytes.Buffer + w := newCompressionWriter(&wire, 256) + if _, err := w.Write(plain); err != nil { + t.Fatalf("write: %v", err) + } + + r := newCompressionReader(bytes.NewReader(wire.Bytes())) + for i, want := range bodies { + got, _, err := readFrame(r, maxUncompressedFrameBody) + if err != nil { + t.Fatalf("frame %d: decode: %v", i, err) + } + if !bytes.Equal(got, want) { + t.Fatalf("frame %d: mismatch", i) + } + } +} + +// Vanilla servers accept uncompressed packets even when the body exceeds the +// negotiated threshold, which is what lets padding frames keep their wire size. +func TestCompressionReaderUncompressedOverThreshold(t *testing.T) { + body := randomBody(t, 64*1024) + var wire bytes.Buffer + length := Varint(1 + len(body)) + if err := length.writeTo(&wire); err != nil { + t.Fatal(err) + } + wire.WriteByte(0) + wire.Write(body) + + got, _, err := readFrame(newCompressionReader(bytes.NewReader(wire.Bytes())), maxUncompressedFrameBody) + if err != nil { + t.Fatalf("decode: %v", err) + } + if !bytes.Equal(got, body) { + t.Fatal("payload mismatch") + } +} + +func TestCompressionReaderErrors(t *testing.T) { + // Compressed packet with a bogus body. + var badZlib bytes.Buffer + length := Varint(1 + 10) + _ = length.writeTo(&badZlib) + dataLength := Varint(64) + _ = dataLength.writeTo(&badZlib) + badZlib.Write(bytes.Repeat([]byte{0xff}, 10)) + if _, _, err := readFrame(newCompressionReader(bytes.NewReader(badZlib.Bytes())), maxUncompressedFrameBody); err == nil { + t.Fatal("expected an error for an invalid zlib body") + } + + // Uncompressed length beyond the vanilla limit. + var tooLong bytes.Buffer + length = Varint(1 + 4) + _ = length.writeTo(&tooLong) + dataLength = Varint(maxUncompressedFrameBody + 1) + _ = dataLength.writeTo(&tooLong) + tooLong.Write([]byte{1, 2, 3, 4}) + if _, _, err := readFrame(newCompressionReader(bytes.NewReader(tooLong.Bytes())), maxUncompressedFrameBody); err == nil { + t.Fatal("expected an error for an oversized uncompressed length") + } + + // Truncated payload. + var truncated bytes.Buffer + length = Varint(1 + 100) + _ = length.writeTo(&truncated) + var zero Varint + _ = zero.writeTo(&truncated) + truncated.Write([]byte{1, 2, 3}) + if _, _, err := readFrame(newCompressionReader(bytes.NewReader(truncated.Bytes())), maxUncompressedFrameBody); err == nil { + t.Fatal("expected an error for a truncated payload") + } +} + +func TestCompressionWriterInvalidFrameLength(t *testing.T) { + var wire bytes.Buffer + w := newCompressionWriter(&wire, 256) + if _, err := w.Write([]byte{0xff, 0xff, 0xff, 0xff, 0xff}); err == nil { + t.Fatal("expected an error for an invalid frame length prefix") + } +} diff --git a/transport/internet/finalmask/xmc/config.go b/transport/internet/finalmask/xmc/config.go index c33447cc7..ab7cea839 100644 --- a/transport/internet/finalmask/xmc/config.go +++ b/transport/internet/finalmask/xmc/config.go @@ -2,11 +2,22 @@ package xmc import ( "fmt" + "math" + "time" "github.com/xtls/xray-core/common/net" "github.com/xtls/xray-core/transport/internet/finalmask" ) +// maxPaddingDelayMilliseconds keeps the millisecond value convertible to a +// time.Duration on every platform. +const maxPaddingDelayMilliseconds = int64(math.MaxInt64 / int64(time.Millisecond)) + +// MaxLegacyPasswordLength is the longest pre-shared password the login can +// carry. The verify token (4 random bytes) and the password travel together in +// one RSA-1024 PKCS#1 v1.5 block, which holds at most 128-11 bytes. +const MaxLegacyPasswordLength = 128 - 11 - 4 + func (c *Config) WrapConnClient(conn net.Conn, dest *net.Destination, dialer *finalmask.Dialer) (net.Conn, error) { profiles, err := profilesFromConfig(c.Profiles) if err != nil { @@ -48,8 +59,11 @@ func paddingScheduleFromConfig(paddings []*Padding) ([]paddingTurn, error) { if turn == nil || turn.LengthMin < 1 || turn.LengthMax < turn.LengthMin || turn.LengthMax > maxPaddingTurnLength { return nil, fmt.Errorf("invalid padding length range at turn %d", i) } - if turn.Delay < 0 { - return nil, fmt.Errorf("invalid padding delay %d at turn %d (must be non-negative)", turn.Delay, i) + if turn.DelayMin < 0 || turn.DelayMax < turn.DelayMin { + return nil, fmt.Errorf("invalid padding delay range %d-%d at turn %d (must be non-negative and ordered)", turn.DelayMin, turn.DelayMax, i) + } + if turn.DelayMax > maxPaddingDelayMilliseconds { + return nil, fmt.Errorf("padding delay range %d-%d at turn %d is too large", turn.DelayMin, turn.DelayMax, i) } var direction paddingDirection switch turn.Direction { @@ -64,7 +78,10 @@ func paddingScheduleFromConfig(paddings []*Padding) ([]paddingTurn, error) { direction: direction, minLength: int(turn.LengthMin), maxLength: int(turn.LengthMax), - delay: int(turn.Delay), + delay: paddingDelayRange{ + min: time.Duration(turn.DelayMin) * time.Millisecond, + max: time.Duration(turn.DelayMax) * time.Millisecond, + }, } } // The first turn includes the two-byte Login Acknowledged packet. diff --git a/transport/internet/finalmask/xmc/config.pb.go b/transport/internet/finalmask/xmc/config.pb.go index a707eee94..8db565a15 100644 --- a/transport/internet/finalmask/xmc/config.pb.go +++ b/transport/internet/finalmask/xmc/config.pb.go @@ -183,8 +183,11 @@ type Padding struct { LengthMax int64 `protobuf:"varint,2,opt,name=length_max,json=lengthMax,proto3" json:"length_max,omitempty"` // 1 = client to server, 2 = server to client Direction int32 `protobuf:"varint,3,opt,name=direction,proto3" json:"direction,omitempty"` - // Delay in milliseconds after sending this turn - Delay int64 `protobuf:"varint,4,opt,name=delay,proto3" json:"delay,omitempty"` + // Millisecond pause after this turn, including the last one. The sender + // waits for a uniformly selected value in [delay_min, delay_max]; the + // receiver waits at least delay_min. Zero means no delay. + DelayMin int64 `protobuf:"varint,4,opt,name=delay_min,json=delayMin,proto3" json:"delay_min,omitempty"` + DelayMax int64 `protobuf:"varint,5,opt,name=delay_max,json=delayMax,proto3" json:"delay_max,omitempty"` unknownFields protoimpl.UnknownFields sizeCache protoimpl.SizeCache } @@ -240,9 +243,16 @@ func (x *Padding) GetDirection() int32 { return 0 } -func (x *Padding) GetDelay() int64 { +func (x *Padding) GetDelayMin() int64 { if x != nil { - return x.Delay + return x.DelayMin + } + return 0 +} + +func (x *Padding) GetDelayMax() int64 { + if x != nil { + return x.DelayMax } return 0 } @@ -264,14 +274,15 @@ const file_transport_internet_finalmask_xmc_config_proto_rawDesc = "" + "\bhostname\x18\n" + " \x01(\tR\bhostname\x12J\n" + "\bprofiles\x18\v \x03(\v2..xray.transport.internet.finalmask.xmc.ProfileR\bprofiles\x12J\n" + - "\bpaddings\x18\f \x03(\v2..xray.transport.internet.finalmask.xmc.PaddingR\bpaddingsJ\x04\b\x02\x10\x03\"{\n" + + "\bpaddings\x18\f \x03(\v2..xray.transport.internet.finalmask.xmc.PaddingR\bpaddingsJ\x04\b\x02\x10\x03\"\x9f\x01\n" + "\aPadding\x12\x1d\n" + "\n" + "length_min\x18\x01 \x01(\x03R\tlengthMin\x12\x1d\n" + "\n" + "length_max\x18\x02 \x01(\x03R\tlengthMax\x12\x1c\n" + - "\tdirection\x18\x03 \x01(\x05R\tdirection\x12\x14\n" + - "\x05delay\x18\x04 \x01(\x03R\x05delayB\x91\x01\n" + + "\tdirection\x18\x03 \x01(\x05R\tdirection\x12\x1b\n" + + "\tdelay_min\x18\x04 \x01(\x03R\bdelayMin\x12\x1b\n" + + "\tdelay_max\x18\x05 \x01(\x03R\bdelayMaxB\x91\x01\n" + ")com.xray.transport.internet.finalmask.xmcP\x01Z:github.com/xtls/xray-core/transport/internet/finalmask/xmc\xaa\x02%Xray.Transport.Internet.Finalmask.XMCb\x06proto3" var ( diff --git a/transport/internet/finalmask/xmc/config.proto b/transport/internet/finalmask/xmc/config.proto index de8c6d5bc..153921eb3 100644 --- a/transport/internet/finalmask/xmc/config.proto +++ b/transport/internet/finalmask/xmc/config.proto @@ -32,6 +32,9 @@ message Padding { int64 length_max = 2; // 1 = client to server, 2 = server to client int32 direction = 3; - // Delay in milliseconds after sending this turn - int64 delay = 4; + // Millisecond pause after this turn, including the last one. The sender + // waits for a uniformly selected value in [delay_min, delay_max]; the + // receiver waits at least delay_min. Zero means no delay. + int64 delay_min = 4; + int64 delay_max = 5; } diff --git a/transport/internet/finalmask/xmc/config_test.go b/transport/internet/finalmask/xmc/config_test.go new file mode 100644 index 000000000..461f0a58b --- /dev/null +++ b/transport/internet/finalmask/xmc/config_test.go @@ -0,0 +1,23 @@ +package xmc + +import ( + "net" + "strings" + "testing" +) + +func TestClientRejectsLongLegacyPassword(t *testing.T) { + client, server := net.Pipe() + defer client.Close() + defer server.Close() + + profiles := []loginProfile{{Username: "TestUser"}} + publicKey := []byte{0x01} + + if _, err := newClientConn(client, profiles, strings.Repeat("p", MaxLegacyPasswordLength), publicKey, "localhost", nil); err != nil { + t.Fatalf("password of %d bytes was rejected: %v", MaxLegacyPasswordLength, err) + } + if _, err := newClientConn(client, profiles, strings.Repeat("p", MaxLegacyPasswordLength+1), publicKey, "localhost", nil); err == nil { + t.Fatal("expected the long password to be rejected") + } +} diff --git a/transport/internet/finalmask/xmc/padding.go b/transport/internet/finalmask/xmc/padding.go index 2343c440b..8fb841bc4 100644 --- a/transport/internet/finalmask/xmc/padding.go +++ b/transport/internet/finalmask/xmc/padding.go @@ -1,7 +1,6 @@ package xmc import ( - "bytes" "crypto/rand" "fmt" "io" @@ -18,8 +17,21 @@ const ( paddingBufferLength = 16 * 1024 maxPaddingChunkLength = 48 * 1024 maxPaddingTurnLength = 8 * 1024 * 1024 + + // Frame padding carries the startup padding in ordinary Minecraft custom + // payload packets so that a vanilla server can parse the whole connection + // without taking the pipeline over. The channel is deliberately different + // from the packet stream channel: both ends drop unknown payloads. + paddingFrameChannel = "xmc:pad" + // Serverbound custom payload data is capped at 32767 bytes by + // ServerboundCustomPayloadPacket; keep the same budget in both directions. + maxPaddingFrameDataLength = 32767 + // Length varint, packet id, channel length varint and the channel itself. + paddingFrameOverhead = 1 + 1 + 1 + len(paddingFrameChannel) ) +var maxPaddingFrameLength = paddingFrameLength(maxPaddingFrameDataLength) + type paddingVariant struct { chunks []int delays []paddingDelayRange @@ -34,7 +46,7 @@ type paddingTurn struct { direction paddingDirection minLength int maxLength int - delay int // milliseconds delay after sending this turn + delay paddingDelayRange // pause after this turn, including the last one variants []paddingVariant startDelay paddingDelayRange chunkDelay paddingDelayRange @@ -45,30 +57,40 @@ type paddingTurn struct { sendVariants []int } -func runPaddingSchedule(reader io.Reader, writer io.Writer, isClient bool, firstTurnPrefixLength int, schedule []paddingTurn) error { +func runPaddingSchedule(writer io.Writer, isClient bool, firstTurnPrefixLength int, schedule []paddingTurn) error { + return runPaddingScheduleWithSleep(writer, isClient, firstTurnPrefixLength, schedule, time.Sleep) +} + +func runPaddingScheduleWithSleep(writer io.Writer, isClient bool, firstTurnPrefixLength int, schedule []paddingTurn, sleep func(time.Duration)) error { if err := validatePaddingSchedule(schedule, firstTurnPrefixLength); err != nil { return err } - var writeBuffer []byte for i, turn := range schedule { prefixLength := 0 if i == 0 { prefixLength = firstTurnPrefixLength } - localSends := isClient == (turn.direction == paddingClientToServer) - if localSends { - if err := writePaddingTurnWithBuffer(writer, turn, prefixLength, time.Sleep, &writeBuffer); err != nil { - return fmt.Errorf("write padding turn %d: %w", i, err) - } - if turn.delay > 0 { - time.Sleep(time.Duration(turn.delay) * time.Millisecond) + if isClient != (turn.direction == paddingClientToServer) { + // Padding is ordinary Minecraft traffic, so the peer's turns are + // never consumed here: they stay in the stream and are dropped as + // unknown packets by whoever reads it next. Both endpoints still + // apply the same minimum pause, including after the last turn. + if turn.delay.min > 0 { + sleep(turn.delay.min) } continue } - if err := readPaddingTurn(reader, turn, prefixLength); err != nil { - return fmt.Errorf("read padding turn %d: %w", i, err) + if _, err := writePaddingTurnFrames(writer, turn, prefixLength, sleep, paddingFramePacketID(isClient)); err != nil { + return fmt.Errorf("write padding turn %d: %w", i, err) + } + delay, err := randomPaddingDelay(turn.delay) + if err != nil { + return fmt.Errorf("select padding turn %d delay: %w", i, err) + } + if delay > 0 { + sleep(delay) } } return nil @@ -95,6 +117,9 @@ func validatePaddingSchedule(schedule []paddingTurn, firstTurnPrefixLength int) if err := validatePaddingDelayRange(turn.chunkDelay); err != nil { return fmt.Errorf("padding turn %d has an invalid chunk delay: %w", i, err) } + if err := validatePaddingDelayRange(turn.delay); err != nil { + return fmt.Errorf("padding turn %d has an invalid inter-turn delay: %w", i, err) + } if turn.writeChunkMinLength < 0 || turn.writeChunkLength < turn.writeChunkMinLength || turn.writeChunkLength > maxPaddingChunkLength { return fmt.Errorf("padding turn %d has an invalid write chunk range: %d-%d", i, turn.writeChunkMinLength, turn.writeChunkLength) } @@ -129,18 +154,23 @@ func validatePaddingSchedule(schedule []paddingTurn, firstTurnPrefixLength int) return nil } -func writePaddingTurn(w io.Writer, turn paddingTurn, prefixLength int) error { - return writePaddingTurnWithSleep(w, turn, prefixLength, time.Sleep) +// paddingFramePacketID returns the configuration-state custom payload packet id +// used by the side that sends the padding bytes; both values come from the +// vanilla configuration protocol, so no proxy-specific id ever reaches the wire. +func paddingFramePacketID(isClient bool) int { + if isClient { + return configurationServerboundCustomPayload + } + return configurationClientboundCustomPayload } -func writePaddingTurnWithSleep(w io.Writer, turn paddingTurn, prefixLength int, sleep func(time.Duration)) error { - return writePaddingTurnWithBuffer(w, turn, prefixLength, sleep, nil) -} - -func writePaddingTurnWithBuffer(w io.Writer, turn paddingTurn, prefixLength int, sleep func(time.Duration), reusableBuffer *[]byte) error { +// writePaddingTurnFrames writes one padding turn as a sequence of valid +// Minecraft custom payload frames. Only the framing is native: payload bodies +// stay zero-filled, so the captured traffic shape is kept. +func writePaddingTurnFrames(w io.Writer, turn paddingTurn, prefixLength int, sleep func(time.Duration), packetID int) (int, error) { startDelay, err := randomPaddingDelay(turn.startDelay) if err != nil { - return fmt.Errorf("select padding start delay: %w", err) + return 0, fmt.Errorf("select padding start delay: %w", err) } if startDelay > 0 { sleep(startDelay) @@ -148,17 +178,11 @@ func writePaddingTurnWithBuffer(w io.Writer, turn paddingTurn, prefixLength int, targetLength, chunks, delays, err := selectPaddingVariant(turn, prefixLength) if err != nil { - return err + return 0, err } - recordLength := targetLength - prefixLength - if recordLength < 1 { - return fmt.Errorf("target length %d leaves an invalid record length %d", targetLength, recordLength) - } - - encodedLength := Varint(recordLength) - var header bytes.Buffer - if err = encodedLength.writeTo(&header); err != nil { - return fmt.Errorf("write padding header: %w", err) + payloadLength := targetLength - prefixLength + if payloadLength < 1 { + return 0, fmt.Errorf("target length %d leaves an invalid padding payload of %d bytes", targetLength, payloadLength) } if len(chunks) == 0 { writeChunkLength := turn.writeChunkLength @@ -167,89 +191,103 @@ func writePaddingTurnWithBuffer(w io.Writer, turn paddingTurn, prefixLength int, } else if turn.writeChunkMinLength > 0 { writeChunkLength, err = randomPaddingTarget(turn.writeChunkMinLength, writeChunkLength) if err != nil { - return fmt.Errorf("select padding write chunk length: %w", err) + return 0, fmt.Errorf("select padding write chunk length: %w", err) } } - chunks = defaultPaddingChunks(recordLength, writeChunkLength) + chunks = defaultPaddingChunks(payloadLength, writeChunkLength) delays = make([]paddingDelayRange, len(chunks)) for i := 1; i < len(delays); i++ { delays[i] = turn.chunkDelay } } - if chunks[0] < header.Len() { - return fmt.Errorf("first padding chunk %d is shorter than header %d", chunks[0], header.Len()) - } - maxChunkLength := 0 - for _, chunkLength := range chunks { - if chunkLength < 1 || chunkLength > maxPaddingChunkLength { - return fmt.Errorf("invalid padding chunk length: %d", chunkLength) - } - maxChunkLength = max(maxChunkLength, chunkLength) - } - var buffer []byte - if reusableBuffer == nil { - buffer = make([]byte, maxChunkLength) - } else { - if cap(*reusableBuffer) < maxChunkLength { - *reusableBuffer = make([]byte, maxChunkLength) - } - buffer = (*reusableBuffer)[:maxChunkLength] - clear(buffer) - } - copy(buffer, header.Bytes()) + zeros := make([]byte, maxPaddingFrameDataLength) + channel := String(paddingFrameChannel) written := 0 for i, chunkLength := range chunks { if i < len(delays) { delay, delayErr := randomPaddingDelay(delays[i]) if delayErr != nil { - return fmt.Errorf("select padding chunk %d delay: %w", i, delayErr) + return written, fmt.Errorf("select padding chunk %d delay: %w", i, delayErr) } if delay > 0 { sleep(delay) } } - if err = writeFull(w, buffer[:chunkLength]); err != nil { - return fmt.Errorf("write padding chunk %d: %w", i, err) + frameLengths, planErr := paddingFramePlan(chunkLength) + if planErr != nil { + return written, planErr } - written += chunkLength - if i == 0 { - clear(buffer[:header.Len()]) + for _, frameLength := range frameLengths { + dataLength, ok := paddingFrameDataLength(frameLength) + if !ok { + return written, fmt.Errorf("unframed padding length %d", frameLength) + } + payload := RestBytes(zeros[:dataLength]) + if err = writePacket(w, packetID, &channel, &payload); err != nil { + return written, fmt.Errorf("write padding frame: %w", err) + } + written += frameLength } } - if written != recordLength { - return fmt.Errorf("padding chunks total %d, want %d", written, recordLength) - } - return nil + return written, nil } -func readPaddingTurn(r io.Reader, turn paddingTurn, prefixLength int) error { - encodedLength, headerLength, err := readVarintWithLength(r) - if err != nil { - return fmt.Errorf("read padding header: %w", err) - } - recordLength := int(encodedLength) - if recordLength < headerLength || recordLength > maxPaddingTurnLength { - return fmt.Errorf("invalid padding record length: %d", recordLength) - } - totalLength := prefixLength + recordLength - if !paddingTurnAcceptsLength(turn, totalLength) { - if len(turn.variants) > 0 { - return fmt.Errorf("padding turn length %d is not an allowed variant", totalLength) - } - return fmt.Errorf("padding turn length %d is outside %d-%d", totalLength, turn.minLength, turn.maxLength) - } +// paddingFrameLength is the wire size of a custom payload frame whose payload +// body is dataLength bytes long. +func paddingFrameLength(dataLength int) int { + bodyLength := 1 + 1 + len(paddingFrameChannel) + dataLength + return varintSize(Varint(bodyLength)) + bodyLength +} - var buffer [paddingBufferLength]byte - remaining := recordLength - headerLength - for remaining > 0 { - chunkLength := min(remaining, len(buffer)) - if _, err := io.ReadFull(r, buffer[:chunkLength]); err != nil { - return fmt.Errorf("read padding body: %w", err) +// paddingFrameDataLength is the payload body that makes a frame exactly +// frameLength bytes long. Some lengths are unreachable because the length +// varint grows in steps. +func paddingFrameDataLength(frameLength int) (int, bool) { + for overhead := paddingFrameOverhead; overhead <= paddingFrameOverhead+2; overhead++ { + dataLength := frameLength - overhead + if dataLength < 0 || dataLength > maxPaddingFrameDataLength { + continue + } + if paddingFrameLength(dataLength) == frameLength { + return dataLength, true } - remaining -= chunkLength } - return nil + return 0, false +} + +// paddingFramePlan splits length bytes into reachable frame sizes. The result is +// exact whenever a partition exists; a length below the smallest frame (the +// captured 2-byte turns) is rounded up to that frame. +func paddingFramePlan(length int) ([]int, error) { + if length <= 0 { + return nil, nil + } + if length < paddingFrameOverhead { + return []int{paddingFrameOverhead}, nil + } + frameLengths := make([]int, 0, length/paddingBufferLength+1) + for remaining := length; remaining > 0; { + candidate := min(remaining, maxPaddingFrameLength) + picked := 0 + for ; candidate >= paddingFrameOverhead; candidate-- { + if _, ok := paddingFrameDataLength(candidate); !ok { + continue + } + rest := remaining - candidate + if rest > 0 && rest < paddingFrameOverhead { + continue + } + picked = candidate + break + } + if picked == 0 { + return nil, fmt.Errorf("cannot frame %d padding bytes", length) + } + frameLengths = append(frameLengths, picked) + remaining -= picked + } + return frameLengths, nil } func selectPaddingVariant(turn paddingTurn, prefixLength int) (int, []int, []paddingDelayRange, error) { diff --git a/transport/internet/finalmask/xmc/padding_frames_test.go b/transport/internet/finalmask/xmc/padding_frames_test.go new file mode 100644 index 000000000..390e39c92 --- /dev/null +++ b/transport/internet/finalmask/xmc/padding_frames_test.go @@ -0,0 +1,137 @@ +package xmc + +import ( + "bytes" + "testing" + "time" +) + +func TestPaddingFrameDataLengthRoundTrip(t *testing.T) { + for dataLength := 0; dataLength <= maxPaddingFrameDataLength; dataLength++ { + frameLength := paddingFrameLength(dataLength) + got, ok := paddingFrameDataLength(frameLength) + if !ok || got != dataLength { + t.Fatalf("data length %d -> frame %d -> data %d (ok=%v)", dataLength, frameLength, got, ok) + } + } +} + +func TestPaddingFramePlanTotals(t *testing.T) { + lengths := []int{paddingFrameOverhead, paddingFrameOverhead + 1, 128, 129, 130, 131, 140, 1024, 48 * 1024, maxPaddingFrameLength - 1, maxPaddingFrameLength, maxPaddingFrameLength + 1, 3 * maxPaddingFrameLength, 1024 * 1024} + for _, length := range lengths { + plan, err := paddingFramePlan(length) + if err != nil { + t.Fatalf("plan %d: %v", length, err) + } + total := 0 + for _, frameLength := range plan { + if _, ok := paddingFrameDataLength(frameLength); !ok { + t.Fatalf("plan %d contains unreachable frame %d", length, frameLength) + } + if frameLength > maxPaddingFrameLength { + t.Fatalf("plan %d contains oversized frame %d", length, frameLength) + } + total += frameLength + } + if total != length { + t.Fatalf("plan %d totals %d", length, total) + } + } +} + +func TestPaddingFramePlanRoundsUpShortTurn(t *testing.T) { + plan, err := paddingFramePlan(2) + if err != nil { + t.Fatal(err) + } + if len(plan) != 1 || plan[0] != paddingFrameOverhead { + t.Fatalf("short turn plan = %v", plan) + } +} + +func TestWritePaddingTurnFramesIsMinecraftTraffic(t *testing.T) { + turns := []paddingTurn{ + {direction: paddingClientToServer, minLength: 44, maxLength: 44}, + {direction: paddingClientToServer, minLength: 2, maxLength: 2}, + {direction: paddingClientToServer, minLength: maxPaddingFrameLength, maxLength: maxPaddingFrameLength}, + {direction: paddingClientToServer, minLength: 3*maxPaddingFrameLength + 7, maxLength: 3*maxPaddingFrameLength + 7}, + } + for i, turn := range turns { + var buffer bytes.Buffer + written, err := writePaddingTurnFrames(&buffer, turn, 0, func(time.Duration) {}, configurationServerboundCustomPayload) + if err != nil { + t.Fatalf("turn %d: %v", i, err) + } + if written != buffer.Len() { + t.Fatalf("turn %d wrote %d bytes, buffer has %d", i, written, buffer.Len()) + } + target := turn.minLength + if written < target { + t.Fatalf("turn %d wrote %d bytes, want at least %d", i, written, target) + } + + reader := bytes.NewReader(buffer.Bytes()) + frames := 0 + for reader.Len() > 0 { + packet, err := readPacket(reader) + if err != nil { + t.Fatalf("turn %d frame %d is not a valid packet: %v", i, frames, err) + } + if packet.packetID != configurationServerboundCustomPayload { + t.Fatalf("turn %d frame %d has packet id %d", i, frames, packet.packetID) + } + _, ok, err := parseCustomPayload(packet) + if err != nil { + t.Fatalf("turn %d frame %d: %v", i, frames, err) + } + if ok { + t.Fatalf("turn %d frame %d uses the packet stream channel", i, frames) + } + if len(packet.data) == 0 { + t.Fatalf("turn %d frame %d has no channel", i, frames) + } + frames++ + } + } +} + +func TestRunPaddingScheduleLeavesPeerTurnsAlone(t *testing.T) { + schedule := []paddingTurn{ + {direction: paddingClientToServer, minLength: 44, maxLength: 44}, + {direction: paddingServerToClient, minLength: 64, maxLength: 64}, + {direction: paddingClientToServer, minLength: 25, maxLength: 25}, + } + + var out bytes.Buffer + if err := runPaddingSchedule(&out, true, 2, schedule); err != nil { + t.Fatal(err) + } + + reader := bytes.NewReader(out.Bytes()) + total := 0 + for reader.Len() > 0 { + before := reader.Len() + packet, err := readPacket(reader) + if err != nil { + t.Fatalf("padding stream is not a valid packet stream: %v", err) + } + if packet.packetID != configurationServerboundCustomPayload { + t.Fatalf("unexpected packet id %d in padding stream", packet.packetID) + } + total += before - reader.Len() + } + // Turn 0 carries the 2 byte Login Acknowledged prefix, turn 1 is the peer's. + want := 44 - 2 + 25 + if total != want { + t.Fatalf("padding stream carries %d payload bytes, want %d", total, want) + } +} + +func TestRunPaddingScheduleRejectsInvalidSchedule(t *testing.T) { + schedule := []paddingTurn{ + {direction: paddingServerToClient, minLength: 10, maxLength: 10}, + } + if err := runPaddingSchedule(&bytes.Buffer{}, true, 2, schedule); err == nil { + t.Fatal("expected an error for a prefixed server-to-client first turn") + } +} diff --git a/transport/internet/finalmask/xmc/protocol.go b/transport/internet/finalmask/xmc/protocol.go index a636d413d..8b53b4442 100644 --- a/transport/internet/finalmask/xmc/protocol.go +++ b/transport/internet/finalmask/xmc/protocol.go @@ -8,7 +8,11 @@ import ( ) const ( - maxPacketDataLength = 32 * 1024 + // Clientbound custom payloads are allowed up to 1 MiB by vanilla, and a + // vanilla configuration state can send large registry data packets. This is + // only an upper bound for decoding; the XMC payload stream keeps its own, + // much smaller, chunk size. + maxPacketDataLength = 1024 * 1024 maxPacketBodyLength = maxPacketDataLength + 5 ) diff --git a/transport/internet/finalmask/xmc/server.go b/transport/internet/finalmask/xmc/server.go index 6b3b79f49..30748017b 100644 --- a/transport/internet/finalmask/xmc/server.go +++ b/transport/internet/finalmask/xmc/server.go @@ -245,7 +245,7 @@ func (c *serverConn) handshake() error { if err = validateLoginAcknowledgedPacket(pkt); err != nil { return err } - if err = runPaddingSchedule(c.reader, c.writer, false, loginAcknowledgedLength, c.paddingSchedule); err != nil { + if err = runPaddingSchedule(c.writer, false, loginAcknowledgedLength, c.paddingSchedule); err != nil { return fmt.Errorf("run startup padding: %w", err) }