x.receive = function(packet){ if(!lob.isPacket(packet) || packet.head.length !== 0) return (x.err='invalid packet')&&false; if(!x.session) return (x.err='handshake sync required')&&false; var inner = x.session.decrypt(packet.body.slice(16)); if(!inner) return (x.err='decrypt failed: '+x.session.err)&&false; return lob.decode(inner); };
function receive_decloak(packet, pipe){ if(!lob.isPacket(packet) && !(packet = lob.decloak(packet))) return new Error('invalid packet ' + typeof packet); if(packet.cloaked) pipe.cloaked = true; return packet; }
pipe.send = function() { pipe.keepalive(); pipe.sentAt = Date.now(); if(typeof pipe.onSend != 'function') return console.log('internal error, no pipe.onSend',pipe); // automatically add cloaking if(pipe.cloaked && lob.isPacket(arguments[0])) arguments[0] = lob.cloak(arguments[0]); pipe.onSend.apply(pipe,arguments); }
x.send = function(inner, arg){ if(typeof inner != 'object') return (x.err='invalid inner packet')&&false; if(!x.sending) return (x.err='send with no sending handler')&&false; if(!x.session) return (x.err='send with no session')&&false; if(!lob.isPacket(inner)) inner = lob.packet(inner.json,inner.body); // convenience self.debug('channel encrypting',inner.json,inner.body.length); var enc = x.session.encrypt(inner); if(!enc) return (x.err='session encryption failed: '+x.session.err)&&false; // use senders token for routing var packet = lob.packet(null,Buffer.concat([x.session.token,enc])) if(typeof x.sending == 'function') x.sending(packet, arg); return packet; };