import { EventEmitter } from 'node:events'; import http from 'node:http'; import net from 'node:net'; import { ProxyConfig } from './config.js'; import { Connection, Logger, ProxyStats, ProxyUpstream } from './types.js'; /** * Base class for implementing proxies. * * Features an HTTP server accepting both HTTP and HTTPS incoming traffic, * and routing such traffic either to a target destination or to the next * proxy in chain (aka "upstream" proxy). */ export declare class BaseProxy extends EventEmitter { server: http.Server | null; clientSockets: Set; trackedConnections: Map; stats: ProxyStats; upstreamStats: Map; defaultUpstream: ProxyUpstream | null; logger: Logger; muteErrorCodes: string[]; warnErrorCodes: string[]; connectRetryAttempts: number; connectRetryInterval: number; connectTimeout: number; constructor(options?: Partial); /** * Hook for implementing routing logic per connection. * * Invoked from CONNECT (HTTPS) or HTTP request handlers prior to * establishing the onward connection. * * Return `null` for a direct connection to `host`, or upstream info * to connect via an upstream proxy. */ matchRoute(_host: string, _request: http.IncomingMessage): ProxyUpstream | null; /** * Returns CA certificates to use when issuing HTTPS requests. * Override to provide custom certificates (e.g. self-signed or custom CAs). */ getCACertificates(): string[]; clearStats(): void; start(port: number, hostname?: string, options?: http.ServerOptions): Promise; /** * Shuts down the proxy server. * * @param force if `true`, forcibly closes all established client connections. */ shutdown(force?: boolean): Promise; isRunning(): boolean; getServerAddress(): string; getServerPort(): number; /** * Forcibly closes established client connections. * In-flight requests fail with ECONNRESET. */ closeAllSockets(): void; /** * Tracks established connections so they can be forcibly closed * by invoking `closeAllSockets`. */ protected onConnection(socket: net.Socket): void; /** * Error handler hook. Override for custom error handling. * By default logs the error unless muted via `muteErrorCodes`. */ onError(error: unknown, details?: Record): void; authenticate(_req: http.IncomingMessage): Promise; onConnect(req: http.IncomingMessage, clientSocket: net.Socket): Promise; protected replyToConnectRequest(clientSocket: net.Socket, connection: Connection): Promise; /** * Creates an onward connection to `host` either directly or via upstream proxy. */ createSslConnection(inboundConnectReq: http.IncomingMessage, upstream: ProxyUpstream | null): Promise; /** * Establishes onward connections with retry logic: * - first attempt is made immediately * - subsequent attempts are scheduled at `connectRetryInterval` * - first successful connection wins; others are destroyed * - each attempt is capped by `connectTimeout` */ /** * Starts up to `connectRetryAttempts + 1` parallel connect attempts, * staggered by `connectRetryInterval`. * * The first successful connection is used. Pending timers are cleared and * sockets from the other attempts are destroyed. */ protected sslConnectWithRetry(inboundConnectReq: http.IncomingMessage, upstream: ProxyUpstream | null): Promise; protected sslProxyConnect(inboundConnectReq: http.IncomingMessage, upstream: ProxyUpstream, onSocket?: (socket: net.Socket) => void): Promise; protected sslDirectConnect(inboundConnectReq: http.IncomingMessage, onSocket?: (socket: net.Socket) => void): Promise; protected createConnectRequest(inboundConnectReq: http.IncomingMessage, upstream: ProxyUpstream): http.ClientRequest; onRequest(req: http.IncomingMessage, res: http.ServerResponse): Promise; protected createProxyHttpRequest(req: http.IncomingMessage, upstream: ProxyUpstream): http.ClientRequest; protected createDirectHttpRequest(req: http.IncomingMessage): http.ClientRequest; protected collectRouteStats(upstreamUrl: string, clientSocket: net.Socket, remoteSocket: net.Socket): void; }