-
Notifications
You must be signed in to change notification settings - Fork 1
/
torrent_context.js
381 lines (336 loc) · 12.2 KB
/
torrent_context.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
var Model = require('./model');
var BEnc = require('benc');
var URL = require('url');
var http = require('http');
var TM = require('./torrent_manager');
var Peer = require('./peer');
var PieceMap = require('piecemap');
var TorrentStream = require('./torrent_stream');
var TorrentContext = module.exports = function(tm, infoHex) {
var that = this;
this.tm = tm;
this.infoHex = infoHex;
this.infoHash = hexToBin(infoHex);
this.peerId = generatePeerId();
this.trackers = {};
this.bytesDownloaded = 0;
this.peers = {};
this.size = 0;
this.streams = [];
this.lastActivity = Date.now();
Model.getFileinfo(infoHex, function(error, fileinfo) {
if (error)
throw error;
that.pieceLength = fileinfo.pieceLength;
fileinfo.files.forEach(function(file) {
that.size += file.length;
});
that.pieceNum = Math.ceil(that.size / fileinfo.pieceLength);
that.piecemap = new PieceMap(that.pieceNum);
});
this.tryAnnounce();
this.announceInterval =
setInterval(function() {
if (that.streams.length > 0) {
// Force a tracker request after 10s idleness,
// don't bore our visitors.
// This really puts load on the trackers if many
// people were doing this. Luckily they don't.
var force = that.lastActivity && (that.lastActivity < Date.now() - 10 * 1000);
that.tryAnnounce(force);
}
}, 2000);
};
TorrentContext.prototype.close = function() {
for(var host in this.peers) {
clearInterval(this.announceInterval);
if (this.workPeersTimeout)
clearTimeout(this.workPeersTimeout);
this.peers[host].close();
}
};
TorrentContext.prototype.getStats = function() {
var stats = { peers: { total: 0, connected: 0 },
downloaded: this.bytesDownloaded,
streams: this.streams.length
};
for(var host in this.peers)
if (this.peers.hasOwnProperty(host)) {
stats.peers.total++;
if (this.peers[host].state == 'connected')
stats.peers.connected++;
}
return stats;
};
TorrentContext.prototype.addPeer = function(host, port) {
if (!this.peers.hasOwnProperty(host)) {
var peer = this.peers[host] = new Peer(this,
{ host: host,
port: port });
this.canWorkPeers();
}
};
TorrentContext.prototype.addIncomingPeer = function(sock, wire) {
// TODO: fix this, and the same in Peer() too
var host = sock.remoteAddress;
if (this.peers.hasOwnProperty(host))
this.peers[host].close();
this.peers[host] = new Peer(this, { socket: sock,
wire: wire });
this.canWorkPeers();
this.onActivity();
};
TorrentContext.prototype.canWorkPeers = function() {
var that = this;
if (!this.workPeersScheduled) {
this.workPeersTimeout =
setTimeout(function() {
that.workPeersScheduled = false;
delete that.workPeersTimeout;
that.workPeers();
}, 50);
this.workPeersScheduled = true;
}
};
TorrentContext.prototype.workPeers = function() {
var didSomething = false, host, connectedPeers = 0;
for(host in this.peers) {
if (this.hasOwnProperty(host) &&
this.peers[host].state == 'connected')
connectedPeers++;
}
// allow a measure of 20 peers per stream, plus 20 for readiness
var allowedPeers = this.streams.length * 20 + 20;
if (connectedPeers < allowedPeers) {
// Moar pls
for(host in this.peers) {
if (this.peers.hasOwnProperty(host)) {
if (this.peers[host].canConnect()) {
this.peers[host].connect();
didSomething = true;
break;
}
}
}
} else if (connectedPeers > allowedPeers * 1.2) {
// Fed up!
var toKick = null;
for(var host in this.peers)
if (this.peers.hasOwnProperty(host)) {
if (this.peers[host].state == 'connected' &&
(!toKick || this.peers[host].score < toKick.score))
toKick = this.peers[host];
}
if (toKick)
toKick.close();
}
if (didSomething) {
this.onInfoUpdate();
this.canWorkPeers();
}
};
TorrentContext.prototype.workStreams = function() {
var that = this;
this.streams.forEach(function(stream) {
var desire = stream.nextDesired();
if (desire) {
var index = Math.floor(desire.offset / that.pieceLength);
var peer = that.getPieceCandidate(index);
if (peer) {
peer.requestPiece(index, desire.offset % that.pieceLength, desire.length);
desire.requested();
}
}
});
};
TorrentContext.prototype.onActivity = function() {
this.lastActivity = Date.now();
this.workStreams();
this.onInfoUpdate();
};
TorrentContext.prototype.onInfoUpdate = function() {
if (this.infoWaiters) {
info = { peers: { total: 0 },
downloaded: this.bytesDownloaded };
this.peers.forEach(function(peer) {
if (!info.peers.hasOwnProperty(peer.state))
info.peers[peer.state] = 0;
info.peers[peer.state]++;
info.peers.total++;
});
this.infoWaiters.forEach(function(cb) {
cb(info);
});
delete this.infoWaiters;
}
};
TorrentContext.prototype.waitInfo = function(cb) {
if (this.infoWaiters === undefined)
this.infoWaiters = [];
this.infoWaiters.push(cb);
};
TorrentContext.prototype.receivePiece = function(index, begin, data) {
var that = this;
var offset = this.pieceLength * index + begin;
this.streams.forEach(function(stream) {
stream.receive(offset, data);
});
this.bytesDownloaded += data.length;
};
TorrentContext.prototype.getPieceCandidate = function(index) {
var candidate;
for(var host in this.peers) {
var peer;
if ((peer = this.peers[host]) && peer.isReady()) {
if (peer.isReady() &&
peer.hasPiece(index)) {
if (!candidate || candidate.score < peer.score)
candidate = peer;
}
}
}
if (candidate)
console.log({candidateScore: candidate.score});
return candidate;
};
TorrentContext.prototype.cancelled = function(req) {
var that = this;
this.streams.forEach(function(stream) {
var offset = req.index * that.pieceLength + req.begin;
stream.cancelled(offset, req.length);
});
this.onActivity();
};
TorrentContext.prototype.stream = function(offset, length) {
var that = this;
var stream = new TorrentStream(offset, length);
stream.on('end', function() {
that.streams.splice(that.streams.indexOf(stream), 1);
});
this.streams.push(stream);
this.workStreams();
return stream;
};
TorrentContext.prototype.refreshTrackers = function(cb) {
var that = this;
Model.getTrackers(this.infoHex, function(error, urls) {
if (urls && urls.forEach)
urls.forEach(function(url) {
if (!that.trackers.hasOwnProperty(url) &&
/^http:/.test(url)) {
console.log("New tracker for " + that.infoHex + ": " + url);
that.trackers[url] = { next: 0 };
}
});
cb(error);
});
};
// Called periodically by ctor
TorrentContext.prototype.tryAnnounce = function(force) {
var that = this;
this.refreshTrackers(function(error) {
if (error)
return;
var now = Date.now();
for(var url in that.trackers)
if (that.trackers.hasOwnProperty(url) &&
(that.trackers[url].next <= now || force)) {
that.announce(url);
break; // only one
}
});
};
TorrentContext.prototype.announce = function(url) {
var that = this;
console.log("Tracker request for " + this.infoHex + " to " + url);
this.trackers[url].next = Date.now() + 600 * 1000;
var u = URL.parse(url);
var cl = http.createClient(u.port || 80, u.hostname);
cl.on('error', function(e) {
console.log(e.stack ? e.stack : e.toString());
});
var req = cl.request('GET', u.pathname + '?' + this.makeAnnounceQuery(),
{'Host': u.hostname});
req.end();
req.on('response', function(res) {
if (res.statusCode == 200) {
var parser = new BEnc.ModelParser();
res.on('data', function(data) {
parser.write(data);
});
parser.on('model', function(resDict) {
if (resDict.interval)
that.trackers[url].next = Date.now() + resDict.interval * 1000;
if (resDict.peers) {
var peers = parsePeers(resDict.peers);
peers.forEach(function(peer) {
that.addPeer(peer.host, peer.port);
});
that.onInfoUpdate();
}
});
} else {
console.log("Tracker request to " + url + " failed: " + res.statusCode);
req.end();
res.socket.end();
}
});
this.lastActivity = Date.now();
};
TorrentContext.prototype.makeAnnounceQuery = function() {
var queryInfoHash = '';
for(i = 0; i < this.infoHex.length; i += 2) {
queryInfoHash += '%' + this.infoHex.slice(i, i + 2).toUpperCase();
}
return "info_hash=" + queryInfoHash +
"&peer_id=%2dBS00%2dYOYOYOYOYOYOYO" +
"&port=" + this.tm.port +
"&uploaded=" + 0 +
"&downloaded=" + this.bytesDownloaded +
"&left=" + this.size +
"&event=started" + // TODO: fix this
"&compact=1";
};
function hexValue(v) {
if (v >= 48 && v <= 57)
return v - 48;
else if (v >= 65 && v <= 70)
return v + 10 - 65;
else if (v >= 97 && v <= 102)
return v + 10 - 97;
else
throw 'hexValue';
}
function hexToBin(s) {
var r = new Buffer(s.length / 2);
for(var i = 0; i < r.length; i++) {
r[i] = (hexValue(s.charCodeAt(i * 2)) << 4) | hexValue(s.charCodeAt(i * 2 + 1));
}
return r;
}
function parsePeers(data) {
var r = [];
if (data.constructor === Array) {
data.forEach(function(peer) {
r.push({ host: peer.ip,
port: peer.port });
});
} else {
for(var i = 0; i < data.length; i += 6) {
var ip = data[i] + '.' +
data[i + 1] + '.' +
data[i + 2] + '.' +
data[i + 3];
var port = (data[i + 4] << 8) | data[i + 5];
r.push({ host: ip,
port: port });
}
}
return r;
}
function generatePeerId() {
var peerId = new Buffer("-BS00- ");
for(var i = 6; i < 20; i++)
peerId[i] = Math.ceil(Math.random * 255);
return peerId;
}