diff --git a/index.js b/index.js index 954be8c..dbe6971 100644 --- a/index.js +++ b/index.js @@ -10,6 +10,7 @@ var server = require('http').Server(app); var io = require('socket.io')(server); var sm = require('sitemap'); var phantomjs = require('phantomjs-prebuilt') +var ipaddr = require('ipaddr.js'); const torrentTypeDetect = require('./lib/content'); @@ -163,6 +164,8 @@ function baseRowData(row) completed: row.completed, leechers: row.leechers, trackersChecked: row.trackersChecked ? row.trackersChecked.getTime() : undefined, + good: row.good, + bad: row.bad, } } @@ -304,6 +307,53 @@ io.on('connection', function(socket) updateTorrentTrackers(hash); }); + let socketIPV4 = () => { + let ip = socket.request.connection.remoteAddress; + if (ipaddr.IPv4.isValid(ip)) { + // all ok + } else if (ipaddr.IPv6.isValid(ip)) { + let ipv6 = ipaddr.IPv6.parse(ip); + if (ipv6.isIPv4MappedAddress()) { + ip = ipv6.toIPv4Address().toString(); + } + } + return ip + }; + + socket.on('vote', function(hash, isGood, callback) + { + if(hash.length != 40) + return; + + if(typeof callback != 'function') + return; + + const ip = socketIPV4(); + isGood = !!isGood; + + socketMysql.query('SELECT * FROM `torrents_actions` WHERE `hash` = ? AND (`action` = \'good\' OR `action` = \'bad\') AND ipv4 = ?', [hash, ip], function (error, rows, fields) { + if(!rows) { + console.error(error); + } + if(rows.length > 0) { + callback(false) + return + } + + const action = isGood ? 'good' : 'bad'; + socketMysql.query('INSERT INTO `torrents_actions` SET ?', {hash, action, ipv4: ip}, function(err, result) { + if(!result) { + console.error(err); + } + socketMysql.query('UPDATE torrents SET ' + action + ' = ' + action + ' + 1 WHERE hash = ?', hash, function(err, result) { + if(!result) { + console.error(err); + } + callback(true) + }); + }); + }); + }); }); let undoneQueries = 0; diff --git a/package.json b/package.json index eb66502..87edb42 100644 --- a/package.json +++ b/package.json @@ -51,6 +51,7 @@ "bencode": "^0.11.0", "bitfield": "^1.1.2", "express": "^4.14.0", + "ipaddr.js": "^1.2.0", "material-ui": "^0.16.6", "moment": "^2.17.1", "mysql": "^2.12.0", diff --git a/sphinx.conf b/sphinx.conf index b1e7d2d..771c48c 100644 --- a/sphinx.conf +++ b/sphinx.conf @@ -17,7 +17,8 @@ source files_index SELECT fls.fileid, fls.path, fls.size as filesize, \ torrents.hash as hash, torrents.name as name, torrents.size as size, \ torrents.seeders, torrents.leechers, torrents.completed, \ - torrents.files, torrents.contentType, torrents.contentCategory \ + torrents.files, torrents.contentType, torrents.contentCategory, \ + torrents.good, torrents.bad \ FROM files as fls INNER JOIN torrents ON(torrents.hash = fls.hash) sql_field_string = path @@ -32,6 +33,8 @@ source files_index sql_attr_uint = completed sql_attr_string = contentType sql_attr_string = contentCategory + sql_attr_uint = good + sql_attr_uint = bad } index files_index @@ -48,7 +51,8 @@ source files_index_delta : files_index SELECT fls.fileid, fls.path, fls.size as filesize, \ torrents.hash as hash, torrents.name as name, torrents.size as size, \ torrents.seeders, torrents.leechers, torrents.completed, \ - torrents.files, torrents.contentType, torrents.contentCategory \ + torrents.files, torrents.contentType, torrents.contentCategory, \ + torrents.good, torrents.bad \ FROM files as fls INNER JOIN torrents ON(torrents.hash = fls.hash) \ WHERE fls.fileid > ( SELECT max_doc_id FROM sphinx_counter WHERE counter_id = 1 ) } @@ -73,7 +77,8 @@ source torrents_index sql_query = \ SELECT torrents.torrentid, torrents.hash as hash, torrents.name as name, torrents.size as size, \ torrents.seeders, torrents.leechers, torrents.completed, \ - torrents.files, torrents.contentType, torrents.contentCategory \ + torrents.files, torrents.contentType, torrents.contentCategory, \ + torrents.good, torrents.bad \ FROM torrents sql_attr_string = hash @@ -85,6 +90,8 @@ source torrents_index sql_attr_uint = completed sql_attr_string = contentType sql_attr_string = contentCategory + sql_attr_uint = good + sql_attr_uint = bad } index torrents_index @@ -100,7 +107,8 @@ source torrents_index_delta : torrents_index sql_query = \ SELECT torrents.torrentid, torrents.hash as hash, torrents.name as name, torrents.size as size, \ torrents.seeders, torrents.leechers, torrents.completed, \ - torrents.files, torrents.contentType, torrents.contentCategory \ + torrents.files, torrents.contentType, torrents.contentCategory, \ + torrents.good, torrents.bad \ FROM torrents \ WHERE torrents.torrentid > ( SELECT max_doc_id FROM sphinx_counter WHERE counter_id = 2 ) } diff --git a/src/torrent-page.js b/src/torrent-page.js index 28ca2af..958698e 100644 --- a/src/torrent-page.js +++ b/src/torrent-page.js @@ -127,7 +127,9 @@ export default class TorrentPage extends Page { super(props); this.state = { value: 'info', - searchingIndicator: false + searchingIndicator: false, + voting: false, + voted: false, }; this.setTitle('Information about torrent'); } @@ -212,6 +214,20 @@ export default class TorrentPage extends Page { if(this.trackerUpdate) window.torrentSocket.off('trackerTorrentUpdate', this.trackerUpdate); } + vote(good) { + if(!this.torrent) + return; + + this.setState({ + voting: true + }); + window.torrentSocket.emit('vote', this.torrent.hash, !!good, window.customLoader((success) => { + this.setState({ + voted: true, + voting: false + }); + })); + } render() { const style = { refresh: { @@ -272,6 +288,54 @@ export default class TorrentPage extends Page { : null } + { + !this.state.voted && !this.state.voting + ? +