feat(search): add remote torrents in db via dht and search requests
This commit is contained in:
@ -284,7 +284,7 @@ module.exports = async ({
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const searchTorrentCall = function(text, navigation, callback)
|
const searchTorrentCall = function(text, navigation, callback, isP2P)
|
||||||
{
|
{
|
||||||
if(typeof callback != 'function')
|
if(typeof callback != 'function')
|
||||||
return;
|
return;
|
||||||
@ -335,16 +335,28 @@ module.exports = async ({
|
|||||||
}
|
}
|
||||||
|
|
||||||
let searchList = [];
|
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) {
|
if(!rows) {
|
||||||
console.log(error)
|
console.log(error)
|
||||||
callback(undefined)
|
callback(undefined)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
rows.forEach((row) => {
|
if(rows.length === 0 && isSHA1 && !isP2P) // trying to get via dht
|
||||||
searchList.push(baseRowData(row));
|
{
|
||||||
});
|
console.log('get torrent via infohash with dht')
|
||||||
callback(searchList);
|
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)
|
if(!text)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
searchTorrentCall(text, navigation, (data) => callback(data))
|
searchTorrentCall(text, navigation, (data) => callback(data), true) // 4 args means remote
|
||||||
})
|
})
|
||||||
|
|
||||||
const searchFilesCall = function(text, navigation, callback)
|
const searchFilesCall = function(text, navigation, callback)
|
||||||
|
@ -47,6 +47,13 @@ class Client extends Emiter
|
|||||||
debug('start download', infohash.toString('hex'), 'connections', this.activeConnections);
|
debug('start download', infohash.toString('hex'), 'connections', this.activeConnections);
|
||||||
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 successful = false;
|
||||||
var socket = new net.Socket();
|
var socket = new net.Socket();
|
||||||
|
|
||||||
|
@ -5,6 +5,7 @@ const fs = require('fs');
|
|||||||
const {single, pool} = require('./mysql')
|
const {single, pool} = require('./mysql')
|
||||||
const getPeersStatisticUDP = require('./bt/udp-tracker-request')
|
const getPeersStatisticUDP = require('./bt/udp-tracker-request')
|
||||||
const crypto = require('crypto')
|
const crypto = require('crypto')
|
||||||
|
const EventEmitter = require('events');
|
||||||
const P2PServer = require('./p2p')
|
const P2PServer = require('./p2p')
|
||||||
const P2PStore = require('./store')
|
const P2PStore = require('./store')
|
||||||
const stun = require('stun')
|
const stun = require('stun')
|
||||||
@ -42,6 +43,7 @@ module.exports = function (send, recive, dataDirectory, version, env)
|
|||||||
let torrentsId = 1;
|
let torrentsId = 1;
|
||||||
let filesId = 1;
|
let filesId = 1;
|
||||||
|
|
||||||
|
const events = new EventEmitter
|
||||||
let sphinx = pool();
|
let sphinx = pool();
|
||||||
|
|
||||||
// initialize p2p
|
// initialize p2p
|
||||||
@ -514,6 +516,7 @@ app.get('*', function(req, res)
|
|||||||
console.error(err);
|
console.error(err);
|
||||||
}
|
}
|
||||||
resolve()
|
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) => {
|
checkInternet((connected) => {
|
||||||
if(!connected)
|
if(!connected)
|
||||||
return
|
return
|
||||||
|
@ -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
|
module.exports = torrentClient
|
Reference in New Issue
Block a user