Files: b3923852a5471785a61c33094414b62fa845b7b5 / plugins / local.js
1442 bytesRaw
1 | var broadcast = require('broadcast-stream') |
2 | var ref = require('ssb-ref') |
3 | // local plugin |
4 | // broadcasts the address:port:pubkey triple of the sbot server |
5 | // on the LAN, using multicast UDP |
6 | |
7 | function isFunction (f) { |
8 | return 'function' === typeof f |
9 | } |
10 | |
11 | module.exports = { |
12 | name: 'local', |
13 | version: '2.0.0', |
14 | init: function (sbot, config) { |
15 | if(config.gossip && config.gossip.local === false) |
16 | return |
17 | |
18 | var local = broadcast(config.port) |
19 | var addrs = {} |
20 | var lastSeen = {} |
21 | |
22 | // cleanup old local peers |
23 | setInterval(function () { |
24 | Object.keys(lastSeen).forEach((key) => { |
25 | if (Date.now() - lastSeen[key] > 10e3) { |
26 | sbot.gossip.remove(addrs[key]) |
27 | delete lastSeen[key] |
28 | } |
29 | }) |
30 | }, 5e3) |
31 | |
32 | // discover new local peers |
33 | local.on('data', function (buf) { |
34 | if (buf.loopback) return |
35 | var data = buf.toString() |
36 | var peer = ref.parseAddress(data) |
37 | if (peer && peer.key !== sbot.id) { |
38 | addrs[peer.key] = peer |
39 | lastSeen[peer.key] = Date.now() |
40 | sbot.gossip.add(data, 'local') |
41 | } |
42 | }) |
43 | |
44 | // broadcast self |
45 | setInterval(function () { |
46 | // TODO: sign beacons, so that receipient can be confidant |
47 | // that is really your id. |
48 | // (which means they can update their peer table) |
49 | // Oh if this includes your local address, |
50 | // then it becomes unforgeable. |
51 | local.write(sbot.getAddress()) |
52 | }, 1000) |
53 | } |
54 | } |
55 |
Built with git-ssb-web