-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.js
More file actions
116 lines (107 loc) · 2.98 KB
/
Copy pathapp.js
File metadata and controls
116 lines (107 loc) · 2.98 KB
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
var utils = require("./utils");
var bencode = require('node-bencode');
var dns = require('dns');
var args = process.argv.slice(2);
var meid = utils.genId();
var ip_reborn;
var ip;
var port;
var tick;
var sock;
var port = parseInt(process.argv[2]);
var MAX_UDP_PER_SECOND = 250; // adjust this value to control upstream speed
var nodes = [];
var nCount = 0;
function init() {
var dgram = require("dgram");
sock = dgram.createSocket("udp4");
sock.on("message", onRecv);
sock.on("listening", onSockCreated);
sock.bind(port);
}
function onSockCreated() {
var address = sock.address();
ip = address.address;
port = address.port;
dns.resolve4('router.bittorrent.com', function(err, addresses) {
if(err == null) {
ip_reborn = addresses[0];
find_node(addresses[0], 6881);
tick = setTimeout(function() {
find_node(addresses[0], 6881);
}, 5 * 1000);
} else {
console.log('DHT init node domain resolve failed');
}
});
setInterval(function() {
send();
}, 1000);
}
function find_node(ip, port) {
var tid = utils.genTid();
var msg = {
"t": tid,
"y": "q",
"q": "find_node",
"a": {"id": utils.getNeighbor(meid), "target": utils.genId()}
};
var encoded = bencode.encode(msg);
try {
//console.log('send first package');
sock.send(encoded, 0, encoded.length, port, ip);
} catch(e) { }
}
function onRecv(msg, rinfo) {
var data = bencode.decode(msg);
//console.log(data);
if(data == null || data.y == null) {
return;
}
// TODO: 收到find_node回复
if(data.y.toString() === 'r' && nCount < MAX_UDP_PER_SECOND) {
if(tick != null) {
clearTimeout(tick);
tick = null;
}
var buf = data.r.nodes;
if(buf == null) {
return;
}
// 返回nodes的节点为活跃节点,加入数组中
var targetid = utils.genId();
var count = buf.length / 26;
var offset = 0;
for(var i = 0; i < count; i++) {
offset += 20;
var nip = utils.buf2ip(buf.slice(offset, offset + 4));
offset += 4;
var nport = utils.buf2port(buf.slice(offset, offset + 2));
if(nport > 65535 || nport < 0) {
continue;
}
offset += 2;
nodes.push({ip: nip, port: nport});
nCount++;
if(nCount >= MAX_UDP_PER_SECOND) {
break;
}
}
} else if(data.y.toString() === 'q' && data.q.toString() === 'get_peers') {
// infohash
console.log(utils.id2str(data.a.info_hash));
}
}
function send() {
//console.log("sent " + nodes.length + " packets");
if(nCount < 2) {
find_node(ip_reborn, 6881);
}
for(var i = 0; nodes[i]; i++) {
var node = nodes[i];
find_node(node.ip, node.port);
}
nodes = [];
nCount = 0;
}
init();