From 1c626fa2bffb5ab1fa033bb5cc9df6cef3409515 Mon Sep 17 00:00:00 2001 From: Alexey Kasyanchuk Date: Mon, 5 Mar 2018 04:01:08 +0300 Subject: [PATCH] fix(search): show torrent page from remote peer --- src/app/pages-pie.js | 18 +++++++----------- src/app/router.js | 19 ++++++------------- src/app/singleton.js | 29 +++++++++++++++++++++++++++++ src/app/torrent-page.js | 2 +- src/app/torrent.js | 7 ++++++- src/background/p2p.js | 7 +++++++ src/background/spider.js | 31 ++++++++++++++++++++++++++++--- 7 files changed, 84 insertions(+), 29 deletions(-) create mode 100644 src/app/singleton.js diff --git a/src/app/pages-pie.js b/src/app/pages-pie.js index 848217f..c3ed4f2 100644 --- a/src/app/pages-pie.js +++ b/src/app/pages-pie.js @@ -1,15 +1,9 @@ import React, { Component } from 'react'; +import singleton from './singleton' + +class PagesPie extends Component { + pie = []; -export default class PagesPie extends Component { - constructor(props) { - super(props); - // синглтон - if ( PagesPie.instance ) { - return PagesPie.instance; - } - PagesPie.instance = this; - this.pie = []; - } open(pages, params) { if (params && params.replace) { if (params.replace === 'all') { @@ -86,4 +80,6 @@ export default class PagesPie extends Component { return null } } -} \ No newline at end of file +} + +export default singleton(PagesPie) \ No newline at end of file diff --git a/src/app/router.js b/src/app/router.js index e92fa93..a52920a 100644 --- a/src/app/router.js +++ b/src/app/router.js @@ -52,14 +52,12 @@ window.router = router; router('/', () => { //singleton - let pie = new PagesPie; - pie.open(IndexPage, {replace: 'all'}); + PagesPie.instance().open(IndexPage, {replace: 'all'}); }); router('/torrent/:hash', (e) => { //singleton - let pie = new PagesPie; - pie.open(TorrentPage, { + PagesPie.instance().open(TorrentPage, { replace: 'all', hash: e.params.hash, }); @@ -67,26 +65,21 @@ router('/torrent/:hash', (e) => { router('/DMCA', () => { //singleton - let pie = new PagesPie; - pie.open(DMCAPage, {replace: 'all'}); + PagesPie.instance().open(DMCAPage, {replace: 'all'}); }); router('/config', () => { //singleton - let pie = new PagesPie; - pie.open(AdminPage, {replace: 'all'}); + PagesPie.instance().open(AdminPage, {replace: 'all'}); }); router('/top', () => { //singleton - let pie = new PagesPie; - pie.open(TopPage, {replace: 'all'}); + PagesPie.instance().open(TopPage, {replace: 'all'}); }); router('/changelog', () => { //singleton - let pie = new PagesPie; - console.log('changelog') - pie.open(ChangelogPage, {replace: 'all'}); + PagesPie.instance().open(ChangelogPage, {replace: 'all'}); }); \ No newline at end of file diff --git a/src/app/singleton.js b/src/app/singleton.js new file mode 100644 index 0000000..a0ca4bf --- /dev/null +++ b/src/app/singleton.js @@ -0,0 +1,29 @@ +export default (Superclass) => { + let instance; + + return class Singleton extends Superclass { + constructor(props) { + super(props) + if (instance) { + instance.props = props + return instance; + } + instance = this; + } + static instance() { + if (instance) { + return instance; + } else { + return new Singleton(); + } + } + static do(key, ...params) { + if ( typeof this.instance()[key] === 'function') { + return this.instance()[key](...params); + } else { + return this.instance()[key]; + } + } + } +} + diff --git a/src/app/torrent-page.js b/src/app/torrent-page.js index 0b24d8e..00babda 100644 --- a/src/app/torrent-page.js +++ b/src/app/torrent-page.js @@ -180,7 +180,7 @@ export default class TorrentPage extends Page { }); }; getTorrentInfo() { - window.torrentSocket.emit('torrent', this.props.hash, {files: true}, window.customLoader((data) => { + window.torrentSocket.emit('torrent', this.props.hash, {files: true, peer: this.props.peer}, window.customLoader((data) => { if(data) { this.torrent = data this.setTitle(this.torrent.name + ' - Rats On TheBoat'); diff --git a/src/app/torrent.js b/src/app/torrent.js index c187e92..ca1e8c7 100644 --- a/src/app/torrent.js +++ b/src/app/torrent.js @@ -3,6 +3,9 @@ import formatBytes from './format-bytes' import {ListItem} from 'material-ui/List'; import Divider from 'material-ui/Divider'; +import PagesPie from './pages-pie.js'; +import TorrentPage from './torrent-page' + import Spinner24 from './images/spinner_24.gif' import LinearProgress from 'material-ui/LinearProgress'; @@ -212,13 +215,15 @@ export default class Torrent extends Component { if(e.button === 1) return false; + /* if(e.ctrlKey && e.button === 0) { let win = window.open(link, '_blank'); //win.focus(); return true; } + */ - window.router(link) + PagesPie.instance().open(TorrentPage, {replace: 'all', hash: torrent.hash, peer: torrent.peer}) }} primaryText={ { diff --git a/src/background/p2p.js b/src/background/p2p.js index c3863c1..0224a3d 100644 --- a/src/background/p2p.js +++ b/src/background/p2p.js @@ -204,6 +204,13 @@ class p2p { return return peers.map(peer => ({address: peer.address, port: peer.port})) } + + find(peer) + { + return this.peersList().find((localPeer) => { + return localPeer.address === peer.address + }) + } } module.exports = p2p \ No newline at end of file diff --git a/src/background/spider.js b/src/background/spider.js index 405e116..57152d0 100644 --- a/src/background/spider.js +++ b/src/background/spider.js @@ -321,14 +321,31 @@ if(dataDirectory && fs.existsSync(dataDirectory + '/peers.p2p')) }); }); - recive('torrent', function(hash, options, callback) - { + const onTorrent = (hash, options, callback) => { if(hash.length != 40) return; if(typeof callback != 'function') return; + // remote request + if(options.peer) + { + console.log('remote torrent request to peer') + const peer = p2p.find(options.peer) + if(!peer) + { + callback(undefined) + return; + } + delete options.peer; + peer.emit('torrent', {hash, options}, (data) => { + console.log('remote torrent result') + callback(data) + }) + return; + } + sphinx.query('SELECT * FROM `torrents` WHERE `hash` = ?', hash, function (error, rows, fields) { if(!rows || rows.length == 0) { callback(undefined) @@ -357,7 +374,15 @@ if(dataDirectory && fs.existsSync(dataDirectory + '/peers.p2p')) send('downloading', torrent.infoHash) } }); - }); + } + + recive('torrent', onTorrent); + p2p.on('torrent', ({hash, options} = {}, callback) => { + if(!hash) + return; + + onTorrent(hash, options, (data) => callback(data)) + }) const searchTorrentCall = function(text, navigation, callback) {