Skip to content

reality: accept a ClientHello once, and learn the target's records ahead of the first client - #7

Merged
Miroshka000 merged 1 commit into
devfrom
fix/reality-replay-and-cold-start
Nov 12, 2026
Merged

Miroshka000 merged 1 commit into
devfrom
fix/reality-replay-and-cold-start

Conversation

@Miroshka000

Copy link
Copy Markdown

Two ways a REALITY server told itself apart from its target.

A recorded ClientHello could be replayed

The proof of a REALITY client sits in its ClientHello, so whoever recorded one can send it again for as long as its time stays valid (MaxTimeDiff, forever with none). The server answered the copy itself. Whoever replays cannot finish that handshake, but the server's behaviour towards it is its own and not the target's.

Now the server remembers the ClientHellos it accepted, by their random, and hands a second copy to the target like any stranger's, so the copy gets the target's own answer.

  • kept for both sides of the time check (2 * MaxTimeDiff), an hour with no check
  • two generations of at most 65536 entries each, so memory stays bounded; a full generation turns early
  • shared by a config and its clones

The first client got no post-handshake records

The target's post-handshake records were learned by a probe that the first client of each (target, SNI, ALPN, cipher suite) started and did not wait for. That client got none of the records the target sends, unlike every client after it.

  • the patterns of the usual handshakes (each server name, h2, each TLS 1.3 suite) are learned on the first connection of any kind, a stranger's included
  • the probe of any other key starts as soon as the target's ServerHello names the suite
  • a client whose pattern is still being learned waits for it, until 2 s after the probe began: a probe that hangs holds up only the clients of its first moments
  • a probe ends once the target has been quiet for 400 ms (or for as long as its own dial and handshake took), instead of always sitting out the 5 s window

Tests

  • TestRealityReplayedClientHelloGoesToTheTarget: a ClientHello recorded from a real client and sent again is forwarded, and what comes back has the shape of the target's own flight. With the guard taken out it fails.
  • TestRealityReplayGuard, TestRealityReplayGuardIsBounded, TestRealityReplayWindow
  • TestRealityPostHandshakeWarm, TestRealityPostHandshakeWaitIsBounded
  • TestRealityPostHandshakeRecordsImitated and TestRealityPostHandshakeALPN now expect the first connection to carry the target's records.

go test . passes on Windows (Go 1.26.8); go test -race -run Reality . passes in golang:1.27.

Not covered

No API change: the new fields of RealityConfig are unexported. Not run inside Citrus or on a live server yet.

…ead of the first client

Two ways a REALITY server told itself apart from its target.

A recorded ClientHello could be sent again for as long as its time stayed
valid (MaxTimeDiff, forever with none). The server answered the copy itself:
whoever replays cannot finish that handshake, but the server's behaviour
towards it is its own and not the target's. The server now remembers the
ClientHellos it accepted, by their random, for both sides of the time check
(an hour with no check), in two bounded generations shared with clones, and
hands a second copy to the target like any stranger's, so the copy gets the
target's own answer.

The target's post-handshake records were learned by a probe that the first
client of each (target, SNI, ALPN, cipher suite) started and did not wait
for, so that client got none of the records the target sends, unlike every
client after it. Now the patterns of the usual handshakes (each server name,
h2, each TLS 1.3 suite) are learned on the first connection of any kind; the
probe of another key starts as soon as the target's ServerHello names the
suite; a client whose pattern is still being learned waits for it, until two
seconds after the probe began, so a probe that hangs holds up only the
clients of its first moments; and a probe ends once the target has been quiet
for 400 ms, or for as long as its own dial and handshake took, instead of
always sitting out the five-second window.
@Miroshka000
Miroshka000 merged commit 4989099 into dev Nov 12, 2026
4 checks passed
@Miroshka000
Miroshka000 deleted the fix/reality-replay-and-cold-start branch November 12, 2026 11:25
Miroshka000 added a commit to Miroshka000/mikan that referenced this pull request Nov 12, 2026
…old by a replay

More of what set a mikan server apart from an ordinary one:

- an inbound no client has reached yet moves off www.microsoft.com, the
  site every installation started on, to a site next to its server; the
  node's such inbounds take one site from one search, retried once a day
  when nothing is found. An inbound with clients keeps its site, as a change
  would cut them off until their subscription is read again
- the REALITY window of a client's clock is 10 minutes, not 2 hours: it is
  how long a recorded ClientHello stays worth sending again
- the core (getmikan/utls#7): an accepted ClientHello is remembered and a
  copy of it goes to the target, and the first client after a start gets
  the target's post-handshake records as every later one does
- a new remote node's API answers the panel alone from the start
- X-Forwarded-For is read from a proxy on the server or its private
  networks, or one named in MIKAN_TRUSTED_PROXIES, and not from a peer that
  came straight from the internet

The API's reason of an automatic change has default_site.
Miroshka000 added a commit that referenced this pull request Dec 5, 2026
reality: carry #7 and the client root re-check onto dev-1.9
Miroshka000 added a commit to Miroshka000/mikan that referenced this pull request Dec 6, 2026
The tag's commit (648e4eb7 on Citrus main) with the replaces it pins:
getmikan/utls dev-1.9 (b60cfbd4, the fork on metacubex/utls v1.9.0-alpha.1),
the others as they were. getmikan/utls#7 (REALITY takes a ClientHello once
and learns the target's records ahead of the first client), which mikan has
pinned on the old line since 0245022, is not in dev-1.9 yet: it is being
carried there and goes in with the next pin.

What the panel does with the new core:
- public-dest goes only with a dest the core takes for one on the internet.
  netaddr.GlobalHost holds a literal to Citrus publicaddr (TEST-NET,
  198.18.0.0/15, 240.0.0.0/4, ::/96, local NAT64, 6to4 by the IPv4 inside):
  the core does not start a listener with the flag on such an address.
  Classify keeps them Public for the rest of the panel and its tests.
- server-names (Citrus#205): a VLESS, Trojan or AnyTLS inbound on a
  certificate gets from the panel the names its clients are given
  (nodeapi.Inbound.ServerNames: the node's or the panel's domain, the
  template's SNI, those of its addresses for clients and of the groups'
  addresses), none when an address is behind a CDN or another server or a
  client gets no name. The node adds its certificate's names, and a
  ClientHello with another name or none ends in an alert. A change of an
  address pushes the state. An expert template's own server-names is
  checked (config_server_names).
- A Hysteria2 range stays at most 1000 ports: one QUIC server takes it now
  (Citrus#200), but every port is still a socket and a reader, and a node
  not updated yet runs a server on each. The texts lose the server per port
  and the 0.12 MB a port.
- fragment-opts: the core's parser takes the split profiles of every kind
  of proxy, behind a CDN and as hops of chains too (since beta.4 it refuses
  one without TLS or over shadow-tls, restls or jls); the hints name Citrus
  1.0.0-beta.4, Mikan and Kabosu.
- h2 on the direct TLS listeners is as Citrus#207 has it, with an h2
  fallback only; fallbacks stays guarded.

Checked: gofmt, vet, go test ./... (but the Windows-only failures: unix
sockets of the terminals, port 80), the real core in internal/node (REALITY
targets, the strict names), internal/sub/mihomo through the core's parser,
TestDepsLinkNoCore with go on PATH, installer clippy and tests in rust:1.
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.

1 participant