| 
									
										
										
										
											2016-09-29 21:10:40 -04:00
										 |  |  | (function () { | 
					
						
							|  |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var WebSocket = require('ws'); | 
					
						
							|  |  |  | var sni = require('sni'); | 
					
						
							| 
									
										
										
										
											2016-10-05 23:55:48 -06:00
										 |  |  | var Packer = require('tunnel-packer'); | 
					
						
							| 
									
										
										
										
											2016-09-29 21:10:40 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-30 15:04:27 -04:00
										 |  |  | function run(copts) { | 
					
						
							| 
									
										
										
										
											2016-10-11 17:43:24 -06:00
										 |  |  |   var tunnelUrl = copts.stunneld.replace(/\/$/, '') + '/?access_token=' + copts.token; | 
					
						
							| 
									
										
										
										
											2017-04-10 11:39:27 -06:00
										 |  |  |   var activityTimeout = copts.activityTimeout || 2*60*1000; | 
					
						
							|  |  |  |   var pongTimeout = copts.pongTimeout || 10*1000; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-30 15:04:27 -04:00
										 |  |  |   var wstunneler; | 
					
						
							| 
									
										
										
										
											2017-04-07 18:49:52 -06:00
										 |  |  |   var authenticated = false; | 
					
						
							| 
									
										
										
										
											2017-04-07 17:38:55 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-30 15:04:27 -04:00
										 |  |  |   var localclients = {}; | 
					
						
							| 
									
										
										
										
											2017-04-07 17:38:55 -06:00
										 |  |  |   var clientHandlers = { | 
					
						
							|  |  |  |     onClose: function (cid, opts, err) { | 
					
						
							| 
									
										
										
										
											2017-04-07 18:49:52 -06:00
										 |  |  |       wsHandlers.sendMessage(Packer.pack(opts, null, err && 'error' || 'end')); | 
					
						
							| 
									
										
										
										
											2017-04-07 17:38:55 -06:00
										 |  |  |       delete localclients[cid]; | 
					
						
							|  |  |  |       console.log('[local onClose] closed "' + cid + '" (' + clientHandlers.count() + ' clients)'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   , onError: function(cid, opts, err) { | 
					
						
							|  |  |  |       console.info("[local onError] closing '" + cid + "' because '" + err.message + "'"); | 
					
						
							|  |  |  |       clientHandlers.onClose(cid, opts, err); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   , closeSingle: function (cid) { | 
					
						
							|  |  |  |       if (!localclients[cid]) { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-07 18:01:47 -06:00
										 |  |  |       console.log('[closeSingle]', cid); | 
					
						
							| 
									
										
										
										
											2017-04-07 17:38:55 -06:00
										 |  |  |       try { | 
					
						
							|  |  |  |         localclients[cid].end(); | 
					
						
							| 
									
										
										
										
											2017-04-07 18:01:47 -06:00
										 |  |  |         setTimeout(function () { | 
					
						
							|  |  |  |           if (localclients[cid]) { | 
					
						
							|  |  |  |             console.warn('[closeSingle]', cid, 'connection still present'); | 
					
						
							|  |  |  |             delete localclients[cid]; | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         }, 500); | 
					
						
							|  |  |  |       } catch (err) { | 
					
						
							|  |  |  |         console.warn('[closeSingle] failed to close connection', cid, err); | 
					
						
							|  |  |  |         delete localclients[cid]; | 
					
						
							| 
									
										
										
										
											2017-04-07 17:38:55 -06:00
										 |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   , closeAll: function () { | 
					
						
							| 
									
										
										
										
											2017-04-07 18:01:47 -06:00
										 |  |  |       console.log('[closeAll]'); | 
					
						
							| 
									
										
										
										
											2017-04-07 17:38:55 -06:00
										 |  |  |       Object.keys(localclients).forEach(function (cid) { | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |           localclients[cid].end(); | 
					
						
							| 
									
										
										
										
											2017-04-07 18:01:47 -06:00
										 |  |  |         } catch (err) { | 
					
						
							|  |  |  |           console.warn('[closeAll] failed to close connection', cid, err); | 
					
						
							| 
									
										
										
										
											2017-04-07 17:38:55 -06:00
										 |  |  |         } | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2017-04-07 18:01:47 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |       setTimeout(function () { | 
					
						
							|  |  |  |         Object.keys(localclients).forEach(function (cid) { | 
					
						
							|  |  |  |           if (localclients[cid]) { | 
					
						
							|  |  |  |             console.warn('[closeAll]', cid, 'connection still present'); | 
					
						
							|  |  |  |             delete localclients[cid]; | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |       }, 500); | 
					
						
							| 
									
										
										
										
											2017-04-07 17:38:55 -06:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-04-07 18:01:47 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-07 17:38:55 -06:00
										 |  |  |   , count: function () { | 
					
						
							|  |  |  |       return Object.keys(localclients).length; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }; | 
					
						
							| 
									
										
										
										
											2017-04-07 18:01:47 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-07 17:38:55 -06:00
										 |  |  |   var packerHandlers = { | 
					
						
							| 
									
										
										
										
											2016-09-30 15:04:27 -04:00
										 |  |  |     onmessage: function (opts) { | 
					
						
							| 
									
										
										
										
											2016-10-05 23:02:28 -06:00
										 |  |  |       var net = copts.net || require('net'); | 
					
						
							| 
									
										
										
										
											2016-10-05 23:55:48 -06:00
										 |  |  |       var cid = Packer.addrToId(opts); | 
					
						
							| 
									
										
										
										
											2017-04-03 15:11:59 -06:00
										 |  |  |       var service = opts.service.toLowerCase(); | 
					
						
							|  |  |  |       var portList = copts.services[service]; | 
					
						
							| 
									
										
										
										
											2016-09-30 15:04:27 -04:00
										 |  |  |       var servername; | 
					
						
							| 
									
										
										
										
											2017-04-03 15:11:59 -06:00
										 |  |  |       var port; | 
					
						
							| 
									
										
										
										
											2016-09-30 15:04:27 -04:00
										 |  |  |       var str; | 
					
						
							|  |  |  |       var m; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       authenticated = true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (localclients[cid]) { | 
					
						
							|  |  |  |         //console.log("[=>] received data from '" + cid + "' =>", opts.data.byteLength);
 | 
					
						
							|  |  |  |         localclients[cid].write(opts.data); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2017-04-03 15:11:59 -06:00
										 |  |  |       if (!portList) { | 
					
						
							| 
									
										
										
										
											2017-04-07 17:38:55 -06:00
										 |  |  |         packerHandlers._onConnectError(cid, opts, new Error("unsupported service '" + service + "'")); | 
					
						
							| 
									
										
										
										
											2017-04-03 15:11:59 -06:00
										 |  |  |         return; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if ('http' === service) { | 
					
						
							| 
									
										
										
										
											2016-09-30 15:04:27 -04:00
										 |  |  |         str = opts.data.toString(); | 
					
						
							|  |  |  |         m = str.match(/(?:^|[\r\n])Host: ([^\r\n]+)[\r\n]*/im); | 
					
						
							|  |  |  |         servername = (m && m[1].toLowerCase() || '').split(':')[0]; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       else if ('https' === service) { | 
					
						
							|  |  |  |         servername = sni(opts.data); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       else { | 
					
						
							| 
									
										
										
										
											2017-04-03 15:11:59 -06:00
										 |  |  |         servername = '*'; | 
					
						
							| 
									
										
										
										
											2016-09-30 15:04:27 -04:00
										 |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (!servername) { | 
					
						
							|  |  |  |         //console.warn(opts.data.toString());
 | 
					
						
							| 
									
										
										
										
											2017-04-07 17:38:55 -06:00
										 |  |  |         packerHandlers._onConnectError(cid, opts, new Error("missing servername for '" + cid + "' " + opts.data.byteLength)); | 
					
						
							| 
									
										
										
										
											2016-09-30 15:04:27 -04:00
										 |  |  |         return; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-07 17:38:55 -06:00
										 |  |  |       port = portList[servername] || portList['*']; | 
					
						
							| 
									
										
										
										
											2017-04-27 18:38:01 -06:00
										 |  |  |       var createOpts = { | 
					
						
							| 
									
										
										
										
											2016-10-11 17:43:24 -06:00
										 |  |  |         port: port | 
					
						
							| 
									
										
										
										
											2016-10-05 23:02:28 -06:00
										 |  |  |       , host: '127.0.0.1' | 
					
						
							| 
									
										
										
										
											2016-10-11 17:43:24 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |       , servername: servername | 
					
						
							| 
									
										
										
										
											2016-10-05 23:10:16 -06:00
										 |  |  |       , data: opts.data | 
					
						
							| 
									
										
										
										
											2016-10-06 15:01:58 -06:00
										 |  |  |       , remoteFamily: opts.family | 
					
						
							|  |  |  |       , remoteAddress: opts.address | 
					
						
							|  |  |  |       , remotePort: opts.port | 
					
						
							| 
									
										
										
										
											2017-04-27 18:38:01 -06:00
										 |  |  |       }; | 
					
						
							|  |  |  |       localclients[cid] = net.createConnection(createOpts, function () { | 
					
						
							|  |  |  |         // this will happen before 'data' or 'readable' is triggered
 | 
					
						
							|  |  |  |         // We use the data from the createOpts object so that the createConnection function has
 | 
					
						
							|  |  |  |         // the oppurtunity of removing/changing it if it wants/needs to handle it differently.
 | 
					
						
							|  |  |  |         if (createOpts.data) { | 
					
						
							|  |  |  |           localclients[cid].write(createOpts.data); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-09-30 15:04:27 -04:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2017-04-07 17:38:55 -06:00
										 |  |  |       console.info("[connect] new client '" + cid + "' for '" + servername + "' (" + clientHandlers.count() + " clients)"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-05 23:02:28 -06:00
										 |  |  |       localclients[cid].on('readable', function (size) { | 
					
						
							|  |  |  |         var chunk; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-11 17:43:24 -06:00
										 |  |  |         if (!localclients[cid]) { | 
					
						
							|  |  |  |           console.error("[error] localclients[cid]", cid); | 
					
						
							|  |  |  |           return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (!localclients[cid].read) { | 
					
						
							|  |  |  |           console.error("[error] localclients[cid].read", cid); | 
					
						
							|  |  |  |           console.log(localclients[cid]); | 
					
						
							|  |  |  |           return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-05 23:02:28 -06:00
										 |  |  |         do { | 
					
						
							|  |  |  |           chunk = localclients[cid].read(size); | 
					
						
							| 
									
										
										
										
											2016-10-12 13:11:21 -06:00
										 |  |  |           if (chunk) { | 
					
						
							| 
									
										
										
										
											2017-04-07 18:49:52 -06:00
										 |  |  |             wsHandlers.sendMessage(Packer.pack(opts, chunk)); | 
					
						
							| 
									
										
										
										
											2016-10-12 13:11:21 -06:00
										 |  |  |           } | 
					
						
							| 
									
										
										
										
											2016-10-05 23:02:28 -06:00
										 |  |  |         } while (chunk); | 
					
						
							| 
									
										
										
										
											2016-09-30 15:04:27 -04:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2017-04-07 17:38:55 -06:00
										 |  |  |       localclients[cid].on('error', clientHandlers.onError.bind(null, cid, opts)); | 
					
						
							|  |  |  |       localclients[cid].on('end',   clientHandlers.onClose.bind(null, cid, opts)); | 
					
						
							| 
									
										
										
										
											2016-09-30 15:04:27 -04:00
										 |  |  |     } | 
					
						
							|  |  |  |   , onend: function (opts) { | 
					
						
							| 
									
										
										
										
											2016-10-05 23:55:48 -06:00
										 |  |  |       var cid = Packer.addrToId(opts); | 
					
						
							| 
									
										
										
										
											2016-09-30 15:04:27 -04:00
										 |  |  |       //console.log("[end] '" + cid + "'");
 | 
					
						
							| 
									
										
										
										
											2017-04-07 17:38:55 -06:00
										 |  |  |       clientHandlers.closeSingle(cid); | 
					
						
							| 
									
										
										
										
											2016-09-30 15:04:27 -04:00
										 |  |  |     } | 
					
						
							|  |  |  |   , onerror: function (opts) { | 
					
						
							| 
									
										
										
										
											2016-10-05 23:55:48 -06:00
										 |  |  |       var cid = Packer.addrToId(opts); | 
					
						
							| 
									
										
										
										
											2016-09-30 15:04:27 -04:00
										 |  |  |       //console.log("[error] '" + cid + "'", opts.code || '', opts.message);
 | 
					
						
							| 
									
										
										
										
											2017-04-07 17:38:55 -06:00
										 |  |  |       clientHandlers.closeSingle(cid); | 
					
						
							| 
									
										
										
										
											2016-09-30 15:04:27 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-04-07 17:38:55 -06:00
										 |  |  |   , _onConnectError: function (cid, opts, err) { | 
					
						
							|  |  |  |       console.info("[_onConnectError] opening '" + cid + "' failed because " + err.message); | 
					
						
							| 
									
										
										
										
											2017-04-07 18:49:52 -06:00
										 |  |  |       wsHandlers.sendMessage(Packer.pack(opts, null, 'error')); | 
					
						
							| 
									
										
										
										
											2016-09-30 15:04:27 -04:00
										 |  |  |     } | 
					
						
							|  |  |  |   }; | 
					
						
							| 
									
										
										
										
											2017-04-07 18:01:47 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-07 18:49:52 -06:00
										 |  |  |   var retry = true; | 
					
						
							| 
									
										
										
										
											2017-04-10 11:39:27 -06:00
										 |  |  |   var lastActivity; | 
					
						
							|  |  |  |   var timeoutId; | 
					
						
							| 
									
										
										
										
											2016-09-30 15:04:27 -04:00
										 |  |  |   var wsHandlers = { | 
					
						
							| 
									
										
										
										
											2017-04-10 11:39:27 -06:00
										 |  |  |     refreshTimeout: function () { | 
					
						
							|  |  |  |       lastActivity = Date.now(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   , checkTimeout: function () { | 
					
						
							|  |  |  |       if (!wstunneler) { | 
					
						
							|  |  |  |         console.warn('checkTimeout called when websocket already closed'); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       // Determine how long the connection has been "silent", ie no activity.
 | 
					
						
							|  |  |  |       var silent = Date.now() - lastActivity; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // If we have had activity within the last activityTimeout then all we need to do is
 | 
					
						
							|  |  |  |       // call this function again at the soonest time when the connection could be timed out.
 | 
					
						
							|  |  |  |       if (silent < activityTimeout) { | 
					
						
							|  |  |  |         timeoutId = setTimeout(wsHandlers.checkTimeout, activityTimeout-silent); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // Otherwise we check to see if the pong has also timed out, and if not we send a ping
 | 
					
						
							|  |  |  |       // and call this function again when the pong will have timed out.
 | 
					
						
							|  |  |  |       else if (silent < activityTimeout + pongTimeout) { | 
					
						
							|  |  |  |         console.log('pinging tunnel server'); | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |           wstunneler.ping(); | 
					
						
							|  |  |  |         } catch (err) { | 
					
						
							|  |  |  |           console.warn('failed to ping tunnel server', err); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         timeoutId = setTimeout(wsHandlers.checkTimeout, pongTimeout); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // Last case means the ping we sent before didn't get a response soon enough, so we
 | 
					
						
							|  |  |  |       // need to close the websocket connection.
 | 
					
						
							|  |  |  |       else { | 
					
						
							|  |  |  |         console.log('connection timed out'); | 
					
						
							|  |  |  |         wstunneler.close(1000, 'connection timeout'); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   , onOpen: function () { | 
					
						
							| 
									
										
										
										
											2016-09-30 15:04:27 -04:00
										 |  |  |       console.info("[open] connected to '" + copts.stunneld + "'"); | 
					
						
							| 
									
										
										
										
											2017-04-10 11:39:27 -06:00
										 |  |  |       wsHandlers.refreshTimeout(); | 
					
						
							|  |  |  |       timeoutId = setTimeout(wsHandlers.checkTimeout, activityTimeout); | 
					
						
							| 
									
										
										
										
											2016-09-29 21:10:40 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-30 15:04:27 -04:00
										 |  |  |   , onClose: function () { | 
					
						
							| 
									
										
										
										
											2017-04-07 17:38:55 -06:00
										 |  |  |       console.log('ON CLOSE'); | 
					
						
							| 
									
										
										
										
											2017-04-10 11:39:27 -06:00
										 |  |  |       clearTimeout(timeoutId); | 
					
						
							| 
									
										
										
										
											2017-04-07 18:49:52 -06:00
										 |  |  |       wstunneler = null; | 
					
						
							| 
									
										
										
										
											2017-04-07 17:38:55 -06:00
										 |  |  |       clientHandlers.closeAll(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-30 15:04:27 -04:00
										 |  |  |       if (!authenticated) { | 
					
						
							|  |  |  |         console.info('[close] failed on first attempt... check authentication.'); | 
					
						
							| 
									
										
										
										
											2017-04-10 11:39:27 -06:00
										 |  |  |         timeoutId = null; | 
					
						
							| 
									
										
										
										
											2016-09-30 15:04:27 -04:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2017-04-07 18:49:52 -06:00
										 |  |  |       else if (retry) { | 
					
						
							| 
									
										
										
										
											2016-09-30 15:04:27 -04:00
										 |  |  |         console.info('[retry] disconnected and waiting...'); | 
					
						
							| 
									
										
										
										
											2017-04-10 11:39:27 -06:00
										 |  |  |         timeoutId = setTimeout(connect, 5000); | 
					
						
							| 
									
										
										
										
											2016-09-30 15:04:27 -04:00
										 |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-09-30 01:46:00 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-30 15:04:27 -04:00
										 |  |  |   , onError: function (err) { | 
					
						
							|  |  |  |       console.error("[tunnel error] " + err.message); | 
					
						
							| 
									
										
										
										
											2016-09-30 01:23:03 -04:00
										 |  |  |       console.error(err); | 
					
						
							| 
									
										
										
										
											2016-09-30 15:04:27 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-09-30 01:46:00 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-07 18:49:52 -06:00
										 |  |  |   , sendMessage: function (msg) { | 
					
						
							|  |  |  |       if (wstunneler) { | 
					
						
							|  |  |  |         try { | 
					
						
							| 
									
										
										
										
											2017-04-10 11:39:27 -06:00
										 |  |  |           wstunneler.send(msg, {binary: true}); | 
					
						
							| 
									
										
										
										
											2017-04-07 18:49:52 -06:00
										 |  |  |         } catch (err) { | 
					
						
							| 
									
										
										
										
											2017-04-26 17:37:52 -06:00
										 |  |  |           // There is a chance that this occurred after the websocket was told to close
 | 
					
						
							|  |  |  |           // and before it finished, in which case we don't need to log the error.
 | 
					
						
							|  |  |  |           if (wstunneler.readyState !== wstunneler.CLOSING) { | 
					
						
							|  |  |  |             console.warn('[sendMessage] error sending websocket message', err); | 
					
						
							|  |  |  |           } | 
					
						
							| 
									
										
										
										
											2017-04-07 18:49:52 -06:00
										 |  |  |         } | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   function connect() { | 
					
						
							|  |  |  |     if (!retry) { | 
					
						
							|  |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-04-10 11:39:27 -06:00
										 |  |  |     timeoutId = null; | 
					
						
							| 
									
										
										
										
											2017-04-07 18:49:52 -06:00
										 |  |  |     var machine = require('tunnel-packer').create(packerHandlers); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     console.info("[connect] '" + copts.stunneld + "'"); | 
					
						
							|  |  |  |     wstunneler = new WebSocket(tunnelUrl, { rejectUnauthorized: !copts.insecure }); | 
					
						
							|  |  |  |     wstunneler.on('open', wsHandlers.onOpen); | 
					
						
							|  |  |  |     wstunneler.on('close', wsHandlers.onClose); | 
					
						
							|  |  |  |     wstunneler.on('error', wsHandlers.onError); | 
					
						
							| 
									
										
										
										
											2017-04-10 11:39:27 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // Our library will automatically handle sending the pong respose to ping requests.
 | 
					
						
							|  |  |  |     wstunneler.on('ping', wsHandlers.refreshTimeout); | 
					
						
							|  |  |  |     wstunneler.on('pong', wsHandlers.refreshTimeout); | 
					
						
							| 
									
										
										
										
											2017-04-07 18:49:52 -06:00
										 |  |  |     wstunneler.on('message', function (data, flags) { | 
					
						
							| 
									
										
										
										
											2017-04-10 11:39:27 -06:00
										 |  |  |       wsHandlers.refreshTimeout(); | 
					
						
							| 
									
										
										
										
											2017-04-07 18:49:52 -06:00
										 |  |  |       if (data.error || '{' === data[0]) { | 
					
						
							|  |  |  |         console.log(data); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       machine.fns.addChunk(data, flags); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   connect(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-14 16:27:25 -06:00
										 |  |  |   return { | 
					
						
							|  |  |  |     end: function() { | 
					
						
							|  |  |  |       retry = false; | 
					
						
							|  |  |  |       if (timeoutId) { | 
					
						
							|  |  |  |         clearTimeout(timeoutId); | 
					
						
							|  |  |  |         timeoutId = null; | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2017-04-07 18:49:52 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-14 16:27:25 -06:00
										 |  |  |       if (wstunneler) { | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |           wstunneler.close(); | 
					
						
							|  |  |  |         } catch(e) { | 
					
						
							|  |  |  |           console.error("[error] wstunneler.close()"); | 
					
						
							|  |  |  |           console.error(e); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-09-30 01:46:00 -04:00
										 |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-04-14 16:27:25 -06:00
										 |  |  |   }; | 
					
						
							| 
									
										
										
										
											2016-09-30 15:04:27 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-09-30 01:46:00 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-30 15:04:27 -04:00
										 |  |  | module.exports.connect = run; | 
					
						
							| 
									
										
										
										
											2017-04-14 16:27:25 -06:00
										 |  |  | module.exports.createConnection = run; | 
					
						
							| 
									
										
										
										
											2016-09-29 21:10:40 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | }()); |