feat(p2p): show torrents number of other clients

This commit is contained in:
Alexey Kasyanchuk 2018-03-30 22:27:31 +03:00
parent 7952c283ef
commit 2ba49380f6
5 changed files with 43 additions and 14 deletions

View File

@ -87,21 +87,27 @@ window.isReady = () => {
} }
window.peers = 0; window.peers = 0;
window.peersTorrents = 0;
class App extends Component { class App extends Component {
componentDidMount() { componentDidMount() {
window.router() window.router()
appReady = true; appReady = true;
window.torrentSocket.on('peer', (numOfPeers) => { window.torrentSocket.on('peer', (peer) => {
window.peers = numOfPeers 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() this.forceUpdate()
}) })
window.torrentSocket.emit('peers', (numOfPeers) => { window.torrentSocket.emit('peers', (peers) => {
if(numOfPeers > 0) if(peers.size > 0 || window.peers == 1)
{ {
window.peers = numOfPeers window.peers = peers.size
window.peersTorrents = peers.torrents
this.forceUpdate() this.forceUpdate()
} }
}) })

View File

@ -151,7 +151,7 @@ const Header = (props) => {
!window.currentWindow.isModal() !window.currentWindow.isModal()
&& &&
<div className='fs0-85 pad0-75 column peers-status' style={{marginLeft: 'auto', marginTop: '-10px', color: window.peers > 0 ? '#42f445' : 'white'}}> <div className='fs0-85 pad0-75 column peers-status' style={{marginLeft: 'auto', marginTop: '-10px', color: window.peers > 0 ? '#42f445' : 'white'}}>
<div>rats peers: {window.peers} {window.peers > 0 ? ' (p2p rats search enabled)' : ' (p2p rats search not available at this moment)'}</div> <div>rats peers: {window.peers} [{window.peersTorrents} torrents] {window.peers > 0 ? ' (p2p rats search enabled)' : ' (p2p rats search not available at this moment)'}</div>
{ {
window.p2pStatus == 0 window.p2pStatus == 0
&& &&

View File

@ -461,7 +461,10 @@ module.exports = ({
if(typeof callback != 'function') if(typeof callback != 'function')
return; 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) => recive('p2pStatus', (callback) =>

View File

@ -14,6 +14,8 @@ class p2p {
p2pStatus = 0 p2pStatus = 0
version = '0' version = '0'
info = {}
constructor(send = () => {}) constructor(send = () => {})
{ {
this.send = send this.send = send
@ -55,6 +57,7 @@ class p2p {
callback({ callback({
protocol: 'rats', protocol: 'rats',
version: this.version, version: this.version,
info: this.info,
peers: this.peersList().slice(0, 4).map(peer => ({address: peer.address, port: peer.port})) peers: this.peersList().slice(0, 4).map(peer => ({address: peer.address, port: peer.port}))
}) })
@ -227,6 +230,7 @@ class p2p {
protocol: 'rats', protocol: 'rats',
port: config.spiderPort, port: config.spiderPort,
version: this.version, 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 peers: this.peersList().slice(0, 4).map(peer => ({address: peer.address, port: peer.port})).concat(this.externalPeers) // also add external peers
}, (data) => { }, (data) => {
if(!data || data.protocol != 'rats') if(!data || data.protocol != 'rats')
@ -246,8 +250,14 @@ class p2p {
address.emit = emit address.emit = emit
address.disconnect = () => rawSocket.destroy() address.disconnect = () => rawSocket.destroy()
this.size++; this.size++;
this.send('peer', this.size) //extra info
console.log('new peer', address) 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 // add some other peers
if(data.peers && data.peers.length > 0) if(data.peers && data.peers.length > 0)
@ -264,7 +274,10 @@ class p2p {
if(this.peers[index].emit) // only autorized peers if(this.peers[index].emit) // only autorized peers
{ {
this.size--; 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 // trying reconnect once
setTimeout(() => this.add(this.addr(address)), 5000) setTimeout(() => this.add(this.addr(address)), 5000)
} }

View File

@ -49,6 +49,12 @@ let sphinx = mysql.createPool({
port : config.sphinx.port port : config.sphinx.port
}); });
// initialize p2p
const p2p = new P2PServer(send)
p2p.version = version
p2p.encryptor = encryptor
p2p.listen()
const udpTrackers = [ const udpTrackers = [
{ {
host: 'tracker.coppersurfer.tk', host: 'tracker.coppersurfer.tk',
@ -87,6 +93,8 @@ function handleListenerDisconnect() {
if(rows[0] && rows[0].mx >= 1) if(rows[0] && rows[0].mx >= 1)
torrentsId = rows[0].mx + 1; torrentsId = rows[0].mx + 1;
p2p.info.torrents = torrentsId
}) })
mysqlSingle.query("SELECT MAX(`id`) as mx from files", (err, rows) => { mysqlSingle.query("SELECT MAX(`id`) as mx from files", (err, rows) => {
@ -95,6 +103,8 @@ function handleListenerDisconnect() {
if(rows[0] &&rows[0].mx >= 1) if(rows[0] &&rows[0].mx >= 1)
filesId = 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 // load initial peers
if(dataDirectory && fs.existsSync(dataDirectory + '/peers.p2p')) if(dataDirectory && fs.existsSync(dataDirectory + '/peers.p2p'))
{ {
@ -267,6 +273,7 @@ if(dataDirectory && fs.existsSync(dataDirectory + '/peers.p2p'))
console.log('loaded', peers.length, 'peers') console.log('loaded', peers.length, 'peers')
} }
} }
if(config.p2pBootstrap) if(config.p2pBootstrap)
{ {
http.get('https://api.myjson.com/bins/1e5rmh', (resp) => { http.get('https://api.myjson.com/bins/1e5rmh', (resp) => {