Skip to content

Commit

Permalink
feat(search): add remote torrents in db via dht and search requests
Browse files Browse the repository at this point in the history
  • Loading branch information
DEgITx committed Jul 28, 2018
1 parent 1e57789 commit 1e44164
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/background/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
});
}

Expand All @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions src/background/bt/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
22 changes: 22 additions & 0 deletions src/background/spider.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -514,6 +516,7 @@ app.get('*', function(req, res)
console.error(err);
}
resolve()
events.emit('insert', torrent)
});
})

Expand Down Expand Up @@ -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
Expand Down
29 changes: 29 additions & 0 deletions src/background/torrentClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 1e44164

Please sign in to comment.