forked from DevExpress/testcafe-hammerhead
/
index.ts
252 lines (202 loc) · 9.67 KB
/
index.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import net from 'net';
import Session from '../session';
import { ExternalProxySettingsRaw } from '../typings/session';
import Router from './router';
import {
StaticContent,
ServiceMessage,
ServerInfo,
ProxyOptions
} from '../typings/proxy';
import http, { ServerOptions } from 'http';
import https from 'https';
import * as urlUtils from '../utils/url';
import scriptProcessor from '../processing/resources/script';
import { readSync as read } from 'read-file-relative';
import { respond500, respondWithJSON, fetchBody, addPreventCachingHeaders } from '../utils/http';
import { run as runRequestPipeline } from '../request-pipeline';
import prepareShadowUIStylesheet from '../shadow-ui/create-shadow-stylesheet';
import { resetKeepAliveConnections } from '../request-pipeline/destination-request/agent';
import SERVICE_ROUTES from './service-routes';
import BUILTIN_HEADERS from '../request-pipeline/builtin-header-names';
import logger from '../utils/logger';
import errToString from '../utils/err-to-string';
import { parse as parseJSON } from '../utils/json';
const SESSION_IS_NOT_OPENED_ERR = 'Session is not opened in proxy';
function parseAsJson (msg: Buffer): ServiceMessage | null {
try {
return parseJSON(msg.toString());
}
catch (err) {
return null;
}
}
function createServerInfo (hostname: string, port: number, crossDomainPort: number, protocol: string, cacheRequests: boolean): ServerInfo {
return {
hostname,
port,
crossDomainPort,
protocol,
cacheRequests,
domain: `${protocol}//${hostname}:${port}`
};
}
export default class Proxy extends Router {
private readonly openSessions: Map<string, Session> = new Map();
private readonly server1Info: ServerInfo;
private readonly server2Info: ServerInfo;
private readonly server1: http.Server | https.Server;
private readonly server2: http.Server | https.Server;
private readonly sockets: Set<net.Socket>;
// Max header size for incoming HTTP requests
// Set to 80 KB as it was the original limit:
// https://github.com/nodejs/node/blob/186035243fad247e3955fa0c202987cae99e82db/deps/http_parser/http_parser.h#L63
// Before the change to 8 KB:
// https://github.com/nodejs/node/commit/186035243fad247e3955fa0c202987cae99e82db#diff-1d0d420098503156cddb601e523b82e7R59
public static MAX_REQUEST_HEADER_SIZE = 80 * 1024;
constructor (hostname: string, port1: number, port2: number, options: Partial<ProxyOptions> = { developmentMode: false, cache: false }) {
super(options);
const { ssl, developmentMode = false, cache = false } = options;
const protocol = ssl ? 'https:' : 'http:';
const opts = this._getOpts(ssl);
const createServer = this._getCreateServerMethod(ssl);
this.server1Info = createServerInfo(hostname, port1, port2, protocol, cache);
this.server2Info = createServerInfo(hostname, port2, port1, protocol, cache);
this.server1 = createServer(opts, (req: http.IncomingMessage, res: http.ServerResponse) => this._onRequest(req, res, this.server1Info));
this.server2 = createServer(opts, (req: http.IncomingMessage, res: http.ServerResponse) => this._onRequest(req, res, this.server2Info));
this.server1.on('upgrade', (req: http.IncomingMessage, socket: net.Socket, head: Buffer) => this._onUpgradeRequest(req, socket, head, this.server1Info));
this.server2.on('upgrade', (req: http.IncomingMessage, socket: net.Socket, head: Buffer) => this._onUpgradeRequest(req, socket, head, this.server2Info));
this.server1.listen(port1);
this.server2.listen(port2);
this.sockets = new Set<net.Socket>();
// BUG: GH-89
this._startSocketsCollecting();
this._registerServiceRoutes(developmentMode);
}
_getOpts (ssl?: {}): ServerOptions {
let opts = {} as ServerOptions;
if (ssl)
opts = ssl;
opts.maxHeaderSize = Proxy.MAX_REQUEST_HEADER_SIZE;
return opts;
}
_getCreateServerMethod (ssl?: {}) {
return ssl ? https.createServer : http.createServer;
}
_closeSockets (): void {
this.sockets.forEach(socket => socket.destroy());
}
_startSocketsCollecting (): void {
const handler = (socket: net.Socket) => {
this.sockets.add(socket);
socket.on('close', () => this.sockets.delete(socket));
};
this.server1.on('connection', handler);
this.server2.on('connection', handler);
}
_registerServiceRoutes (developmentMode: boolean): void {
const developmentModeSuffix = developmentMode ? '' : '.min';
const hammerheadFileName = `hammerhead${developmentModeSuffix}.js`;
const hammerheadScriptContent = read(`../client/${hammerheadFileName}`) as Buffer;
const transportWorkerFileName = `transport-worker${developmentModeSuffix}.js`;
const transportWorkerContent = read(`../client/${transportWorkerFileName}`) as Buffer;
const workerHammerheadFileName = `worker-hammerhead${developmentModeSuffix}.js`;
const workerHammerheadContent = read(`../client/${workerHammerheadFileName}`) as Buffer;
this.GET(SERVICE_ROUTES.hammerhead, {
contentType: 'application/x-javascript',
content: hammerheadScriptContent
});
this.GET(SERVICE_ROUTES.transportWorker, {
contentType: 'application/x-javascript',
content: transportWorkerContent
});
this.GET(SERVICE_ROUTES.workerHammerhead, {
contentType: 'application/x-javascript',
content: workerHammerheadContent
});
this.POST(SERVICE_ROUTES.messaging, (req: http.IncomingMessage, res: http.ServerResponse, serverInfo: ServerInfo) => this._onServiceMessage(req, res, serverInfo));
this.GET(SERVICE_ROUTES.task, (req: http.IncomingMessage, res: http.ServerResponse, serverInfo: ServerInfo) => this._onTaskScriptRequest(req, res, serverInfo, false));
this.GET(SERVICE_ROUTES.iframeTask, (req: http.IncomingMessage, res: http.ServerResponse, serverInfo: ServerInfo) => this._onTaskScriptRequest(req, res, serverInfo, true));
}
async _onServiceMessage (req: http.IncomingMessage, res: http.ServerResponse, serverInfo: ServerInfo): Promise<void> {
const body = await fetchBody(req);
const msg = parseAsJson(body);
const session = msg && this.openSessions.get(msg.sessionId);
if (msg && session) {
try {
const result = await session.handleServiceMessage(msg, serverInfo);
logger.serviceMsg.onMessage(msg, result);
respondWithJSON(res, result, false);
}
catch (err) {
logger.serviceMsg.onError(msg, err);
respond500(res, errToString(err));
}
}
else
respond500(res, SESSION_IS_NOT_OPENED_ERR);
}
async _onTaskScriptRequest (req: http.IncomingMessage, res: http.ServerResponse, serverInfo: ServerInfo, isIframe: boolean): Promise<void> {
const referer = req.headers[BUILTIN_HEADERS.referer] as string;
const refererDest = referer && urlUtils.parseProxyUrl(referer);
const session = refererDest && this.openSessions.get(refererDest.sessionId);
const windowId = refererDest && refererDest.windowId || void 0;
if (session) {
res.setHeader(BUILTIN_HEADERS.contentType, 'application/x-javascript');
addPreventCachingHeaders(res);
const taskScript = await session.getTaskScript({
referer,
cookieUrl: refererDest ? refererDest.destUrl : '',
serverInfo,
isIframe,
withPayload: true,
windowId
});
res.end(taskScript);
}
else
respond500(res, SESSION_IS_NOT_OPENED_ERR);
}
_onRequest (req: http.IncomingMessage, res: http.ServerResponse | net.Socket, serverInfo: ServerInfo): void {
// NOTE: Not a service request, execute the proxy pipeline.
if (!this._route(req, res, serverInfo))
runRequestPipeline(req, res, serverInfo, this.openSessions);
}
_onUpgradeRequest (req: http.IncomingMessage, socket: net.Socket, head: Buffer, serverInfo: ServerInfo): void {
if (head && head.length)
socket.unshift(head);
this._onRequest(req, socket, serverInfo);
}
_processStaticContent (handler: StaticContent): void {
if (handler.isShadowUIStylesheet)
handler.content = prepareShadowUIStylesheet(handler.content as string);
}
// API
close (): void {
scriptProcessor.jsCache.reset()
this.server1.close();
this.server2.close();
this._closeSockets();
resetKeepAliveConnections();
}
openSession (url: string, session: Session, externalProxySettings: ExternalProxySettingsRaw): string {
session.proxy = this;
this.openSessions.set(session.id, session);
if (externalProxySettings)
session.setExternalProxySettings(externalProxySettings);
if (this.options.disableHttp2)
session.disableHttp2();
url = urlUtils.prepareUrl(url);
return urlUtils.getProxyUrl(url, {
proxyHostname: this.server1Info.hostname,
proxyPort: this.server1Info.port.toString(),
proxyProtocol: this.server1Info.protocol,
sessionId: session.id,
windowId: session.options.windowId
});
}
closeSession (session: Session): void {
session.proxy = null;
this.openSessions.delete(session.id);
}
}