refactor(repo)!: move apps, shared packages, and patch definitions
Everything sat in packages/: two programs next to the small libraries
they use, and the patch data loose at the root. The programs now live in
apps/ and the shared libraries in packages/:
apps/server the local server (was packages/local-service),
package @wto/server
packages/patcher builds the game copy (was packages/build),
package @wto/patcher
patches/exe exe patch definitions (was exe-patches)
patches/gui GUI script patches (was gui-patches)
Inside the server, the network layer folder becomes transport/ so it is
not server/server/, the entry module is server.ts, and startLocalService
and its types become startServer, ServerOptions and RunningServer. The
health answer names the app wto-server, and the TLS files in the data
folder are server.key and server.crt (made again on the next start).
Comments, logs and tool texts say "local server".
The patcher no longer reaches into the server's folder. It read the
default tuning/store.jsonc by a relative path to register bundle GUIDs
for the multi-vehicle packs, which also ignored the player's overrides
in <data>/tuning/. It now takes the pack ids from its caller; the
server exports multiVehiclePackIds(dataFolder), which reads them the way
the running server does. rebuildGuiPack takes the patch folder from its
caller as well.
The development build moves out of the patcher to
scripts/build-game-copy.ts, since it uses both the server and the
patcher. It needs --data (the folder the server will run with) and
defaults the patch folders and the hook library to this repository.
Checked with the root typecheck, check-patches, check-gui,
check-offsets, the native build, a run of build-game-copy.ts into a
scratch folder (12 exe patches, 35 GUI patches, 496 bundle GUIDs as
before) and a server start from apps/server answering wto-server.
BREAKING CHANGE: Use @wto/server and @wto/patcher from the new workspace paths. Run the development build with scripts/build-game-copy.ts and pass --data.
This commit is contained in:
1 parent
9c813ccf7d
commit
930ea07630
476 files changed
+275
-251
No files matched your search
@@ -0,0 +1,55 @@
|
||||
/**
|
||||
* Describe the first bytes of a connection that did not speak HTTP, for the traffic log:
|
||||
* a TLS handshake (an address still using https://), or some other protocol (the matching
|
||||
* server and chat connect to the same port with their own protocols).
|
||||
*/
|
||||
|
||||
export interface FirstBytesInfo {
|
||||
readonly kind: 'tls' | 'other';
|
||||
/** Host name the client asked for in a TLS handshake (server name indication), if any. */
|
||||
readonly serverName?: string;
|
||||
readonly hexHead: string;
|
||||
readonly textHead: string;
|
||||
}
|
||||
|
||||
const TLS_HANDSHAKE_RECORD = 0x16;
|
||||
const CLIENT_HELLO = 0x01;
|
||||
const SERVER_NAME_EXTENSION = 0x0000;
|
||||
|
||||
export function describeFirstBytes(bytes: Uint8Array): FirstBytesInfo {
|
||||
const head = bytes.subarray(0, 64);
|
||||
const common = {
|
||||
hexHead: Buffer.from(head).toString('hex'),
|
||||
textHead: Buffer.from(head).toString('latin1').replace(/[^\x20-\x7e]/g, '.'),
|
||||
};
|
||||
if (bytes[0] !== TLS_HANDSHAKE_RECORD) return { kind: 'other', ...common };
|
||||
const serverName = readServerName(bytes);
|
||||
return serverName === undefined ? { kind: 'tls', ...common } : { kind: 'tls', serverName, ...common };
|
||||
}
|
||||
|
||||
/** Walk a TLS ClientHello (RFC 8446 4.1.2) to the server_name extension (RFC 6066 3). */
|
||||
function readServerName(bytes: Uint8Array): string | undefined {
|
||||
const view = Buffer.from(bytes);
|
||||
try {
|
||||
let at = 5;
|
||||
if (view[at] !== CLIENT_HELLO) return undefined;
|
||||
at += 4 + 2 + 32;
|
||||
at += 1 + view.readUInt8(at);
|
||||
at += 2 + view.readUInt16BE(at);
|
||||
at += 1 + view.readUInt8(at);
|
||||
const extensionsEnd = at + 2 + view.readUInt16BE(at);
|
||||
at += 2;
|
||||
while (at + 4 <= extensionsEnd) {
|
||||
const type = view.readUInt16BE(at);
|
||||
const length = view.readUInt16BE(at + 2);
|
||||
if (type === SERVER_NAME_EXTENSION) {
|
||||
const nameLength = view.readUInt16BE(at + 4 + 3);
|
||||
return view.toString('ascii', at + 4 + 5, at + 4 + 5 + nameLength);
|
||||
}
|
||||
at += 4 + length;
|
||||
}
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
/**
|
||||
* Record of everything the game sent to the local server, so that what the client actually
|
||||
* asks for can be read off instead of guessed (implementation notes, milestone 1 steps 3 and 4).
|
||||
*
|
||||
* <data>/logs/<start time>/requests.jsonl one JSON line per HTTP exchange or raw connection
|
||||
* <data>/logs/<start time>/bodies/ request and response bodies, plus readable text
|
||||
* (decoded BLK) next to them
|
||||
*
|
||||
* Each exchange also prints one line to the console.
|
||||
*/
|
||||
import { appendFileSync, mkdirSync, writeFileSync } from 'node:fs';
|
||||
import type { IncomingHttpHeaders } from 'node:http';
|
||||
import { join } from 'node:path';
|
||||
import type { ServiceRequest, ServiceResponse } from '../transport/exchange.ts';
|
||||
import type { FirstBytesInfo } from './first-bytes.ts';
|
||||
|
||||
export interface MatchingLogEntry {
|
||||
readonly direction: 'in' | 'out';
|
||||
/** First byte of the message (0x1a request, 0x1b answer, other: notification). */
|
||||
readonly kind: number;
|
||||
readonly summary: string;
|
||||
readonly unhandled: boolean;
|
||||
readonly text: string;
|
||||
}
|
||||
|
||||
export class TrafficLog {
|
||||
readonly folder: string;
|
||||
readonly #linesPath: string;
|
||||
readonly #bodiesFolder: string;
|
||||
#sequence = 0;
|
||||
|
||||
constructor(dataFolder: string) {
|
||||
this.folder = join(dataFolder, 'logs', new Date().toISOString().replace(/[:.]/g, '-'));
|
||||
this.#linesPath = join(this.folder, 'requests.jsonl');
|
||||
this.#bodiesFolder = join(this.folder, 'bodies');
|
||||
mkdirSync(this.#bodiesFolder, { recursive: true });
|
||||
}
|
||||
|
||||
exchange(request: ServiceRequest, service: string, response: ServiceResponse, connectionId: number): void {
|
||||
const id = this.#next();
|
||||
const notes = response.logNotes ?? {};
|
||||
const files = {
|
||||
request: this.#writeBody(id, 'req.bin', notes.redactedRequestBody ?? request.body),
|
||||
requestText: this.#writeBody(id, 'req.txt', notes.requestText),
|
||||
response: notes.omitResponseBody ? null : this.#writeBody(id, 'resp.bin', response.body),
|
||||
responseText: this.#writeBody(id, 'resp.txt', notes.responseText),
|
||||
};
|
||||
this.#append({
|
||||
id, time: new Date().toISOString(), kind: 'http', connection: connectionId,
|
||||
method: request.method, host: request.originalHost ?? null, path: request.path, query: request.query.toString(),
|
||||
headers: plainHeaders(request.headers), requestBytes: request.body.length,
|
||||
service, summary: notes.summary ?? null, unhandled: notes.unhandled ?? false,
|
||||
status: response.status, responseBytes: byteLength(response.body), files,
|
||||
});
|
||||
const flag = notes.unhandled ? ' [UNHANDLED]' : '';
|
||||
console.log(`#${id} ${request.method} ${request.originalHost ?? '(no host)'}${request.path} -> ${service}` +
|
||||
`${notes.summary ? ` ${notes.summary}` : ''} = ${response.status}${flag}`);
|
||||
}
|
||||
|
||||
connectionOpened(connectionId: number, remotePort: number | undefined): void {
|
||||
this.#append({ id: this.#next(), time: new Date().toISOString(), kind: 'connection-open', connection: connectionId, remotePort: remotePort ?? null });
|
||||
}
|
||||
|
||||
connectionClosed(connectionId: number, bytesRead: number, bytesWritten: number): void {
|
||||
this.#append({ id: this.#next(), time: new Date().toISOString(), kind: 'connection-close', connection: connectionId, bytesRead, bytesWritten });
|
||||
}
|
||||
|
||||
nonHttpConnection(connectionId: number, info: FirstBytesInfo, error: string): void {
|
||||
const id = this.#next();
|
||||
const { kind, ...details } = info;
|
||||
this.#append({ id, time: new Date().toISOString(), kind: `non-http-${kind}`, connection: connectionId, ...details, parserError: error });
|
||||
console.log(`#${id} non-HTTP connection (${info.kind}${info.serverName ? `, asks for ${info.serverName}` : ''}): ${info.textHead}`);
|
||||
}
|
||||
|
||||
/** One message of the matching protocol; the text is already redacted by the sender. */
|
||||
matchingMessage(connectionId: number, entry: MatchingLogEntry): void {
|
||||
const id = this.#next();
|
||||
const text = this.#writeBody(id, `matching-${entry.direction}.json`, entry.text);
|
||||
this.#append({ id, time: new Date().toISOString(), kind: 'matching', connection: connectionId, direction: entry.direction,
|
||||
messageKind: entry.kind, summary: entry.summary, unhandled: entry.unhandled, files: { text } });
|
||||
const flag = entry.unhandled ? ' [UNHANDLED]' : '';
|
||||
console.log(`#${id} matching ${entry.direction === 'in' ? '<-' : '->'} ${entry.summary}${flag}`);
|
||||
}
|
||||
|
||||
matchingError(connectionId: number, message: string): void {
|
||||
const id = this.#next();
|
||||
this.#append({ id, time: new Date().toISOString(), kind: 'matching-error', connection: connectionId, message });
|
||||
console.log(`#${id} matching connection ${connectionId} closed: ${message}`);
|
||||
}
|
||||
|
||||
webSocket(connectionId: number, request: ServiceRequest): void {
|
||||
const id = this.#next();
|
||||
this.#append({ id, time: new Date().toISOString(), kind: 'websocket', connection: connectionId, host: request.originalHost ?? null,
|
||||
path: request.path, query: request.query.toString(), headers: plainHeaders(request.headers) });
|
||||
console.log(`#${id} WebSocket ${request.originalHost ?? '(no host)'}${request.path} (refused)`);
|
||||
}
|
||||
|
||||
#next(): number {
|
||||
this.#sequence += 1;
|
||||
return this.#sequence;
|
||||
}
|
||||
|
||||
#append(line: object): void {
|
||||
appendFileSync(this.#linesPath, JSON.stringify(line) + '\n');
|
||||
}
|
||||
|
||||
#writeBody(id: number, suffix: string, content: Uint8Array | string | undefined): string | null {
|
||||
if (content === undefined || byteLength(content) === 0) return null;
|
||||
const name = `${String(id).padStart(6, '0')}.${suffix}`;
|
||||
writeFileSync(join(this.#bodiesFolder, name), content);
|
||||
return name;
|
||||
}
|
||||
}
|
||||
|
||||
function byteLength(body: Uint8Array | string | undefined): number {
|
||||
if (body === undefined) return 0;
|
||||
return typeof body === 'string' ? Buffer.byteLength(body) : body.length;
|
||||
}
|
||||
|
||||
function plainHeaders(headers: IncomingHttpHeaders): Record<string, string> {
|
||||
const result: Record<string, string> = {};
|
||||
for (const [name, value] of Object.entries(headers)) {
|
||||
if (value !== undefined) result[name] = Array.isArray(value) ? value.join(', ') : value;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
Reference in new issue
Block a user