Files: 9216a0ee5a7da1602222d794ef538ca059e1face / lib / local-with-list.js
1568 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 localKeys = new Set() |
25 | |
26 | setInterval(function () { |
27 | Object.keys(lastSeen).forEach((key) => { |
28 | if (Date.now() - lastSeen[key] < 10e3) { |
29 | localKeys.add(key) |
30 | } else { |
31 | localKeys.delete(key) |
32 | delete lastSeen[key] |
33 | } |
34 | }) |
35 | }, 5e3) |
36 | |
37 | local.on('data', function (buf) { |
38 | if (buf.loopback) return |
39 | var data = buf.toString() |
40 | var peer = ref.parseAddress(data) |
41 | if (peer) { |
42 | lastSeen[peer.key] = Date.now() |
43 | sbot.gossip.add(data, 'local') |
44 | } |
45 | }) |
46 | |
47 | setInterval(function () { |
48 | // broadcast self |
49 | // TODO: sign beacons, so that receipient can be confidant |
50 | // that is really your id. |
51 | // (which means they can update their peer table) |
52 | // Oh if this includes your local address, |
53 | // then it becomes unforgeable. |
54 | local.write(sbot.getAddress()) |
55 | }, 1000) |
56 | |
57 | return { |
58 | list: valid.sync(function () { |
59 | return Array.from(localKeys) |
60 | }) |
61 | } |
62 | } |
63 | } |
64 |
Built with git-ssb-web