import * as url from 'node:url'; import * as net from 'node:net'; import { Socket } from 'node:net'; import * as tls from 'node:tls'; import { TLSSocket } from 'node:tls'; import * as http from 'node:http'; import { IncomingMessage, ServerResponse } from 'node:http'; import * as https from 'node:https'; import * as buffer from 'node:buffer'; import * as stream from 'node:stream'; import { EventEmitter } from 'node:events'; declare class ProxyServer extends EventEmitter { #private; options: Server.ServerOptions; webPasses: WebPassthrough[]; wsPasses: WsPassthrough[]; /** * Creates the proxy server with specified options. * @param options - Config object passed to the proxy */ constructor(options?: Server.ServerOptions); onError(err: NodeJS.ErrnoException): void; /** * A function that wraps the object in a webserver, for your convenience * @param port - Port to listen on * @param hostname - Hostname to listen on */ listen(port: number, hostname: string): this; /** * A function that closes the inner webserver and stops listening on given port */ close(callback?: () => void): void; before(type: 'web' | 'ws', passName: string, callback: WebPassthrough & WsPassthrough): void; after(type: 'web' | 'ws', passName: string, callback: WebPassthrough & WsPassthrough): void; /** * Used for proxying regular HTTP(S) requests * @param req - Client request. * @param res - Client response. * @param args - Additional arguments for the web proxy. */ web(req: IncomingMessage, res: ServerResponse, ...args: any[]): void; /** * Used for proxying regular HTTP(S) requests * @param req - Client request. * @param socket - Client socket. * @param args - Additional arguments for the websocket proxy */ ws(req: IncomingMessage, socket: Socket | TLSSocket, ...args: any[]): void; } declare interface ProxyServer { on(event: string, listener: () => void): this; on(event: "error", listener: Server.ErrorCallback): this; on(event: "start", listener: Server.StartCallback): this; on(event: "proxyReq", listener: Server.ProxyReqCallback): this; on(event: "proxyRes", listener: Server.ProxyResCallback): this; on(event: "proxyReqWs", listener: Server.ProxyReqWsCallback): this; on(event: "econnreset", listener: Server.EconnresetCallback): this; on(event: "end", listener: Server.EndCallback): this; on(event: "open", listener: Server.OpenCallback): this; on(event: "close", listener: Server.CloseCallback): this; once(event: string, listener: () => void): this; once(event: "error", listener: Server.ErrorCallback): this; once(event: "start", listener: Server.StartCallback): this; once(event: "proxyReq", listener: Server.ProxyReqCallback): this; once(event: "proxyRes", listener: Server.ProxyResCallback): this; once(event: "proxyReqWs", listener: Server.ProxyReqWsCallback): this; once(event: "econnreset", listener: Server.EconnresetCallback): this; once(event: "end", listener: Server.EndCallback): this; once(event: "open", listener: Server.OpenCallback): this; once(event: "close", listener: Server.CloseCallback): this; addListener(event: string, listener: () => void): this; removeListener(event: string, listener: () => void): this; removeAllListeners(event?: string): this; getMaxListeners(): number; setMaxListeners(n: number): this; listeners(event: string): Array<() => void>; emit(event: string, ...args: any[]): boolean; listenerCount(type: string): number; } declare const createProxyServer: (options?: Server.ServerOptions) => ProxyServer; declare const createServer: (options?: Server.ServerOptions) => ProxyServer; declare const createProxy: (options?: Server.ServerOptions) => ProxyServer; interface ProxyTargetDetailed { host: string; port: number; href?: string; method?: string; protocol?: string; hostname?: string; socketPath?: string; key?: string; passphrase?: string; pfx?: buffer.Buffer | string; cert?: string | buffer.Buffer | Array; ca?: string | buffer.Buffer | Array; ciphers?: string; secureProtocol?: string; servername?: string; searchParams?: url.URLSearchParams; pathname?: string; path?: string; } interface OutgoingOptions extends ProxyTargetDetailed, Server.ServerOptions { localAddress?: string; headers?: Server.ServerOptions['headers']; agent?: Server.ServerOptions['agent']; auth?: Server.ServerOptions['auth']; rejectUnauthorized?: boolean; } interface WebPassthrough { (req: http.IncomingMessage, res: http.ServerResponse): boolean | void; (req: http.IncomingMessage, res: http.ServerResponse, options: Server.ServerOptions): boolean | void; (req: http.IncomingMessage, res: http.ServerResponse, options: Server.ServerOptions, server?: ProxyServer, callback?: Server.ErrorCallback): boolean | void; } interface WsPassthrough { (req: http.IncomingMessage, socket: net.Socket | tls.TLSSocket): boolean | void; (req: http.IncomingMessage, socket: net.Socket | tls.TLSSocket, head?: buffer.Buffer): boolean | void; (req: http.IncomingMessage, socket: net.Socket | tls.TLSSocket, options: Server.ServerOptions, head?: buffer.Buffer, server?: ProxyServer, callback?: Server.ErrorCallback): boolean | void; } declare namespace Server { type ProxyTarget = ProxyTargetUrl | url.URL & ProxyTargetDetailed; type ProxyTargetUrl = Partial; interface ServerOptions { /** URL string to be parsed with the url module. */ target?: string | ProxyTarget; /** URL string to be parsed with the url module. */ forward?: string | ProxyTarget; /** Object to be passed to http(s).request. */ agent?: http.Agent | boolean; /** Object to be passed to https.createServer(). */ ssl?: https.ServerOptions; /** If you want to proxy websockets. */ ws?: boolean; /** Adds x- forward headers. */ xfwd?: boolean; /** Verify SSL certificate. */ secure?: boolean; /** Explicitly specify if we are proxying to another proxy. */ toProxy?: boolean; /** Specify whether you want to prepend the target's path to the proxy path. */ prependPath?: boolean; /** Specify whether you want to ignore the proxy path of the incoming request. */ ignorePath?: boolean; /** Local interface string to bind for outgoing connections. */ localAddress?: string; /** * @deprecated use `changeHost` instead * * Changes the origin of the host header to the target URL. */ changeOrigin?: boolean; /** Changes the origin of the host header to the target URL. */ changeHost?: boolean; /** specify whether you want to keep letter case of response header key */ preserveHeaderKeyCase?: boolean; /** Basic authentication i.e. 'user:password' to compute an Authorization header. */ auth?: string; /** Rewrites the location hostname on (301 / 302 / 307 / 308) redirects, Default: null. */ hostRewrite?: string; /** Rewrites the location host/ port on (301 / 302 / 307 / 308) redirects based on requested host/ port.Default: false. */ autoRewrite?: boolean; /** Rewrites the location protocol on (301 / 302 / 307 / 308) redirects to 'http' or 'https'.Default: null. */ protocolRewrite?: string; /** rewrites domain of set-cookie headers. */ cookieDomainRewrite?: false | string | Record; /** rewrites path of set-cookie headers. Default: false */ cookiePathRewrite?: false | string | Record; /** specify if you want to remove the secure flag from the cookie */ cookieRemoveSecure?: boolean; /** allows to merge `set-cookie` headers from passed response and response from target. Default: false. */ mergeCookies?: boolean; /** object with extra headers to be added to target requests. */ headers?: Record | http.IncomingHttpHeaders; /** object with extra headers to be added to proxy requests. */ outgoingHeaders?: Record | http.IncomingHttpHeaders; /** Timeout (in milliseconds) when proxy receives no response from target. Default: 120000 (2 minutes) */ proxyTimeout?: number; /** specify whether you want to throw a custom `ETIMEDOUT` error when the `proxyTimeout` is reached. If false then the default `ECONNRESET` error will be thrown. Default: false */ proxyTimeoutCustomError?: boolean; /** Timeout (in milliseconds) for incoming requests */ timeout?: number; /** if set to true the web passes will be run even if `selfHandleResponse` is also set to true. */ forcePasses?: boolean; /** If set to true, none of the webOutgoing passes are called and it's your responsibility to appropriately return the response by listening and acting on the proxyRes event */ selfHandleResponse?: boolean | Function; /** if set, this function will be called with three arguments `req`, `proxyReq` and `proxyRes` and should return a Duplex stream, data from the client websocket will be piped through this stream before being piped to the server, allowing you to influence the request data. */ createWsClientTransformStream?: (req: http.IncomingMessage, proxyReq: http.ClientRequest, proxyRes: http.IncomingMessage) => net.Socket | tls.TLSSocket; /** if set, this function will be called with three arguments `req`, `proxyReq` and `proxyRes` and should return a Duplex stream, data from the server websocket will be piped through this stream before being piped to the client, allowing you to influence the response data. */ createWsServerTransformStream?: (req: http.IncomingMessage, proxyReq: http.ClientRequest, proxyRes: http.IncomingMessage) => net.Socket | tls.TLSSocket; /** Buffer */ buffer?: stream.Stream; /** Custom lookup function to pass to http(s).request */ lookup?: net.LookupFunction | undefined; } type StartCallback = (req: TIncomingMessage, res: TServerResponse, target: ProxyTargetUrl) => void; type ProxyReqCallback = (proxyReq: TClientRequest, req: TIncomingMessage, res: TServerResponse, options: ServerOptions) => void; type ProxyResCallback = (proxyRes: TIncomingMessage, req: TIncomingMessage, res: TServerResponse) => void; type ProxyReqWsCallback = (proxyReq: TClientRequest, req: TIncomingMessage, socket: net.Socket | tls.TLSSocket, options: ServerOptions, head: buffer.Buffer) => void; type EconnresetCallback = (err: TError, req: TIncomingMessage, res: TServerResponse, target: ProxyTargetUrl) => void; type EndCallback = (req: TIncomingMessage, res: TServerResponse, proxyRes: TIncomingMessage) => void; type OpenCallback = (proxySocket: net.Socket | tls.TLSSocket) => void; type CloseCallback = (proxyRes: TIncomingMessage, proxySocket: net.Socket | tls.TLSSocket, proxyHead: buffer.Buffer) => void; type ErrorCallback = (err: TError, req: TIncomingMessage, res: TServerResponse | net.Socket | tls.TLSSocket, target?: Server.ServerOptions['target']) => void; } export { type OutgoingOptions, ProxyServer, type ProxyTargetDetailed, Server, type WebPassthrough, type WsPassthrough, createProxy, createProxyServer, createServer };