Files
WT-Offline/packages/local-service/src/server/request-target.ts
T
N0VAN0M4LY afb22c4f08 Route client traffic to the loopback service
Build the socket library and client-local port configuration for redirecting
traffic to the offline service. Accept HTTPS and the existing matching and
HTTP protocols on the same port. Route requests by the original host.
2026-11-25 05:02:34 +08:00

36 lines
1.7 KiB
TypeScript

/** Split a request into the original Gaijin host (when known), the path and the query. */
export interface RequestTarget {
readonly originalHost: string | undefined;
readonly path: string;
readonly query: URLSearchParams;
}
/**
* Addresses rewritten by the build carry the original host as the first path segment (packages/build,
* network/address-rewrite.ts moves only Gaijin hosts and external IPv4 addresses there, so only
* those count as a host segment; "/login.php" is a path, not a host). Connections the network
* hook library redirected keep their original URL, so their host is the
* Host header (for https, the one sent inside the TLS stream) unless that names this machine.
*/
const GAIJIN_DOMAINS = ['gaijin.net', 'gaijinent.com', 'warthunder.com', 'gaijin.ops'];
const IPV4 = /^\d{1,3}(\.\d{1,3}){3}$/;
const LOCAL_HOSTS = new Set(['127.0.0.1', '127.0.0.2', 'localhost', '::1', '[::1]']);
export function parseRequestTarget(url: string, hostHeader?: string): RequestTarget {
const parsed = new URL(url, 'http://local');
const segments = parsed.pathname.split('/');
const first = (segments[1] ?? '').toLowerCase();
if (isHostSegment(first)) return { originalHost: first, path: `/${segments.slice(2).join('/')}`, query: parsed.searchParams };
return { originalHost: headerHost(hostHeader), path: parsed.pathname, query: parsed.searchParams };
}
function isHostSegment(segment: string): boolean {
return IPV4.test(segment) || GAIJIN_DOMAINS.some((domain) => segment === domain || segment.endsWith(`.${domain}`));
}
function headerHost(header: string | undefined): string | undefined {
const host = header?.trim().toLowerCase().replace(/:\d+$/, '');
return host && !LOCAL_HOSTS.has(host) ? host : undefined;
}