Merge pull request #342 from shocknet/fix/tunnel-persistence

fix tunnel
This commit is contained in:
CapDog 2021-04-20 15:15:18 -04:00 committed by GitHub
commit d4cc93d39b
2 changed files with 63 additions and 16 deletions

View file

@ -291,7 +291,12 @@ const server = program => {
await Storage.init({
dir: storageDirectory
})
}) /*
if (false) {
await Storage.removeItem('tunnel/token')
await Storage.removeItem('tunnel/subdomain')
await Storage.removeItem('tunnel/url')
}*/
if (program.tunnel) {
// setup localtunnel ==========
const [tunnelToken, tunnelSubdomain, tunnelUrl] = await Promise.all([
@ -305,11 +310,18 @@ const server = program => {
tunnelOpts.subdomain = tunnelSubdomain
logger.info('Recreating tunnel... with subdomain: ' + tunnelSubdomain)
} else {
logger.info('Creating new tunnel... ')
logger.info('Creating new tunnel...This will require a pair ')
}
tunnelHealthManager.emit('fork', {
params: tunnelOpts,
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)
const dataToQr = JSON.stringify({
internalIP: tunnel.url,
@ -317,18 +329,8 @@ const server = program => {
externalIP: tunnel.url
})
qrcode.generate(dataToQr, { small: true })
if (!tunnelToken) {
await Promise.all([
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')
if (tunnel.token) {
console.log('writing to storage...')
await Promise.all([
Storage.setItem('tunnel/token', tunnel.token),
Storage.setItem('tunnel/subdomain', tunnel.clientId),

View file

@ -1,14 +1,59 @@
const localtunnel = require('localtunnel')
let tunnelRef = null
process.on('message', async (tunnelOpts) => {
console.log('Message from parent:', tunnelOpts);
const tunnel = await localtunnel(tunnelOpts)
tunnelRef = tunnel
console.log(tunnelOpts)
const {subdomain:tunnelSubdomain} = tunnelOpts
process.send({ type: 'info', tunnel:{
url:tunnel.url,
token:tunnel.token,
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(() => {
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()*/