feat(p2p): random peer exchange
This commit is contained in:
parent
5daf9f5c65
commit
fac6736710
@ -1,4 +1,5 @@
|
|||||||
import ssh from './ssh'
|
import ssh from './ssh'
|
||||||
|
import shuffle from './shuffle'
|
||||||
const config = require('./config');
|
const config = require('./config');
|
||||||
const net = require('net')
|
const net = require('net')
|
||||||
const JsonSocket = require('json-socket')
|
const JsonSocket = require('json-socket')
|
||||||
@ -58,7 +59,7 @@ class p2p {
|
|||||||
protocol: 'rats',
|
protocol: 'rats',
|
||||||
version: this.version,
|
version: this.version,
|
||||||
info: this.info,
|
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
|
// try to connect back
|
||||||
@ -231,7 +232,7 @@ class p2p {
|
|||||||
port: config.spiderPort,
|
port: config.spiderPort,
|
||||||
version: this.version,
|
version: this.version,
|
||||||
info: this.info,
|
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) => {
|
}, (data) => {
|
||||||
if(!data || data.protocol != 'rats')
|
if(!data || data.protocol != 'rats')
|
||||||
return
|
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