Files: 78f95a6d1fe17f7747c2076c9ca3c86762b5e7f5 / plugins / local.js
1374 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 | |
16 | var local = broadcast(config.port) |
17 | var addrs = {} |
18 | var lastSeen = {} |
19 | |
20 | // cleanup old local peers |
21 | setInterval(function () { |
22 | Object.keys(lastSeen).forEach((key) => { |
23 | if (Date.now() - lastSeen[key] > 10e3) { |
24 | sbot.gossip.remove(addrs[key]) |
25 | delete lastSeen[key] |
26 | } |
27 | }) |
28 | }, 5e3) |
29 | |
30 | // discover new local peers |
31 | local.on('data', function (buf) { |
32 | if (buf.loopback) return |
33 | var data = buf.toString() |
34 | var peer = ref.parseAddress(data) |
35 | if (peer && peer.key !== sbot.id) { |
36 | addrs[peer.key] = peer |
37 | lastSeen[peer.key] = Date.now() |
38 | sbot.gossip.add(data, 'local') |
39 | } |
40 | }) |
41 | |
42 | // broadcast self |
43 | setInterval(function () { |
44 | // TODO: sign beacons, so that receipient can be confidant |
45 | // that is really your id. |
46 | // (which means they can update their peer table) |
47 | // Oh if this includes your local address, |
48 | // then it becomes unforgeable. |
49 | local.write(sbot.getAddress()) |
50 | }, 1000) |
51 | } |
52 | } |
53 |
Built with git-ssb-web