fix(relay): priority to relays peers
This commit is contained in:
parent
4cb47e35a9
commit
620a6c0c5e
@ -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;
|
||||
@ -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())
|
||||
})
|
||||
@ -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) => {
|
||||
@ -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)
|
||||
@ -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) => {
|
||||
@ -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)
|
||||
})
|
||||
|
@ -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 = () => {
|
||||
@ -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())
|
||||
}))
|
||||
})
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user