forked from joeferner/node-http-mitm-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.d.ts
233 lines (174 loc) · 10.1 KB
/
index.d.ts
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
//definitions by jason swearingen. jasons aat novaleaf doot coom. for node-htt-mitm-proxy v0.5.2.
import http = require("http");
import https = require("https");
import net = require("net");
declare namespace HttpMitmProxy {
export interface IProxyStatic {
(): IProxy;
/** mod to pass to the use() function: Gunzip response filter (uncompress gzipped content before onResponseData and compress back after)*/
gunzip: any;
/** mod to pass to the use() function: Generates wilcard certificates by default (so less certificates are generated)*/
wildcard: any;
}
export interface IProxyOptions {
/**port - The port or named socket to listen on (default: 8080).*/
port?: number;
/**host - The hostname or local address to listen on.*/
host?: string;
/** - Path to the certificates cache directory (default: process.cwd() + '/.http-mitm-proxy')*/
sslCaDir?: string;
/** - enable HTTP persistent connection*/
keepAlive?: boolean;
/** - The number of milliseconds of inactivity before a socket is presumed to have timed out. Defaults to no timeout. */
timeout?: number;
/** - The http.Agent to use when making http requests. Useful for chaining proxys. (default: internal Agent) */
httpAgent?: http.Agent;
/** - The https.Agent to use when making https requests. Useful for chaining proxys. (default: internal Agent) */
httpsAgent?: https.Agent;
/** - force use of SNI by the client. Allow node-http-mitm-proxy to handle all HTTPS requests with a single internal server. */
forceSNI?: boolean;
/** - The port or named socket for https server to listen on. (forceSNI must be enabled) */
httpsPort?: number;
/** - Setting this option will remove the content-length from the proxy to server request, forcing chunked encoding */
forceChunkedRequest?: boolean;
}
export type IProxy = ICallbacks & {
/** Starts the proxy listening on the given port.. example: proxy.listen({ port: 80 }); */
listen(/** An object with the following options: */ options?: IProxyOptions, callback?: Function): void;
/** proxy.close
Stops the proxy listening.
Example
proxy.close(); */
close(): void;
onCertificateRequired(hostname: string, callback: (error: Error | undefined, certDetails: { keyFile: string; certFile: string; hosts: string[]; }) => void): void;
onCertificateMissing(ctx: IContext, files: any, callback: (error: Error | undefined, certDetails: { keyFileData: string; certFileData: string; hosts: string[]; }) => void): void;
//undocumented helpers
onConnect(fcn: (req: http.IncomingMessage, socket: net.Socket, head: any, callback: (error?: Error) => void) => void): void;
onRequestHeaders(fcn: (ctx: IContext, callback: (error?: Error) => void) => void): void;
onResponseHeaders(fcn: (ctx: IContext, callback: (error?: Error) => void) => void): void;
onWebSocketConnection(fcn: (ctx: IContext, callback: (error?: Error) => void) => void): void;
onWebSocketSend(fcn: (ctx: IContext, message: any, flags: any, callback: (err: Error | undefined, message: any, flags: any) => void) => void): void;
onWebSocketMessage(fcn: (ctx: IContext, message: any, flags: any, callback: (err: Error | undefined, message: any, flags: any) => void) => void): void;
onWebSocketFrame(fcn: (ctx: IContext, type: any, fromServer: boolean, message: any, flags: any, callback: (err: Error | undefined, message: any, flags: any) => void) => void): void;
onWebSocketError(fcn: (ctx: IContext, err: Error | undefined) => void): void;
onWebSocketClose(fcn: (ctx: IContext, code: any, message: any, callback: (err: Error | undefined, code: any, message: any) => void) => void): void;
// onConnectHandlers:((req,socket,head,callback)=>void)[];
// onRequestHandlers:((ctx,callback)=>void)[];
options: IProxyOptions;
httpPort: number;
timeout: number;
keepAlive: boolean;
httpAgent: http.Agent;
httpsAgent: https.Agent;
forceSNI: boolean;
httpsPort?: number;
sslCaDir: string;
}
/** signatures for various callback functions */
export interface ICallbacks {
onError(/**Adds a function to the list of functions to get called if an error occures.
Arguments
fn(ctx, err, errorKind) - The function to be called on an error.*/callback: (context: IContext, err?: Error, errorKind?: string) => void): void;
/** Adds a function to get called at the beginning of a request.
Arguments
fn(ctx, callback) - The function that gets called on each request.
Example
proxy.onRequest(function(ctx, callback) {
console.log('REQUEST:', ctx.clientToProxyRequest.url);
return callback();
}); */
onRequest(fcn: (ctx: IContext, callback: (error?: Error) => void) => void): void;
onRequestData(fcn: (ctx: IContext, chunk: Buffer, callback: (error?: Error, chunk?: Buffer) => void) => void): void;
onRequestEnd(fcn: (ctx: IContext, callback: (error?: Error) => void) => void): void;
/** Adds a function to get called at the beginning of the response.
Arguments
fn(ctx, callback) - The function that gets called on each response.
Example
proxy.onResponse(function(ctx, callback) {
console.log('BEGIN RESPONSE');
return callback();
}); */
onResponse(fcn: (ctx: IContext, callback: (error?: Error) => void) => void): void;
onResponseData(fcn: (ctx: IContext, chunk: Buffer, callback: (error?: Error, chunk?: Buffer) => void) => void): void;
onResponseEnd(fcn: (ctx: IContext, callback: (error?: Error) => void) => void): void;
/** Adds a module into the proxy. Modules encapsulate multiple life cycle processing functions into one object.
Arguments
module - The module to add. Modules contain a hash of functions to add.
Example
proxy.use({
onError: function(ctx, err) { },
onCertificateRequired: function(hostname, callback) { return callback(); },
onCertificateMissing: function(ctx, files, callback) { return callback(); },
onRequest: function(ctx, callback) { return callback(); },
onRequestData: function(ctx, chunk, callback) { return callback(null, chunk); },
onResponse: function(ctx, callback) { return callback(); },
onResponseData: function(ctx, chunk, callback) { return callback(null, chunk); },
onWebSocketConnection: function(ctx, callback) { return callback(); },
onWebSocketSend: function(ctx, message, flags, callback) { return callback(null, message, flags); },
onWebSocketMessage: function(ctx, message, flags, callback) { return callback(null, message, flags); },
onWebSocketError: function(ctx, err) { },
onWebSocketClose: function(ctx, code, message, callback) { },
});
node-http-mitm-proxy provide some ready to use modules:
Proxy.gunzip Gunzip response filter (uncompress gzipped content before onResponseData and compress back after)
Proxy.wildcard Generates wilcard certificates by default (so less certificates are generated) */
use(mod: any): void;
}
export type IContext = ICallbacks & {
isSSL: boolean;
/** may be set to true/false when dealing with websockets. */
closedByServer?: boolean;
clientToProxyRequest: http.IncomingMessage;
proxyToClientResponse: http.ServerResponse;
proxyToServerRequest: http.ClientRequest;
serverToProxyResponse: http.IncomingMessage;
/** instance of WebSocket object from https://github.com/websockets/ws */
clientToProxyWebSocket: any;
/** instance of WebSocket object from https://github.com/websockets/ws */
proxyToServerWebSocket: any;
/** user defined tags, initially constructed in the proxy-internals.tx proxy.onRequest() callback, you can add what you like here. */
tags: {
id: number;
uri:string;
/** ln 743 of proxy.js, hack to retry */
failedUpstreamCalls:number;
/** ln 743 of proxy.js, hack to retry */
retryProxyRequest:boolean;
[key: string]: any;
}
/**Adds a stream into the request body stream.
Arguments
stream - The read/write stream to add in the request body stream.
Example
ctx.addRequestFilter(zlib.createGunzip()); */
addRequestFilter(stream: any): void;
/** Adds a stream into the response body stream.
Arguments
stream - The read/write stream to add in the response body stream.
Example
ctx.addResponseFilter(zlib.createGunzip()); */
addResponseFilter(stream: any): void;
/** filters added by .addRequestFilter() */
requestFilters: any[];
/** filters added by .addResponseFilter() */
responseFilters: any[];
/** undocumented, allows adjusting the request in callbacks (such as .onRequest()) before sending upstream (to proxy or target host)..
* FYI these values seem pre-populated with defaults based on the request, you can modify them to change behavior. */
proxyToServerRequestOptions: {
/** ex: "GET" */
method: string;
/** ex: "/success.txt" */
path: string;
/** example: "detectportal.firefox.com" */
host: string;
port: null;
headers: { [key: string]: string };
agent: http.Agent;
};
onResponseDataHandlers:Function[];
onResponseEndHandlers:Function[];
}
}
declare const HttpMitmProxy: HttpMitmProxy.IProxyStatic
export = HttpMitmProxy;
export as namespace HttpMitmProxy;