Handle matching login and decorative content manifests

Add matching frame and message handling, per-connection session
encryption, and loopback traffic dispatch. Serve the decorative content
manifest and empty indices to remove startup download waits.

Resolve character requests to their explicit account ID, include the
nickname in profile replies, and redact matching traffic logs.
This commit is contained in:
N0VAN0M4LY committed 2026-11-24 08:12:40 +08:00
1 parent 18f63bf33e
commit 5a1fd594e0
24 files changed
+857 -37

No files matched your search

@@ -1,8 +1,8 @@
/**
* The local service's one listening port. Every address in the game's network.blk points
* here, so besides HTTP it also receives connections in other protocols
* (TLS from addresses that still say https://, the matching server's and chat's own
* protocols). Those are recorded in the traffic log and left open without an answer.
* The HTTP side of the local service's port. port-listener.ts hands it every connection
* that is not the matching protocol; besides HTTP that includes TLS from addresses that still
* say https:// and other protocols (chat), which are recorded in the traffic log and left
* open without an answer.
*/
import { createServer, type IncomingMessage, type Server, type ServerResponse } from 'node:http';
import type { Socket } from 'node:net';
@@ -16,11 +16,10 @@ const MAX_BODY_BYTES = 64 * 1024 * 1024;
/** Lets a launcher check that this process is the local service and is ready. */
export const HEALTH_PATH = '/_wto/health';
export function createListener(router: Router, log: TrafficLog): Server {
const connections = new WeakMap<Socket, number>();
let nextConnection = 0;
const connectionOf = (socket: Socket) => connections.get(socket) ?? 0;
/** Connection number given by the port listener, for the traffic log. */
export type ConnectionNumbers = (socket: Socket) => number;
export function createHttpServer(router: Router, log: TrafficLog, connectionOf: ConnectionNumbers): Server {
const server = createServer({ requestTimeout: 0, headersTimeout: 0, keepAliveTimeout: 0 }, (incoming, outgoing) => {
readBody(incoming).then(
(body) => respond(incoming, outgoing, body),
@@ -41,14 +40,6 @@ export function createListener(router: Router, log: TrafficLog): Server {
outgoing.end(response.body ?? '');
}
server.on('connection', (socket: Socket) => {
nextConnection += 1;
const id = nextConnection;
connections.set(socket, id);
log.connectionOpened(id, socket.remotePort);
socket.on('close', () => log.connectionClosed(id, socket.bytesRead, socket.bytesWritten));
});
server.on('clientError', (error: NodeJS.ErrnoException & { rawPacket?: Buffer }, socket: Socket) => {
if (error.rawPacket && error.rawPacket.length > 0) {
log.nonHttpConnection(connectionOf(socket), describeFirstBytes(error.rawPacket), error.code ?? error.message);
@@ -0,0 +1,55 @@
/**
* The local service's one listening port. Every address in the game's network.blk points
* here, so connections arrive in several protocols. The first bytes of
* a connection decide where it goes: the matching protocol (first byte 1, see
* matching/matching-frame.ts) to the matching service, everything else (HTTP, TLS, other)
* to the HTTP server.
*/
import { createServer, type Server, type Socket } from 'node:net';
import { MatchingService } from '../matching/matching-service.ts';
import { startsMatchingPacket } from '../matching/matching-frame.ts';
import type { TrafficLog } from '../traffic-log/traffic-log.ts';
import { createHttpServer } from './http-listener.ts';
import type { Router } from './router.ts';
export interface PortListener {
readonly server: Server;
/** Drop every open connection (on shutdown; the game keeps some open indefinitely). */
destroyConnections(): void;
}
export function createPortListener(router: Router, log: TrafficLog): PortListener {
const numbers = new WeakMap<Socket, number>();
const open = new Set<Socket>();
let lastNumber = 0;
const http = createHttpServer(router, log, (socket) => numbers.get(socket) ?? 0);
const matching = new MatchingService(log);
const server = createServer((socket) => {
lastNumber += 1;
const number = lastNumber;
numbers.set(socket, number);
open.add(socket);
log.connectionOpened(number, socket.remotePort);
socket.on('close', () => {
open.delete(socket);
log.connectionClosed(number, socket.bytesRead, socket.bytesWritten);
});
socket.on('error', () => socket.destroy());
socket.once('data', (first: Buffer) => {
socket.pause();
socket.unshift(first);
if (startsMatchingPacket(first)) matching.accept(socket, number);
else http.emit('connection', socket);
process.nextTick(() => socket.resume());
});
});
return {
server,
destroyConnections: () => {
for (const socket of open) socket.destroy();
},
};
}