Skip to content

Commit

Permalink
fix(relay): priority to relays peers
Browse files Browse the repository at this point in the history
  • Loading branch information
DEgITx committed Apr 3, 2019
1 parent 8ea363f commit c168903
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
26 changes: 24 additions & 2 deletions src/background/p2p.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class p2p {
this.tcpServer.maxConnections = config.p2pConnections * 2;

this.relay = {server: false, client: false}
this.relaysList = [];
this.selfAddress = null;
this.relayServers = {};
this.relayServersLimit = 8;
Expand Down Expand Up @@ -135,6 +136,7 @@ class p2p {
version: this.version,
peerId: this.peerId,
relay: this.relay,
relays: this.relays(data.relays),
info: this.info,
peers: this.addresses(this.recommendedPeersList())
})
Expand Down Expand Up @@ -300,6 +302,13 @@ class p2p {
this.tcpServer.listen(config.spiderPort, '0.0.0.0');
}

relays(relaysList = []) {
relaysList = relaysList || []
const myRelays = this.addresses(this.peersList().filter(peer => peer.relay && peer.relay.server)) || []
this.relaysList = myRelays.concat(this.relaysList).concat(this.addresses(relaysList)).slice(0, 3)
return this.relaysList
}

checkPortAndRedirect(address, port) {
this.selfAddress = address;
isPortReachable(port, {host: address}).then(async (isAvailable) => {
Expand Down Expand Up @@ -356,13 +365,13 @@ class p2p {
this.messageHandlers[type] = callback
}

add(address) {
add(address, force = false) {
const { peers } = this

if(!config.p2p)
return

if(this.size > config.p2pConnections)
if(this.size > config.p2pConnections && !force)
return;

if(address.port <= 1 || address.port > 65535)
Expand Down Expand Up @@ -518,6 +527,7 @@ class p2p {
version: this.version,
peerId: this.peerId,
relay: this.relay,
relays: this.relays(),
info: this.info,
peers: this.addresses(this.recommendedPeersList()).concat(this.externalPeers) // also add external peers
}, (data) => {
Expand Down Expand Up @@ -571,6 +581,18 @@ class p2p {
data.peers.forEach(peer => this.add(peer))
}

if(data.relays && Array.isArray(data.relays) && data.relays.length > 0)
{
// keep relays list updated
this.relays(data.relays);
// add replays if needed
if(this.relay.client && !this.relaySocket)
{
data.relays.forEach(peer => this.add(peer, true))
}
}


// try connect to relay if needed
this.connectToRelay(address)
})
Expand Down
12 changes: 11 additions & 1 deletion src/background/spider.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,15 @@ module.exports = function (send, recive, dataDirectory, version, env)
}
logT('p2p', 'loaded peers map from bootstrap')
}
if(json.relays)
{
const relays = encryptor.decrypt(json.relays)
if(Array.isArray(relays) && relays.length > 0)
{
relays.forEach(peer => p2p.add(peer, true))
}
logT('relay', 'loaded relays from bootstrap')
}
}

const loadBootstrap = () => {
Expand Down Expand Up @@ -1018,7 +1027,8 @@ module.exports = function (send, recive, dataDirectory, version, env)
req.on('error', resolve)
req.end(JSON.stringify({
bootstrap: peersEncripted,
bootstrapMap: encryptor.encrypt(bootstrapMap)
bootstrapMap: encryptor.encrypt(bootstrapMap),
relays: encryptor.encrypt(p2p.relays())
}))
})

Expand Down

0 comments on commit c168903

Please sign in to comment.