From 1de642efa4c82549e23dd196b3c31c60d34b551b Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Wed, 5 Oct 2016 23:55:48 -0600 Subject: [PATCH] use Packer utils --- wsclient.js | 30 +++++++----------------------- 1 file changed, 7 insertions(+), 23 deletions(-) diff --git a/wsclient.js b/wsclient.js index 8c647e9..1ab2a29 100644 --- a/wsclient.js +++ b/wsclient.js @@ -3,25 +3,9 @@ var WebSocket = require('ws'); var sni = require('sni'); -var pack = require('tunnel-packer').pack; +var Packer = require('tunnel-packer'); var authenticated = false; -// TODO move these helpers to tunnel-packer package -function addrToId(address) { - return address.family + ',' + address.address + ',' + address.port; -} - -/* -function socketToAddr(socket) { - return { family: socket.remoteFamily, address: socket.remoteAddress, port: socket.remotePort }; -} - -function socketToId(socket) { - return addrToId(socketToAddr(socket)); -} -*/ - - /* var request = require('request'); request.get('https://pokemap.hellabit.com/?access_token=' + token, { rejectUnauthorized: false }, function (err, resp) { @@ -47,7 +31,7 @@ function run(copts) { var handlers = { onmessage: function (opts) { var net = copts.net || require('net'); - var cid = addrToId(opts); + var cid = Packer.addrToId(opts); var service = opts.service; var port = services[service]; var servername; @@ -77,7 +61,7 @@ function run(copts) { if (!servername) { console.info("[error] missing servername for '" + cid + "'", opts.data.byteLength); //console.warn(opts.data.toString()); - wstunneler.send(pack(opts, null, 'error'), { binary: true }); + wstunneler.send(Packer.pack(opts, null, 'error'), { binary: true }); return; } @@ -105,7 +89,7 @@ function run(copts) { chunk = localclients[cid].read(size); //console.log("[<=] local '" + opts.service + "' sent to '" + cid + "' <= ", chunk.byteLength, "bytes"); //console.log(JSON.stringify(chunk.toString())); - wstunneler.send(pack(opts, chunk), { binary: true }); + wstunneler.send(Packer.pack(opts, chunk), { binary: true }); } while (chunk); }); localclients[cid].on('error', function (err) { @@ -117,12 +101,12 @@ function run(copts) { }); } , onend: function (opts) { - var cid = addrToId(opts); + var cid = Packer.addrToId(opts); //console.log("[end] '" + cid + "'"); handlers._onend(cid); } , onerror: function (opts) { - var cid = addrToId(opts); + var cid = Packer.addrToId(opts); //console.log("[error] '" + cid + "'", opts.code || '', opts.message); handlers._onend(cid); } @@ -138,7 +122,7 @@ function run(copts) { } , _onLocalClose: function (cid, opts, err) { try { - wstunneler.send(pack(opts, null, err && 'error' || 'end'), { binary: true }); + wstunneler.send(Packer.pack(opts, null, err && 'error' || 'end'), { binary: true }); } catch(e) { // ignore }