feat(p2p): other rats peers detection

This commit is contained in:
Alexey Kasyanchuk 2018-02-16 23:23:38 +03:00
parent d497ee9051
commit b1d2c4a334

View File

@ -51,6 +51,8 @@ class Spider extends Emiter {
this.walkInterval = config.spider.walkInterval; this.walkInterval = config.spider.walkInterval;
this.cpuLimit = config.spider.cpuLimit; this.cpuLimit = config.spider.cpuLimit;
this.cpuInterval = config.spider.cpuInterval; this.cpuInterval = config.spider.cpuInterval;
this.announceHashes = [crypto.createHash('sha1').update('degrats-v1').digest()]
} }
send(message, address) { send(message, address) {
@ -87,24 +89,21 @@ class Spider extends Emiter {
} }
} }
onFoundPeers(peers, token, address) {
console.log('found peers', peers)
this.announcePeer(crypto.createHash('sha1').update('degrats-v3').digest(), token, address)
}
announcePeer(infoHash, token, address, port) announcePeer(infoHash, token, address, port)
{ {
const message = { const message = {
t: generateTid(),
y: 'q',
q: 'announce_peer', q: 'announce_peer',
a: { a: {
id: this.table.id, id: this.table.id,
token: token, // queryAll sets this token: token,
info_hash: infoHash, info_hash: infoHash,
port: port, port: port,
implied_port: port ? 0 : 1 implied_port: port ? 0 : 1
} }
} }
//this.send(message, address) this.send(message, address)
} }
join() { join() {
@ -133,7 +132,7 @@ class Spider extends Emiter {
setTimeout(()=>this.walk(), this.walkInterval) setTimeout(()=>this.walk(), this.walkInterval)
} }
onFoundNodes(data) { onFoundNodes(data, token, address) {
const nodes = Node.decodeNodes(data) const nodes = Node.decodeNodes(data)
nodes.forEach((node) => { nodes.forEach((node) => {
if (node.id != this.table.id && isValidPort(node.port)) { if (node.id != this.table.id && isValidPort(node.port)) {
@ -141,6 +140,32 @@ class Spider extends Emiter {
} }
}) })
this.emit('nodes', nodes) this.emit('nodes', nodes)
// announce torrents
if(token)
{
for(const hash of this.announceHashes)
{
this.announcePeer(hash, token, address)
}
}
}
onFoundPeers(peers, token, address) {
if(token)
{
for(const hash of this.announceHashes)
{
this.announcePeer(hash, token, address)
}
}
if(!peers || peers.length == 0)
return;
const nodes = Node.decodeNodes(peers)
console.log('peers', peers, nodes)
this.emit('peer', nodes)
} }
onFindNodeRequest(message, address) { onFindNodeRequest(message, address) {
@ -179,13 +204,6 @@ class Spider extends Emiter {
const {t: tid, a: {id: nid, info_hash: infohash}} = message const {t: tid, a: {id: nid, info_hash: infohash}} = message
if(infohash.toString('hex') == crypto.createHash('sha1').update('degrats-v2').digest('hex'))
{
console.log('-----------------------')
console.log('-----------------------')
console.log('-----------------------')
}
if (tid === undefined || infohash.length != 20 || nid.length != 20) { if (tid === undefined || infohash.length != 20 || nid.length != 20) {
return return
} }
@ -222,7 +240,7 @@ class Spider extends Emiter {
if(this.client && !this.ignore) { if(this.client && !this.ignore) {
cpuDebug('cpu usage:' + cpuUsage()) cpuDebug('cpu usage:' + cpuUsage())
if(this.cpuLimit <= 0 || cpuUsage() <= this.cpuLimit + this.cpuInterval) { if(this.cpuLimit <= 0 || cpuUsage() <= this.cpuLimit + this.cpuInterval) {
this.client.add(addressPair, infohash); // this.client.add(addressPair, infohash);
} }
} }
} }
@ -244,7 +262,7 @@ class Spider extends Emiter {
const message = bencode.decode(data) const message = bencode.decode(data)
if (message.y.toString() == 'r') { if (message.y.toString() == 'r') {
if(message.r.nodes) { if(message.r.nodes) {
this.onFoundNodes(message.r.nodes) this.onFoundNodes(message.r.nodes, message.r.token, address)
} else if(message.r.values) { } else if(message.r.values) {
this.onFoundPeers(message.r.values, message.r.token, address) this.onFoundPeers(message.r.values, message.r.token, address)
} }
@ -316,15 +334,16 @@ class Spider extends Emiter {
} }
} }
const m = crypto.createHash('sha1').update('degrats-v3').digest() this.announceSearchInterval = setInterval(() => this.getPeersRequest(this.announceHashes[0]), 2000)
setTimeout(() => {
//console.log('m')
//this.announcePeer(m)
}, 8000)
setTimeout(() => { /*
this.getPeersRequest(m) setInterval(() => {
}, 8000) for(const address of this.table.nodes)
{
this.announcePeer(this.announcePeer[0], null, address)
}
}, 3000)
*/
} }
close(callback) close(callback)
@ -337,6 +356,8 @@ class Spider extends Emiter {
clearInterval(this.joinInterval) clearInterval(this.joinInterval)
if(this.trafficInterval) if(this.trafficInterval)
clearInterval(this.trafficInterval) clearInterval(this.trafficInterval)
if(this.announceSearchInterval)
clearInterval(this.announceSearchInterval)
this.closing = true this.closing = true
this.udp.close(() => { this.udp.close(() => {
this.initialized = false this.initialized = false