-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathcrawler.js
152 lines (123 loc) · 3.93 KB
/
crawler.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
const bencode = require('bencode');
const config = require('./../config');
const crypto = require('crypto');
const dgram = require('dgram');
const decodeNodes = (data) => {
const nodes = [];
for (let i = 0; i + 26 <= data.length; i += 26) {
nodes.push({
address: `${data[i + 20]}.${data[i + 21]}.${data[i + 22]}.${data[i + 23]}`,
nid: data.slice(i, i + 20),
port: data.readUInt16BE(i + 24),
});
}
return nodes;
};
const getNeighborID = (target, nid) => Buffer.concat([target.slice(0, 10), nid.slice(10)]);
const getRandomID = () =>
crypto
.createHash('sha1')
.update(crypto.randomBytes(20))
.digest();
const handleError = () => {
// Do nothing
};
const safe = (fn) => (...params) => {
try {
const response = fn(...params);
return response;
} catch (error) {
handleError(error);
}
return undefined;
};
const TID_LENGTH = 4;
const TOKEN_LENGTH = 2;
const clientID = getRandomID();
const serverSocket = dgram.createSocket('udp4');
let nodes = [];
let onTorrent = (torrent) => console.log(torrent);
const sendMessage = safe((message, rinfo) => {
const buf = bencode.encode(message);
serverSocket.send(buf, 0, buf.length, rinfo.port, rinfo.address);
});
const onFindNodeResponse = safe((responseNodes) => {
const decodedNodes = decodeNodes(responseNodes);
decodedNodes.forEach((node) => {
if (node.address !== '0.0.0.0' && node.nid !== clientID && node.port < 65536 && node.port > 0) {
if (nodes.length < 2000) {
nodes.push(node);
}
}
});
});
const onGetPeersRequest = safe((msg, rinfo) => {
const {
a: { id: nid, info_hash: infohash },
t: tid,
} = msg;
const token = infohash.slice(0, TOKEN_LENGTH);
if (!tid || infohash.length !== 20 || nid.length !== 20) {
throw new Error('No tid or nid or bad infohash');
}
sendMessage({ r: { id: getNeighborID(infohash, clientID), nodes: '', token }, t: tid, y: 'r' }, rinfo);
});
const onAnnouncePeerRequest = safe((msg, rinfo) => {
const {
a: { id: nid, info_hash: infohash, implied_port: impliedPort, port, token },
t: tid,
} = msg;
const peerPort = impliedPort ? rinfo.port : port;
if (!tid) {
throw new Error('No tid');
} else if (infohash.slice(0, TOKEN_LENGTH).toString() !== token.toString()) {
throw new Error('invalid infohash length');
} else if (!peerPort || peerPort >= 65536 || peerPort <= 0) {
throw new Error('no port', peerPort);
}
sendMessage({ r: { id: getNeighborID(nid, clientID) }, t: tid, y: 'r' }, rinfo);
// downloadTorrent({ address: rinfo.address, port }, infohash);
onTorrent(infohash, { address: rinfo.address, port });
});
const onMessage = safe((message, rinfo) => {
const msg = bencode.decode(message);
const type = msg.y && Buffer.isBuffer(msg.y) ? msg.y.toString() : msg.y;
const query = msg.q && Buffer.isBuffer(msg.q) ? msg.q.toString() : msg.q;
if (type === 'r' && msg.r.nodes) {
onFindNodeResponse(msg.r.nodes);
} else if (type === 'q' && query === 'get_peers') {
onGetPeersRequest(msg, rinfo);
} else if (type === 'q' && query === 'announce_peer') {
onAnnouncePeerRequest(msg, rinfo);
}
});
const sendFindNodeRequest = ({ address, port }, nid) => {
const t = getRandomID().slice(0, TID_LENGTH);
const id = nid ? getNeighborID(nid, clientID) : clientID;
sendMessage({ a: { id, target: getRandomID() }, q: 'find_node', t, y: 'q' }, { address, port });
};
const makeNeighbours = () => {
nodes.forEach((node) => {
sendFindNodeRequest(node, node.nid);
});
nodes = [];
};
const start = () => {
nodes = nodes.concat(config.bootstrapNodes);
makeNeighbours();
setTimeout(() => start(), 1000);
};
const onListening = () => {
console.log(`Crawler listening on ${config.crawler.address}:${config.crawler.port}`);
start();
};
const crawler = (fn) => {
serverSocket.bind(config.crawler.port, config.crawler.address);
serverSocket.on('listening', onListening);
serverSocket.on('message', onMessage);
serverSocket.on('error', handleError);
if (typeof fn === 'function') {
onTorrent = fn;
}
};
module.exports = crawler;