117 lines
4.5 KiB
TypeScript
117 lines
4.5 KiB
TypeScript
/**
|
|
* char service: profiles and player actions.
|
|
*
|
|
* The client names the action in the request header "action" (request headers assembled at
|
|
* aces.exe 0x336c590). Requests carry no "userid" header; the player is identified by
|
|
* "uidhint", the user id from the sign-in answer (added by
|
|
* EconomicSerializeJob::doJob, 0x2968a5). The local server trusts uidhint 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 { ModCharRequest, ModCharResponse } from '@wto/mods';
|
|
import type { ServerModRuntime } from '../mods/server-mod-runtime.ts';
|
|
import type { Service, ServiceRequest, ServiceResponse } from '../transport/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>;
|
|
readonly #modRuntime: ServerModRuntime | undefined;
|
|
|
|
constructor(actions: ReadonlyMap<string, CharActionHandler>, modRuntime?: ServerModRuntime) {
|
|
this.#actions = actions;
|
|
this.#modRuntime = modRuntime;
|
|
}
|
|
|
|
accepts(request: ServiceRequest): boolean {
|
|
return request.originalHost !== undefined && CHAR_HOST.test(request.originalHost);
|
|
}
|
|
|
|
handle(request: ServiceRequest): ServiceResponse | Promise<ServiceResponse> {
|
|
const action = header(request, 'action') ?? '(no action header)';
|
|
if (this.#modRuntime?.hasCharOverride(action)) {
|
|
return this.#handleWithModOverride(action, request);
|
|
}
|
|
return this.#handleCore(action, request);
|
|
}
|
|
|
|
#handleCore(action: string, request: ServiceRequest): ServiceResponse {
|
|
const payload = decodeRequestPayload(request.body);
|
|
const handler = this.#actions.get(action);
|
|
const answer: CharAnswer = handler
|
|
? handler({
|
|
userId: header(request, 'uidhint'),
|
|
payload: payload.blk,
|
|
rawText: payload.text,
|
|
rawBody: request.body,
|
|
headerParam: (name) => header(request, name.toLowerCase()),
|
|
})
|
|
: { 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,
|
|
},
|
|
};
|
|
}
|
|
|
|
async #handleWithModOverride(action: string, request: ServiceRequest): Promise<ServiceResponse> {
|
|
const headers: Record<string, string | undefined> = {};
|
|
for (const [key, value] of Object.entries(request.headers)) {
|
|
headers[key] = Array.isArray(value) ? value[0] : value;
|
|
}
|
|
const query: Record<string, string> = {};
|
|
for (const [key, value] of request.query.entries()) query[key] = value;
|
|
|
|
const modReq: ModCharRequest = {
|
|
action,
|
|
userId: header(request, 'uidhint') ?? '',
|
|
headers,
|
|
body: request.body,
|
|
query,
|
|
};
|
|
|
|
const result = await this.#modRuntime!.dispatchCharOverride(action, modReq, async (nextReq): Promise<ModCharResponse> => {
|
|
const forwarded: ServiceRequest = { ...request, body: nextReq.body };
|
|
const coreResp = this.#handleCore(nextReq.action, forwarded);
|
|
return {
|
|
status: coreResp.status,
|
|
...(coreResp.headers ? { headers: coreResp.headers } : {}),
|
|
body: coreResp.body ?? '',
|
|
};
|
|
});
|
|
|
|
const responseHeaders: Record<string, string> = { ...(result.headers ?? {}) };
|
|
if (result.contentType && !responseHeaders['content-type'] && !responseHeaders['Content-Type']) {
|
|
responseHeaders['content-type'] = result.contentType;
|
|
}
|
|
return {
|
|
status: result.status ?? 200,
|
|
headers: responseHeaders,
|
|
body: result.body,
|
|
logNotes: {
|
|
summary: `${action} [mod handler] -> ${result.status ?? 200}`,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|