Dedicated server: online mode, and refused logins in the log

online-mode=true now authenticates players as vanilla's
ServerLoginPacketListenerImpl does. The server makes a 1024-bit RSA key
pair at startup ("Generating keypair"), sends it with a random challenge,
checks the challenge and decrypts the shared secret from the client's key
packet, encrypts the connection with AES/CFB8, and asks the session
service's verify endpoint (hasJoined, through the services discovery
document) whether the player joined with the server's hash, from the
"User Authenticator #N" thread. A verified profile keeps its UUID and
properties, so skins show in the player list; an unverified one is
refused with "Failed to verify username!", and an unreachable service
with authservers_down. The offline-mode warnings are only printed in
offline mode.

Refused logins are logged as vanilla logs them: "Disconnecting
<name> (/<address>): <reason>" and "<name> (/<address>) lost
connection: <reason>", with log-ips respected.

horizon-protocol gains RSA key generation (Miller-Rabin primes,
e = 65537), the X.509 encoding of the public key and PKCS#1 v1.5
decryption. test_online_mode_parity.py runs its logins against Horizon's
server too, plus a verified player the whitelist refuses, and compares
the two servers' log lines about the player.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-16 22:59:36 +02:00
parent f535d1b48c
commit e7003e7d49
10 changed files with 713 additions and 64 deletions
+80 -28
View File
@@ -1,5 +1,5 @@
#!/usr/bin/env python3
"""Horizon's client joins vanilla's online-mode server, with no real account.
"""Horizon's client joins vanilla's online-mode server and Horizon's, with no real account.
A small mock of the Minecraft services (dev only: the discovery document and
the session service's join and hasJoined endpoints) stands in for Mojang's.
@@ -20,7 +20,13 @@ in the world with its chunks, chats, runs /list, leaves).
Also checked, with vanilla's messages:
- a token the session service rejects ("Failed to log in: Invalid session");
- a join the session service forgets (the server: "Failed to verify username!");
- no account at all (the client refuses before joining).
- no account at all (the client refuses before joining);
- a verified player the whitelist refuses.
Horizon's dedicated server then runs the same steps in online mode
(`HORIZON_API_DISCOVERY` points it at the mock too): it must give the client
the same answers and log the same lines about the player as vanilla's
("UUID of player ...", "Disconnecting ...", "... lost connection: ...").
Chat is unsigned here: the mock serves no Mojang public keys, so the server
can't validate profile keys and doesn't enforce secure chat.
@@ -31,9 +37,11 @@ import os
from pathlib import Path
import platform
import shutil
import re
import subprocess
import sys
import threading
import time
import urllib.parse
try:
@@ -167,43 +175,42 @@ def run_client(binary, build, port, label, token, discovery):
got['session file left'] = (directory / 'session.json').exists()
return got
# The server's lines about the player, as both servers print them.
PLAYER_LINE = re.compile(r'^\[\d\d:\d\d:\d\d\] \[([^\]]+)\]: (.*(?:Tester|Username|Disconnecting|verify username).*)$')
def main():
lock, jars, libraries = base.verify_artifacts()
build = lab.LOCAL / NAME
build.mkdir(parents=True, exist_ok=True)
report = build / 'report.json'
report.unlink(missing_ok=True)
oh.run_logged(['cargo', 'build', '--release', '-p', 'horizon-client', '--bin', 'horizon-client'], build,
'compile-rust')
metadata = json.loads(subprocess.check_output(['cargo', 'metadata', '--no-deps', '--format-version', '1'],
cwd=lab.ROOT, text=True))
binary = Path(metadata['target_directory']) / 'release' / ('horizon-client.exe' if os.name == 'nt' else 'horizon-client')
dependencies = os.pathsep.join(map(str, [jars['server'], *libraries]))
services = Services()
discovery = services.url + '/minecraft/client'
def player_lines(lines):
out = []
for line in lines:
if m := PLAYER_LINE.match(line):
text = re.sub(r'/127\.0\.0\.1:\d+', '/127.0.0.1:_', m.group(2))
text = re.sub(r'entity id \d+ at \(.*\)', 'entity id _ at _', text)
out.append(f'[{m.group(1)}] {text}')
return out
def run_side(label, command, binary, build, services, discovery):
"""The five logins against one server; its checks and its log about the player."""
port = free_port()
directory = build / 'server'
directory = build / label
shutil.rmtree(directory, ignore_errors=True)
directory.mkdir()
props = dict(PROPERTIES, **{'server-port': str(port)})
(directory / 'server.properties').write_text(''.join(f'{k}={v}\n' for k, v in props.items()))
(directory / 'eula.txt').write_text('eula=true\n')
command = ['java', '-Xmx2g', '-Duser.timezone=UTC', f'-Dminecraft.api.discovery.host={discovery}',
'-cp', dependencies, 'net.minecraft.server.Main', 'nogui']
server = Server(command, directory, 'vanilla-online')
os.environ.update(HORIZON_NO_AUDIO='1', HORIZON_API_DISCOVERY=discovery)
server = Server(command, directory, f'{label}-online')
checks = []
def check(label, actual, expected):
checks.append(f'{label}: ' + ('ok' if actual == expected else f'expected {expected!r}, got {actual!r}'))
def check(name, actual, expected):
checks.append(f'{name}: ' + ('ok' if actual == expected else f'expected {expected!r}, got {actual!r}'))
try:
server.wait_for(r'Done \(', timeout=600)
services.take_log()
# 1. Signed in: the full encrypted handshake, then play.
got = run_client(binary, build, port, 'signed-in', GOOD, discovery)
got = run_client(binary, build, port, f'{label}-signed-in', GOOD, discovery)
server.wait_for(r'Tester left the game', timeout=30)
log = services.take_log()
joins = [e for e in log if e[0] == 'join']
@@ -227,27 +234,71 @@ def main():
# 2. A token the session service rejects: vanilla's message, no key sent.
mark = len(server.lines)
got = run_client(binary, build, port, 'rejected', BAD, discovery)
got = run_client(binary, build, port, f'{label}-rejected', BAD, discovery)
check('rejected token: client message', got['error'],
'disconnected: Failed to log in: Invalid session (Try restarting your game and the launcher)')
check('rejected token: no verification', [e[0] for e in services.take_log()], ['join'])
# 3. The session service forgets the join: the server refuses the player.
got = run_client(binary, build, port, 'forgotten', FORGET, discovery)
got = run_client(binary, build, port, f'{label}-forgotten', FORGET, discovery)
server.wait_for(r'Failed to verify username', timeout=30)
check('forgotten join: client message', got['error'], 'disconnected: Failed to verify username!')
check('forgotten join: server verified and refused',
[(e[0], e[3]) for e in services.take_log()], [('join', True), ('verify', False)])
# 4. No account: refused by the client before anything is sent to the services.
got = run_client(binary, build, port, 'no-account', None, discovery)
got = run_client(binary, build, port, f'{label}-no-account', None, discovery)
check('no account: client message', got['error'] or '', 'disconnected: This server is in online mode. '
'Sign in with your Microsoft account in the Horizon launcher to join it.')
check('no account: no service calls', services.take_log(), [])
check('no player joined after the first', 'joined the game' in '\n'.join(server.lines[mark:]), False)
# 5. Verified, but not on the whitelist.
server.console('whitelist on')
server.wait_for(r'Whitelist is now turned on', timeout=30)
got = run_client(binary, build, port, f'{label}-not-whitelisted', GOOD, discovery)
server.wait_for(r'Disconnecting Tester', timeout=30)
check('not whitelisted: client message', got['error'], 'disconnected: You are not white-listed on this server!')
check('not whitelisted: verified first', [(e[0], e[3]) for e in services.take_log()],
[('join', True), ('verify', True)])
time.sleep(1)
finally:
server.stop()
return checks, player_lines(server.lines)
def main():
lock, jars, libraries = base.verify_artifacts()
build = lab.LOCAL / NAME
build.mkdir(parents=True, exist_ok=True)
report = build / 'report.json'
report.unlink(missing_ok=True)
oh.run_logged(['cargo', 'build', '--release', '-p', 'horizon-client', '--bin', 'horizon-client'], build,
'compile-rust')
metadata = json.loads(subprocess.check_output(['cargo', 'metadata', '--no-deps', '--format-version', '1'],
cwd=lab.ROOT, text=True))
binary = Path(metadata['target_directory']) / 'release' / ('horizon-client.exe' if os.name == 'nt' else 'horizon-client')
dependencies = os.pathsep.join(map(str, [jars['server'], *libraries]))
services = Services()
discovery = services.url + '/minecraft/client'
vanilla = ['java', '-Xmx2g', '-Duser.timezone=UTC', f'-Dminecraft.api.discovery.host={discovery}',
'-cp', dependencies, 'net.minecraft.server.Main', 'nogui']
horizon = [binary, '--resources', lab.LOCAL / 'horizon-client/26.3', '--dedicated', '--nogui']
checks, transcripts = [], {}
try:
for label, command in [('vanilla', vanilla), ('horizon', horizon)]:
side_checks, transcripts[label] = run_side(label, command, binary, build, services, discovery)
checks += [f'{label}: {c}' for c in side_checks]
finally:
services.stop()
for label in transcripts:
(build / f'{label}.txt').write_text('\n'.join(transcripts[label]) + '\n', encoding='utf-8')
if 'horizon' in transcripts:
import difflib
diff = [l for l in difflib.unified_diff(transcripts['vanilla'], transcripts['horizon'], 'vanilla', 'horizon',
n=0, lineterm='') if l[:1] in '+-' and l[:3] not in ('+++', '---')]
checks.append('server log about the player: ' + ('ok' if not diff else 'differs:\n ' + '\n '.join(diff)))
for line in checks:
print(line)
@@ -257,8 +308,9 @@ def main():
lab.write_json(report, dict(
schema=1, minecraft=lock['minecraft']['version'], platform=platform.platform(),
java=subprocess.check_output(['java', '--version'], text=True).splitlines()[0], checks=len(checks),
scope='Online-mode login against vanilla\'s dedicated server and a mock session service: server hash, '
'RSA key packet, AES/CFB8, session join and verify, play; rejected token, forgotten join, no account.'))
scope='Online-mode login against vanilla\'s dedicated server and Horizon\'s, with a mock session service: '
'server hash, RSA key packet, AES/CFB8, session join and verify, play; rejected token, forgotten '
'join, no account, whitelist; the servers\' log lines about the player.'))
print(f'online mode: {len(checks)} checks ok')