Attempt to avoid QUIC addresses from site-local networks.

This commit is contained in:
cheatfate 2024-10-04 04:11:47 +03:00
parent 2f0580faf3
commit 268ac36144
No known key found for this signature in database
GPG Key ID: 46ADD633A7201F95
1 changed files with 42 additions and 36 deletions

View File

@ -132,44 +132,50 @@ proc getLastSeenAddress(node: BeaconNode, id: PeerId): string =
var temp: seq[tuple[address: MultiAddress, position: int]]
for address in addresses:
let res =
if TCP.matchPartial(address):
block:
let
isUDP = UDP.matchPartial(address)
isTCP = TCP.matchPartial(address)
if isUDP or isTCP:
# TODO (cheatfate): We match TCP here because `nim-libp2p` do not have
# QUIC support yet. So we give TCP addresses priority.
let boost = if isUDP: 100 else 0
if IP4.matchPartial(address):
let address4 = address.getProtocolArgument(multiCodec("ip4")).valueOr:
let address4 =
address.getProtocolArgument(multiCodec("ip4")).valueOr:
continue
var ta4 = TransportAddress(family: AddressFamily.IPv4)
ta4.address_v4[0 .. 3] = address4[0 .. 3]
if ta4.isLoopback():
(address, 9)
(address, boost + 9)
elif ta4.isLinkLocal():
(address, 7)
(address, boost + 7)
elif ta4.isSiteLocal():
(address, 5)
(address, boost + 5)
elif ta4.isGlobal():
(address, 2)
(address, boost + 2)
else:
(address, 11)
(address, boost + 11)
elif IP6.matchPartial(address):
let address6 = address.getProtocolArgument(multiCodec("ip6")).valueOr:
let address6 =
address.getProtocolArgument(multiCodec("ip6")).valueOr:
continue
var ta6 = TransportAddress(family: AddressFamily.IPv4)
ta6.address_v6[0 .. 15] = address6[0 .. 15]
if ta6.isLoopback():
(address, 8)
(address, boost + 8)
elif ta6.isLinkLocal():
(address, 6)
(address, boost + 6)
elif ta6.isSiteLocal():
(address, 4)
(address, boost + 4)
elif ta6.isGlobal():
(address, 1)
(address, boost + 1)
else:
(address, 10)
(address, boost + 10)
else:
(address, 3)
(address, boost + 3)
else:
# TODO (chatfate): As soon as QUIC protocol will be implemented in
# `nim-libp2p` - TCP prioritization should be dropped
(address, 3)
temp.add(res)