Spaces:
Runtime error
Runtime error
File size: 3,095 Bytes
cd8bd0a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | /**
* Runtime state for Traffic Inspector capture modes.
*
* Held in module-level variables (process-singleton). Survives across route
* handler calls for the lifetime of the process.
*
* Exported mutation functions are the single write path so all route handlers
* stay stateless.
*/
import type { HttpProxyServerHandle } from "@/mitm/inspector/httpProxyServer";
import type { PreviousState } from "@/mitm/inspector/systemProxyConfig";
// ββ HTTP Proxy ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
let httpProxyHandle: HttpProxyServerHandle | null = null;
export function getHttpProxyHandle(): HttpProxyServerHandle | null {
return httpProxyHandle;
}
export function setHttpProxyHandle(handle: HttpProxyServerHandle | null): void {
httpProxyHandle = handle;
}
// ββ System Proxy ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
interface SystemProxyState {
applied: boolean;
port: number | null;
guardUntil: string | null; // ISO 8601
previousState: PreviousState | null;
}
let systemProxyState: SystemProxyState = {
applied: false,
port: null,
guardUntil: null,
previousState: null,
};
let guardTimer: ReturnType<typeof setTimeout> | null = null;
export function getSystemProxyState(): Readonly<SystemProxyState> {
return { ...systemProxyState };
}
export function setSystemProxyApplied(
port: number,
previousState: PreviousState,
guardMinutes: number
): void {
if (guardTimer) clearTimeout(guardTimer);
const guardUntil = new Date(Date.now() + guardMinutes * 60_000).toISOString();
systemProxyState = { applied: true, port, guardUntil, previousState };
guardTimer = setTimeout(
() => {
// Auto-revert after guard period β fire-and-forget.
// Import lazily to avoid circular deps at module load.
import("@/mitm/inspector/systemProxyConfig").then(({ revert }) => {
const ps = systemProxyState.previousState;
systemProxyState = { applied: false, port: null, guardUntil: null, previousState: null };
if (ps) revert(ps).catch(() => {/* best-effort */});
}).catch(() => {/* best-effort */});
},
guardMinutes * 60_000
);
}
export function clearSystemProxy(): void {
if (guardTimer) {
clearTimeout(guardTimer);
guardTimer = null;
}
systemProxyState = { applied: false, port: null, guardUntil: null, previousState: null };
}
// ββ TLS Intercept βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
let tlsInterceptEnabled = process.env.INSPECTOR_TLS_INTERCEPT === "true";
export function isTlsInterceptEnabled(): boolean {
return tlsInterceptEnabled;
}
export function setTlsIntercept(enabled: boolean): void {
tlsInterceptEnabled = enabled;
}
|