diff --git a/bin/stunnel.js b/bin/stunnel.js index 6e48968..66a9012 100755 --- a/bin/stunnel.js +++ b/bin/stunnel.js @@ -151,7 +151,7 @@ function connectTunnel() { }); console.info(''); - stunnel.connect({ + var tun = stunnel.connect({ stunneld: program.stunneld , locals: program.locals , services: program.services @@ -159,6 +159,17 @@ function connectTunnel() { , insecure: program.insecure , token: program.token }); + + function sigHandler() { + console.log('SIGINT'); + + // We want to handle cleanup properly unless something is broken in our cleanup process + // that prevents us from exitting, in which case we want the user to be able to send + // the signal again and exit the way it normally would. + process.removeListener('SIGINT', sigHandler); + tun.end(); + } + process.on('SIGINT', sigHandler); } function rawTunnel() { diff --git a/wsclient.js b/wsclient.js index c4225f5..c2506d2 100644 --- a/wsclient.js +++ b/wsclient.js @@ -272,32 +272,27 @@ function run(copts) { } connect(); - function sigHandler() { - console.log('SIGINT'); + return { + end: function() { + retry = false; + if (timeoutId) { + clearTimeout(timeoutId); + timeoutId = null; + } - // We want to handle cleanup properly unless something is broken in our cleanup process - // that prevents us from exitting, in which case we want the user to be able to send - // the signal again and exit the way it normally would. - process.removeListener('SIGINT', sigHandler); - - retry = false; - if (timeoutId) { - clearTimeout(timeoutId); - timeoutId = null; - } - - if (wstunneler) { - try { - wstunneler.close(); - } catch(e) { - console.error("[error] wstunneler.close()"); - console.error(e); + if (wstunneler) { + try { + wstunneler.close(); + } catch(e) { + console.error("[error] wstunneler.close()"); + console.error(e); + } } } - } - process.on('SIGINT', sigHandler); + }; } module.exports.connect = run; +module.exports.createConnection = run; }());