-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbugout.no.rpc.no.encryption.js
437 lines (396 loc) · 12.7 KB
/
bugout.no.rpc.no.encryption.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
module.exports = Bugout;
var debug = console.log;
var WebTorrent = require("webtorrent");
var bencode = require("bencode");
var nacl = require("tweetnacl");
var EventEmitter = require("events").EventEmitter;
var inherits = require("inherits");
var bs58 = require("bs58");
var bs58check = require("bs58check");
var ripemd160 = require("ripemd160");
inherits(Bugout, EventEmitter);
var EXT = "bo_channel";
var PEERTIMEOUT = 5 * 60 * 1000;
var SEEDPREFIX = "490a";
var ADDRESSPREFIX = "55";
/**
* Multi-party data channels on WebTorrent extension.
*/
function Bugout(identifier, opts) {
if (identifier && typeof identifier == "object") {
opts = identifier;
identifier = null;
}
var opts = opts || {};
if (!(this instanceof Bugout)) return new Bugout(identifier, opts);
var trackeropts = opts.tracker || {};
trackeropts.getAnnounceOpts =
trackeropts.getAnnounceOpts ||
function() {
return { numwant: 4 };
};
if (opts.iceServers) {
trackeropts.rtcConfig = { iceServers: opts.iceServers };
}
this.announce = opts.announce || [
"wss://hub.bugout.link",
"wss://tracker.openwebtorrent.com"
];
this.wt = opts.wt || new WebTorrent({ tracker: trackeropts });
this.nacl = nacl;
if (opts["seed"]) {
this.seed = opts["seed"];
} else {
//random seed
this.seed = this.encodeseed(nacl.randomBytes(32));
}
this.timeout = opts["timeout"] || PEERTIMEOUT; //5 minutes
this.keyPair =
opts["keyPair"] ||
nacl.sign.keyPair.fromSeed(
Uint8Array.from(bs58check.decode(this.seed)).slice(2)
);
this.pk = bs58.encode(Buffer.from(this.keyPair.publicKey));
this.identifier = identifier || this.address();
this.peers = {}; // list of peers seen recently: address -> pk, timestamp
this.seen = {}; // messages we've seen recently: hash -> timestamp
this.lastwirecount = null;
// pending callback functions
this.callbacks = {};
this.serveraddress = null;
this.heartbeattimer = null;
debug("address", this.address());
debug("identifier", this.identifier);
debug("public key", this.pk);
if (typeof File == "object") {
var blob = new File([this.identifier], this.identifier);
} else {
var blob = new Buffer.from(this.identifier);
blob.name = this.identifier;
}
//seeding the webtorrent is where the magic happens
var torrent = this.wt.seed(
blob,
{ name: this.identifier, announce: this.announce },
//function onseed(torrent)
partial(function(bugout, torrent) {
// debug("torrent", bugout.identifier, torrent);
debug("torrent", "torrent.name", torrent.name);
debug(
"torrent.infoHash",
torrent.infoHash,
"torrent.magnetURI",
torrent.magnetURI
);
bugout.emit("torrent", bugout.identifier, torrent);
//using torrent discovery API
if (torrent.discovery.tracker) {
torrent.discovery.tracker.on("update", function(update) {
bugout.emit("tracker", bugout.identifier, update);
});
}
torrent.discovery.on("trackerAnnounce", function() {
bugout.emit("announce", bugout);
bugout.connections();
});
}, this)
);
// Emitted whenever a new peer is connected for this torrent.
torrent.on("wire", partial(attach, this, this.identifier));
console.log("connected to peer with identifier " + this.identifier);
this.torrent = torrent;
if (opts.heartbeat) {
this.heartbeat(opts.heartbeat);
}
}
Bugout.prototype.WebTorrent = WebTorrent;
//I wonder why he is encoding the seed, I don't think it was needed
Bugout.encodeseed = Bugout.prototype.encodeseed = function(material) {
return bs58check.encode(
Buffer.concat([Buffer.from(SEEDPREFIX, "hex"), Buffer.from(material)])
);
};
//I also don't understand why he is encoding the heartbeat
Bugout.encodeaddress = Bugout.prototype.encodeaddress = function(material) {
return bs58check.encode(
Buffer.concat([
Buffer.from(ADDRESSPREFIX, "hex"),
new ripemd160().update(Buffer.from(nacl.hash(material))).digest()
])
);
};
// smart way of removing old peers
// start a heartbeat and expire old "seen" peers who don't send us a heartbeat
Bugout.prototype.heartbeat = function(interval) {
var interval = interval || 30000;
this.heartbeattimer = setInterval(
partial(function(bugout) {
// broadcast a 'ping' message
bugout.ping();
var t = now();
// remove any 'peers' entries with timestamps older than timeout
for (var p in bugout.peers) {
var pk = bugout.peers[p].pk;
var address = bugout.address(pk);
var last = bugout.peers[p].last;
if (last + bugout.timeout < t) {
delete bugout.peers[p];
bugout.emit("timeout", address);
bugout.emit("left", address);
}
}
}, this),
interval
);
};
// cleaning up means removing the torrent
// clean up this bugout instance
Bugout.prototype.destroy = function(cb) {
clearInterval(this.heartbeattimer);
var packet = makePacket(this, { y: "x" });
sendRaw(this, packet);
this.wt.remove(this.torrent, cb);
};
Bugout.prototype.close = Bugout.prototype.destroy;
Bugout.prototype.connections = function() {
if (this.torrent.wires.length != this.lastwirecount) {
this.lastwirecount = this.torrent.wires.length;
this.emit("connections", this.torrent.wires.length);
}
return this.lastwirecount;
};
// This is where this.address() goes
// So it encodes your public key, which is also your address?
Bugout.prototype.address = function(pk) {
if (pk && typeof pk == "string") {
pk = bs58.decode(pk);
} else if (pk && pk.length == 32) {
pk = pk;
} else {
pk = this.keyPair.publicKey;
}
return this.encodeaddress(pk);
};
Bugout.address = Bugout.prototype.address;
Bugout.prototype.ping = function() {
// send a ping out so they know about us too
var packet = makePacket(this, { y: "p" });
sendRaw(this, packet);
};
Bugout.prototype.send = function(address, message) {
if (!message) {
var message = address;
var address = null;
}
var packet = makePacket(this, { y: "m", v: JSON.stringify(message) });
sendRaw(this, packet);
};
// outgoing
function makePacket(bugout, params) {
var p = {
t: now(),
i: bugout.identifier,
pk: bugout.pk,
n: nacl.randomBytes(8)
};
for (var k in params) {
p[k] = params[k];
}
pe = bencode.encode(p);
return bencode.encode({
s: nacl.sign.detached(pe, bugout.keyPair.secretKey),
p: pe
});
}
//So you need to send a message over the wires?
//*And* use the extension that he made, called bo_channel?
function sendRaw(bugout, message) {
var wires = bugout.torrent.wires;
//for each wire
for (var w = 0; w < wires.length; w++) {
//get the key "peerExtendedHankshake"
var extendedhandshake = wires[w]["peerExtendedHandshake"];
if (extendedhandshake && extendedhandshake.m && extendedhandshake.m[EXT]) {
//This is where the magic happens
//See github.com/webtorrent/bittorrent-protocol and http://www.bittorrent.org/beps/bep_0010.html
//The explanation is a bit confusing though
wires[w].extended(EXT, message);
}
}
var hash = toHex(nacl.hash(message).slice(16)); //pure debug value
debug("sent", hash, "to", wires.length, "wires"); //for this log
}
// incoming -- this is where message unpacking happens and where you can see his message packing scheme the best
// message types: (m)essage, (r)pc, (r)pc (r)esponse, (p)ing, (x)rossed out/leave/split/kruisje
function onMessage(bugout, identifier, wire, message) {
// hash to reference incoming message
var hash = toHex(nacl.hash(message).slice(16));
var t = now();
debug("raw message", identifier, message.length, hash);
if (!bugout.seen[hash]) {
var unpacked = bencode.decode(message); //he needs to decode bencode, because that is how the bittorrent protocol communicates, I think...
if (unpacked && unpacked.p) {
debug(
"unpacked message"
// unpacked
);
var packet = bencode.decode(unpacked.p);
var pk = packet.pk.toString();
var id = packet.i.toString();
var checksig = nacl.sign.detached.verify(
unpacked.p,
unpacked.s,
bs58.decode(pk)
);
var checkid = id == identifier;
var checktime = packet.t + bugout.timeout > t;
debug(
"packet"
// packet
);
if (checksig && checkid && checktime) {
//note that this means the sender is pinged back
sawPeer(bugout, pk, identifier);
// check packet types
// m stands for message
if (packet.y == "m") {
debug(
"message",
identifier
// packet
);
var messagestring = packet.v.toString();
var messagejson = null;
try {
var messagejson = JSON.parse(messagestring);
} catch (e) {
debug("Malformed message JSON: " + messagestring);
}
if (messagejson) {
bugout.emit("message", bugout.address(pk), messagejson, packet);
}
}
// p stands for ping
else if (packet.y == "p") {
var address = bugout.address(pk);
debug("ping from", address);
bugout.emit("ping", address);
}
// x stands for split/leave
else if (packet.y == "x") {
var address = bugout.address(pk);
debug("got left from", address);
delete bugout.peers[address];
bugout.emit("left", address);
} else {
// TODO: handle ping/keep-alive message
debug("unknown packet type");
}
} else {
debug("dropping bad packet", hash, checksig, checkid, checktime);
}
} else {
debug("skipping packet with no payload", hash, unpacked);
}
// forward first-seen message to all connected wires
// TODO: block flooders
sendRaw(bugout, message);
} else {
debug("already seen", hash);
}
// refresh last-seen timestamp on this message
bugout.seen[hash] = now();
}
// network functions
function sawPeer(bugout, pk, identifier) {
debug("sawPeer", bugout.address(pk));
var t = now();
var address = bugout.address(pk);
// ignore ourself
if (address != bugout.address()) {
// if we haven't seen this peer for a while
if (
!bugout.peers[address] ||
bugout.peers[address].last + bugout.timeout < t
) {
bugout.peers[address] = {
pk: pk,
last: t
};
debug("seen", bugout.address(pk));
bugout.emit("seen", bugout.address(pk));
if (bugout.address(pk) == bugout.identifier) {
bugout.serveraddress = address;
debug("seen server", bugout.address(pk));
bugout.emit("server", bugout.address(pk));
}
// send a ping out so they know about us too
var packet = makePacket(bugout, { y: "p" });
sendRaw(bugout, packet);
} else {
bugout.peers[address].last = t;
}
}
}
// extension protocol plumbing
// see also https://github.com/webtorrent/ut_metadata/blob/master/index.js for another example
function attach(bugout, identifier, wire, addr) {
debug("saw wire", wire.peerId, addr);
wire.use(extension(bugout, identifier, wire));
wire.on("close", partial(detach, bugout, identifier, wire));
}
function detach(bugout, identifier, wire) {
debug("wire left", wire.peerId, identifier);
bugout.emit("wireleft", bugout.torrent.wires.length, wire);
bugout.connections();
}
// I need to debug this pure magic -- Melvin
function extension(bugout, identifier, wire) {
var ext = partial(wirefn, bugout, identifier);
ext.prototype.name = EXT;
ext.prototype.onExtendedHandshake = partial(
onExtendedHandshake,
bugout,
identifier,
wire
);
ext.prototype.onMessage = partial(onMessage, bugout, identifier, wire);
return ext;
}
function wirefn(bugout, identifier, wire) {
// TODO: sign handshake to prove key custody
wire.extendedHandshake.id = identifier;
wire.extendedHandshake.pk = bugout.pk;
}
function onExtendedHandshake(bugout, identifier, wire, handshake) {
debug(
"wire extended handshake",
bugout.address(handshake.pk.toString()),
wire.peerId
// handshake
);
bugout.emit("wireseen", bugout.torrent.wires.length, wire);
bugout.connections();
// TODO: check sig ðfnd drop on failure - wire.peerExtendedHandshake
sawPeer(bugout, handshake.pk.toString(), identifier);
}
// utility fns
function now() {
return new Date().getTime();
}
// https://stackoverflow.com/a/39225475/2131094
function toHex(x) {
return x.reduce(function(memo, i) {
return memo + ("0" + i.toString(16)).slice(-2);
}, "");
}
// javascript why
function partial(fn) {
var slice = Array.prototype.slice;
var stored_args = slice.call(arguments, 1);
return function() {
var new_args = slice.call(arguments);
var args = stored_args.concat(new_args);
return fn.apply(null, args);
};
}