fix(relay): priority to relays peers

This commit is contained in:
Alexey Kasyanchuk
2019-04-03 00:25:37 +01:00
parent 4cb47e35a9
commit 620a6c0c5e
2 changed files with 35 additions and 3 deletions

View File

@ -52,6 +52,7 @@ class p2p {
this.tcpServer.maxConnections = config.p2pConnections * 2; this.tcpServer.maxConnections = config.p2pConnections * 2;
this.relay = {server: false, client: false} this.relay = {server: false, client: false}
this.relaysList = [];
this.selfAddress = null; this.selfAddress = null;
this.relayServers = {}; this.relayServers = {};
this.relayServersLimit = 8; this.relayServersLimit = 8;
@ -135,6 +136,7 @@ class p2p {
version: this.version, version: this.version,
peerId: this.peerId, peerId: this.peerId,
relay: this.relay, relay: this.relay,
relays: this.relays(data.relays),
info: this.info, info: this.info,
peers: this.addresses(this.recommendedPeersList()) peers: this.addresses(this.recommendedPeersList())
}) })
@ -300,6 +302,13 @@ class p2p {
this.tcpServer.listen(config.spiderPort, '0.0.0.0'); 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) { checkPortAndRedirect(address, port) {
this.selfAddress = address; this.selfAddress = address;
isPortReachable(port, {host: address}).then(async (isAvailable) => { isPortReachable(port, {host: address}).then(async (isAvailable) => {
@ -356,13 +365,13 @@ class p2p {
this.messageHandlers[type] = callback this.messageHandlers[type] = callback
} }
add(address) { add(address, force = false) {
const { peers } = this const { peers } = this
if(!config.p2p) if(!config.p2p)
return return
if(this.size > config.p2pConnections) if(this.size > config.p2pConnections && !force)
return; return;
if(address.port <= 1 || address.port > 65535) if(address.port <= 1 || address.port > 65535)
@ -518,6 +527,7 @@ class p2p {
version: this.version, version: this.version,
peerId: this.peerId, peerId: this.peerId,
relay: this.relay, relay: this.relay,
relays: this.relays(),
info: this.info, info: this.info,
peers: this.addresses(this.recommendedPeersList()).concat(this.externalPeers) // also add external peers peers: this.addresses(this.recommendedPeersList()).concat(this.externalPeers) // also add external peers
}, (data) => { }, (data) => {
@ -571,6 +581,18 @@ class p2p {
data.peers.forEach(peer => this.add(peer)) 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 // try connect to relay if needed
this.connectToRelay(address) this.connectToRelay(address)
}) })

View File

@ -316,6 +316,15 @@ module.exports = function (send, recive, dataDirectory, version, env)
} }
logT('p2p', 'loaded peers map from bootstrap') 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 = () => { const loadBootstrap = () => {
@ -1018,7 +1027,8 @@ module.exports = function (send, recive, dataDirectory, version, env)
req.on('error', resolve) req.on('error', resolve)
req.end(JSON.stringify({ req.end(JSON.stringify({
bootstrap: peersEncripted, bootstrap: peersEncripted,
bootstrapMap: encryptor.encrypt(bootstrapMap) bootstrapMap: encryptor.encrypt(bootstrapMap),
relays: encryptor.encrypt(p2p.relays())
})) }))
}) })