Files
WT-Offline/packages/patcher/src/network/address-rewrite.ts
T
N0VAN0M4LY dcc67ec148 docs(repo): use local server in remaining comments and titles
Comments where "local" and "service" fell on two lines, and the Chinese
titles of five patch definitions, still named the local service.

Checked with the root typecheck, check-patches and check-gui.
2026-11-29 03:28:56 +08:00

61 lines
2.8 KiB
TypeScript

/**
* Rewrite one configuration string that names a Gaijin server so that it names the local
* server instead. Only values that clearly are server addresses are touched:
*
* http(s)://host[:port]/path -> http://127.0.0.1:<port>/host/path (host is a Gaijin domain or an external IP)
* ws(s)://host[:port]/path -> ws://127.0.0.1:<port>/host/path
* host:port -> 127.0.0.1:<port> (raw TCP endpoints: matching, chat)
*
* Bare Gaijin host names without a port (e.g. the event log host) are left as they are so the
* client keeps their Host header and SNI; wto_ws.dll resolves them locally and sends their
* connections to the local server port. Everything else (file patterns,
* placeholders such as "https://not.used", DNS server IPs) is also left as it is.
*/
import { visitParams, type BlkBlock } from '@wto/blk';
import { hostPort, localUrl, type LocalEndpoint } from './local-endpoint.ts';
export interface AddressChange {
readonly path: string;
readonly before: string;
readonly after: string;
}
/** Rewrite every server address among the string parameters of `root`, in place. */
export function redirectAddresses(root: BlkBlock, endpoint: LocalEndpoint, pathPrefix: readonly string[] = []): AddressChange[] {
const changes: AddressChange[] = [];
visitParams(root, (param, path) => {
if (param.value.type !== 'string') return;
const rewritten = rewriteAddress(param.value.value, endpoint);
if (rewritten === null || rewritten === param.value.value) return;
changes.push({ path: [...pathPrefix, ...path, param.name].join('/'), before: param.value.value, after: rewritten });
param.value = { type: 'string', value: rewritten };
});
return changes;
}
/** gaijin.ops is Gaijin's internal domain, used by the internal test environments. */
const GAIJIN_DOMAINS = ['gaijin.net', 'gaijinent.com', 'warthunder.com', 'gaijin.ops'];
const URL_PATTERN = /^(https?|wss?):\/\/([^/:?#]+)(?::\d+)?([/?#].*)?$/i;
const HOST_PORT_PATTERN = /^([a-z0-9.-]+):(\d{1,5})$/i;
const IPV4_PATTERN = /^\d{1,3}(?:\.\d{1,3}){3}$/;
export function rewriteAddress(value: string, endpoint: LocalEndpoint): string | null {
const url = URL_PATTERN.exec(value);
if (url) {
const host = url[2]!.toLowerCase();
const scheme = url[1]!.toLowerCase().startsWith('ws') ? 'ws' : 'http';
return isServerHost(host) ? localUrl(endpoint, host, url[3] ?? '/', scheme) : null;
}
const hostAndPort = HOST_PORT_PATTERN.exec(value);
if (hostAndPort) return isServerHost(hostAndPort[1]!.toLowerCase()) ? hostPort(endpoint) : null;
return null;
}
function isServerHost(host: string): boolean {
return isGaijinDomain(host) || (IPV4_PATTERN.test(host) && !host.startsWith('127.'));
}
function isGaijinDomain(host: string): boolean {
return GAIJIN_DOMAINS.some((domain) => host === domain || host.endsWith(`.${domain}`));
}