forked from sagemathinc/http-proxy-3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
573 lines (533 loc) · 18 KB
/
Copy pathindex.ts
File metadata and controls
573 lines (533 loc) · 18 KB
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
import * as http from "node:http";
import * as http2 from "node:http2";
import * as net from "node:net";
import { WEB_PASSES } from "./passes/web-incoming";
import { WS_PASSES } from "./passes/ws-incoming";
import { EventEmitter } from "node:events";
import type { Stream } from "node:stream";
import debug from "debug";
import { toURL } from "./common";
const log = debug("http-proxy-3");
export interface ProxyTargetDetailed {
host: string;
port: number;
protocol?: string;
hostname?: string;
socketPath?: string;
key?: string;
passphrase?: string;
pfx?: Buffer | string;
cert?: string;
ca?: string;
ciphers?: string;
secureProtocol?: string;
}
export type ProxyType = "ws" | "web";
export type ProxyTarget = ProxyTargetUrl | ProxyTargetDetailed;
export type ProxyTargetUrl =
| URL
| string
| { port: number; host: string; protocol?: string };
export type NormalizeProxyTarget<T extends ProxyTargetUrl> =
| Exclude<T, string>
| URL;
export interface ServerOptions {
// NOTE: `options.target and `options.forward` cannot be both missing when the
// actually proxying is called. However, they can be missing when creating the
// proxy server in the first place! E.g., you could make a proxy server P with
// no options, then use P.web(req,res, {target:...}).
/** URL string to be parsed with the url module. */
target?: ProxyTarget;
/** URL string to be parsed with the url module or a URL object. */
forward?: ProxyTargetUrl;
/** Object to be passed to http(s).request. */
agent?: any;
/** Object to be passed to https.createServer(). */
ssl?: any;
/** 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;
/** Changes the origin of the host header to the target URL. */
changeOrigin?: 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 | { [oldDomain: string]: string };
/** rewrites path of set-cookie headers. Default: false */
cookiePathRewrite?: false | string | { [oldPath: string]: string };
/** object with extra headers to be added to target requests. */
headers?: { [header: string]: string | string[] | undefined };
/** Timeout (in milliseconds) when proxy receives no response from target. Default: 120000 (2 minutes) */
proxyTimeout?: number;
/** Timeout (in milliseconds) for incoming requests */
timeout?: number;
/** Specify whether you want to follow redirects. Default: false */
followRedirects?: 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;
/** Buffer */
buffer?: Stream;
/** Explicitly set the method type of the ProxyReq */
method?: string;
/**
* Optionally override the trusted CA certificates.
* This is passed to https.request.
*/
ca?: string;
/** Optional fetch implementation to use instead of global fetch, use this to activate fetch-based proxying,
* for example to proxy HTTP/2 requests
*/
fetch?: typeof fetch;
/** Optional configuration object for fetch-based proxy requests.
* Use this to customize fetch request and response handling.
* For custom fetch implementations, use the `fetch` property.*/
fetchOptions?: FetchOptions;
}
export interface FetchOptions {
/** Fetch request options */
requestOptions?: RequestInit;
/** Called before making the fetch request */
onBeforeRequest?: (
requestOptions: RequestInit,
req: http.IncomingMessage,
res: http.ServerResponse,
options: NormalizedServerOptions,
) => void | Promise<void>;
/** Called after receiving the fetch response */
onAfterResponse?: (
response: Response,
req: http.IncomingMessage,
res: http.ServerResponse,
options: NormalizedServerOptions,
) => void | Promise<void>;
}
export interface NormalizedServerOptions extends ServerOptions {
target?: NormalizeProxyTarget<ProxyTarget>;
forward?: NormalizeProxyTarget<ProxyTargetUrl>;
}
export type ErrorCallback<
TIncomingMessage extends
typeof http.IncomingMessage = typeof http.IncomingMessage,
TServerResponse extends
typeof http.ServerResponse = typeof http.ServerResponse,
TError = Error,
> = (
err: TError,
req: InstanceType<TIncomingMessage>,
res: InstanceType<TServerResponse> | net.Socket,
target?: ProxyTargetUrl,
) => void;
type ProxyServerEventMap<
TIncomingMessage extends
typeof http.IncomingMessage = typeof http.IncomingMessage,
TServerResponse extends
typeof http.ServerResponse = typeof http.ServerResponse,
TError = Error,
> = {
error: Parameters<ErrorCallback<TIncomingMessage, TServerResponse, TError>>;
start: [
req: InstanceType<TIncomingMessage>,
res: InstanceType<TServerResponse>,
target: ProxyTargetUrl,
];
open: [socket: net.Socket];
proxyReq: [
proxyReq: http.ClientRequest,
req: InstanceType<TIncomingMessage>,
res: InstanceType<TServerResponse>,
options: ServerOptions,
socket: net.Socket,
];
proxyRes: [
proxyRes: InstanceType<TIncomingMessage>,
req: InstanceType<TIncomingMessage>,
res: InstanceType<TServerResponse>,
];
proxyReqWs: [
proxyReq: http.ClientRequest,
req: InstanceType<TIncomingMessage>,
socket: net.Socket,
options: ServerOptions,
head: any,
];
econnreset: [
err: Error,
req: InstanceType<TIncomingMessage>,
res: InstanceType<TServerResponse>,
target: ProxyTargetUrl,
];
end: [
req: InstanceType<TIncomingMessage>,
res: InstanceType<TServerResponse>,
proxyRes: InstanceType<TIncomingMessage>,
];
close: [
proxyRes: InstanceType<TIncomingMessage>,
proxySocket: net.Socket,
proxyHead: any,
];
};
type ProxyMethodArgs<
TIncomingMessage extends
typeof http.IncomingMessage = typeof http.IncomingMessage,
TServerResponse extends
typeof http.ServerResponse = typeof http.ServerResponse,
TError = Error,
> = {
ws: [
req: InstanceType<TIncomingMessage>,
socket: any,
head: any,
...args:
| [
options?: ServerOptions,
callback?: ErrorCallback<TIncomingMessage, TServerResponse, TError>,
]
| [callback?: ErrorCallback<TIncomingMessage, TServerResponse, TError>],
];
web: [
req: InstanceType<TIncomingMessage>,
res: InstanceType<TServerResponse>,
...args:
| [
options: ServerOptions,
callback?: ErrorCallback<TIncomingMessage, TServerResponse, TError>,
]
| [callback?: ErrorCallback<TIncomingMessage, TServerResponse, TError>],
];
};
type PassFunctions<
TIncomingMessage extends
typeof http.IncomingMessage = typeof http.IncomingMessage,
TServerResponse extends
typeof http.ServerResponse = typeof http.ServerResponse,
TError = Error,
> = {
ws: (
req: InstanceType<TIncomingMessage>,
socket: net.Socket,
options: NormalizedServerOptions,
head: Buffer | undefined,
server: ProxyServer<TIncomingMessage, TServerResponse, TError>,
cb?: ErrorCallback<TIncomingMessage, TServerResponse, TError>,
) => unknown;
web: (
req: InstanceType<TIncomingMessage>,
res: InstanceType<TServerResponse>,
options: NormalizedServerOptions,
head: Buffer | undefined,
server: ProxyServer<TIncomingMessage, TServerResponse, TError>,
cb?: ErrorCallback<TIncomingMessage, TServerResponse, TError>,
) => unknown;
};
export class ProxyServer<
TIncomingMessage extends
typeof http.IncomingMessage = typeof http.IncomingMessage,
TServerResponse extends
typeof http.ServerResponse = typeof http.ServerResponse,
TError = Error,
> extends EventEmitter<
ProxyServerEventMap<TIncomingMessage, TServerResponse, TError>
> {
/**
* Used for proxying WS(S) requests
* @param req - Client request.
* @param socket - Client socket.
* @param head - Client head.
* @param options - Additional options.
*/
public readonly ws: (
...args: ProxyMethodArgs<TIncomingMessage, TServerResponse, TError>["ws"]
) => void;
/**
* Used for proxying regular HTTP(S) requests
* @param req - Client request.
* @param res - Client response.
* @param options - Additional options.
*/
public readonly web: (
...args: ProxyMethodArgs<TIncomingMessage, TServerResponse, TError>["web"]
) => void;
private options: ServerOptions;
private webPasses: Array<
PassFunctions<TIncomingMessage, TServerResponse, TError>["web"]
>;
private wsPasses: Array<
PassFunctions<TIncomingMessage, TServerResponse, TError>["ws"]
>;
private _server?:
| http.Server<TIncomingMessage, TServerResponse>
| http2.Http2SecureServer<TIncomingMessage, TServerResponse>
| null;
/**
* Creates the proxy server with specified options.
* @param options - Config object passed to the proxy
*/
constructor(options: ServerOptions = {}) {
super();
log("creating a ProxyServer", options);
options.prependPath = options.prependPath !== false;
this.options = options;
this.web = this.createRightProxy("web")(options);
this.ws = this.createRightProxy("ws")(options);
this.webPasses = Object.values(WEB_PASSES) as Array<
PassFunctions<TIncomingMessage, TServerResponse, TError>["web"]
>;
this.wsPasses = Object.values(WS_PASSES) as Array<
PassFunctions<TIncomingMessage, TServerResponse, TError>["ws"]
>;
this.on("error", this.onError);
}
/**
* Creates the proxy server with specified options.
* @param options Config object passed to the proxy
* @returns Proxy object with handlers for `ws` and `web` requests
*/
static createProxyServer<
TIncomingMessage extends typeof http.IncomingMessage,
TServerResponse extends typeof http.ServerResponse,
TError = Error,
>(
options?: ServerOptions,
): ProxyServer<TIncomingMessage, TServerResponse, TError> {
return new ProxyServer<TIncomingMessage, TServerResponse, TError>(options);
}
/**
* Creates the proxy server with specified options.
* @param options Config object passed to the proxy
* @returns Proxy object with handlers for `ws` and `web` requests
*/
static createServer<
TIncomingMessage extends typeof http.IncomingMessage,
TServerResponse extends typeof http.ServerResponse,
TError = Error,
>(
options?: ServerOptions,
): ProxyServer<TIncomingMessage, TServerResponse, TError> {
return new ProxyServer<TIncomingMessage, TServerResponse, TError>(options);
}
/**
* Creates the proxy server with specified options.
* @param options Config object passed to the proxy
* @returns Proxy object with handlers for `ws` and `web` requests
*/
static createProxy<
TIncomingMessage extends typeof http.IncomingMessage,
TServerResponse extends typeof http.ServerResponse,
TError = Error,
>(
options?: ServerOptions,
): ProxyServer<TIncomingMessage, TServerResponse, TError> {
return new ProxyServer<TIncomingMessage, TServerResponse, TError>(options);
}
// createRightProxy - Returns a function that when called creates the loader for
// either `ws` or `web`'s passes.
createRightProxy = <PT extends ProxyType>(type: PT): Function => {
log("createRightProxy", { type });
return (options: ServerOptions) => {
return (
...args: ProxyMethodArgs<
TIncomingMessage,
TServerResponse,
TError
>[PT] /* req, res, [head], [opts] */
) => {
const req = args[0];
log("proxy: ", { type, path: (req as http.IncomingMessage).url });
const res = args[1];
const passes = type === "ws" ? this.wsPasses : this.webPasses;
if (type == "ws") {
// socket -- proxy websocket errors to our error handler;
// see https://github.com/sagemathinc/http-proxy-3/issues/5
// NOTE: as mentioned below, res is the socket in this case.
// One of the passes does add an error handler, but there's no
// guarantee we even get to that pass before something bad happens,
// and there's no way for a user of http-proxy-3 to get ahold
// of this res object and attach their own error handler until
// after the passes. So we better attach one ASAP right here:
(res as net.Socket).on("error", (err: TError) => {
this.emit("error", err, req, res);
});
}
let counter = args.length - 1;
let head: Buffer | undefined;
let cb:
| ErrorCallback<TIncomingMessage, TServerResponse, TError>
| undefined;
// optional args parse begin
if (typeof args[counter] === "function") {
cb = args[counter];
counter--;
}
let requestOptions: ServerOptions;
if (!(args[counter] instanceof Buffer) && args[counter] !== res) {
// Copy global options, and overwrite with request options
requestOptions = { ...options, ...args[counter] };
counter--;
} else {
requestOptions = { ...options };
}
if (args[counter] instanceof Buffer) {
head = args[counter];
}
for (const e of ["target", "forward"] as const) {
if (typeof requestOptions[e] === "string") {
requestOptions[e] = toURL(requestOptions[e]);
}
}
if (!requestOptions.target && !requestOptions.forward) {
this.emit(
"error",
new Error("Must set target or forward") as TError,
req,
res,
);
return;
}
for (const pass of passes) {
/**
* Call of passes functions
* pass(req, res, options, head)
*
* In WebSockets case, the `res` variable
* refer to the connection socket
* pass(req, socket, options, head)
*/
if (
pass(
req,
res,
requestOptions as NormalizedServerOptions,
head,
this,
cb,
)
) {
// passes can return a truthy value to halt the loop
break;
}
}
};
};
};
onError = (err: TError) => {
// Force people to handle their own errors
if (this.listeners("error").length === 1) {
throw err;
}
};
/**
* A function that wraps the object in a webserver, for your convenience
* @param port - Port to listen on
* @param hostname - The hostname to listen on
*/
listen = (port: number, hostname?: string) => {
log("listen", { port, hostname });
const requestListener = (
req: InstanceType<TIncomingMessage> | http2.Http2ServerRequest,
res: InstanceType<TServerResponse> | http2.Http2ServerResponse,
) => {
this.web(
req as InstanceType<TIncomingMessage>,
res as InstanceType<TServerResponse>,
);
};
this._server = this.options.ssl
? http2.createSecureServer(
{ ...this.options.ssl, allowHTTP1: true },
requestListener,
)
: http.createServer<TIncomingMessage, TServerResponse>(requestListener);
if (this.options.ws) {
this._server.on(
"upgrade",
(req: InstanceType<TIncomingMessage>, socket, head) => {
this.ws(req, socket, head);
},
);
}
this._server.listen(port, hostname);
return this;
};
// if the proxy started its own http server, this is the address of that server.
address = () => {
return this._server?.address();
};
/**
* A function that closes the inner webserver and stops listening on given port
*/
close = (cb?: Function) => {
if (this._server == null) {
cb?.();
return;
}
// Wrap cb anb nullify server after all open connections are closed.
this._server.close((err?) => {
this._server = null;
cb?.(err);
});
};
before = <PT extends ProxyType>(
type: PT,
passName: string,
cb: PassFunctions<TIncomingMessage, TServerResponse, TError>[PT],
) => {
if (type !== "ws" && type !== "web") {
throw new Error("type must be `web` or `ws`");
}
const passes = (
type === "ws" ? this.wsPasses : this.webPasses
) as PassFunctions<TIncomingMessage, TServerResponse, TError>[PT][];
let i: false | number = false;
passes.forEach((v, idx) => {
if (v.name === passName) {
i = idx;
}
});
if (i === false) {
throw new Error("No such pass");
}
passes.splice(i, 0, cb);
};
after = <PT extends ProxyType>(
type: PT,
passName: string,
cb: PassFunctions<TIncomingMessage, TServerResponse, TError>[PT],
) => {
if (type !== "ws" && type !== "web") {
throw new Error("type must be `web` or `ws`");
}
const passes = (
type === "ws" ? this.wsPasses : this.webPasses
) as PassFunctions<TIncomingMessage, TServerResponse, TError>[PT][];
let i: false | number = false;
passes.forEach((v, idx) => {
if (v.name === passName) {
i = idx;
}
});
if (i === false) {
throw new Error("No such pass");
}
passes.splice(i++, 0, cb);
};
}