Skip to content

XMC finalmask: Switch to native frame padding (breaking); delay supports range (receiver waits at least minimal ms) - #7402

Merged
RPRX merged 11 commits into
XTLS:mainfrom
yandujun363:takeover-6769
Nov 16, 2026
Merged

RPRX merged 11 commits into
XTLS:mainfrom
yandujun363:takeover-6769

Conversation

@yandujun363

@yandujun363 yandujun363 commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Follow-up to #6769 (merged in 5d17b872): completes the remaining delay requirement and, per this reply, switches padding to the more realistic wire format even though that is a breaking change.

Delay

  • delay is now a string: "100" or "50-150" (milliseconds). A number written against XMC finalmask: Make startup paddings configurable (direction, length and delay) #6769 no longer parses, so this is a configuration-level breaking change as well.
  • The sender waits a uniformly sampled value in [min, max] after its turn.
  • The receiver waits at least min after each turn, including the last one, before the proxy stream starts.
  • config.proto: delay becomes delay_min / delay_max; negative, reversed and duration-overflowing ranges are rejected at config load.
  • infra/conf: parses the string delay; tests cover fixed values, ranges and invalid forms.

Native frame padding (ea2c7edc, breaking)

The opaque format wrote a length field that includes its own varint. A vanilla server therefore split the stream one header short and had to take the protocol pipeline over — something a Fabric server mod cannot do without affecting ordinary players.

  • One padding chunk becomes one or more configuration-state custom payload frames on xmc:pad; the length field now has the vanilla meaning (bytes after the field).
  • Chunks above the 32767-byte serverbound payload limit are split; captured 2-byte turns send the smallest possible frame.
  • Nothing reads padding any more: it is ordinary traffic that the next reader drops as an unknown packet. Both ends only send and apply the configured pauses.
  • The first turn still counts the two Login Acknowledged bytes as its prefix; they are not repeated.
  • maxPacketDataLength is raised from 32 KiB to 1 MiB (vanilla's clientbound custom payload limit), which a real configuration state can reach with registry data.

Breaking: both ends must run this version. No proto field was added for padding; the paddings length/direction semantics are unchanged, and delay moved from a number to a millisecond string.

Example:

"paddings": [
  {"length": "44", "direction": "c2s", "delay": "0"},
  {"length": "64-128", "direction": "s2c", "delay": "100"},
  {"length": "25", "direction": "c2s", "delay": "50-150"},
  {"length": "32768-49152", "direction": "s2c"}
]

Both endpoints should still use the same array and the same delay ranges, because that is what reproduces the captured shape. Nothing is compared against the peer's schedule any more, though: the receiver does not consume padding, so a length, direction or turn-count difference can no longer fail or stall the handshake, it only changes what each side sends.

Scope

Per the cleanup in #6769, this PR does not restore the XMC README.md / *_test.go files deleted in that squash commit. The only new test file is padding_frames_test.go, which covers the frame plan/reachability and the source of the padding stream, because the wire format changed here.

Tests

go test -count=1 ./infra/conf
go test -count=1 ./transport/internet/finalmask/xmc
go build -o /tmp/xray-core-build ./main
go run ./infra/vformat -pwd . -mode check
go run ./infra/vprotogen -pwd . -mode check

Validation fix

A legacy password longer than 113 bytes is now rejected when the configuration is built (035c45de), instead of being accepted and then failing every handshake: the verify token plus the password must fit one RSA-1024 PKCS#1 v1.5 block (128-11 bytes), so rsa.EncryptPKCS1v15 was failing on the client and the server could not decrypt it either.

go test -count=1 ./infra/conf ./transport/internet/finalmask/xmc

spr334-a and others added 6 commits September 14, 2026 21:56
Add direction field to Padding protobuf message to allow users to specify whether each padding turn goes from client to server (1) or server to client (2). This removes the previous limitation where padding turns had to alternate directions.

Changes:
- Add direction field to Padding message in config.proto
- Update config.go to read direction from config instead of alternating
- Remove validation that prevented consecutive same-direction turns
- Update all tests to specify direction explicitly
- Add test case for consecutive same-direction padding
Parse direction field and use explicit object format.
Per rprx's request: array fields use plural form, and add configurable delay in milliseconds after each turn.
Bring the PR branch up to date without rewriting the original author's
commits. Regenerate the XMC protobuf from config.proto and adapt the
packet-stream test to the new (conn, dest, dialer) WrapConnClient
signature introduced upstream.

Assisted-by: DeepSeek (AI)
Replace the integer per-turn delay with delay_min/delay_max, parsed from
the JSON "delay" string (a fixed value or an inclusive "min-max" range).
The sender now samples the pause uniformly inside the range after sending
its turn, while the receiver waits at least the minimum before reading or
sending the next turn, so both endpoints enforce the same spacing.

Skip the delay of the last turn to avoid adding latency before the proxy
stream starts, reject negative/reversed/overflowing ranges, and update the
README example plus timing tests.

Assisted-by: DeepSeek (AI)
@yandujun363

This comment was marked as outdated.

@RPRXX
RPRXX force-pushed the main branch 2 times, most recently from 9ade89d to 5d17b87 Compare November 14, 2026 15:58
@RPRX

RPRX commented Nov 14, 2026

Copy link
Copy Markdown
Member

#6769 (comment) rebase main

Sync with the squashed XTLS#6769 merge and keep the follow-up delay-range
change. Follow the maintainer's cleanup of XTLS#6769 and drop the XMC
README/*_test.go files it removed; the unrelated XMC tests deleted by
that squash are not restored here.

The XMC delay implementation, its validation, and the infra/conf delay
parsing tests remain.

Assisted-by: DeepSeek (AI)
@yandujun363 yandujun363 changed the title XMC: make startup padding configurable with per-turn delay ranges XMC: accept padding delay as a range string and wait on both ends Nov 15, 2026
@yandujun363

Copy link
Copy Markdown
Contributor Author

#6769 (comment) rebase main

下一步动作是什么,还需要处理什么吗

@yandujun363

Copy link
Copy Markdown
Contributor Author

因为目前还有一些问题没修,按要求一个PR一个功能,这些问题后续修复PR再说

  1. delay 不可取消、没有实际上限(优先级最高)
    time.Sleep 不响应 close/deadline,配置很大的 delay 会让 handshake goroutine 在连接关闭后继续睡。需要 close/deadline-aware timer 或合理上限。
  2. delay 的 range 语义需要确认
    目前 receiver 只保证等待 min;sender 随机到的 >min 只影响它自己的下一步,不会让对端等到那个值。如果 RPRX 期望线上间隔在 [min,max] 内抖动,需要重新设计。
  3. 自定义 paddings 整体替换 40 轮 preset
    包括后 33 轮 join 尾段;是否要保留 tail 需要确认。
  4. 没有 turn 数量 / 总 padding 字节上限
    单轮有 8MiB 上限,数组长度不限。
  5. 没有“早到”检测
    receiver 等 min 后再读,但不会因为对端提前发送而失败。
  6. paddings: [] 仍回落到 preset
    没有显式“不 padding”模式。

@RPRX

RPRX commented Nov 15, 2026

Copy link
Copy Markdown
Member

The last turn's delay is ignored (no extra latency before the proxy stream starts).

这个去掉

@yandujun363

Copy link
Copy Markdown
Contributor Author

The last turn's delay is ignored (no extra latency before the proxy stream starts).

这个去掉

OK,这边我处理下,可能要点时间

@yandujun363

Copy link
Copy Markdown
Contributor Author

因为目前还有一些问题没修,按要求一个PR一个功能,这些问题后续修复PR再说

  1. delay 不可取消、没有实际上限(优先级最高)
    time.Sleep 不响应 close/deadline,配置很大的 delay 会让 handshake goroutine 在连接关闭后继续睡。需要 close/deadline-aware timer 或合理上限。
  2. delay 的 range 语义需要确认
    目前 receiver 只保证等待 min;sender 随机到的 >min 只影响它自己的下一步,不会让对端等到那个值。如果 RPRX 期望线上间隔在 [min,max] 内抖动,需要重新设计。
  3. 自定义 paddings 整体替换 40 轮 preset
    包括后 33 轮 join 尾段;是否要保留 tail 需要确认。
  4. 没有 turn 数量 / 总 padding 字节上限
    单轮有 8MiB 上限,数组长度不限。
  5. 没有“早到”检测
    receiver 等 min 后再读,但不会因为对端提前发送而失败。
  6. paddings: [] 仍回落到 preset
    没有显式“不 padding”模式。

然后这里有哪些先开PR修,说明一下。因为目前还在折腾其它处理,如果不给顺序,我一个人忙不过来

RPRX asked to drop the special case that ignored the last turn's delay.
The sender now samples the configured range and the receiver waits at
least the minimum after the final padding turn, before the proxy stream
starts.

Assisted-by: DeepSeek (AI)
@yandujun363

Copy link
Copy Markdown
Contributor Author

Done in 76e53309: the special case that ignored the last turn's delay is removed. The sender now samples the delay and the receiver waits at least the minimum after the final padding turn, before the proxy stream starts.

@yandujun363

Copy link
Copy Markdown
Contributor Author

@RPRX 借这个 padding PR 问一个关于 padding 线格式的问题(和 delay 语义相关但独立):能否把 padding 做成完全原生的 MC 帧?

背景:我在写 XMC 的服务端 Fabric 模组(真实 26.1 服务端、同端口),目标是不接管 MC 的登录/认证,只注册 xmc:data 通道做转发。现在卡在 Login Ack 之后的第一轮 padding:

  • 第一轮是 c2s,targetLength 包含 Login Ack 的 2 字节,recordLength = targetLength - prefixLength;
  • 写出的长度字段值是 recordLength,而它包含自身 varint(writePaddingTurnWithBuffer 里 written != recordLength 校验的就是"含长度头的记录总字节数",读侧 readPaddingTurn 也是 recordLength - headerLength);
  • MC 原生语义是"长度 = 长度字段之后的字节数"。所以真实服务端的 splitter 读第一帧就会少 header 长度(1–4 字节),之后整条流错位。

结果就是:真服务端必须在 Login Ack 处拆掉 vanilla 的 splitter/decoder 才能跑 padding —— 这正是想避免的接管(也因此不得不搞"密码/专用端口"之类的区分手段)。

想确认的是,padding 能否改成合法 MC 帧,例如承载在配置状态的 ServerboundCustomPayloadPacket 里(26.1 的 ServerCommonPacketListenerImpl#handleCustomPayload 是空实现,未知 channel 会被静默丢弃,vanilla 不报错),长度字段按原生语义。这样真实服务端可以完全保持 vanilla 的配置状态:vanilla 会停在 registry 同步等 known packs,keepConnectionAlive 只在 keep-alive 未回应时断开(0x04 由客户端照常回),不会踢人;模组于是只需要处理 xmc:data,不需要区分"Xray / 真人",也不需要任何额外标记。

另外两个相关问题:

  1. 连真实 MC 服务端时,s2c 方向的 padding 还有必要吗?真实服务端自己就会发 brand / registry data 等配置流量,比合成字节更接近真实。
  2. 如果 padding 保持现状(非 MC 帧),服务端就只能在"专用端口"或"预共享密码"里二选一,两者都会引入本来可以没有的部署/流量特征。

如果这个方向可行,我可以单独出一个只改 padding 承载方式的 PR(不碰现有 length/direction/delay 语义),模组那边配合改。

@spr334-a

Copy link
Copy Markdown
Contributor

要不这样,原来的xmc保留现在的模式,不要求兼容真实 MC
配置和交互能简化就简化下,保留UUID和必要认证材质等字段
之后的模拟交互,可以继续用padding

然后你再单独做个兼容真实mc的模式,需要真实mc服务端,fabric等,这样有需要的人就能自己选择🤔

@yandujun363

Copy link
Copy Markdown
Contributor Author

我的担心是特征问题,具体看

普通客户端:Login Ack 后立刻发第一个 Configuration 包(完整帧)→ 立即同步调用 vanilla 的 handleLoginAcknowledgement 并转发缓冲字节,零延迟、零改动;
XMC 客户端:不发 Configuration 包,改跑 padding;而 padding 记录把长度字段算进自身(Xray padding.go),永远比合法 MC 帧少 header 字节,解码器不可能产出帧 → probeTimeoutMillis(默认 800ms) 后接管,把解码器里缓冲的首段 padding 作为流开头;
兜底:超时但解码器里一个字节都没有(对端 Ack 后什么都没发)→ 仍然交回 vanilla,不误伤慢客户端。
判定纯协议形状:不加字节、不看地址、不看用户名。

@yandujun363

Copy link
Copy Markdown
Contributor Author

这边还是看PRRX怎么选择

@RPRX

RPRX commented Nov 15, 2026

Copy link
Copy Markdown
Member

选更像的就行,可以 breaking

@yandujun363

Copy link
Copy Markdown
Contributor Author

选更像的就行,可以 breaking

行,我修一下,等这个解决了,再算前面 #7402 (comment) 的账,然后去处理下 #7416 这个草案

The startup padding used opaque records whose length field includes its own
varint, so a vanilla server split the stream one header short and had to take
the protocol pipeline over. RPRX said picking the more realistic option is fine
even if it is a breaking change, so padding is now carried in ordinary
Minecraft configuration-state custom payload packets.

Wire format:

- one padding chunk becomes one or more custom payload frames on "xmc:pad";
  the length field has the vanilla meaning (bytes after the field), the frame
  overhead is whatever the vanilla encoder adds;
- a chunk longer than the 32767 byte serverbound payload limit is split, and
  captured turns smaller than the smallest frame (the 2-byte turns) send that
  smallest frame instead;
- the receiver no longer reads or validates turns: they are ordinary traffic
  that the next reader drops as unknown packets, so both sides only send and
  keep the configured pauses;
- the first turn still counts the two Login Acknowledged bytes as its prefix;
  those bytes are simply not repeated.

This is a breaking change: both ends must run this version. No config or proto
field was added; the existing paddings/length/direction/delay semantics are
unchanged. The packet reader bound is raised from 32 KiB to 1 MiB, vanilla's
own clientbound custom payload limit, because a real configuration state can
send registry data of that size.

Assisted-by: DeepSeek (AI)
@yandujun363 yandujun363 changed the title XMC: accept padding delay as a range string and wait on both ends XMC: native frame padding and delay range Nov 15, 2026
@yandujun363

Copy link
Copy Markdown
Contributor Author

Picked the native one, breaking as allowed, in ea2c7edc.

Each padding chunk is now one or more configuration-state custom payload frames on xmc:pad (length = bytes after the field, serverbound data capped at vanilla's 32767), and nothing reads padding any more — the receiver treats it as ordinary traffic and the next reader drops it as an unknown packet. The first turn still counts the two Login Acknowledged bytes as its prefix.

Why it matters in practice: with the old format a real vanilla server split one header short and a server-side Fabric mod had to take the login pipeline over to run padding, which also meant reloading all decoder/splitter handlers and breaking ordinary players' path. With valid frames the mod only registers xmc:data and relays it; vanilla parses everything else itself. I verified the full path locally (Fabric 26.1 online-mode server + authlib-injector + a real Yggdrasil API, Xray client with padding, and an ordinary vanilla-style client reaching Play).

The reader bound also goes from 32 KiB to 1 MiB, since a real configuration state can send registry data of that size.

Remaining known items from the earlier list (cancellable delay, caps, early-arrival check, explicit no-padding mode, tail behavior) are untouched — separate PRs as agreed.

Minecraft enables zlib framing from the login Set Compression packet
onwards, so Login Success, the configuration state and the padding
schedule all use Packet Length + Data Length framing. Read the
threshold in client.go and wrap the connection before the packet that
follows it, and implement the framing in a new compression.go:
compressed packets are inflated back to the plain frame, and packets
below the threshold are written with Data Length = 0.

A zero Data Length keeps a body uncompressed, which vanilla servers
accept at any size, so padding frames can keep their configured wire
size; there is no need to compress payloads that do not benefit from
it. threshold < 0 leaves the connection uncompressed.

Adds compression_test.go for the threshold boundary, chunked writes and
the uncompressed-over-threshold case.

Tested end to end against Fabric 26.1 (protocol 775) with this mod at
network-compression-threshold 256 and 65536, 64 MiB and 256 MiB in both
directions, sha256 verified:
XTLS#7394 (comment)

This commit was produced with AI assistance (DeepSeek Harness).
yandujun363 added a commit to yandujun363/xmc-mcserver-core that referenced this pull request Nov 15, 2026
The Xray client now reads the login Set Compression framing
(XTLS/Xray-core#7402), so the mod no longer
requires network-compression-threshold=-1. Recommend 65536 so padding
and tunnel payloads stay uncompressed while large registry packets are
still compressed, and update the startup warning and the architecture
notes to match. Link the pull request and the test comment in full
instead of short references.

This commit was produced with AI assistance (DeepSeek Harness).
@yandujun363

Copy link
Copy Markdown
Contributor Author

Added the login compression codec on top of the native padding work (eb12f9c0):

  • transport/internet/finalmask/xmc/compression.go: compressed framing (Packet Length + Data Length + zlib body, Data Length = 0 for uncompressed bodies)
  • client.go: handle Set Compression before Login Success; threshold < 0 leaves the connection uncompressed
  • compression_test.go: threshold boundary, chunked writes, uncompressed-over-threshold

There is no Z_NO_COMPRESSION trick: vanilla accepts uncompressed packets above the threshold, so the codec only decodes what the server sends and leaves the policy to network-compression-threshold (65536 keeps padding and tunnel payloads uncompressed).

E2E: Fabric 26.1 (protocol 775), threshold 256 and 65536, 64 MiB / 256 MiB in both directions, sha256 verified. Numbers and caveats: #7394 (comment)

@RPRX

RPRX commented Nov 15, 2026

Copy link
Copy Markdown
Member

可以合了吗

@yandujun363

Copy link
Copy Markdown
Contributor Author

可以合了吗

我总感觉还有什么流程没跑完,因为这边改太多了,我再本地扫一下看看,没问题就合,然后前面哪些BUG后面的PR再算账

The pre-shared password travels with the four-byte verify token inside one
RSA-1024 PKCS#1 v1.5 block, which holds at most 128-11 bytes. A longer
password passed configuration validation and then failed every handshake at
runtime: rsa.EncryptPKCS1v15 reports "message too long for RSA key size" on
the client, and the server cannot decrypt the block either. Both sides were
already deriving their key pair from that password, so the configuration
looked valid and simply never connected.

Validate the limit when the configuration is built, and also in
newClientConn for configurations assembled without infra/conf.

This commit was produced with AI assistance (DeepSeek Harness).
@yandujun363

Copy link
Copy Markdown
Contributor Author

@RPRX 这个可以先合并了,然后剩下的草案后续再处理

@RPRX RPRX changed the title XMC: native frame padding and delay range XMC finalmask: Switch to native frame padding (breaking); delay supports range (receiver waits at least minimal ms) Nov 16, 2026
@RPRX
RPRX merged commit 01eaf28 into XTLS:main Nov 16, 2026
24 checks passed
@RPRX

RPRX commented Nov 16, 2026

Copy link
Copy Markdown
Member

话说这个 delay 本意是模拟处理数据的延迟,不过公网环境下 wait 可能意义不大,毕竟还有通信延迟,加 RTT 似乎又不太合适

目前应该是跟在 padding 后的而不是之前,这样的话逻辑上似乎有点问题?wait 选择的值是哪个?

@yandujun363

Copy link
Copy Markdown
Contributor Author

Delay only paces the local schedule: after ea2c7edc nothing reads padding, so it cannot affect any decision — it only spreads the encrypted bursts in time (sizes are unchanged). Without it the whole startup burst goes out in a few ms.

Where it sits: for every turn except the last, "pause after turn i" and "pause before turn i+1" are the same wire gap — the preset uses the latter (startDelay), the config uses the former. The difference is only at the boundaries: turn 0 goes out right after Login Acknowledged (like a real client), and the last turn used to get a trailing pause. That one was wrong: nothing follows it, so it only delayed the first proxied packet — delay: "300" on the final turn moved the first packet from 3 ms to 304 ms (measured through a relay). Fixed locally by pausing only between turns.

Which value the wait uses: the side that sends a turn samples [delay_min, delay_max]; the peer, which sends nothing for that turn, waits exactly delay_min — the proto comment said "at least", the code is "exactly". Consequence measured with 20-200: the gap you see after the turn is 20 (the min), and the sampled excess lands on the sender's own next turn (20 then 138). So a fixed value reproduces a captured gap; a range only jitters the sending side.

On the struck-through part: agreed. The pause is local, so the observed cross-direction gap is local pause + one one-way delay — RTT is the wrong unit, and compensating for it is not stable either.

本地Agent扫出来的情况,具体为啥要delay就得问问看 #6769 是怎么打算的了 @spr334-a
要修的话就说一声怎么修,这边把旧屎山一起在 #7423 算完

目前应该是跟在 padding 后的而不是之前,这样的话逻辑上似乎有点问题?wait 选择的值是哪个?

@RPRX

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants