-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtunnel.js
66 lines (50 loc) · 1.72 KB
/
tunnel.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
55
56
57
58
59
60
61
62
63
64
65
66
const localtunnel = require('localtunnel')
let allowClose = false
let once
let firstTime
function runTunnel(addonPort, remoteOpts) {
const tunnel = localtunnel(addonPort, remoteOpts, (err, tunnel) => {
if (err) {
console.error(err)
return
}
if (!firstTime || !remoteOpts.subdomain) {
firstTime = true
console.log('Remote Add-on URL: '+tunnel.url+'/[my-jackett-key]/manifest.json')
console.log('Replace "[my-jackett-key]" with your Jackett API Key')
} else {
console.log('Reconnected Tunnel as: '+tunnel.url)
}
if (remoteOpts.subdomain && !tunnel.url.startsWith('https://' + remoteOpts.subdomain + '.')) {
console.log('Subdomain set but tunnel urls do not match, closing tunnel and trying again in 30 secs')
cleanClose(30)
}
})
function cleanClose(secs) {
tunnel.removeListener('close', onClose)
tunnel.removeListener('error', onError)
tunnel.close()
setTimeout(() => {
runTunnel(addonPort, remoteOpts)
}, secs * 1000)
}
function onClose() { if (allowClose) process.exit() }
function onError(err) {
console.log('caught exception:')
console.log(err)
console.log('Tunnel error, closing tunnel and trying again in 30 secs')
cleanClose(30)
}
tunnel.on('close', onClose)
tunnel.on('error', onError)
if (!once) {
once = true
const cleanUp = require('death')({ uncaughtException: true })
cleanUp((sig, err) => {
console.error(err)
allowClose = true
tunnel.close()
})
}
}
module.exports = runTunnel