-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.js
666 lines (622 loc) · 19.6 KB
/
index.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
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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
/* tslint:disable */
/* eslint-disable */
/* prettier-ignore */
/* auto-generated by NAPI-RS */
const { existsSync, readFileSync } = require('fs')
const { join } = require('path')
const { platform, arch } = process
let nativeBinding = null
let localFileExisted = false
let loadError = null
function isMusl() {
// For Node 10
if (!process.report || typeof process.report.getReport !== 'function') {
try {
const lddPath = require('child_process').execSync('which ldd').toString().trim()
return readFileSync(lddPath, 'utf8').includes('musl')
} catch (e) {
return true
}
} else {
const { glibcVersionRuntime } = process.report.getReport().header
return !glibcVersionRuntime
}
}
switch (platform) {
case 'android':
switch (arch) {
case 'arm64':
localFileExisted = existsSync(join(__dirname, 'ngrok.android-arm64.node'))
try {
if (localFileExisted) {
nativeBinding = require('./ngrok.android-arm64.node')
} else {
nativeBinding = require('@ngrok/ngrok-android-arm64')
}
} catch (e) {
loadError = e
}
break
case 'arm':
localFileExisted = existsSync(join(__dirname, 'ngrok.android-arm-eabi.node'))
try {
if (localFileExisted) {
nativeBinding = require('./ngrok.android-arm-eabi.node')
} else {
nativeBinding = require('@ngrok/ngrok-android-arm-eabi')
}
} catch (e) {
loadError = e
}
break
default:
throw new Error(`Unsupported architecture on Android ${arch}`)
}
break
case 'win32':
switch (arch) {
case 'x64':
localFileExisted = existsSync(
join(__dirname, 'ngrok.win32-x64-msvc.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./ngrok.win32-x64-msvc.node')
} else {
nativeBinding = require('@ngrok/ngrok-win32-x64-msvc')
}
} catch (e) {
loadError = e
}
break
case 'ia32':
localFileExisted = existsSync(
join(__dirname, 'ngrok.win32-ia32-msvc.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./ngrok.win32-ia32-msvc.node')
} else {
nativeBinding = require('@ngrok/ngrok-win32-ia32-msvc')
}
} catch (e) {
loadError = e
}
break
case 'arm64':
localFileExisted = existsSync(
join(__dirname, 'ngrok.win32-arm64-msvc.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./ngrok.win32-arm64-msvc.node')
} else {
nativeBinding = require('@ngrok/ngrok-win32-arm64-msvc')
}
} catch (e) {
loadError = e
}
break
default:
throw new Error(`Unsupported architecture on Windows: ${arch}`)
}
break
case 'darwin':
localFileExisted = existsSync(join(__dirname, 'ngrok.darwin-universal.node'))
try {
if (localFileExisted) {
nativeBinding = require('./ngrok.darwin-universal.node')
} else {
nativeBinding = require('@ngrok/ngrok-darwin-universal')
}
break
} catch {}
switch (arch) {
case 'x64':
localFileExisted = existsSync(join(__dirname, 'ngrok.darwin-x64.node'))
try {
if (localFileExisted) {
nativeBinding = require('./ngrok.darwin-x64.node')
} else {
nativeBinding = require('@ngrok/ngrok-darwin-x64')
}
} catch (e) {
loadError = e
}
break
case 'arm64':
localFileExisted = existsSync(
join(__dirname, 'ngrok.darwin-arm64.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./ngrok.darwin-arm64.node')
} else {
nativeBinding = require('@ngrok/ngrok-darwin-arm64')
}
} catch (e) {
loadError = e
}
break
default:
throw new Error(`Unsupported architecture on macOS: ${arch}`)
}
break
case 'freebsd':
if (arch !== 'x64') {
throw new Error(`Unsupported architecture on FreeBSD: ${arch}`)
}
localFileExisted = existsSync(join(__dirname, 'ngrok.freebsd-x64.node'))
try {
if (localFileExisted) {
nativeBinding = require('./ngrok.freebsd-x64.node')
} else {
nativeBinding = require('@ngrok/ngrok-freebsd-x64')
}
} catch (e) {
loadError = e
}
break
case 'linux':
switch (arch) {
case 'x64':
if (isMusl()) {
localFileExisted = existsSync(
join(__dirname, 'ngrok.linux-x64-musl.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./ngrok.linux-x64-musl.node')
} else {
nativeBinding = require('@ngrok/ngrok-linux-x64-musl')
}
} catch (e) {
loadError = e
}
} else {
localFileExisted = existsSync(
join(__dirname, 'ngrok.linux-x64-gnu.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./ngrok.linux-x64-gnu.node')
} else {
nativeBinding = require('@ngrok/ngrok-linux-x64-gnu')
}
} catch (e) {
loadError = e
}
}
break
case 'arm64':
if (isMusl()) {
localFileExisted = existsSync(
join(__dirname, 'ngrok.linux-arm64-musl.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./ngrok.linux-arm64-musl.node')
} else {
nativeBinding = require('@ngrok/ngrok-linux-arm64-musl')
}
} catch (e) {
loadError = e
}
} else {
localFileExisted = existsSync(
join(__dirname, 'ngrok.linux-arm64-gnu.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./ngrok.linux-arm64-gnu.node')
} else {
nativeBinding = require('@ngrok/ngrok-linux-arm64-gnu')
}
} catch (e) {
loadError = e
}
}
break
case 'arm':
localFileExisted = existsSync(
join(__dirname, 'ngrok.linux-arm-gnueabihf.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./ngrok.linux-arm-gnueabihf.node')
} else {
nativeBinding = require('@ngrok/ngrok-linux-arm-gnueabihf')
}
} catch (e) {
loadError = e
}
break
default:
throw new Error(`Unsupported architecture on Linux: ${arch}`)
}
break
default:
throw new Error(`Unsupported OS: ${platform}, architecture: ${arch}`)
}
if (!nativeBinding) {
if (loadError) {
throw loadError
}
throw new Error(`Failed to load native binding`)
}
const { connect, forward, disconnect, kill, Listener, listeners, getListener, getListenerByUrl, HttpListenerBuilder, TcpListenerBuilder, TlsListenerBuilder, LabeledListenerBuilder, loggingCallback, authtoken, SessionBuilder, Session, UpdateRequest } = nativeBinding
module.exports.connect = connect
module.exports.forward = forward
module.exports.disconnect = disconnect
module.exports.kill = kill
module.exports.Listener = Listener
module.exports.listeners = listeners
module.exports.getListener = getListener
module.exports.getListenerByUrl = getListenerByUrl
module.exports.HttpListenerBuilder = HttpListenerBuilder
module.exports.TcpListenerBuilder = TcpListenerBuilder
module.exports.TlsListenerBuilder = TlsListenerBuilder
module.exports.LabeledListenerBuilder = LabeledListenerBuilder
module.exports.loggingCallback = loggingCallback
module.exports.authtoken = authtoken
module.exports.SessionBuilder = SessionBuilder
module.exports.Session = Session
module.exports.UpdateRequest = UpdateRequest
//
// 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;