|
| 1 | +import http = require("http"); |
| 2 | +import https = require("https"); |
| 3 | +import type CA from "../lib/ca"; |
| 4 | +import type WebSocket from "ws"; |
| 5 | +import type { Server } from "https"; |
| 6 | +import type { WebSocket as WebSocketType, WebSocketServer } from "ws"; |
| 7 | + |
| 8 | +export interface IProxyStatic { |
| 9 | + (): IProxy; |
| 10 | + /** mod to pass to the use() function: Gunzip response filter (uncompress gzipped content before onResponseData and compress back after)*/ |
| 11 | + gunzip: any; |
| 12 | + /** mod to pass to the use() function: Generates wilcard certificates by default (so less certificates are generated)*/ |
| 13 | + wildcard: any; |
| 14 | +} |
| 15 | + |
| 16 | +export interface IProxyOptions { |
| 17 | + /**port - The port or named socket to listen on (default: 8080).*/ |
| 18 | + port?: number; |
| 19 | + /**host - The hostname or local address to listen on.*/ |
| 20 | + host?: string; |
| 21 | + /** - Path to the certificates cache directory (default: process.cwd() + '/.http-mitm-proxy')*/ |
| 22 | + sslCaDir?: string; |
| 23 | + /** - enable HTTP persistent connection*/ |
| 24 | + keepAlive?: boolean; |
| 25 | + /** - The number of milliseconds of inactivity before a socket is presumed to have timed out. Defaults to no timeout. */ |
| 26 | + timeout?: number; |
| 27 | + /** - The http.Agent to use when making http requests. Useful for chaining proxys. (default: internal Agent) */ |
| 28 | + httpAgent?: http.Agent; |
| 29 | + /** - The https.Agent to use when making https requests. Useful for chaining proxys. (default: internal Agent) */ |
| 30 | + httpsAgent?: https.Agent; |
| 31 | + /** - force use of SNI by the client. Allow node-http-mitm-proxy to handle all HTTPS requests with a single internal server. */ |
| 32 | + forceSNI?: boolean; |
| 33 | + /** - The port or named socket for https server to listen on. (forceSNI must be enabled) */ |
| 34 | + httpsPort?: number; |
| 35 | + /** - Setting this option will remove the content-length from the proxy to server request, forcing chunked encoding */ |
| 36 | + forceChunkedRequest?: boolean; |
| 37 | +} |
| 38 | + |
| 39 | +export interface IProxySSLServer { |
| 40 | + port: number; |
| 41 | + server?: Server; |
| 42 | + wsServer?: WebSocketServer; |
| 43 | +} |
| 44 | +export type ICreateServerCallback = ( |
| 45 | + port: number, |
| 46 | + server: Server, |
| 47 | + wssServer: WebSocketServer |
| 48 | +) => void; |
| 49 | +export type ErrorCallback = (error?: Error | null, data?: any) => void; |
| 50 | +export type OnRequestParams = (ctx: IContext, callback: ErrorCallback) => void; |
| 51 | +export type OnWebsocketRequestParams = ( |
| 52 | + ctx: IWebSocketContext, |
| 53 | + callback: ErrorCallback |
| 54 | +) => void; |
| 55 | +export type IWebSocketCallback = ( |
| 56 | + err: MaybeError, |
| 57 | + message?: any, |
| 58 | + flags?: any |
| 59 | +) => void; |
| 60 | +export type OnWebSocketSendParams = ( |
| 61 | + ctx: IWebSocketContext, |
| 62 | + message: any, |
| 63 | + flags: any, |
| 64 | + callback: IWebSocketCallback |
| 65 | +) => void; |
| 66 | +export type OnWebSocketMessageParams = ( |
| 67 | + ctx: IWebSocketContext, |
| 68 | + message: any, |
| 69 | + flags: any, |
| 70 | + callback: IWebSocketCallback |
| 71 | +) => void; |
| 72 | +export type OnWebSocketFrameParams = ( |
| 73 | + ctx: IWebSocketContext, |
| 74 | + type: any, |
| 75 | + fromServer: boolean, |
| 76 | + message: any, |
| 77 | + flags: any, |
| 78 | + callback: IWebSocketCallback |
| 79 | +) => void; |
| 80 | +export type OnWebSocketErrorParams = ( |
| 81 | + ctx: IWebSocketContext, |
| 82 | + err: MaybeError |
| 83 | +) => void; |
| 84 | +export type OnWebSocketCloseParams = ( |
| 85 | + ctx: IWebSocketContext, |
| 86 | + code: any, |
| 87 | + message: any, |
| 88 | + callback: IWebSocketCallback |
| 89 | +) => void; |
| 90 | + |
| 91 | +export interface ICertDetails { |
| 92 | + keyFile: string; |
| 93 | + certFile: string; |
| 94 | + hosts: string[]; |
| 95 | +} |
| 96 | + |
| 97 | +export type MaybeError = Error | null | undefined; |
| 98 | +export type OnCertificateMissingCallback = ( |
| 99 | + error: MaybeError, |
| 100 | + certDetails: ICertDetails |
| 101 | +) => void; |
| 102 | +export type OnCertificateRequiredCallback = ( |
| 103 | + error: MaybeError, |
| 104 | + certDetails: ICertDetails |
| 105 | +) => void; |
| 106 | +export type OnRequestDataCallback = (error?: MaybeError, chunk?: Buffer) => void; |
| 107 | +export type OnRequestDataParams = ( |
| 108 | + ctx: IContext, |
| 109 | + chunk: Buffer, |
| 110 | + callback: OnRequestDataCallback |
| 111 | +) => void; |
| 112 | +export type OnErrorParams = ( |
| 113 | + context: IContext | null, |
| 114 | + err?: MaybeError, |
| 115 | + errorKind?: string |
| 116 | +) => void; |
| 117 | +export type OnConnectParams = ( |
| 118 | + req: http.IncomingMessage, |
| 119 | + // eslint-disable-next-line @typescript-eslint/consistent-type-imports |
| 120 | + socket: import("stream").Duplex, |
| 121 | + head: any, |
| 122 | + callback: ErrorCallback |
| 123 | +) => void; |
| 124 | +export type IProxy = ICallbacks & { |
| 125 | + /** Starts the proxy listening on the given port. example: proxy.listen({ port: 80 }); */ |
| 126 | + listen(options?: IProxyOptions, callback?: () => void): void; |
| 127 | + |
| 128 | + /** proxy.close |
| 129 | + Stops the proxy listening. |
| 130 | +
|
| 131 | + Example |
| 132 | +
|
| 133 | + proxy.close(); */ |
| 134 | + close(): void; |
| 135 | + |
| 136 | + onCertificateRequired( |
| 137 | + hostname: string, |
| 138 | + callback: OnCertificateRequiredCallback |
| 139 | + ): void; |
| 140 | + onCertificateMissing( |
| 141 | + ctx: ICertficateContext, |
| 142 | + files: any, |
| 143 | + callback: OnCertificateMissingCallback |
| 144 | + ): void; |
| 145 | + |
| 146 | + onConnect(fcn: OnConnectParams): void; |
| 147 | + onWebSocketConnection(fcn: OnWebsocketRequestParams): void; |
| 148 | + onWebSocketSend(fcn: OnWebSocketSendParams): void; |
| 149 | + onWebSocketMessage(fcn: OnWebSocketMessageParams): void; |
| 150 | + onWebSocketFrame(fcn: OnWebSocketFrameParams): void; |
| 151 | + onWebSocketError(fcn: OnWebSocketErrorParams): void; |
| 152 | + onWebSocketClose(fcn: OnWebSocketCloseParams): void; |
| 153 | + |
| 154 | + options: IProxyOptions; |
| 155 | + httpPort: number; |
| 156 | + timeout: number; |
| 157 | + keepAlive: boolean; |
| 158 | + httpAgent: http.Agent; |
| 159 | + httpsAgent: https.Agent; |
| 160 | + forceSNI: boolean; |
| 161 | + httpsPort?: number; |
| 162 | + sslCaDir: string; |
| 163 | + ca: CA; |
| 164 | +}; |
| 165 | + |
| 166 | +/** signatures for various callback functions */ |
| 167 | +export interface ICallbacks { |
| 168 | + /**Adds a function to the list of functions to get called if an error occures. |
| 169 | +
|
| 170 | + Arguments |
| 171 | +
|
| 172 | + fn(ctx, err, errorKind) - The function to be called on an error.*/ |
| 173 | + onError(callback: OnErrorParams): void; |
| 174 | + |
| 175 | + /** Adds a function to get called at the beginning of a request. |
| 176 | +
|
| 177 | + Arguments |
| 178 | +
|
| 179 | + fn(ctx, callback) - The function that gets called on each request. |
| 180 | + Example |
| 181 | +
|
| 182 | + proxy.onRequest(function(ctx, callback) { |
| 183 | + console.log('REQUEST:', ctx.clientToProxyRequest.url); |
| 184 | + return callback(); |
| 185 | + }); */ |
| 186 | + onRequest(fcn: OnRequestParams): void; |
| 187 | + |
| 188 | + onRequestHeaders(fcn: OnRequestParams): void; |
| 189 | + onResponseHeaders(fcn: OnRequestParams): void; |
| 190 | + |
| 191 | + onRequestData(fcn: OnRequestDataParams): void; |
| 192 | + |
| 193 | + onRequestEnd(fcn: OnRequestParams): void; |
| 194 | + /** Adds a function to get called at the beginning of the response. |
| 195 | +
|
| 196 | + Arguments |
| 197 | +
|
| 198 | + fn(ctx, callback) - The function that gets called on each response. |
| 199 | + Example |
| 200 | +
|
| 201 | + proxy.onResponse(function(ctx, callback) { |
| 202 | + console.log('BEGIN RESPONSE'); |
| 203 | + return callback(); |
| 204 | + }); */ |
| 205 | + onResponse(fcn: OnRequestParams): void; |
| 206 | + |
| 207 | + onResponseData(fcn: OnRequestDataParams): void; |
| 208 | + |
| 209 | + onResponseEnd(fcn: OnRequestParams): void; |
| 210 | + |
| 211 | + /** Adds a module into the proxy. Modules encapsulate multiple life cycle processing functions into one object. |
| 212 | +
|
| 213 | + Arguments |
| 214 | +
|
| 215 | + module - The module to add. Modules contain a hash of functions to add. |
| 216 | + Example |
| 217 | +
|
| 218 | + proxy.use({ |
| 219 | + onError: function(ctx, err) { }, |
| 220 | + onCertificateRequired: function(hostname, callback) { return callback(); }, |
| 221 | + onCertificateMissing: function(ctx, files, callback) { return callback(); }, |
| 222 | + onRequest: function(ctx, callback) { return callback(); }, |
| 223 | + onRequestData: function(ctx, chunk, callback) { return callback(null, chunk); }, |
| 224 | + onResponse: function(ctx, callback) { return callback(); }, |
| 225 | + onResponseData: function(ctx, chunk, callback) { return callback(null, chunk); }, |
| 226 | + onWebSocketConnection: function(ctx, callback) { return callback(); }, |
| 227 | + onWebSocketSend: function(ctx, message, flags, callback) { return callback(null, message, flags); }, |
| 228 | + onWebSocketMessage: function(ctx, message, flags, callback) { return callback(null, message, flags); }, |
| 229 | + onWebSocketError: function(ctx, err) { }, |
| 230 | + onWebSocketClose: function(ctx, code, message, callback) { }, |
| 231 | + }); |
| 232 | + node-http-mitm-proxy provide some ready to use modules: |
| 233 | +
|
| 234 | + Proxy.gunzip Gunzip response filter (uncompress gzipped content before onResponseData and compress back after) |
| 235 | + Proxy.wildcard Generates wilcard certificates by default (so less certificates are generated) */ |
| 236 | + use(mod: any): void; |
| 237 | +} |
| 238 | + |
| 239 | +export interface IBaseContext { |
| 240 | + isSSL: boolean; |
| 241 | + uuid: string; |
| 242 | + |
| 243 | + /** may be set to true/false when dealing with websockets. */ |
| 244 | + closedByServer?: boolean; |
| 245 | + closedByClient?: boolean; |
| 246 | + |
| 247 | + connectRequest: http.IncomingMessage; |
| 248 | + /** user defined tags, initially constructed in the proxy-internals.tx proxy.onRequest() callback, you can add what you like here. */ |
| 249 | + tags?: { |
| 250 | + id: number; |
| 251 | + uri: string; |
| 252 | + /** ln 743 of proxy.js, hack to retry */ |
| 253 | + failedUpstreamCalls: number; |
| 254 | + /** ln 743 of proxy.js, hack to retry */ |
| 255 | + retryProxyRequest: boolean; |
| 256 | + [key: string]: any; |
| 257 | + }; |
| 258 | + |
| 259 | + use(mod: any): void; |
| 260 | +} |
| 261 | + |
| 262 | +export type IContext = ICallbacks & |
| 263 | + IBaseContext & { |
| 264 | + clientToProxyRequest: http.IncomingMessage; |
| 265 | + proxyToClientResponse: http.ServerResponse; |
| 266 | + proxyToServerRequest: http.ClientRequest | undefined; |
| 267 | + serverToProxyResponse: http.IncomingMessage | undefined; |
| 268 | + |
| 269 | + /**Adds a stream into the request body stream. |
| 270 | +
|
| 271 | + Arguments |
| 272 | +
|
| 273 | + stream - The read/write stream to add in the request body stream. |
| 274 | + Example |
| 275 | +
|
| 276 | + ctx.addRequestFilter(zlib.createGunzip()); */ |
| 277 | + addRequestFilter(stream: any): void; |
| 278 | + /** Adds a stream into the response body stream. |
| 279 | +
|
| 280 | + Arguments |
| 281 | +
|
| 282 | + stream - The read/write stream to add in the response body stream. |
| 283 | + Example |
| 284 | +
|
| 285 | + ctx.addResponseFilter(zlib.createGunzip()); */ |
| 286 | + addResponseFilter(stream: any): void; |
| 287 | + |
| 288 | + /** filters added by .addRequestFilter() */ |
| 289 | + requestFilters: any[]; |
| 290 | + |
| 291 | + /** filters added by .addResponseFilter() */ |
| 292 | + responseFilters: any[]; |
| 293 | + |
| 294 | + /** undocumented, allows adjusting the request in callbacks (such as .onRequest()) before sending upstream (to proxy or target host).. |
| 295 | + * FYI these values seem pre-populated with defaults based on the request, you can modify them to change behavior. */ |
| 296 | + proxyToServerRequestOptions: |
| 297 | + | undefined |
| 298 | + | { |
| 299 | + /** ex: "GET" */ |
| 300 | + method: string; |
| 301 | + /** ex: "/success.txt" */ |
| 302 | + path: string; |
| 303 | + |
| 304 | + /** example: "detectportal.firefox.com" */ |
| 305 | + host: string; |
| 306 | + port: string | number | null | undefined; |
| 307 | + headers: { [key: string]: string }; |
| 308 | + agent: http.Agent; |
| 309 | + }; |
| 310 | + |
| 311 | + onRequestHandlers: OnRequestParams[]; |
| 312 | + onResponseHandlers: OnRequestParams[]; |
| 313 | + onErrorHandlers: OnErrorParams[]; |
| 314 | + onRequestDataHandlers: OnRequestDataParams[]; |
| 315 | + onResponseDataHandlers: OnRequestDataParams[]; |
| 316 | + onRequestEndHandlers: OnRequestParams[]; |
| 317 | + onResponseEndHandlers: OnRequestParams[]; |
| 318 | + onRequestHeadersHandlers: OnRequestParams[]; |
| 319 | + onResponseHeadersHandlers: OnRequestParams[]; |
| 320 | + responseContentPotentiallyModified: boolean; |
| 321 | + }; |
| 322 | + |
| 323 | +export interface ICertficateContext { |
| 324 | + hostname: string; |
| 325 | + files: ICertDetails; |
| 326 | + data: { keyFileExists: boolean; certFileExists: boolean }; |
| 327 | +} |
| 328 | + |
| 329 | +export type IWebSocketContext = IBaseContext & { |
| 330 | + /** instance of WebSocket object from https://github.com/websockets/ws */ |
| 331 | + clientToProxyWebSocket?: WebSocketType; |
| 332 | + /** instance of WebSocket object from https://github.com/websockets/ws */ |
| 333 | + proxyToServerWebSocket?: WebSocketType; |
| 334 | + |
| 335 | + proxyToServerWebSocketOptions?: WebSocket.ClientOptions & { url?: string }; |
| 336 | + /** WebSocket Connection Hanlders */ |
| 337 | + onWebSocketConnectionHandlers: OnWebsocketRequestParams[]; |
| 338 | + onWebSocketFrameHandlers: OnWebSocketFrameParams[]; |
| 339 | + onWebSocketCloseHandlers: OnWebSocketCloseParams[]; |
| 340 | + onWebSocketErrorHandlers: OnWebSocketErrorParams[]; |
| 341 | + |
| 342 | + onWebSocketConnection: (ws: OnWebsocketRequestParams) => void; |
| 343 | + onWebSocketSend: (ws: OnWebSocketSendParams) => void; |
| 344 | + onWebSocketMessage: (ws: OnWebSocketMessageParams) => void; |
| 345 | + onWebSocketFrame: (ws: OnWebSocketFrameParams) => void; |
| 346 | + onWebSocketClose: (ws: OnWebSocketCloseParams) => void; |
| 347 | + onWebSocketError: (ws: OnWebSocketErrorParams) => void; |
| 348 | +}; |
0 commit comments