From c6bef2f94ab851c4f260a56b530e0c07a011af68 Mon Sep 17 00:00:00 2001 From: Alexey Kasyanchuk Date: Sat, 28 Jul 2018 14:56:46 +0300 Subject: [PATCH] feat(search): add remote torrents in db via dht and search requests --- src/background/api.js | 26 +++++++++++++++++++------- src/background/bt/client.js | 7 +++++++ src/background/spider.js | 22 ++++++++++++++++++++++ src/background/torrentClient.js | 29 +++++++++++++++++++++++++++++ 4 files changed, 77 insertions(+), 7 deletions(-) diff --git a/src/background/api.js b/src/background/api.js index 5d0a8bb..f47086d 100644 --- a/src/background/api.js +++ b/src/background/api.js @@ -284,7 +284,7 @@ module.exports = async ({ return } - const searchTorrentCall = function(text, navigation, callback) + const searchTorrentCall = function(text, navigation, callback, isP2P) { if(typeof callback != 'function') return; @@ -335,16 +335,28 @@ module.exports = async ({ } let searchList = []; - sphinx.query('SELECT * FROM `torrents` WHERE ' + (isSH1Hash(text) ? 'hash = ?' : 'MATCH(?)') + ' ' + where + ' ' + order + ' LIMIT ?,?', args, function (error, rows, fields) { + const isSHA1 = isSH1Hash(text) + sphinx.query('SELECT * FROM `torrents` WHERE ' + (isSHA1 ? 'hash = ?' : 'MATCH(?)') + ' ' + where + ' ' + order + ' LIMIT ?,?', args, function (error, rows, fields) { if(!rows) { console.log(error) callback(undefined) return; } - rows.forEach((row) => { - searchList.push(baseRowData(row)); - }); - callback(searchList); + if(rows.length === 0 && isSHA1 && !isP2P) // trying to get via dht + { + console.log('get torrent via infohash with dht') + torrentClient.getMetadata(text, (torrent) => { + searchList.push(baseRowData(torrent)); + callback(searchList); + }) + } + else + { + rows.forEach((row) => { + searchList.push(baseRowData(row)); + }); + callback(searchList); + } }); } @@ -366,7 +378,7 @@ module.exports = async ({ if(!text) return; - searchTorrentCall(text, navigation, (data) => callback(data)) + searchTorrentCall(text, navigation, (data) => callback(data), true) // 4 args means remote }) const searchFilesCall = function(text, navigation, callback) diff --git a/src/background/bt/client.js b/src/background/bt/client.js index 5aa65a1..74c4201 100644 --- a/src/background/bt/client.js +++ b/src/background/bt/client.js @@ -47,6 +47,13 @@ class Client extends Emiter debug('start download', infohash.toString('hex'), 'connections', this.activeConnections); this.activeConnections++; + // move host -> address + if(rinfo.host) + { + rinfo = Object.assign({}, rinfo) + rinfo.address = rinfo.host + delete rinfo.host + } var successful = false; var socket = new net.Socket(); diff --git a/src/background/spider.js b/src/background/spider.js index 2993bb9..cc248dc 100644 --- a/src/background/spider.js +++ b/src/background/spider.js @@ -5,6 +5,7 @@ const fs = require('fs'); const {single, pool} = require('./mysql') const getPeersStatisticUDP = require('./bt/udp-tracker-request') const crypto = require('crypto') +const EventEmitter = require('events'); const P2PServer = require('./p2p') const P2PStore = require('./store') const stun = require('stun') @@ -42,6 +43,7 @@ module.exports = function (send, recive, dataDirectory, version, env) let torrentsId = 1; let filesId = 1; + const events = new EventEmitter let sphinx = pool(); // initialize p2p @@ -514,6 +516,7 @@ app.get('*', function(req, res) console.error(err); } resolve() + events.emit('insert', torrent) }); }) @@ -649,6 +652,25 @@ app.get('*', function(req, res) } }); + + let downloadersCallbacks = {} + events.on('insert', (torrent) => { + const { hash } = torrent + const callback = downloadersCallbacks[hash] + if(!callback) + return + + delete downloadersCallbacks[hash] + callback(torrent) + }) + + torrentClient._downloader = (peer, infoHash, callback) => { + const hash = infoHash.toString('hex') + downloadersCallbacks[hash] = callback + setTimeout(() => delete downloadersCallbacks[hash], 8000) + client._download(peer, infoHash) + } + checkInternet((connected) => { if(!connected) return diff --git a/src/background/torrentClient.js b/src/background/torrentClient.js index 3885b0b..6cb22a1 100644 --- a/src/background/torrentClient.js +++ b/src/background/torrentClient.js @@ -54,4 +54,33 @@ torrentClient.loadSession = (sessionFile) => { } }) } + +const metaHashes = {} + +torrentClient.dht.on('peer', (peer, infoHash) => { + const hash = infoHash.toString('hex') + if(!(hash in metaHashes)) + return + + if(torrentClient._downloader) + { + torrentClient._downloader(peer, infoHash, (...data) => { + if(metaHashes[hash]) + metaHashes[hash](...data) + + delete metaHashes[hash] + }) + } + else + { + delete metaHashes[hash] + } +}) + +torrentClient.getMetadata = (hash, callback) => { + hash = hash.toLowerCase() + torrentClient.dht.lookup(hash) + metaHashes[hash] = callback +} + module.exports = torrentClient \ No newline at end of file