Skip to content

XMC finalmask: Bound padding schedules; Fix delay placement; Stop compressing frames - #7423

Merged
RPRX merged 4 commits into
XTLS:mainfrom
yandujun363:fix/xmc-padding-guards
Nov 18, 2026
Merged

RPRX merged 4 commits into
XTLS:mainfrom
yandujun363:fix/xmc-padding-guards

Conversation

@yandujun363

@yandujun363 yandujun363 commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Follow-up to #7402, rebased onto main after both #7402 and #7430 merged: this is no longer a stacked draft, the diff is four commits, and it also settles the delay question and the compression side effect that #7402 left behind.

  1. Bound padding schedules and cancel their pauses on Close (unchanged from the draft). Padding pauses are timers that watch a per-connection done channel; a configured schedule is rejected above 256 turns, 64 MiB of turns, or the handshake deadline minus ten seconds of pauses; an explicit empty paddings array (or "noPadding": true) disables padding, and an absent field keeps the preset.

  2. Pause before a padding turn and never after the last one. A configured delay used to be applied after its turn, so the last turn of a schedule paused even though nothing followed it: with delay: "300" on the final turn the first proxied packet went out 300 ms later, while both ends otherwise waited for nothing. The pause now leads into the turn it is configured on - a captured gap is written on the turn that follows it - and the last turn is not followed by a pause. Which value each end waits is unchanged and now stated in the proto comment: the side that sends the turn samples [delay_min, delay_max], the peer spends exactly delay_min (it waits the minimum, not "at least" it), so a range jitters only the sending side and delay_min == delay_max reproduces a captured gap.

  3. State what a configured padding list replaces. Two decisions only lived in the pull request: a configured list is the complete schedule, so the preset's Play-state join tail runs only with the preset; and a peer cannot be checked for arriving early, because the turns are ordinary Minecraft packets that carry nothing to compare an early arrival against, with the pause bound covering a stalled peer. Both are now next to the code and pinned by a test.

  4. Never compress the frames XMC writes. After Set Compression the writer deflated everything at or above the threshold, which is exactly the padding (zeros, ~300:1) and the proxied bytes (already compressed or encrypted): the padded shape disappeared and the CPU work bought nothing. The writer now always sends the uncompressed variant, which vanilla accepts at any size (Data Length = 0 is not subject to the threshold check), and the reader still inflates what a real server sends.

#7430 landed while this was open and added a startup reader that drains the peer's padding independently while the local schedule writes. That is why point 3 no longer says the peer's turns stay in the stream for the next reader: they are read and dropped by that reader, while the pause limit still bounds a stalled peer. This branch is rebased onto #7430, and the two changes coexist - the reader drains, and the schedule still spends delay_min on the peer's turn. The handshake test that #7430 added now builds its schedule through this branch's paddingScheduleFromConfig instead of passing []*Padding to the constructors.

Tests

go test -count=1 ./transport/internet/finalmask/xmc ./infra/conf
go test -count=1 ./transport/internet/finalmask/...
go test -count=1 -race ./transport/internet/finalmask/xmc
go build ./...
go run ./infra/vformat -pwd . -mode check
go run ./infra/vprotogen -pwd . -mode check

New coverage: pause placement for both roles and the absence of a trailing pause; which end samples the range and which spends the minimum; a configured list replacing the whole preset on both ends; the writer never compressing (zero-filled and random bodies, 1 B to 64 KiB) while the reader still accepts deflated bodies, chunked writes and multiple frames per write.

End-to-end, real server, compression on

Local Fabric 26.1 server (protocol 775) with the server-side mod, online-mode=true through a local Yggdrasil API and authlib-injector, network-compression-threshold=256 (the default), and a byte-counting relay in front of the server. The client side is this branch with the Yggdrasil draft #7416 merged locally for the real login.

Measurement (threshold 256) Before After
Client-to-server bytes for two 64 KiB padding turns 889 B 131,519 B
Upload 32 MiB through XMC 19.2-20.9 MB/s 46.9-52.1 MB/s
Download 32 MiB through XMC - 19.4-22.2 MB/s, sha256 OK

The built-in preset and a configured schedule both log in and reach the tunnel, and the server accepts the uncompressed bodies above the threshold (padding frames of 32 KiB, tunnel frames of 24 KiB). A configured delay of 600 ms on two turns adds 1.2 s to the login as expected, so the final turn's pause is spent where it belongs.

With this, the server-side mod no longer needs to ask for network-compression-threshold=65536 to keep padding frames intact; that requirement and its warning can be dropped after this lands.

This PR was produced with AI assistance (DeepSeek Harness).

