feat(accounts): implement local login

Route login and character requests by their original host names on one
loopback port. Add local accounts, saves, and credential redaction.
This commit is contained in:
N0VAN0M4LY committed 2026-11-24 05:01:26 +08:00
1 parent c3914ec0d6
commit 7f482ae530
55 files changed
+1559 -58

No files matched your search

@@ -0,0 +1,15 @@
/** Char action names mapped to their handlers (full list of names: research-data/char_actions_37.json). */
import type { PlayerProfiles } from '../../profile/player-profiles.ts';
import type { CharActionHandler } from '../char-action.ts';
import { initialMeta, legacyUserData } from './online-init-actions.ts';
export interface ActionDependencies {
readonly profiles: PlayerProfiles;
}
export function actionTable(deps: ActionDependencies): ReadonlyMap<string, CharActionHandler> {
return new Map<string, CharActionHandler>([
['cln_get_initial_meta', initialMeta()],
['cln_legacy_get_userdata', legacyUserData(deps.profiles)],
]);
}
@@ -0,0 +1,31 @@
/**
* Char actions of the online initialisation after sign-in, up to the profile
*.
*/
import { emptyBlock } from '@wto/blk';
import type { PlayerProfiles } from '../../profile/player-profiles.ts';
import type { CharAnswer } from '../char-answers.ts';
import type { CharActionContext, CharActionHandler } from '../char-action.ts';
/**
* cln_get_initial_meta: server-side metadata the client reads in GameSaveData's initial-meta
* handling (aces.exe 0xeb5f60). binaryDataMetainfo lists server-side binary files the client
* must fetch and compare by hash; the local service has none, so the list is empty and no
* downloads follow.
*/
export function initialMeta(): CharActionHandler {
return () => {
const meta = emptyBlock();
meta.blocks.push(emptyBlock('binaryDataMetainfo'), emptyBlock('generalBlkMeta'));
return { kind: 'data', blk: meta };
};
}
/** cln_legacy_get_userdata: the whole profile (ClientEconomicState::loadOnline, aces.exe 0x2a23b0). */
export function legacyUserData(profiles: PlayerProfiles): CharActionHandler {
return (context) => withUser(context, (userId) => ({ kind: 'data', blk: profiles.profileOf(userId) }));
}
function withUser(context: CharActionContext, run: (userId: string) => CharAnswer): CharAnswer {
return context.userId ? run(context.userId) : { kind: 'error', code: 'AUTH_REQ_ERROR' };
}
@@ -0,0 +1,12 @@
/** What a char action handler gets and returns. Handlers belong to the rules layer's domains. */
import type { BlkBlock } from '@wto/blk';
import type { CharAnswer } from './char-answers.ts';
export interface CharActionContext {
/** From the request's userid header; undefined when the client sent none. */
readonly userId: string | undefined;
/** The request's BLK parameters, when the body carried a BLK. */
readonly payload: BlkBlock | undefined;
}
export type CharActionHandler = (context: CharActionContext) => CharAnswer;
@@ -0,0 +1,29 @@
/**
* The char service's three kinds of answer (implementation notes; aces.exe 2.61.0.37,
* CharServerClient::handleReqResult at 0x336d590):
* - data: HTTP 200, a compression marker and a BLK;
* - done: HTTP 200 with a body starting "!OK", for requests that expect no data;
* - error: a body starting "!ERROR:" followed by an error code.
*/
import { formatBlkText, type BlkBlock } from '@wto/blk';
import type { ServiceResponse } from '../server/exchange.ts';
import { encodeDataAnswer } from './char-payload.ts';
export type CharAnswer =
| { readonly kind: 'data'; readonly blk: BlkBlock }
| { readonly kind: 'done' }
| { readonly kind: 'error'; readonly code: string };
export function toResponse(answer: CharAnswer): ServiceResponse {
switch (answer.kind) {
case 'data':
return { status: 200, headers: BINARY, body: encodeDataAnswer(answer.blk), logNotes: { responseText: formatBlkText(answer.blk) } };
case 'done':
return { status: 200, headers: TEXT, body: '!OK' };
case 'error':
return { status: 200, headers: TEXT, body: `!ERROR:${answer.code}` };
}
}
const BINARY = { 'content-type': 'application/octet-stream' };
const TEXT = { 'content-type': 'text/plain' };
@@ -0,0 +1,46 @@
/**
* Bodies exchanged with the char service.
*
* Answers carrying data are a compression marker followed by a binary BLK; the client loads
* them with loadBlkFromAnswer (aces.exe 0x336fd60). We always send them uncompressed.
*
* What the client sends in request bodies is not established yet, so
* decoding is attempted in the forms that are plausible (marked or bare binary BLK, form
* text) and the traffic log records which form it was.
*/
import { decodeBlk, encodeBlk, formatBlkText, type BlkBlock } from '@wto/blk';
import { markUncompressed, unpackMarked } from './compression-marker.ts';
export function encodeDataAnswer(blk: BlkBlock): Uint8Array {
return markUncompressed(encodeBlk(blk));
}
export interface DecodedPayload {
/** How the body was laid out, for the traffic log. */
readonly form: string;
readonly blk?: BlkBlock;
/** Readable rendering for the traffic log. */
readonly text: string;
}
const BINARY_BLK_LABELS = new Set([1, 2, 3, 6]);
export function decodeRequestPayload(body: Uint8Array): DecodedPayload {
if (body.length === 0) return { form: 'empty', text: '' };
try {
const marked = unpackMarked(body);
if (marked) {
if (!marked.data) return { form: `marked ${marked.method}, not unpacked`, text: '' };
return describeBlk(`marked ${marked.method}`, marked.data);
}
if (BINARY_BLK_LABELS.has(body[0]!)) return describeBlk('bare binary BLK', body);
} catch (error) {
return { form: `undecodable (${(error as Error).message})`, text: '' };
}
return { form: 'text', text: Buffer.from(body).toString('utf8') };
}
function describeBlk(form: string, bytes: Uint8Array): DecodedPayload {
const blk = decodeBlk(bytes);
return { form, blk, text: formatBlkText(blk) };
}
@@ -0,0 +1,57 @@
/**
* char service: profiles and player actions.
*
* The client names the action in the request header "action" and identifies the player in
* "userid" (request headers assembled at aces.exe 0x336c590). The local service trusts
* userid as it is: there is nothing to protect on a single-player machine.
*
* An action with no handler yet gets a data answer holding an empty BLK and is marked
* unhandled in the traffic log. handleReqResult treats any 200 answer that is not "!ERROR:"
* or "!RETRY" as success for both data requests and requests that expect "!OK", so this
* default lets the client continue and reveal what it asks for next.
*/
import { emptyBlock } from '@wto/blk';
import type { Service, ServiceRequest, ServiceResponse } from '../server/exchange.ts';
import type { CharActionHandler } from './char-action.ts';
import { toResponse, type CharAnswer } from './char-answers.ts';
import { decodeRequestPayload } from './char-payload.ts';
const CHAR_HOST = /^char-[a-z0-9-]+\.warthunder\.com$/;
export class CharService implements Service {
readonly name = 'char';
readonly #actions: ReadonlyMap<string, CharActionHandler>;
constructor(actions: ReadonlyMap<string, CharActionHandler>) {
this.#actions = actions;
}
accepts(request: ServiceRequest): boolean {
return request.originalHost !== undefined && CHAR_HOST.test(request.originalHost);
}
handle(request: ServiceRequest): ServiceResponse {
const action = header(request, 'action') ?? '(no action header)';
const payload = decodeRequestPayload(request.body);
const handler = this.#actions.get(action);
const answer: CharAnswer = handler
? handler({ userId: header(request, 'userid'), payload: payload.blk })
: { kind: 'data', blk: emptyBlock() };
const response = toResponse(answer);
return {
...response,
logNotes: {
...response.logNotes,
summary: `${action} [request body: ${payload.form}] -> ${answer.kind}${answer.kind === 'error' ? ` ${answer.code}` : ''}`,
unhandled: !handler,
requestText: payload.text,
},
};
}
}
function header(request: ServiceRequest, name: string): string | undefined {
const value = request.headers[name];
const text = Array.isArray(value) ? value[0] : value;
return text ? text : undefined;
}
@@ -0,0 +1,45 @@
/**
* Data answers of the char service start with one byte naming how the rest is compressed.
*
* Source: aces.exe 2.61.0.37. The client's table of compression methods is filled at
* 0x3373cd0 (name, implementation, marker byte); the answer is unpacked by
* processPotentiallyCompressedAnswer (0x3372190), which takes the bytes after an 'i' marker
* as they are. Only the methods Node can unpack are decoded here.
*/
import { gunzipSync, inflateSync, zstdDecompressSync } from 'node:zlib';
export const MARKER_NONE = 0x69; // 'i'
const DECODERS: ReadonlyMap<number, { readonly name: string; readonly unpack?: (data: Uint8Array) => Uint8Array }> = new Map([
[MARKER_NONE, { name: 'none', unpack: (data: Uint8Array) => data }],
[0x5a, { name: 'zstd', unpack: (data: Uint8Array) => zstdDecompressSync(data) }], // 'Z'
[0x7a, { name: 'zlib', unpack: (data: Uint8Array) => inflateSync(data) }], // 'z'
[0x1f, { name: 'gzip', unpack: (data: Uint8Array) => gunzipSync(data) }],
[0x4c, { name: 'lz4hc' }], // 'L'
[0x6c, { name: 'lz4' }], // 'l'
[0x73, { name: 'snappy' }], // 's'
[0x62, { name: 'bzip2' }], // 'b'
[0x56, { name: 'vromfs' }], // 'V'
]);
export interface UnpackResult {
readonly method: string;
/** Undefined when the method is known but cannot be unpacked here. */
readonly data: Uint8Array | undefined;
}
/** Undefined when the first byte is not a compression marker. */
export function unpackMarked(bytes: Uint8Array): UnpackResult | undefined {
const decoder = DECODERS.get(bytes[0] ?? -1);
if (!decoder) return undefined;
// gzip's marker is the first byte of the gzip stream itself.
const rest = decoder.name === 'gzip' ? bytes : bytes.subarray(1);
return { method: decoder.name, data: decoder.unpack ? decoder.unpack(rest) : undefined };
}
export function markUncompressed(data: Uint8Array): Uint8Array {
const marked = new Uint8Array(data.length + 1);
marked[0] = MARKER_NONE;
marked.set(data, 1);
return marked;
}