-
Notifications
You must be signed in to change notification settings - Fork 15
/
trailer.js
393 lines (358 loc) · 11.6 KB
/
trailer.js
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
//
// javascript trailer
//
const net = require("net");
const fs = require("fs");
const os = require("os");
const path = require("path");
// wrap connect with the code for extending exception with error code
SessionBuilder.prototype._connect = SessionBuilder.prototype.connect;
SessionBuilder.prototype.connect = ngrokSessionConnect;
// wrap listen with the bind code for passing to net.Server.listen()
HttpListenerBuilder.prototype._listen = HttpListenerBuilder.prototype.listen;
TcpListenerBuilder.prototype._listen = TcpListenerBuilder.prototype.listen;
TlsListenerBuilder.prototype._listen = TlsListenerBuilder.prototype.listen;
LabeledListenerBuilder.prototype._listen = LabeledListenerBuilder.prototype.listen;
HttpListenerBuilder.prototype.listen = ngrokBind;
TcpListenerBuilder.prototype.listen = ngrokBind;
TlsListenerBuilder.prototype.listen = ngrokBind;
LabeledListenerBuilder.prototype.listen = ngrokBind;
HttpListenerBuilder.prototype.listenAndServe = listenAndServe;
TcpListenerBuilder.prototype.listenAndServe = listenAndServe;
TlsListenerBuilder.prototype.listenAndServe = listenAndServe;
LabeledListenerBuilder.prototype.listenAndServe = listenAndServe;
// Wrap session connect to fill in exception's errorCode
async function ngrokSessionConnect() {
try {
return await this._connect();
} catch (err) {
populateErrorCode(err);
throw err;
}
}
// Begin listening for new connections on this listener,
// and bind to a local socket so this listener can be
// passed into net.Server.listen().
async function ngrokBind(bind) {
try {
const listener = await this._listen();
if (bind !== false) {
const socket = await randomTcpSocket();
listener.socket = socket;
defineListenerHandle(listener, socket);
}
return listener;
} catch (err) {
populateErrorCode(err);
throw err;
}
}
/// Begin listening for new connections on this listener and forwarding them to the given server.
async function listenAndServe(server) {
const listener = await this._listen();
listener.socket = await ngrokListen(server, listener);
return listener;
}
function populateErrorCode(err) {
if (err.message) {
const regex = /error_code: (ERR_NGROK_\d+)$/;
const errorCode = err.message.match(regex);
if (errorCode && errorCode.length > 1) {
err.errorCode = errorCode[1];
}
}
}
// add a 'handle' getter to the listener so it can be
// passed into net.Server.listen().
function defineListenerHandle(listener, socket) {
// NodeJS net.Server asks passed-in object for 'handle',
// Return the native TCP object so the pre-existing socket is used.
Object.defineProperty(listener, "handle", {
get: function () {
// turn on forwarding now that it has been requested
listener.forward("localhost:" + socket.address().port);
return socket._handle;
},
});
}
// generate a net.Server listening to a random port
async function randomTcpSocket() {
return await asyncListen(new net.Server(), { host: "localhost", port: 0 });
}
// NodeJS has not promisified 'net': https://github.com/nodejs/node/issues/21482
function asyncListen(server, options) {
return new Promise((resolve, reject) => {
const socket = server.listen(options);
socket
.once("listening", () => {
resolve(socket);
})
.once("error", (err) => {
reject(err);
});
});
}
// Make a session using NGROK_AUTHTOKEN from the environment,
// and then return a listening HTTP listener.
async function defaultListener(bind) {
// set up a default session and listener
var builder = new SessionBuilder();
builder.authtokenFromEnv();
var session = await builder.connect();
var listener = await session.httpEndpoint().listen(bind);
listener.session = session; // surface to caller
return listener;
}
// Get a listenable ngrok listener, suitable for passing to net.Server.listen().
// Uses the NGROK_AUTHTOKEN environment variable to authenticate.
async function listenable() {
return await defaultListener();
}
// Bind a server to a new ngrok listener, optionally passing in a pre-existing listener instead.
// Uses the NGROK_AUTHTOKEN environment variable to authenticate if a new listener is created.
async function ngrokListen(server, listener) {
if (listener && listener.socket) {
// close the default bound port
listener.socket.close();
}
if (!listener) {
// turn off automatic bind
listener = await defaultListener(false);
}
// attempt pipe socket
try {
socket = await ngrokLinkPipe(listener, server);
} catch (err) {
console.debug("Using TCP socket. " + err);
// fallback to tcp socket
socket = await ngrokLinkTcp(listener, server);
}
registerCleanup(listener, socket);
server.listener = listener; // surface to caller
socket.listener = listener; // surface to caller
// return the newly created net.Server, which will be different in the express case
return socket;
}
async function ngrokLinkTcp(listener, server) {
// random local port
const socket = await asyncListen(server, { host: "localhost", port: 0 });
// forward to socket
listener.forward("localhost:" + socket.address().port);
return socket;
}
function generatePipeFilename(listener, server) {
var proposed = "tun-" + listener.id() + ".sock";
// windows leaves little choice
if (platform == "win32") {
return "\\\\.\\pipe\\" + proposed;
}
// try to make a directory in the current working directory
const dir = ".ngrok";
try {
fs.mkdirSync(dir);
} catch (err) {
// move on
}
try {
fs.accessSync(dir, fs.constants.W_OK);
return dir + path.sep + proposed;
} catch (err) {
// move on
}
// try the OS temp directory, being careful not to exceed the maximum path length for unix sockets
// https://linux.die.net/man/7/unix
// https://unix.stackexchange.com/a/367012
if (os.tmpdir().length < 90) {
try {
fs.accessSync(os.tmpdir(), fs.constants.W_OK);
filepath = os.tmpdir() + path.sep + proposed;
if (filepath.length > 100) {
// truncate
filepath = filepath.substring(0, 100);
}
return filepath;
} catch (err) {
// move on
}
}
// fallback to current working directory. allow any exception to propagate
fs.accessSync(process.cwd(), fs.constants.W_OK);
return proposed;
}
async function ngrokLinkPipe(listener, server) {
var filename = generatePipeFilename(listener);
// begin listening
const socket = await asyncListen(server, { path: filename });
// tighten permissions
try {
if (platform != "win32") {
fs.chmodSync(filename, fs.constants.S_IRWXU);
}
} catch (err) {
console.debug("Cannot change permissions of file: " + filename);
}
// forward listener
var addr = "unix:" + filename;
if (platform == "win32") {
// convert pipe path to url
addr = "pipe:" + filename.replace("\\\\.\\pipe\\", "//./");
}
listener.forward(addr);
socket.path = filename; // surface to caller
return socket;
}
// protect against multiple calls, for instance from npm
var sigHandlerRan = false;
function registerCleanup(listener, socket) {
for (const signal of ["SIGINT", "SIGTERM"]) {
process.on(signal, function () {
if (process.listenerCount(signal) > 1) {
// user has registered a handler, abort this one
return;
}
if (sigHandlerRan) return;
sigHandlerRan = true;
// close listener
if (listener) {
listener.close().catch((err) => {
console.error(`Error closing listener: ${err}`);
});
}
// close webserver's socket
if (socket) socket.close();
// unregister any logging callback
loggingCallback();
});
}
}
function consoleLog(level) {
loggingCallback((level, target, message) => {
console.log(`${level} ${target} - ${message}`);
}, level);
}
// wrap forward with code to vectorize and split out functions
const _forward = forward;
async function ngrokForward(config) {
if (config == undefined) config = 80;
if (Number.isInteger(config) || typeof config === "string" || config instanceof String) {
address = String(config);
if (Number.isInteger(config) && !address.includes(":")) {
address = `localhost:${address}`;
}
config = { addr: address };
}
if (typeof config["port"] === "string" || config["port"] instanceof String) {
const num = parseInt(config["port"], 10);
if (isNaN(num)) {
throw new Error(`port must be a number: '${config["port"]}'`);
}
config["port"] = num;
}
// Convert addr to string to allow for numeric port numbers
const addr = config["addr"];
if (Number.isInteger(addr)) config["addr"] = "localhost:" + String(config["addr"]);
// convert scalar values to arrays to meet what napi-rs expects
[
"allow_user_agent",
"auth",
"basic_auth",
"deny_user_agent",
"ip_restriction.allow_cidrs",
"ip_restriction.deny_cidrs",
"labels",
"oauth.allow_domains",
"oauth.allow_emails",
"oauth.scopes",
"oidc.scopes",
"oidc.allow_domains",
"oidc.allow_emails",
"request_header.add",
"request_header.remove",
"response_header.add",
"response_header.remove",
"schemes",
].forEach((key) => {
vectorize(config, key);
});
// convert dotted values to underscores for backwards compatibility
[
"ip_restriction.allow_cidrs",
"ip_restriction.deny_cidrs",
"oauth.allow_domains",
"oauth.allow_emails",
"oauth.scopes",
"oauth.provider",
"oidc.client_id",
"oidc.client_secret",
"oidc.scopes",
"oidc.issuer_url",
"oidc.allow_domains",
"oidc.allow_emails",
"request_header.add",
"request_header.remove",
"response_header.add",
"response_header.remove",
"verify_webhook.provider",
"verify_webhook.secret",
].forEach((key) => {
undot(config, key);
});
// break out the logging callback function to meet what napi-rs expects
var on_log_event;
if (config["onLogEvent"]) {
const onLogEvent = config.onLogEvent;
on_log_event = (level, target, message) => {
onLogEvent(`${level} ${target} - ${message}`);
};
config["onLogEvent"] = true;
}
// break out the status change callback functions to what napi-rs expects
var on_connection, on_disconnection;
if (config["onStatusChange"]) {
const onStatusChange = config.onStatusChange;
on_connection = (status, err) => {
onStatusChange(status);
};
on_disconnection = (addr, err) => {
onStatusChange("closed");
};
config["onStatusChange"] = true;
}
// call into rust
try {
return await _forward(config, on_log_event, on_connection, on_disconnection);
} catch (err) {
populateErrorCode(err);
throw err;
}
}
function undot(config, dotKey) {
const noDotKey = dotKey.replace(".", "_");
if (config[dotKey] == null) return; // no dotKey value, done
if (config[noDotKey] == null) {
// nothing at destination, just set and be done
config[noDotKey] = config[dotKey];
return;
}
if (config[dotKey] instanceof Array && config[noDotKey] instanceof Array) {
// merge arrays
for (const obj of config[dotKey]) {
config[noDotKey].push(obj);
}
}
// destination exists and is not an array, do nothing so noDotKey can take precedence
}
function vectorize(config, key) {
// backwards compatible keys are passed in, check the new style as well
const noDotKey = key.replace(".", "_");
if (key != noDotKey) vectorize(config, noDotKey);
if (config[key] == null) return; // no value, done
if (!(config[key] instanceof Array)) {
config[key] = [config[key]];
}
}
module.exports.connect = ngrokForward;
module.exports.forward = ngrokForward;
module.exports.consoleLog = consoleLog;
module.exports.listen = ngrokListen;
module.exports.listenable = listenable;