The startup padding schedule runs inside the login handshake, and its pauses
used time.Sleep: a connection closed during a configured pause kept its
handshake goroutine asleep, and a schedule whose pauses add up to more than
the two-minute handshake deadline could never finish. A configured schedule
also had no upper bound on its turn count, so one handshake could be told to
send an arbitrary amount of padding.

- Padding pauses are now timers that also watch a per-connection done channel,
  which Close closes, so a closed connection stops the schedule at once.
- A configured schedule is rejected when it has more than 256 turns, more than
  64 MiB of turns in total, or more than the handshake timeout minus ten
  seconds of pauses in total.
- An explicit empty "paddings" array (or "noPadding": true) now disables
  padding instead of falling back to the preset; an absent field keeps the
  preset. The new no_padding proto field carries that across the wire because
  an unset repeated field cannot express it.
- The proto comment states how the delay range is split: the side that sends a
  turn samples [delay_min, delay_max], and the peer only knows delay_min.

This commit was produced with AI assistance (DeepSeek Harness).
A configured padding turn carried a pause that was applied after the turn, so
the last turn of a schedule paused even though nothing followed it: with
`delay: "300"` on the final turn the first proxied packet went out 300 ms later
while both ends otherwise waited for nothing. Measured through a relay, the same
configuration also made the pause look like it belonged to the previous turn.

The pause now leads into the turn it is configured on: it is spent before that
turn's bytes, which puts a captured gap on the turn that follows it, and the
last turn is no longer followed by a pause. The peer, which sends nothing for
that turn, spends the same pause at the same place so both timelines stay in
phase.

The value each end waits is unchanged and now stated in the proto comment: the
side that sends a turn samples [delay_min, delay_max], the peer spends exactly
delay_min (the peer waits the minimum, not "at least" it), so a range only
jitters the sending side and delay_min == delay_max reproduces a captured gap.

Tests pin the event order for both roles, the absence of a trailing pause, and
that only the sending side samples the range. This commit was produced with AI
assistance (DeepSeek Harness).
Two decisions in this branch were only described in the pull request, where
nobody reading the code will find them:

- A configured "paddings" list is the complete schedule. The built-in preset
  also carries a Play-state join tail, and that tail runs only with the preset,
  so a user who captured their own login decides the whole shape.
- A peer cannot be checked for arriving early: the turns are ordinary Minecraft
  packets that carry nothing to compare an early arrival against, the startup
  reader drains them while the local schedule writes, and a stalled peer is
  bounded by the configured pause limit instead.

Record both next to the code and the proto/JSON fields, and pin the replacement
with a test that compares a configured schedule on both ends against the preset.

This commit was produced with AI assistance (DeepSeek Harness).
After `Set Compression` the writer deflated every body at or above the
negotiated threshold, and that covered exactly the two things XMC writes:
padding, whose zeros deflated about 300 times, and proxied bytes, which are
already compressed or encrypted. It also cost real throughput: on a local
Fabric 26.1 server at the default threshold of 256, uploading 32 MiB through
XMC ran at 19.2-20.9 MB/s with the compressing writer and at 46.9-52.1 MB/s
without it, while the compressed body was no smaller than the original.

What a padded turn declares is its size on the wire, so deflating it removed the
shape the turn exists for: two 64 KiB client-to-server turns went out as 889
bytes in total, and a real server could not tell them from ordinary traffic of
that size. Vanilla accepts the uncompressed variant of the framing at any size -
`Data Length = 0` means the body follows as is, without the threshold check
that only applies to deflated bodies - so the writer now always uses it.

The reader is unchanged and still inflates whatever the peer sends, since a real
server keeps compressing its own packets.

Verified end to end against a real Fabric 26.1 server (protocol 775, online-mode
through a local Yggdrasil API and authlib-injector) with
`network-compression-threshold=256`, through a byte-counting relay in front of
the server:

- the same two 64 KiB turns now put 131,519 bytes on the wire instead of 889;
- the built-in preset and a configured schedule both log in and reach the
  tunnel, and 32 MiB downloads keep their sha256;
- the server accepts the uncompressed bodies above the threshold, including
  padding frames of 32 KiB and tunnel frames of 24 KiB.

The server-side Fabric mod no longer has to ask for
`network-compression-threshold=65536` once this lands.

This commit was produced with AI assistance (DeepSeek Harness).
@yandujun363
yandujun363 force-pushed the fix/xmc-padding-guards branch from a3b8842 to 8f64bf0 Compare November 18, 2026 02:06
@yandujun363

Copy link
Copy Markdown
Contributor Author

本地 E2E 复测(2026-11-18)

