Files: 8a16b0bd418cece3ea84550e7642fb0e6a0025b8 / plugins / gossip / index.js
7272 bytesRaw
1 | |
2 | var pull = require('pull-stream') |
3 | var Notify = require('pull-notify') |
4 | var mdm = require('mdmanifest') |
5 | var valid = require('../../lib/validators') |
6 | var apidoc = require('../../lib/apidocs').gossip |
7 | var u = require('../../lib/util') |
8 | var ref = require('ssb-ref') |
9 | var ping = require('pull-ping') |
10 | var stats = require('statistics') |
11 | var Schedule = require('./schedule') |
12 | var Init = require('./init') |
13 | var AtomicFile = require('atomic-file') |
14 | var path = require('path') |
15 | var deepEqual = require('deep-equal') |
16 | |
17 | function isFunction (f) { |
18 | return 'function' === typeof f |
19 | } |
20 | |
21 | function stringify(peer) { |
22 | return [peer.host, peer.port, peer.key].join(':') |
23 | } |
24 | |
25 | /* |
26 | Peers : [{ |
27 | key: id, |
28 | host: ip, |
29 | port: int, |
30 | //to be backwards compatible with patchwork... |
31 | announcers: {length: int} |
32 | source: 'pub'|'manual'|'local' |
33 | }] |
34 | */ |
35 | |
36 | |
37 | module.exports = { |
38 | name: 'gossip', |
39 | version: '1.0.0', |
40 | manifest: mdm.manifest(apidoc), |
41 | permissions: { |
42 | anonymous: {allow: ['ping']} |
43 | }, |
44 | init: function (server, config) { |
45 | var notify = Notify() |
46 | var conf = config.gossip || {} |
47 | var home = ref.parseAddress(server.getAddress()) |
48 | |
49 | var stateFile = AtomicFile(path.join(config.path, 'gossip.json')) |
50 | |
51 | //Known Peers |
52 | var peers = [] |
53 | |
54 | function getPeer(id) { |
55 | return u.find(peers, function (e) { |
56 | return e && e.key === id |
57 | }) |
58 | } |
59 | |
60 | var timer_ping = 5*6e4 |
61 | |
62 | var gossip = { |
63 | wakeup: 0, |
64 | peers: function () { |
65 | return peers |
66 | }, |
67 | get: function (addr) { |
68 | addr = ref.parseAddress(addr) |
69 | return u.find(peers, function (a) { |
70 | return ( |
71 | addr.port === a.port |
72 | && addr.host === a.host |
73 | && addr.key === a.key |
74 | ) |
75 | }) |
76 | }, |
77 | connect: valid.async(function (addr, cb) { |
78 | addr = ref.parseAddress(addr) |
79 | if (!addr || typeof addr != 'object') |
80 | return cb(new Error('first param must be an address')) |
81 | |
82 | if(!addr.key) return cb(new Error('address must have ed25519 key')) |
83 | // add peer to the table, incase it isn't already. |
84 | gossip.add(addr, 'manual') |
85 | var p = gossip.get(addr) |
86 | if(!p) return cb() |
87 | |
88 | p.stateChange = Date.now() |
89 | p.state = 'connecting' |
90 | server.connect(p, function (err, rpc) { |
91 | if (err) { |
92 | p.state = undefined |
93 | p.failure = (p.failure || 0) + 1 |
94 | p.stateChange = Date.now() |
95 | notify({ type: 'connect-failure', peer: p }) |
96 | server.emit('log:info', ['SBOT', p.host+':'+p.port+p.key, 'connection failed', err.message || err]) |
97 | p.duration = stats(p.duration, 0) |
98 | return (cb && cb(err)) |
99 | } |
100 | else { |
101 | p.state = 'connected' |
102 | p.failure = 0 |
103 | } |
104 | cb && cb(null, rpc) |
105 | }) |
106 | |
107 | }, 'string|object'), |
108 | |
109 | disconnect: valid.async(function (addr, cb) { |
110 | var peer = this.get(addr) |
111 | |
112 | peer.state = 'disconnecting' |
113 | peer.stateChange = Date.now() |
114 | if(!peer || !peer.disconnect) cb && cb() |
115 | else peer.disconnect(null, function (err) { |
116 | peer.stateChange = Date.now() |
117 | }) |
118 | |
119 | }, 'string|object'), |
120 | |
121 | changes: function () { |
122 | return notify.listen() |
123 | }, |
124 | //add an address to the peer table. |
125 | add: valid.sync(function (addr, source) { |
126 | addr = ref.parseAddress(addr) |
127 | if(!ref.isAddress(addr)) |
128 | throw new Error('not a valid address:' + JSON.stringify(addr)) |
129 | // check that this is a valid address, and not pointing at self. |
130 | |
131 | if(addr.key === home.key) return |
132 | if(addr.host === home.host && addr.port === home.port) return |
133 | |
134 | var f = gossip.get(addr) |
135 | |
136 | if(!f) { |
137 | // new peer |
138 | addr.source = source |
139 | addr.announcers = 1 |
140 | addr.duration = addr.duration || null |
141 | peers.push(addr) |
142 | notify({ type: 'discover', peer: addr, source: source || 'manual' }) |
143 | return addr |
144 | } else if (source === 'friends' || source === 'local') { |
145 | // this peer is a friend or local, override old source to prioritize gossip |
146 | f.source = source |
147 | } |
148 | //don't count local over and over |
149 | else if(f.source != 'local') |
150 | f.announcers ++ |
151 | |
152 | return f |
153 | }, 'string|object', 'string?'), |
154 | ping: function (opts) { |
155 | var timeout = config.timers && config.timers.ping || 5*60e3 |
156 | //between 10 seconds and 30 minutes, default 5 min |
157 | timeout = Math.max(10e3, Math.min(timeout, 30*60e3)) |
158 | return ping({timeout: timeout}) |
159 | }, |
160 | reconnect: function () { |
161 | for(var id in server.peers) |
162 | if(id !== server.id) //don't disconnect local client |
163 | server.peers[id].forEach(function (peer) { |
164 | peer.close(true) |
165 | }) |
166 | return gossip.wakeup = Date.now() |
167 | } |
168 | } |
169 | |
170 | Schedule (gossip, config, server) |
171 | Init (gossip, config, server) |
172 | //get current state |
173 | |
174 | server.on('rpc:connect', function (rpc, isClient) { |
175 | var peer = getPeer(rpc.id) |
176 | //don't track clients that connect, but arn't considered peers. |
177 | //maybe we should though? |
178 | if(!peer) return |
179 | console.log('Connected', stringify(peer)) |
180 | //means that we have created this connection, not received it. |
181 | peer.client = !!isClient |
182 | peer.state = 'connected' |
183 | peer.stateChange = Date.now() |
184 | peer.disconnect = function (err, cb) { |
185 | if(isFunction(err)) cb = err, err = null |
186 | rpc.close(err, cb) |
187 | } |
188 | |
189 | if(isClient) { |
190 | //default ping is 5 minutes... |
191 | var pp = ping({serve: true, timeout: timer_ping}, function (_) {}) |
192 | peer.ping = {rtt: pp.rtt, skew: pp.skew} |
193 | pull( |
194 | pp, |
195 | rpc.gossip.ping({timeout: timer_ping}, function (err) { |
196 | if(err.name === 'TypeError') peer.ping.fail = true |
197 | }), |
198 | pp |
199 | ) |
200 | } |
201 | |
202 | rpc.on('closed', function () { |
203 | console.log('Disconnected', stringify(peer)) |
204 | //track whether we have successfully connected. |
205 | //or how many failures there have been. |
206 | var since = peer.stateChange |
207 | peer.stateChange = Date.now() |
208 | // if(peer.state === 'connected') //may be "disconnecting" |
209 | peer.duration = stats(peer.duration, peer.stateChange - since) |
210 | // console.log(peer.duration) |
211 | peer.state = undefined |
212 | notify({ type: 'disconnect', peer: peer }) |
213 | server.emit('log:info', ['SBOT', rpc.id, 'disconnect']) |
214 | }) |
215 | |
216 | notify({ type: 'connect', peer: peer }) |
217 | }) |
218 | |
219 | var last |
220 | stateFile.get(function (err, ary) { |
221 | last = ary || [] |
222 | if(Array.isArray(ary)) |
223 | ary.forEach(function (v) { |
224 | delete v.state |
225 | // don't add local peers (wait to rediscover) |
226 | if(v.source !== 'local') { |
227 | gossip.add(v, 'stored') |
228 | } |
229 | }) |
230 | }) |
231 | |
232 | var int = setInterval(function () { |
233 | var copy = JSON.parse(JSON.stringify(peers)) |
234 | copy.forEach(function (e) { |
235 | delete e.state |
236 | }) |
237 | if(deepEqual(copy, last)) return |
238 | last = copy |
239 | stateFile.set(copy, function(err) { |
240 | if (err) console.log(err) |
241 | }) |
242 | }, 10*1000) |
243 | |
244 | if(int.unref) int.unref() |
245 | |
246 | return gossip |
247 | } |
248 | } |
249 |
Built with git-ssb-web