This repository has been archived by the owner on Mar 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
54 lines (44 loc) · 1.63 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
const localtunnel = require('localtunnel');
const util = require('node:util');
// Force console.* output into one line. This makes searching in Grafana a lot easier.
util.inspect.defaultOptions.breakLength = Infinity;
util.inspect.defaultOptions.compact = true;
// Keep process running:
// https://stackoverflow.com/a/47456805
setInterval(() => {
}, 1 << 30);
let tunnel = null;
let host = null;
async function run() {
if (!process.env.hasOwnProperty('LOCALTUNNEL_HOST') || process.env.LOCALTUNNEL_HOST.trim().length === 0) {
console.error(`ENV variable LOCALTUNNEL_HOST missing or invalid. Given value: ${process.env.LOCALTUNNEL_HOST}`);
process.exit(1);
}
if (!process.env.hasOwnProperty('LOCALTUNNEL_SUBDOMAIN') || process.env.LOCALTUNNEL_SUBDOMAIN.trim().length === 0) {
console.error(`ENV variable LOCALTUNNEL_SUBDOMAIN missing or invalid. Given value: ${process.env.LOCALTUNNEL_SUBDOMAIN}`);
process.exit(1);
}
const config = {
host: `https://${process.env.LOCALTUNNEL_HOST}`,
local_host: process.env.LOCALTUNNEL_LOCAL_HOST || 'localhost',
port: process.env.LOCALTUNNEL_PORT || 80,
subdomain: process.env.LOCALTUNNEL_SUBDOMAIN,
};
console.log('Tunnel config', config);
tunnel = await localtunnel(config);
host = `https://${tunnel.clientId}.${process.env.LOCALTUNNEL_HOST}`;
console.log(`Started tunnel for ${host}`);
tunnel.on('close', () => {
process.exit(0);
});
}
['SIGINT', 'SIGTERM'].forEach(function(signal) {
process.on(signal, function() {
if (tunnel !== null) {
tunnel.close();
console.log(`Stopped tunnel for ${host}`);
process.exit(0);
}
});
});
run();