在 current head 8f64bf0e(已 rebase 到 main 82b5b76b,含 #7430)上复跑。

1. Xray XMC 客户端 ↔ Xray XMC 服务端(legacy)

  • 自定义 padding:65536 c2s / 32768 s2c / 65536 c2s,delay 200ms。
  • 1 KiB 与 32 MiB 下载 sha256 均一致;32 MiB 约 33.4 MB/s。
  • relay 计数:两轮 64 KiB c2s padding 的连接上行总量 131,509 B,s2c 34,934 B。
  • go test ./transport/internet/finalmask/xmc ./infra/conf 及 -race 通过。

2. 真实 Minecraft 26.1(协议 775)+ 服务端模组

环境:Fabric 26.1 开发服,online-mode=true,本地最小 Yggdrasil API + authlib-injector,network-compression-threshold=256;服务端模组 xmc-mcserver-core master 695fa38。

为完成真实登录,本地 E2E 客户端临时合并了草案 #7416(仅测试用,未改动本 PR 分支)。同样是两轮 64 KiB c2s padding:

客户端 连接上行总字节数
#7416 only 889 B
本 PR + #7416 131,519 B

32 MiB 下载 sha256 一致(约 12.3 MB/s);32 MiB 上传 sha256 一致。结论:真实服务端 Set Compression 之后,never compress 生效,padding 不再被压掉。

补充(与本 PR 无关):模组的 256-payload 转发队列在回环网络上遇到“刚登录就立即 32 MiB 上行”时可能先溢出;用 Expect: 100-continue 等隧道建立后再发 body 即正常。常规加入与小流量不受影响。

以上数据由 AI 编码代理复跑整理。


Local E2E re-run (2026-11-18)

Re-ran on current head 8f64bf0e (rebased onto main 82b5b76b, includes #7430).

1. Xray XMC client ↔ Xray XMC server (legacy)

  • Custom padding 65536 c2s / 32768 s2c / 65536 c2s, 200 ms delays.
  • 1 KiB and 32 MiB downloads: sha256 OK; 32 MiB at ~33.4 MB/s.
  • Relay count for two 64 KiB c2s padding turns: 131,509 B client-to-server, 34,934 B server-to-client.
  • go test ./transport/internet/finalmask/xmc ./infra/conf and -race pass.

2. Real Minecraft 26.1 (protocol 775) + server-side mod

Fabric 26.1 dev server, online-mode=true, local minimal Yggdrasil API + authlib-injector, network-compression-threshold=256; server-side mod xmc-mcserver-core master 695fa38.

For the real login the local E2E client merged draft #7416 on top (test-only; the PR branch itself is unchanged). Same two 64 KiB c2s padding turns:

Client Client-to-server bytes
#7416 only 889 B
this PR + #7416 131,519 B

32 MiB download sha256 OK (~12.3 MB/s); 32 MiB upload sha256 OK. So after the real server's Set Compression, never compress holds and the padding shape survives.

Side note (not caused by this PR): the mod's 256-payload relay queue can overflow on loopback when a client immediately uploads 32 MiB right after login; Expect: 100-continue (waiting for the tunnel) makes it pass. Normal joins and small transfers are unaffected.

Numbers reproduced and summarized with AI assistance.

@yandujun363

Copy link
Copy Markdown
Contributor Author

@RPRX 你看看这个还有问题吗,因为我没权限重新跑CI,麻烦重新跑一下,然后这边找个时间合并

@RPRX

RPRX commented Nov 18, 2026

Copy link
Copy Markdown
Member

虽然我提出了 #7402 (comment) 但其实还有一个问题是,如果 the last one 之后确实需要 delay 呢?你测一下真实 MC 有没有

@RPRX RPRX changed the title XMC: bound padding schedules, fix delay placement, stop compressing frames XMC finalmask: Bound padding schedules; Fix delay placement; Stop compressing frames Nov 18, 2026
@RPRX
RPRX merged commit a32023b into XTLS:main Nov 18, 2026
23 of 24 checks passed
@yandujun363

Copy link
Copy Markdown
Contributor Author

虽然我提出了 #7402 (comment) 但其实还有一个问题是,如果 the last one 之后确实需要 delay 呢?你测一下真实 MC 有没有

晚上有空我看看。因为我现在正在修被我自己玩炸的网站,早知道能跑不要动

@yandujun363
yandujun363 deleted the fix/xmc-padding-guards branch November 18, 2026 12:10
@yandujun363

Copy link
Copy Markdown
Contributor Author

@RPRX 结论先说:MC 协议本身没有 padding,也没有任何"延迟/时序"字段;真机上 join 收尾的最后一个包之后也没有停顿——同一个 tick 里就会发 Client Tick End,之后每 50 ms 一个。所以"the last one 之后需不需要 delay"在真机上对应的是"下一个包在同一个 tick 内到",而不是"停一下"。

MC 协议到底是什么

  1. 帧:一条 TCP 连接上就是 VarInt 长度分帧,长度 | 包体;包体是 packet id + 字段。Set Compression 之后包体前面多一个 data length:data length = 0 表示未压缩,非 0 才走 zlib。vanilla 服务端接受超过 threshold 的未压缩包,只拒绝小于 threshold 的压缩包(这个 PR 第 4 点靠的就是它)。帧上限 2^21-1;serverbound custom payload 数据 ≤ 32767 B,clientbound ≤ 1 MiB。
  2. 状态机:Handshaking → Status / Login → Configuration → Play。登录阶段可选 RSA + AES-CFB8;之后整条流(包括长度字段)都是密文,路径上的观察者只看得到"什么时候、多少字节"。
  3. 协议里唯一和时间有关的规则是 keep-alive:服务端每 1–15 s 发一次,客户端 15 s 不回被踢,20 s 收不到就 Timed out。除此之外没有节流、没有填充、没有时序协商。
  4. 客户端的"节奏"是 20 Hz 游戏循环(50 ms/tick):一个 tick 里产生的包各自 write+flush,Nagle 关掉,所以 wire 上的间隔基本就是 tick。

"最后一个包之后"在真机上

查了 26.1 官方客户端(反编译),Minecraft.tick() 里的顺序是:

  • gameMode.tick() → Connection.tick() → ClientPacketListener.tick() → notifyPlayerLoaded():加载地形结束时发 Player Loaded;
  • level.tickEntities():玩家第一次 tick(wiki 说"第一次玩家 tick 发生在这个包和下一个 Client Tick End 之间"就是这里);
  • connection.send(ServerboundClientTickEndPacket.INSTANCE):Client Tick End,无字段,2 字节帧。

所以 Player Loaded 和下一个 Client Tick End 在同一个 tick 内,间隔就是该 tick 剩下的工作量(通常几 ms),然后才是 50 ms 网格。capture 看起来"停在最后",是因为 schedule 只建模到 player_loaded,后面交给隧道了;真机在后面是持续的心跳。

对 #7423

  • "pause 前移到 turn 上、最后一个 turn 之后不暂停"是对的:captured gap 属于"下一个包什么时候来",不属于"上一个包之后停多久";最后一个 turn 后面没有下一个 turn,这个 pause 没有地方放。
  • 硬加一个尾部 delay 反而会造出真机没有的空洞:真机那个位置只有 tick 的剩余时间(几 ms)。
  • join 收尾之后真正缺的不是 delay,而是 20 Hz 心跳;而 XMC 停在 Configuration,这个状态里 vanilla 客户端只有回应、没有 tick 包,所以它也不是 padding schedule 能补的,属于另一个设计问题。

结论:这个 PR 不需要为"最后一个之后的 delay"改模型。

参考:Packets#Packet_format、FAQ 登录序列、Player Loaded、Keep Alive


DeepSeek-V4.1-Flash的分析

你自己判断看看,如果需要,我可以开PR,尝试模仿20T的速率
但这个可能比较麻烦,你自己选择,或者问问看其他贡献者是怎么打算的
但目前这些数据是跑代码分析的。因为我内存不够开测试环境。

@yandujun363

Copy link
Copy Markdown
Contributor Author

一句话结论

不做“进 Play 的强行伪装”,也不靠纯 pad 硬糊。走“真协议 + 假节奏”:登录和 Configuration 阶段按 MC 协议真走一遍(Client Info、Known Packs、Registry、Tags、Finish),客户端不回最后的确认包,于是停在 Configuration——不建玩家、不发区块。缺的两样东西用 pad 补:进服的体量、之后的 20 次/秒心跳。真客户端如果把确认包回了,就按 vanilla 原来的位置(白名单检查处)把它踢掉。

抓包(26.3,第三方登录,加密)

状态 客户端→服务端 服务端→客户端
站着不动 21 包/秒,89 B/s 41 包/秒,5.6 KB/s
转视角 35 包/秒,267 B/s 42 包/秒,6 KB/s
跑动 43 包/秒,697 B/s 99 包/秒,69 KB/s
进服加载 43 包/秒(含 20/秒的小回执) 2.2 MB / 12 秒
  • 心跳间隔中位 50.0 ms,很稳;加载卡顿时出现过 4.7 秒完全没有上行。
  • 开压缩后真机心跳只有 3 字节;xmc:pad 帧是 11 字节(7 字符通道)或 7 字节(3 字符通道),包数能对上,字节多一点点。
  • 下行大头是 1400 字节的 TCP 段,所以“大流量”在链路上天然像区块下载——这点对我们有利。

第三方登录

通道名

  • 通道名必须可配:固定名字对有账号的探测者是特征;而且名字长度直接决定 pad 帧长度(7 字符 11 字节 / 3 字符 7 字节)。

拿公会服务器正常数据抓包让DeepSeek-V4.1-Flash分析的,虽然这里没什么人懂MC,但这条评论可能有用

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.

2 participants