forked from clubdrei/docker-localhost-tunnel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
50 lines (40 loc) · 1.26 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
const localtunnel = require('localtunnel');
const util = require('node:util');
const { buildConfig } = require('./lib/config');
// 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() {
const config = buildConfig();
console.log('Tunnel config', config);
tunnel = await localtunnel(config);
host = `${config.protocol}://${tunnel.clientId}.${process.env.LOCALTUNNEL_HOST}`;
console.log(`Started tunnel for ${host}`);
tunnel.on('request', (request) => {
console.debug(`Request for tunnel ${host}`, request);
});
tunnel.on('error', (error) => {
console.error(`Error in tunnel ${host}`, error);
});
tunnel.on('close', () => {
console.debug(`Closed tunnel for ${host}`);
process.exit(0);
});
}
['SIGINT', 'SIGTERM'].forEach(function(signal) {
process.on(signal, function() {
console.debug(`Received signal ${signal}`)
if (tunnel !== null) {
tunnel.close();
console.log(`Stopped tunnel for ${host}`);
process.exit(0);
}
});
});
run();