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,45 +132,51 @@ proc getLastSeenAddress(node: BeaconNode, id: PeerId): string =
var temp: seq[tuple[address: MultiAddress, position: int]] var temp: seq[tuple[address: MultiAddress, position: int]]
for address in addresses: for address in addresses:
let res = let res =
if TCP.matchPartial(address): block:
# TODO (cheatfate): We match TCP here because `nim-libp2p` do not have let
# QUIC support yet. So we give TCP addresses priority. isUDP = UDP.matchPartial(address)
if IP4.matchPartial(address): isTCP = TCP.matchPartial(address)
let address4 = address.getProtocolArgument(multiCodec("ip4")).valueOr:
continue if isUDP or isTCP:
var ta4 = TransportAddress(family: AddressFamily.IPv4) # TODO (cheatfate): We match TCP here because `nim-libp2p` do not have
ta4.address_v4[0 .. 3] = address4[0 .. 3] # QUIC support yet. So we give TCP addresses priority.
if ta4.isLoopback(): let boost = if isUDP: 100 else 0
(address, 9) if IP4.matchPartial(address):
elif ta4.isLinkLocal(): let address4 =
(address, 7) address.getProtocolArgument(multiCodec("ip4")).valueOr:
elif ta4.isSiteLocal(): continue
(address, 5) var ta4 = TransportAddress(family: AddressFamily.IPv4)
elif ta4.isGlobal(): ta4.address_v4[0 .. 3] = address4[0 .. 3]
(address, 2) if ta4.isLoopback():
(address, boost + 9)
elif ta4.isLinkLocal():
(address, boost + 7)
elif ta4.isSiteLocal():
(address, boost + 5)
elif ta4.isGlobal():
(address, boost + 2)
else:
(address, boost + 11)
elif IP6.matchPartial(address):
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, boost + 8)
elif ta6.isLinkLocal():
(address, boost + 6)
elif ta6.isSiteLocal():
(address, boost + 4)
elif ta6.isGlobal():
(address, boost + 1)
else:
(address, boost + 10)
else: else:
(address, 11) (address, boost + 3)
elif IP6.matchPartial(address):
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)
elif ta6.isLinkLocal():
(address, 6)
elif ta6.isSiteLocal():
(address, 4)
elif ta6.isGlobal():
(address, 1)
else:
(address, 10)
else: else:
(address, 3) (address, 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) temp.add(res)
if len(temp) > 0: if len(temp) > 0: