получения информации с трекеров

This commit is contained in:
Alexey Kasyanchuk
2017-01-07 13:49:52 +03:00
parent 1a1153b0ea
commit 093df8b31b
3 changed files with 110 additions and 16 deletions

View File

@ -1,6 +1,7 @@
const client = new (require('./lib/client')) const client = new (require('./lib/client'))
const spider = new (require('./lib/spider'))(client) const spider = new (require('./lib/spider'))(client)
const mysql = require('mysql'); const mysql = require('mysql');
const getPeersStatisticUDP = require('./lib/udp-tracker-request')
var express = require('express'); var express = require('express');
var app = express(); var app = express();
@ -27,6 +28,25 @@ let socketMysql = mysql.createPool({
database : mysqlSettings.database database : mysqlSettings.database
}); });
const udpTrackers = [
{
host: 'tracker.coppersurfer.tk',
port: 6969
},
{
host: 'tracker.leechers-paradise.org',
port: 6969
},
{
host: 'tracker.opentrackr.org',
port: 1337
},
{
host: '9.rarbg.me',
port: 2710
}
]
let listenerMysql; let listenerMysql;
function handleListenerDisconnect() { function handleListenerDisconnect() {
listenerMysql = mysql.createConnection(mysqlSettings); listenerMysql = mysql.createConnection(mysqlSettings);
@ -79,6 +99,9 @@ io.on('connection', function(socket)
added: row.added.getTime(), added: row.added.getTime(),
contentType: row.contentType, contentType: row.contentType,
contentCategory: row.contentCategory, contentCategory: row.contentCategory,
seeders: row.seeders,
completed: row.completed,
leechers: row.leechers,
} }
} }
@ -330,6 +353,51 @@ client.on('complete', function (metadata, infohash, rinfo) {
contentType: torrentQ.contentType, contentType: torrentQ.contentType,
contentCategory: torrentQ.contentCategory, contentCategory: torrentQ.contentCategory,
}); });
let maxSeeders = 0, maxLeechers = 0, maxCompleted = 0;
udpTrackers.forEach((tracker) => {
getPeersStatisticUDP(tracker.host, tracker.port, hash, ({seeders, completed, leechers}) => {
if(seeders == 0 && completed == 0 && leechers == 0)
return;
/*
pushDatabaseBalance();
listenerMysql.query('INSERT INTO trackers SET ?', statistic, function(err, result) {
popDatabaseBalance();
});
*/
if(seeders < maxSeeders)
{
return;
}
if(seeders == maxSeeders && leechers < maxLeechers)
{
return;
}
if(seeders == maxSeeders && leechers == maxLeechers && completed <= maxCompleted)
{
return;
}
maxSeeders = seeders;
maxLeechers = leechers;
maxCompleted = completed;
pushDatabaseBalance();
listenerMysql.query('UPDATE torrents SET seeders = ?, completed = ?, leechers = ? WHERE hash = ?', [seeders, completed, leechers, hash], function(err, result) {
popDatabaseBalance();
if(!result) {
return
}
io.sockets.emit('trackerTorrentUpdate', {
hash,
seeders,
completed,
leechers
});
});
});
});
} }
else else
{ {

View File

@ -31,6 +31,13 @@ let connectTracker = function(connection) {
buffer.writeUInt32BE(ACTION_CONNECT, 8); buffer.writeUInt32BE(ACTION_CONNECT, 8);
buffer.writeUInt32BE(transactionId, 12); buffer.writeUInt32BE(transactionId, 12);
// очистка старых соединений
for(transaction in requests) {
if((new Date).getTime() - requests[transaction].date.getTime() > 60 * 1000) {
delete requests[transaction];
}
}
requests[transactionId] = connection; requests[transactionId] = connection;
message(buffer, connection.host, connection.port); message(buffer, connection.host, connection.port);
}; };
@ -58,12 +65,9 @@ let scrapeTorrent = function (connectionIdHigh, connectionIdLow, transactionId)
server.on("message", function (msg, rinfo) { server.on("message", function (msg, rinfo) {
let buffer = new Buffer(msg) let buffer = new Buffer(msg)
console.log('response from ' + rinfo);
const action = buffer.readUInt32BE(0, 4); const action = buffer.readUInt32BE(0, 4);
const transactionId = buffer.readUInt32BE(4, 4); const transactionId = buffer.readUInt32BE(4, 4);
console.log(Object.keys(requests));
if(!(transactionId in requests)) if(!(transactionId in requests))
return; return;
@ -103,7 +107,7 @@ server.on("message", function (msg, rinfo) {
let getPeersStatistic = (host, port, hash, callback) => { let getPeersStatistic = (host, port, hash, callback) => {
let connection = { let connection = {
host, port, hash, callback host, port, hash, callback, date: new Date()
} }
connectTracker(connection); connectTracker(connection);
} }
@ -113,15 +117,11 @@ server.on("listening", function () {
console.log("listening udp tracker respose on " + address.address + ":" + address.port); console.log("listening udp tracker respose on " + address.address + ":" + address.port);
}); });
server.bind(4444); server.bind(4446);
getPeersStatistic('tracker.coppersurfer.tk', 6969, "d096ff66557a5ea7030680967610e38b37434ea9", (data) => { module.exports = getPeersStatistic;
console.log(data)
}); //getPeersStatistic('tracker.glotorrents.com', 6969, "d096ff66557a5ea7030680967610e38b37434ea8", (data) => {
// console.log(data)
//});
getPeersStatistic('tracker.coppersurfer.tk', 6969, "d096ff66557a5ea7030680967610e38b37434ea8", (data) => {
console.log(data)
});
getPeersStatistic('tracker.coppersurfer.tk', 6969, "d096ff66557a5ea7030680967610e38b37434ea8", (data) => {
console.log(data)
});

View File

@ -30,6 +30,17 @@ return (
: :
null null
} }
{
torrent.seeders || torrent.leechers || torrent.completed
?
<div className='break-word fs0-85' style={{paddingTop: '0.3em'}}>
<span style={{color: (torrent.seeders > 0 ? '#00C853' : 'grey')}}>{torrent.seeders} seeders</span>
<span style={{color: (torrent.leechers > 0 ? '#AA00FF' : 'grey'), marginLeft: '12px'}}>{torrent.leechers} leechers</span>
<span style={{color: (torrent.completed > 0 ? '#FF6D00' : 'grey'), marginLeft: '12px'}}>{torrent.completed} completed downloads</span>
</div>
:
null
}
</div> </div>
} }
leftIcon={ leftIcon={
@ -156,6 +167,7 @@ export default class RecentTorrents extends Component {
super() super()
this.torrents = []; this.torrents = [];
this.displayQueue = []; this.displayQueue = [];
this.displayQueueAssoc = {};
this.state = { pause: false } this.state = { pause: false }
} }
componentDidMount() { componentDidMount() {
@ -174,17 +186,29 @@ export default class RecentTorrents extends Component {
let torrent = this.displayQueue.shift(); let torrent = this.displayQueue.shift();
this.torrents.unshift(torrent); this.torrents.unshift(torrent);
if(this.torrents.length > 10) if(this.torrents.length > 10) {
this.torrents.pop() let toDelete = this.torrents.pop()
delete this.displayQueueAssoc[toDelete.hash];
}
this.forceUpdate(); this.forceUpdate();
}, 850); }, 850);
}); });
this.newTorrentFunc = (torrent) => { this.newTorrentFunc = (torrent) => {
this.displayQueue.push(torrent); this.displayQueue.push(torrent);
this.displayQueueAssoc[torrent.hash] = torrent;
this.forceUpdate(); this.forceUpdate();
}; };
window.torrentSocket.on('newTorrent', this.newTorrentFunc); window.torrentSocket.on('newTorrent', this.newTorrentFunc);
this.tracketUpdate = (statistic) => {
if(statistic.hash in this.displayQueueAssoc)
{
Object.assign(this.displayQueueAssoc[statistic.hash], statistic);
this.forceUpdate();
}
}
window.torrentSocket.on('trackerTorrentUpdate', this.tracketUpdate);
} }
pauseAndContinue() { pauseAndContinue() {
this.setState({ this.setState({
@ -194,6 +218,8 @@ export default class RecentTorrents extends Component {
componentWillUnmount() { componentWillUnmount() {
if(this.newTorrentFunc) if(this.newTorrentFunc)
window.torrentSocket.off('newTorrent', this.newTorrentFunc); window.torrentSocket.off('newTorrent', this.newTorrentFunc);
if(this.tracketUpdate)
window.torrentSocket.off('trackerTorrentUpdate', this.tracketUpdate);
if(this.displayNewTorrent) if(this.displayNewTorrent)
clearInterval(this.displayNewTorrent); clearInterval(this.displayNewTorrent);
} }