fix tunnel

This commit is contained in:
hatim boufnichel 2021-04-20 21:11:33 +02:00
parent 65d34b72b7
commit 561a52bd14
2 changed files with 63 additions and 16 deletions

View file

@ -291,7 +291,12 @@ const server = program => {
await Storage.init({ await Storage.init({
dir: storageDirectory dir: storageDirectory
}) }) /*
if (false) {
await Storage.removeItem('tunnel/token')
await Storage.removeItem('tunnel/subdomain')
await Storage.removeItem('tunnel/url')
}*/
if (program.tunnel) { if (program.tunnel) {
// setup localtunnel ========== // setup localtunnel ==========
const [tunnelToken, tunnelSubdomain, tunnelUrl] = await Promise.all([ const [tunnelToken, tunnelSubdomain, tunnelUrl] = await Promise.all([
@ -305,11 +310,18 @@ const server = program => {
tunnelOpts.subdomain = tunnelSubdomain tunnelOpts.subdomain = tunnelSubdomain
logger.info('Recreating tunnel... with subdomain: ' + tunnelSubdomain) logger.info('Recreating tunnel... with subdomain: ' + tunnelSubdomain)
} else { } else {
logger.info('Creating new tunnel... ') logger.info('Creating new tunnel...This will require a pair ')
} }
tunnelHealthManager.emit('fork', { tunnelHealthManager.emit('fork', {
params: tunnelOpts, params: tunnelOpts,
cb: async tunnel => { cb: async tunnel => {
if (tunnelSubdomain !== tunnel.clientId && !tunnel.token) {
logger.error(
'An error occurred while opening tunnel, will try again in 2 sec with a new one'
)
return
}
logger.info('Tunnel created! connect to: ' + tunnel.url) logger.info('Tunnel created! connect to: ' + tunnel.url)
const dataToQr = JSON.stringify({ const dataToQr = JSON.stringify({
internalIP: tunnel.url, internalIP: tunnel.url,
@ -317,18 +329,8 @@ const server = program => {
externalIP: tunnel.url externalIP: tunnel.url
}) })
qrcode.generate(dataToQr, { small: true }) qrcode.generate(dataToQr, { small: true })
if (!tunnelToken) { if (tunnel.token) {
await Promise.all([ console.log('writing to storage...')
Storage.setItem('tunnel/token', tunnel.token),
Storage.setItem('tunnel/subdomain', tunnel.clientId),
Storage.setItem('tunnel/url', tunnel.url)
])
}
if (tunnelUrl && tunnel.url !== tunnelUrl) {
logger.error('New tunnel URL different from OLD tunnel url')
logger.error('OLD: ' + tunnelUrl + ':80')
logger.error('NEW: ' + tunnel.url + ':80')
logger.error('New pair required')
await Promise.all([ await Promise.all([
Storage.setItem('tunnel/token', tunnel.token), Storage.setItem('tunnel/token', tunnel.token),
Storage.setItem('tunnel/subdomain', tunnel.clientId), Storage.setItem('tunnel/subdomain', tunnel.clientId),

View file

@ -1,14 +1,59 @@
const localtunnel = require('localtunnel') const localtunnel = require('localtunnel')
let tunnelRef = null
process.on('message', async (tunnelOpts) => { process.on('message', async (tunnelOpts) => {
console.log('Message from parent:', tunnelOpts);
const tunnel = await localtunnel(tunnelOpts) const tunnel = await localtunnel(tunnelOpts)
tunnelRef = tunnel
console.log(tunnelOpts)
const {subdomain:tunnelSubdomain} = tunnelOpts
process.send({ type: 'info', tunnel:{ process.send({ type: 'info', tunnel:{
url:tunnel.url, url:tunnel.url,
token:tunnel.token, token:tunnel.token,
clientId:tunnel.clientId, clientId:tunnel.clientId,
} }); } });
if(tunnelSubdomain !== tunnel.clientId && !tunnel.token){
console.log("AM killing it yo!")
console.log(tunnel.clientId)
tunnel.close()
// eslint-disable-next-line no-process-exit
process.exit()
}
}); });
setInterval(() => { setInterval(() => {
process.send({ type: "ping" }); process.send({ type: "ping" });
}, 1000); }, 1000);
process.on('uncaughtException', ()=> {
if(tunnelRef){
console.log("clogin yo")
tunnelRef.close()
}
// eslint-disable-next-line no-process-exit
process.exit()
});
process.on('SIGINT', ()=>{
if(tunnelRef){
console.log("clogin yo")
tunnelRef.close()
}
// eslint-disable-next-line no-process-exit
process.exit()})
process.on('exit', ()=> {
if(tunnelRef){
console.log("clogin yo")
tunnelRef.close()
}
});
/*
const f = async () => {
const tunnelOpts =
{ port: 9835, host: 'https://tunnel.rip' ,
tunnelToken:'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjp7InRpbWVzdGFtcCI6MTYxODg2NTAxNjkzNywic3ViZG9tYWluIjoidGVycmlibGUtZWFyd2lnLTU2In0sImlhdCI6MTYxODg2NTAxNiwiZXhwIjo1MjE4ODY1MDE2fQ.m2H4B1NatErRqcriB9lRfusZmLdRee9-VXACfnKT-QY',
subdomain:'terrible-earwig-56'
}
const tunnel = await localtunnel(tunnelOpts)
console.log(tunnel)
tunnelRef = tunnel
}
f()*/