Use setupAddress for better IP and ports configuration

This commit is contained in:
kdeme 2021-02-16 21:35:10 +01:00 committed by zah
parent d47f53cd9d
commit 5f750f84b4
7 changed files with 19 additions and 58 deletions

View File

@ -5,7 +5,7 @@ import
chronicles, chronicles/options as chroniclesOptions,
confutils, confutils/defs, confutils/std/net, stew/shims/net as stewNet,
stew/io2, unicodedb/properties, normalize,
eth/common/eth_types as commonEthTypes,
eth/common/eth_types as commonEthTypes, eth/net/nat,
eth/p2p/discoveryv5/enr,
json_serialization, web3/[ethtypes, confutils_defs],
spec/[crypto, keystore, digest, datatypes, network],
@ -170,7 +170,8 @@ type
nat* {.
desc: "Specify method to use for determining public address. " &
"Must be one of: any, none, upnp, pmp, extip:<IP>"
defaultValue: "any" }: string
defaultValue: NatConfig(hasExtIp: false, nat: NatAny)
name: "nat" .}: NatConfig
enrAutoUpdate* {.
defaultValue: false

View File

@ -75,7 +75,7 @@ proc loadBootstrapFile*(bootstrapFile: string,
proc new*(T: type Eth2DiscoveryProtocol,
config: BeaconNodeConf,
ip: Option[ValidIpAddress], tcpPort, udpPort: Port,
enrIp: Option[ValidIpAddress], enrTcpPort, enrUdpPort: Option[Port],
pk: PrivateKey,
enrFields: openArray[(string, seq[byte])], rng: ref BrHmacDrbgContext):
T {.raises: [Exception, Defect].} =
@ -92,6 +92,6 @@ proc new*(T: type Eth2DiscoveryProtocol,
if fileExists(persistentBootstrapFile):
loadBootstrapFile(persistentBootstrapFile, bootstrapEnrs)
newProtocol(pk, ip, tcpPort, udpPort, enrFields, bootstrapEnrs,
bindIp = config.listenAddress, enrAutoUpdate = config.enrAutoUpdate,
rng = rng)
newProtocol(pk, enrIp, enrTcpPort, enrUdpPort, enrFields, bootstrapEnrs,
bindPort = config.udpPort, bindIp = config.listenAddress,
enrAutoUpdate = config.enrAutoUpdate, rng = rng)

View File

@ -1062,7 +1062,7 @@ proc onConnEvent(node: Eth2Node, peerId: PeerID, event: ConnEvent) {.async.} =
proc new*(T: type Eth2Node, config: BeaconNodeConf, enrForkId: ENRForkID,
switch: Switch, pubsub: GossipSub, ip: Option[ValidIpAddress],
tcpPort, udpPort: Port, privKey: keys.PrivateKey, discovery: bool,
tcpPort, udpPort: Option[Port], privKey: keys.PrivateKey, discovery: bool,
rng: ref BrHmacDrbgContext): T =
let
metadata = getPersistentNetMetadata(config)
@ -1315,49 +1315,6 @@ proc p2pProtocolBackendImpl*(p: P2PProtocol): Backend =
result.implementProtocolInit = proc (p: P2PProtocol): NimNode =
return newCall(initProtocol, newLit(p.name), p.peerInit, p.netInit)
proc setupNat(config: BeaconNodeConf): tuple[ip: Option[ValidIpAddress],
tcpPort: Port,
udpPort: Port] =
# defaults
result.tcpPort = config.tcpPort
result.udpPort = config.udpPort
var nat: NatStrategy
case config.nat.toLowerAscii:
of "any":
nat = NatAny
of "none":
nat = NatNone
of "upnp":
nat = NatUpnp
of "pmp":
nat = NatPmp
else:
if config.nat.startsWith("extip:"):
try:
# any required port redirection is assumed to be done by hand
result.ip = some(ValidIpAddress.init(config.nat[6..^1]))
nat = NatNone
except ValueError:
error "nor a valid IP address", address = config.nat[6..^1]
quit QuitFailure
else:
error "not a valid NAT mechanism", value = config.nat
quit QuitFailure
if nat != NatNone:
let extIp = getExternalIP(nat)
if extIP.isSome:
result.ip = some(ValidIpAddress.init extIp.get)
# TODO redirectPorts in considered a gcsafety violation
# because it obtains the address of a non-gcsafe proc?
let extPorts = ({.gcsafe.}:
redirectPorts(tcpPort = result.tcpPort,
udpPort = result.udpPort,
description = clientId))
if extPorts.isSome:
(result.tcpPort, result.udpPort) = extPorts.get()
func asLibp2pKey*(key: keys.PublicKey): PublicKey =
PublicKey(scheme: Secp256k1, skkey: secp.SkPublicKey(key))
@ -1540,10 +1497,11 @@ proc createEth2Node*(rng: ref BrHmacDrbgContext,
netKeys: KeyPair,
enrForkId: ENRForkID): Eth2Node =
var
(extIp, extTcpPort, extUdpPort) = setupNat(config)
(extIp, extTcpPort, extUdpPort) = setupAddress(config.nat,
config.listenAddress, config.tcpPort, config.udpPort, clientId)
hostAddress = tcpEndPoint(config.listenAddress, config.tcpPort)
announcedAddresses = if extIp.isNone(): @[]
else: @[tcpEndPoint(extIp.get(), extTcpPort)]
announcedAddresses = if extIp.isNone() or extTcpPort.isNone(): @[]
else: @[tcpEndPoint(extIp.get(), extTcpPort.get())]
debug "Initializing networking", hostAddress,
network_public_key = netKeys.pubkey,

View File

@ -17,7 +17,7 @@ import
chronicles, bearssl, blscurve,
json_serialization/std/[options, sets, net], serialization/errors,
eth/[keys, async_utils],
eth/[keys, async_utils], eth/net/nat,
eth/db/[kvstore, kvstore_sqlite3],
eth/p2p/enode, eth/p2p/discoveryv5/[protocol, enr, random2],

View File

@ -412,9 +412,11 @@ proc bootstrapDiscovery(conf: InspectorConf,
if enrFields.isSome():
let fields = enrFields.get()
let pairs = {"eth2": fields.eth2, "attnets": fields.attnets}
result = newProtocol(pk, host, tcpPort, udpPort, pairs, bootnodes)
result = newProtocol(pk, host, some(tcpPort), some(udpPort), pairs,
bootnodes, bindPort = udpPort)
else:
result = newProtocol(pk, host, tcpPort, udpPort, [], bootnodes)
result = newProtocol(pk, host, some(tcpPort), some(udpPort), [],
bootnodes, bindPort = udpPort)
result.open()
result.start()

2
vendor/nim-chronos vendored

@ -1 +1 @@
Subproject commit 25688cd0aa51f5e28c1e55dca1b3a9a73cb3dafa
Subproject commit 03707426e43d03cccc1de2e7284de168b79f7bf6

2
vendor/nim-eth vendored

@ -1 +1 @@
Subproject commit e8c9691b35f4875d9a97193cb965fbf1a956dd7e
Subproject commit be5e088b21e06a85cac4826454412db8459ed4f1