From 093df8b31be3742b68a9e18a3ced10dded9e5478 Mon Sep 17 00:00:00 2001 From: Alexey Kasyanchuk Date: Sat, 7 Jan 2017 13:49:52 +0300 Subject: [PATCH] =?UTF-8?q?=D0=BF=D0=BE=D0=BB=D1=83=D1=87=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F=20=D0=B8=D0=BD=D1=84=D0=BE=D1=80=D0=BC=D0=B0=D1=86?= =?UTF-8?q?=D0=B8=D0=B8=20=D1=81=20=D1=82=D1=80=D0=B5=D0=BA=D0=B5=D1=80?= =?UTF-8?q?=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 68 ++++++++++++++++++++++++++++++++++++++ lib/udp-tracker-request.js | 28 ++++++++-------- src/recent-torrents.js | 30 +++++++++++++++-- 3 files changed, 110 insertions(+), 16 deletions(-) diff --git a/index.js b/index.js index 7d32d66..2f97c10 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,7 @@ const client = new (require('./lib/client')) const spider = new (require('./lib/spider'))(client) const mysql = require('mysql'); +const getPeersStatisticUDP = require('./lib/udp-tracker-request') var express = require('express'); var app = express(); @@ -27,6 +28,25 @@ let socketMysql = mysql.createPool({ 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; function handleListenerDisconnect() { listenerMysql = mysql.createConnection(mysqlSettings); @@ -79,6 +99,9 @@ io.on('connection', function(socket) added: row.added.getTime(), contentType: row.contentType, 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, 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 { diff --git a/lib/udp-tracker-request.js b/lib/udp-tracker-request.js index 84ab7bf..a54c770 100644 --- a/lib/udp-tracker-request.js +++ b/lib/udp-tracker-request.js @@ -31,6 +31,13 @@ let connectTracker = function(connection) { buffer.writeUInt32BE(ACTION_CONNECT, 8); buffer.writeUInt32BE(transactionId, 12); + // очистка старых соединений + for(transaction in requests) { + if((new Date).getTime() - requests[transaction].date.getTime() > 60 * 1000) { + delete requests[transaction]; + } + } + requests[transactionId] = connection; message(buffer, connection.host, connection.port); }; @@ -58,12 +65,9 @@ let scrapeTorrent = function (connectionIdHigh, connectionIdLow, transactionId) server.on("message", function (msg, rinfo) { let buffer = new Buffer(msg) - console.log('response from ' + rinfo); - const action = buffer.readUInt32BE(0, 4); const transactionId = buffer.readUInt32BE(4, 4); - console.log(Object.keys(requests)); if(!(transactionId in requests)) return; @@ -103,7 +107,7 @@ server.on("message", function (msg, rinfo) { let getPeersStatistic = (host, port, hash, callback) => { let connection = { - host, port, hash, callback + host, port, hash, callback, date: new Date() } connectTracker(connection); } @@ -113,15 +117,11 @@ server.on("listening", function () { console.log("listening udp tracker respose on " + address.address + ":" + address.port); }); -server.bind(4444); +server.bind(4446); -getPeersStatistic('tracker.coppersurfer.tk', 6969, "d096ff66557a5ea7030680967610e38b37434ea9", (data) => { - console.log(data) -}); +module.exports = getPeersStatistic; + +//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) -}); diff --git a/src/recent-torrents.js b/src/recent-torrents.js index c54a8ac..f16c7ef 100644 --- a/src/recent-torrents.js +++ b/src/recent-torrents.js @@ -30,6 +30,17 @@ return ( : null } + { + torrent.seeders || torrent.leechers || torrent.completed + ? +
+ 0 ? '#00C853' : 'grey')}}>{torrent.seeders} seeders + 0 ? '#AA00FF' : 'grey'), marginLeft: '12px'}}>{torrent.leechers} leechers + 0 ? '#FF6D00' : 'grey'), marginLeft: '12px'}}>{torrent.completed} completed downloads +
+ : + null + } } leftIcon={ @@ -156,6 +167,7 @@ export default class RecentTorrents extends Component { super() this.torrents = []; this.displayQueue = []; + this.displayQueueAssoc = {}; this.state = { pause: false } } componentDidMount() { @@ -174,17 +186,29 @@ export default class RecentTorrents extends Component { let torrent = this.displayQueue.shift(); this.torrents.unshift(torrent); - if(this.torrents.length > 10) - this.torrents.pop() + if(this.torrents.length > 10) { + let toDelete = this.torrents.pop() + delete this.displayQueueAssoc[toDelete.hash]; + } this.forceUpdate(); }, 850); }); this.newTorrentFunc = (torrent) => { this.displayQueue.push(torrent); + this.displayQueueAssoc[torrent.hash] = torrent; this.forceUpdate(); }; 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() { this.setState({ @@ -194,6 +218,8 @@ export default class RecentTorrents extends Component { componentWillUnmount() { if(this.newTorrentFunc) window.torrentSocket.off('newTorrent', this.newTorrentFunc); + if(this.tracketUpdate) + window.torrentSocket.off('trackerTorrentUpdate', this.tracketUpdate); if(this.displayNewTorrent) clearInterval(this.displayNewTorrent); }