From 2ba49380f61200264b5e1a9baf347dc57dd20e56 Mon Sep 17 00:00:00 2001 From: Alexey Kasyanchuk Date: Fri, 30 Mar 2018 22:27:31 +0300 Subject: [PATCH] feat(p2p): show torrents number of other clients --- src/app/app.js | 16 +++++++++++----- src/app/header.js | 2 +- src/background/api.js | 5 ++++- src/background/p2p.js | 19 ++++++++++++++++--- src/background/spider.js | 15 +++++++++++---- 5 files changed, 43 insertions(+), 14 deletions(-) diff --git a/src/app/app.js b/src/app/app.js index f2fba1d..a7f2897 100644 --- a/src/app/app.js +++ b/src/app/app.js @@ -87,21 +87,27 @@ window.isReady = () => { } window.peers = 0; +window.peersTorrents = 0; class App extends Component { componentDidMount() { window.router() appReady = true; - window.torrentSocket.on('peer', (numOfPeers) => { - window.peers = numOfPeers + window.torrentSocket.on('peer', (peer) => { + if(peer.size > window.peers) + window.peersTorrents = (window.peersTorrents || 0) + peer.torrents + else + window.peersTorrents = (window.peersTorrents || 0) - peer.torrents + window.peers = peer.size this.forceUpdate() }) - window.torrentSocket.emit('peers', (numOfPeers) => { - if(numOfPeers > 0) + window.torrentSocket.emit('peers', (peers) => { + if(peers.size > 0 || window.peers == 1) { - window.peers = numOfPeers + window.peers = peers.size + window.peersTorrents = peers.torrents this.forceUpdate() } }) diff --git a/src/app/header.js b/src/app/header.js index 9fa8daf..1447ce1 100644 --- a/src/app/header.js +++ b/src/app/header.js @@ -151,7 +151,7 @@ const Header = (props) => { !window.currentWindow.isModal() &&
0 ? '#42f445' : 'white'}}> -
rats peers: {window.peers} {window.peers > 0 ? ' (p2p rats search enabled)' : ' (p2p rats search not available at this moment)'}
+
rats peers: {window.peers} [{window.peersTorrents} torrents] {window.peers > 0 ? ' (p2p rats search enabled)' : ' (p2p rats search not available at this moment)'}
{ window.p2pStatus == 0 && diff --git a/src/background/api.js b/src/background/api.js index 0a91708..ccf4f45 100644 --- a/src/background/api.js +++ b/src/background/api.js @@ -461,7 +461,10 @@ module.exports = ({ if(typeof callback != 'function') return; - callback(p2p.size) + callback({ + size: p2p.size, + torrents: p2p.peersList().reduce((a, b) => (a.info ? a.info.torrents || 0 : 0) + (b.info ? b.info.torrents || 0 : 0), 0) + }) }); recive('p2pStatus', (callback) => diff --git a/src/background/p2p.js b/src/background/p2p.js index b53440c..9bdcde9 100644 --- a/src/background/p2p.js +++ b/src/background/p2p.js @@ -14,6 +14,8 @@ class p2p { p2pStatus = 0 version = '0' + info = {} + constructor(send = () => {}) { this.send = send @@ -55,6 +57,7 @@ class p2p { callback({ protocol: 'rats', version: this.version, + info: this.info, peers: this.peersList().slice(0, 4).map(peer => ({address: peer.address, port: peer.port})) }) @@ -227,6 +230,7 @@ class p2p { protocol: 'rats', port: config.spiderPort, version: this.version, + info: this.info, peers: this.peersList().slice(0, 4).map(peer => ({address: peer.address, port: peer.port})).concat(this.externalPeers) // also add external peers }, (data) => { if(!data || data.protocol != 'rats') @@ -246,8 +250,14 @@ class p2p { address.emit = emit address.disconnect = () => rawSocket.destroy() this.size++; - this.send('peer', this.size) - console.log('new peer', address) + //extra info + address.version = data.version + address.info = data.info + this.send('peer', { + size: this.size, + torrents: data.info ? data.info.torrents || 0 : 0 + }) + console.log('new peer', address) // add some other peers if(data.peers && data.peers.length > 0) @@ -264,7 +274,10 @@ class p2p { if(this.peers[index].emit) // only autorized peers { this.size--; - this.send('peer', this.size) + this.send('peer', { + size: this.size, + torrents: this.peers[index].info ? this.peers[index].info.torrents || 0 : 0 + }) // trying reconnect once setTimeout(() => this.add(this.addr(address)), 5000) } diff --git a/src/background/spider.js b/src/background/spider.js index c27a5fa..b8fe2e4 100644 --- a/src/background/spider.js +++ b/src/background/spider.js @@ -49,6 +49,12 @@ let sphinx = mysql.createPool({ port : config.sphinx.port }); +// initialize p2p +const p2p = new P2PServer(send) +p2p.version = version +p2p.encryptor = encryptor +p2p.listen() + const udpTrackers = [ { host: 'tracker.coppersurfer.tk', @@ -87,6 +93,8 @@ function handleListenerDisconnect() { if(rows[0] && rows[0].mx >= 1) torrentsId = rows[0].mx + 1; + + p2p.info.torrents = torrentsId }) mysqlSingle.query("SELECT MAX(`id`) as mx from files", (err, rows) => { @@ -95,6 +103,8 @@ function handleListenerDisconnect() { if(rows[0] &&rows[0].mx >= 1) filesId = rows[0].mx + 1; + + p2p.info.files = filesId }) }); @@ -252,10 +262,6 @@ function baseRowData(row) } } -const p2p = new P2PServer(send) -p2p.version = version -p2p.encryptor = encryptor -p2p.listen() // load initial peers if(dataDirectory && fs.existsSync(dataDirectory + '/peers.p2p')) { @@ -267,6 +273,7 @@ if(dataDirectory && fs.existsSync(dataDirectory + '/peers.p2p')) console.log('loaded', peers.length, 'peers') } } + if(config.p2pBootstrap) { http.get('https://api.myjson.com/bins/1e5rmh', (resp) => {