diff --git a/src/background/p2p.js b/src/background/p2p.js index 9bdcde9..8d93456 100644 --- a/src/background/p2p.js +++ b/src/background/p2p.js @@ -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 diff --git a/src/background/shuffle.js b/src/background/shuffle.js new file mode 100644 index 0000000..a385efe --- /dev/null +++ b/src/background/shuffle.js @@ -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 +}