Skip to content

REALITY protocol: Let the target answer anything before the Client Finished - #47

Open
mcrmck wants to merge 1 commit into
XTLS:mainfrom
mcrmck:guard-client-finished
Open

mcrmck wants to merge 1 commit into
XTLS:mainfrom
mcrmck:guard-client-finished

Conversation

@mcrmck

@mcrmck mcrmck commented Nov 22, 2026 •

Copy link
Copy Markdown

For XTLS/Xray-core#6121

This implements the fix described in XTLS/Xray-core#6121 (comment) ("直到读到正确的 client finished 之前都交给 target 来响应"). It is offered as a ready, tested version of that design; feel free to take it as is or use it as a reference.

The problem. A censor captures a ClientHello and replays it twice, each time followed by the same invalid record:

  • A, unchanged. REALITY authenticates it and answers the invalid record with Go's TLS stack.
  • B, with a random session ID. REALITY forwards it, and the target answers.

Any difference between the two answers identifies the server.

The fix. While REALITY waits for the Client Finished, the client's conn is a guardConn (new file guard.go):

  • Forwarding. The target has already seen the same ClientHello and has sent its own flight, so it gets the client's bytes as they arrive. The exception is the last byte of each record REALITY has not processed yet. A valid Client Finished therefore never reaches the target, and the target cannot answer before REALITY has verified it.
  • When REALITY rejects a record. REALITY sends nothing of its own (its alert is dropped). The held bytes go to the target, and the connection becomes the same bidirectional forwarding as for an unauthenticated client. The client gets the target's own answer: alert, FIN, or nothing.
  • When the target answers first. If the target says or closes anything before the Finished is verified (its own timeout, or a check on a record header that Go accepts), that answer goes to the client. The connection is handed over the same way.

With a valid client, the target sees ChangeCipherSpec and the Client Finished minus its last byte, then a close. Before, it saw a close after the ClientHello.

Also: the first goroutine now reads hs.c.conn before mutex.Unlock(). hs.c.conn is temporarily the guard, and it was read without the lock.

Test. The tester replays a captured ClientHello as A and B and sends each of the 20 probes from VLESS-cracker's characteristic.txt, with 2 rounds per probe. It runs on loopback, with v26.11.9-main Xray built against REALITY 4bb46b2 with and without this change. The table counts probes whose answers differ between A and B:

Target Before After
Go crypto/tls (net/http) 0 / 20 0 / 20
OpenSSL 3.6.4 s_server -alpn h2,http/1.1 2 / 20 0 / 20

The two OpenSSL mismatches are the ones reported in the issue: 15 03 03 00 02 03 00 and 15 03 03 00 02 01 FF. Go answers with an alert, OpenSSL with FIN. With this change, A answers FIN like the target.

  • New unit test. guard_test.go covers which bytes are held back and released: partial headers, several records in one read, a zero-length record, and the hand-over. go test -race ./... passes.
  • Valid clients. VLESS + REALITY + Vision through a patched server moved 64 MiB up and 64 MiB down, and completed 20 of 20 back-to-back connections.

Not covered.


中文:按 RPRX 在 #6121 的方案实现:在读到正确的 Client Finished 之前,客户端发来的数据同时转发给 target(每个 REALITY 尚未处理完的记录保留最后一个字节,保证正确的 Finished 永远不会完整到达 target)。如果 REALITY 拒绝了某个记录,或者 target 先有响应,REALITY 不发送自己的 alert,把剩余字节交给 target 并转为双向转发,客户端收到的就是 target 自己的响应(alert / FIN / 超时)。测试中 OpenSSL target 的两个已知差异探针(15 03 03 00 02 03 00、15 03 03 00 02 01 FF)由 2/20 降为 0/20,Go target 保持 0/20,正常客户端不受影响。

@RPRX

RPRX commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

AI 写的还是

Client finished 最后一个字节要先按住不发,等一个 RTT 等目标服务端响应,我写的由于担心这会导致特征不稳定所以还在本地

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