|
| 1 | +const TLSClient = require('sendy-axolotl') |
| 2 | +const Sendy = require('sendy') |
| 3 | +const SendyWS = require('sendy-ws') |
| 4 | +const newSwitchboard = SendyWS.Switchboard |
| 5 | +const WebSocketClient = SendyWS.Client |
| 6 | +const tradle = require('@tradle/engine') |
| 7 | +// const utils = require('./utils') |
| 8 | + |
| 9 | +module.exports = function createConnector (node) { |
| 10 | + const connector = { |
| 11 | + myIdentifier: function (tls) { |
| 12 | + return connector.getIdentifier(node.identity, tls) |
| 13 | + }, |
| 14 | + |
| 15 | + getIdentifier: function (identity, tls) { |
| 16 | + const pk = tls ? tradle.utils.find(identity.pubkeys, k => { |
| 17 | + return k.type === 'ec' && k.purpose === 'tls' |
| 18 | + }) : tradle.utils.find(identity.pubkeys, k => { |
| 19 | + return k.type === 'ec' && k.purpose === 'sign' |
| 20 | + }) |
| 21 | + |
| 22 | + return tradle.utils.serializePubKey(pk).toString('hex') |
| 23 | + }, |
| 24 | + |
| 25 | + parseIdentifier: function (identifier) { |
| 26 | + const pubKey = tradle.utils.unserializePubKey(new Buffer(identifier, 'hex')) |
| 27 | + pubKey.pub = new Buffer(pubKey.pub, 'hex') |
| 28 | + return pubKey |
| 29 | + }, |
| 30 | + |
| 31 | + networkingStack: function (opts) { |
| 32 | + const url = opts.url |
| 33 | + const tlsEnabled = opts.tls |
| 34 | + const webSocketClient = new WebSocketClient({ |
| 35 | + url: url, |
| 36 | + autoConnect: true, |
| 37 | + // for now, till we figure out why binary |
| 38 | + // doesn't work (socket.io parser errors on decode) |
| 39 | + // forceBase64: true |
| 40 | + }) |
| 41 | + |
| 42 | + webSocketClient.on('disconnect', function () { |
| 43 | + switchboard.clients().forEach(function (c) { |
| 44 | + // reset OTR session, restart on connect |
| 45 | + debug('aborting pending sends due to disconnect') |
| 46 | + c.destroy() |
| 47 | + }) |
| 48 | + |
| 49 | + // pause all channels |
| 50 | + node.sender.pause() |
| 51 | + }) |
| 52 | + |
| 53 | + webSocketClient.on('connect', function (recipient) { |
| 54 | + // resume all paused channels |
| 55 | + node.sender.resume() |
| 56 | + }) |
| 57 | + |
| 58 | + const tlsKey = tlsEnabled && tradle.utils.find(node.keys, key => { |
| 59 | + return key.get('purpose') === 'tls' |
| 60 | + }) |
| 61 | + |
| 62 | + const switchboard = newSwitchboard({ |
| 63 | + identifier: connector.myIdentifier(tlsEnabled), |
| 64 | + unreliable: webSocketClient, |
| 65 | + clientForRecipient: function (recipient) { |
| 66 | + const sendy = new Sendy(opts) |
| 67 | + if (!tlsKey) return sendy |
| 68 | + |
| 69 | + return new TLSClient({ |
| 70 | + key: { |
| 71 | + secretKey: tlsKey.priv, |
| 72 | + publicKey: tlsKey.pub |
| 73 | + }, |
| 74 | + client: sendy, |
| 75 | + theirPubKey: new Buffer(connector.parseIdentifier(recipient).pub, 'hex') |
| 76 | + }) |
| 77 | + } |
| 78 | + }) |
| 79 | + |
| 80 | + switchboard.on('timeout', function (identifier) { |
| 81 | + switchboard.cancelPending(identifier) |
| 82 | + }) |
| 83 | + |
| 84 | + node._send = function (msg, recipientInfo, cb) { |
| 85 | + const identifier = connector.getIdentifier(recipientInfo.object, tlsEnabled) |
| 86 | + switchboard.send(identifier, msg, cb) |
| 87 | + } |
| 88 | + |
| 89 | + switchboard.on('message', function (msg, sender) { |
| 90 | + const pubKey = connector.parseIdentifier(sender) |
| 91 | + node.receive(msg, { pubKey }) |
| 92 | + }) |
| 93 | + |
| 94 | + return { |
| 95 | + switchboard, |
| 96 | + webSocketClient |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + return connector |
| 102 | +} |
0 commit comments