feat(p2p): random peer exchange
This commit is contained in:
parent
5daf9f5c65
commit
fac6736710
@ -1,4 +1,5 @@
|
||||
import ssh from './ssh'
|
||||
import shuffle from './shuffle'
|
||||
const config = require('./config');
|
||||
const net = require('net')
|
||||
const JsonSocket = require('json-socket')
|
||||
@ -58,7 +59,7 @@ class p2p {
|
||||
protocol: 'rats',
|
||||
version: this.version,
|
||||
info: this.info,
|
||||
peers: this.peersList().slice(0, 4).map(peer => ({address: peer.address, port: peer.port}))
|
||||
peers: shuffle(this.peersList()).slice(0, 4).map(peer => ({address: peer.address, port: peer.port}))
|
||||
})
|
||||
|
||||
// try to connect back
|
||||
@ -231,7 +232,7 @@ class p2p {
|
||||
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
|
||||
peers: shuffle(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')
|
||||
return
|
||||
|
14
src/background/shuffle.js
Normal file
14
src/background/shuffle.js
Normal file
@ -0,0 +1,14 @@
|
||||
/**
|
||||
* Shuffles array in place.
|
||||
* @param {Array} a items An array containing the items.
|
||||
*/
|
||||
export default function shuffle(a) {
|
||||
let j, x, i;
|
||||
for (i = a.length - 1; i > 0; i--) {
|
||||
j = Math.floor(Math.random() * (i + 1));
|
||||
x = a[i];
|
||||
a[i] = a[j];
|
||||
a[j] = x;
|
||||
}
|
||||
return a
|
||||
}
|
Loading…
Reference in New Issue
Block a user