2021-08-19 17:36:03 +03:00
|
|
|
/**
|
|
|
|
* Copyright (c) Microsoft Corporation.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-04-07 00:57:14 +03:00
|
|
|
import type { IncomingMessage, Server } from 'http';
|
|
|
|
import type { Socket } from 'net';
|
2021-08-19 17:36:03 +03:00
|
|
|
import createProxy from 'proxy';
|
|
|
|
|
|
|
|
export class TestProxy {
|
|
|
|
readonly PORT: number;
|
|
|
|
readonly URL: string;
|
|
|
|
|
2021-08-19 23:34:32 +03:00
|
|
|
connectHosts: string[] = [];
|
|
|
|
requestUrls: string[] = [];
|
|
|
|
|
|
|
|
private readonly _server: Server;
|
2021-08-19 17:36:03 +03:00
|
|
|
private readonly _sockets = new Set<Socket>();
|
2021-08-19 23:34:32 +03:00
|
|
|
private _handlers: { event: string, handler: (...args: any[]) => void }[] = [];
|
2021-08-19 17:36:03 +03:00
|
|
|
|
|
|
|
static async create(port: number): Promise<TestProxy> {
|
|
|
|
const proxy = new TestProxy(port);
|
2021-10-02 05:40:47 +03:00
|
|
|
await new Promise<void>(f => proxy._server.listen(port, f));
|
2021-08-19 17:36:03 +03:00
|
|
|
return proxy;
|
|
|
|
}
|
|
|
|
|
|
|
|
private constructor(port: number) {
|
|
|
|
this.PORT = port;
|
|
|
|
this.URL = `http://localhost:${port}`;
|
2021-08-19 23:34:32 +03:00
|
|
|
this._server = createProxy();
|
|
|
|
this._server.on('connection', socket => this._onSocket(socket));
|
2021-08-19 17:36:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
async stop(): Promise<void> {
|
|
|
|
this.reset();
|
|
|
|
for (const socket of this._sockets)
|
|
|
|
socket.destroy();
|
|
|
|
this._sockets.clear();
|
2021-08-19 23:34:32 +03:00
|
|
|
await new Promise(x => this._server.close(x));
|
2021-08-19 17:36:03 +03:00
|
|
|
}
|
|
|
|
|
2023-10-17 23:41:23 +03:00
|
|
|
forwardTo(port: number, options?: { allowConnectRequests: boolean }) {
|
2021-08-19 23:34:32 +03:00
|
|
|
this._prependHandler('request', (req: IncomingMessage) => {
|
|
|
|
this.requestUrls.push(req.url);
|
|
|
|
const url = new URL(req.url);
|
|
|
|
url.host = `localhost:${port}`;
|
|
|
|
req.url = url.toString();
|
|
|
|
});
|
|
|
|
this._prependHandler('connect', (req: IncomingMessage) => {
|
2023-10-17 23:41:23 +03:00
|
|
|
if (!options?.allowConnectRequests)
|
2021-12-11 01:01:56 +03:00
|
|
|
return;
|
2021-08-19 23:34:32 +03:00
|
|
|
this.connectHosts.push(req.url);
|
|
|
|
req.url = `localhost:${port}`;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
setAuthHandler(handler: (req: IncomingMessage) => boolean) {
|
|
|
|
(this._server as any).authenticate = (req: IncomingMessage, callback) => {
|
|
|
|
try {
|
|
|
|
callback(null, handler(req));
|
|
|
|
} catch (e) {
|
|
|
|
callback(e, false);
|
|
|
|
}
|
|
|
|
};
|
2021-08-19 17:36:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
reset() {
|
2021-08-19 23:34:32 +03:00
|
|
|
this.connectHosts = [];
|
|
|
|
this.requestUrls = [];
|
|
|
|
for (const { event, handler } of this._handlers)
|
|
|
|
this._server.removeListener(event, handler);
|
|
|
|
this._handlers = [];
|
|
|
|
(this._server as any).authenticate = undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
private _prependHandler(event: string, handler: (...args: any[]) => void) {
|
|
|
|
this._handlers.push({ event, handler });
|
|
|
|
this._server.prependListener(event, handler);
|
2021-08-19 17:36:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private _onSocket(socket: Socket) {
|
|
|
|
this._sockets.add(socket);
|
|
|
|
// ECONNRESET and HPE_INVALID_EOF_STATE are legit errors given
|
|
|
|
// that tab closing aborts outgoing connections to the server.
|
|
|
|
socket.on('error', (error: any) => {
|
|
|
|
if (error.code !== 'ECONNRESET' && error.code !== 'HPE_INVALID_EOF_STATE')
|
|
|
|
throw error;
|
|
|
|
});
|
|
|
|
socket.once('close', () => this._sockets.delete(socket));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|