Files: 38b0812f2f3a84faff8a7c371f0d54fe34ad12c8 / lib / local-with-list.js
1683 bytesRaw
1 | // FROM: https://github.com/ssbc/scuttlebot/blob/master/plugins/local.js |
2 | |
3 | var broadcast = require('broadcast-stream') |
4 | var ref = require('ssb-ref') |
5 | var valid = require('scuttlebot/lib/validators') |
6 | |
7 | // local plugin |
8 | // broadcasts the address:port:pubkey triple of the sbot server |
9 | // on the LAN, using multicast UDP |
10 | |
11 | function isFunction (f) { |
12 | return 'function' === typeof f |
13 | } |
14 | |
15 | module.exports = { |
16 | name: 'local', |
17 | version: '2.0.0', |
18 | manifest: { |
19 | list: 'sync' |
20 | }, |
21 | init: function (sbot, config) { |
22 | var local = broadcast(config.port) |
23 | var lastSeen = {} |
24 | var addrs = {} |
25 | var localKeys = new Set() |
26 | |
27 | setInterval(function () { |
28 | Object.keys(lastSeen).forEach((key) => { |
29 | if (Date.now() - lastSeen[key] < 10e3) { |
30 | localKeys.add(key) |
31 | } else { |
32 | localKeys.delete(key) |
33 | sbot.gossip.delete(addrs[key]) |
34 | delete lastSeen[key] |
35 | } |
36 | }) |
37 | }, 5e3) |
38 | |
39 | local.on('data', function (buf) { |
40 | if (buf.loopback) return |
41 | var data = buf.toString() |
42 | var peer = ref.parseAddress(data) |
43 | if (peer && peer.key !== sbot.id) { |
44 | addrs[peer.key] = peer |
45 | lastSeen[peer.key] = Date.now() |
46 | sbot.gossip.add(data, 'local') |
47 | } |
48 | }) |
49 | |
50 | setInterval(function () { |
51 | // broadcast self |
52 | // TODO: sign beacons, so that receipient can be confidant |
53 | // that is really your id. |
54 | // (which means they can update their peer table) |
55 | // Oh if this includes your local address, |
56 | // then it becomes unforgeable. |
57 | local.write(sbot.getAddress()) |
58 | }, 1000) |
59 | |
60 | return { |
61 | list: valid.sync(function () { |
62 | return Array.from(localKeys) |
63 | }) |
64 | } |
65 | } |
66 | } |
67 |
Built with git-ssb-web