Last active
December 20, 2023 11:03
-
-
Save tilap/3c0b5e0ad9b4435f2641aa667e756959 to your computer and use it in GitHub Desktop.
Localtunnel auto reconnect
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Stupid nodejs script to use localtunnel and auto reconnect | |
* usage: node localtunnel.js port=3000 subdomain=toto | |
* | |
*/ | |
/* global console, require, process, setTimeout */ | |
const assert = require('assert'); | |
const localtunnel = require('localtunnel'); | |
var argv = process.argv.reduce(function(accumulator, str) { | |
if (str.indexOf('=') > -1) { | |
var split = str.split('='); | |
var val = split[1].match(/^\d+$/) ? Number(split[1]) : split[1]; | |
accumulator[split[0]] = val; | |
} | |
return accumulator; | |
}, {}); | |
const port = argv.port; | |
const subdomain = argv.subdomain; | |
const reconnectionTimeout = argv.timeout || 1000; | |
assert(port, 'No port provided'); | |
assert(subdomain, 'No subdomain provided'); | |
function openLocalTunnel() { | |
try { | |
var tunnel = localtunnel(port, { subdomain: subdomain }, function(error, tunnel) { | |
if (error) { | |
return setTimeout(openLocalTunnel, reconnectionTimeout); | |
} | |
console.log('Tunnel opened', { port: port, url: tunnel.url }); | |
}); | |
tunnel.on('error', function() { | |
setTimeout(openLocalTunnel, reconnectionTimeout); | |
}); | |
tunnel.on('close', function() { | |
setTimeout(openLocalTunnel, reconnectionTimeout); | |
}); | |
} catch(error) { | |
setTimeout(openLocalTunnel, reconnectionTimeout); | |
} | |
} | |
openLocalTunnel(); |
you're welcome :)
Thanks bro
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Tnk for sharin this man, really apreciate this.