Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

96 lines
3.0 KiB
Nim
Raw Normal View History

## Logos Storage
## Copyright (c) 2022 Status Research & Development GmbH
## Licensed under either of
## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
## * MIT license ([LICENSE-MIT](LICENSE-MIT))
## at your option.
## This file may not be copied, modified, or distributed except according to
## those terms.
2025-12-11 10:03:43 +01:00
{.push raises: [], gcsafe.}
2025-12-11 22:03:36 +01:00
import std/net
import std/strutils
import std/options
import pkg/libp2p
2026-06-10 10:54:02 +04:00
import pkg/libp2p/wire
2026-06-05 19:49:29 +04:00
import pkg/stew/endians2
func remapAddr*(
address: MultiAddress,
Chore/update nim version (#1052) * Move to version 2.0.6 * Update nim-confutils submodule to latest version * Update dependencies * Update Nim version to 2.0.12 * Add gcsafe pragma * Add missing import * Update specific conf for Nim 2.x * Fix method signatures * Revert erasure coding attempt to fix bug * More gcsafe pragma * Duplicate code from libp2p because it is not exported anymore * Fix camelcase function names * Use alreadySeen because need is not a bool anymore * newLPStreamReadError does not exist anymore so use another error * Replace ValidIpAddress by IpAddress * Add gcsafe pragma * Restore maintenance parameter deleted by mistake when removing esasure coding fix attempt code * Update method signatures * Copy LPStreamReadError code from libp2p which was removed * Fix camel case * Fix enums in tests * Fix camel case * Extract node components to a variable to make Nim 2 happy * Update the tests using ValidIpAddress to IpAddress * Fix cast for value which is already an option * Set nim version to 2.0.x for CI * Set nim version to 2.0.x for CI * Move to miniupnp version 2.2.4 to avoid symlink error * Set core.symlinks to false for Windows for miniupnp >= 2.2.5 support * Update to Nim 2.0.14 * Update CI nim versions to 2.0.14 * Try with GCC 14 * Replace apt-fast by apt-get * Update ubuntu runner to latest * Use Ubuntu 20.04 for coverage * Disable CI cache for coverage * Add coverage property description * Remove commented test * Check the node value of seen instead of using alreadySeen * Fix the merge. The taskpool work was reverted. * Update nim-ethers submodule * Remove deprecated ValidIpAddress. Fix missing case and imports. * Fix a weird issue where nim-confutils cannot find NatAny * Fix tests and remove useless static keyword
2025-01-10 15:12:37 +01:00
ip: Option[IpAddress] = IpAddress.none,
port: Option[Port] = Port.none,
protocol: Option[string] = string.none,
): MultiAddress =
## Remap addresses to new IP, port, and/or transport protocol (e.g. "tcp" → "udp")
##
2026-06-05 18:54:06 +04:00
## Assumes a /ip4|ip6/<ip>/tcp|udp/<port> address: anything else crashes (Defect).
var parts = ($address).split("/")
parts[2] =
if ip.isSome:
$ip.get
else:
parts[2]
parts[3] =
if protocol.isSome:
protocol.get
else:
parts[3]
parts[4] =
if port.isSome:
$port.get
else:
parts[4]
MultiAddress.init(parts.join("/")).expect("Should construct multiaddress")
2026-06-04 14:37:47 +04:00
func getTcpPort*(ma: MultiAddress): Option[Port] =
2026-06-05 19:49:29 +04:00
## Extracts the TCP port from a multiaddress; none when there is no TCP part.
let tcpPart = ma[multiCodec("tcp")]
if tcpPart.isErr:
return Port.none
let portBytes = tcpPart.get().protoArgument()
if portBytes.isErr or portBytes.get().len != 2:
return Port.none
some(Port(fromBytesBE(uint16, portBytes.get())))
2026-06-04 14:37:47 +04:00
2026-06-10 10:54:02 +04:00
proc hasPublicRelayTransport*(ma: MultiAddress): bool =
## True when ``ma`` is a circuit address whose relay is publicly dialable.
## A circuit address is <relay wire addr>/p2p/<relayId>/p2p-circuit; the part
## before /p2p/ is the relay's wire address, which isPublicMA can check.
## Unlike libp2p's publicRoutableAddressPolicy we drop non-public relays: our
## relay path only runs on a genuine NAT, where the relay is always public.
let relayWireStr = ($ma).split("/p2p/")[0]
let relayWireAddr = MultiAddress.init(relayWireStr).valueOr:
return false
relayWireAddr.isPublicMA()
proc dialableAddressPolicy*(ma: MultiAddress): bool {.gcsafe, raises: [].} =
# Use with switchBuilder.withAddressPolicy.
# Filter the peerInfo.addrs updated by libp2p without
# declaring another address mapper.
if ma.isCircuitRelayMA():
ma.hasPublicRelayTransport()
else:
ma.isPublicMA()
proc getMultiAddrWithIpAndTcpPort*(ip: IpAddress, port: Port): MultiAddress =
## Creates a MultiAddress with the specified IP address and TCP port
2026-06-10 10:54:02 +04:00
##
## Parameters:
## - ip: A valid IP address (IPv4 or IPv6)
## - port: The TCP port number
##
## Returns:
## A MultiAddress in the format "/ip4/<address>/tcp/<port>" or "/ip6/<address>/tcp/<port>"
let ipFamily = if ip.family == IpAddressFamily.IPv4: "/ip4/" else: "/ip6/"
return MultiAddress.init(ipFamily & $ip & "/tcp/" & $port).expect(
"Failed to construct multiaddress with IP and TCP port"
)