git ssb

4+

Dominic / scuttlebot



Tree: 7975a4e61e24ba1ff579ecd2d276b1fc5a4514f6

Files: 7975a4e61e24ba1ff579ecd2d276b1fc5a4514f6 / plugins / local.js

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

Built with git-ssb-web