diff --git a/beacon_chain/conf.nim b/beacon_chain/conf.nim index 3f6a7dd18..72ab42dd3 100644 --- a/beacon_chain/conf.nim +++ b/beacon_chain/conf.nim @@ -60,6 +60,10 @@ type desc: "UDP listening port", defaultValue: config.defaultPort().}: int + nat* {. + desc: "Specify method to use for determining public address. Must be one of: any, extip:" + defaultValue: "any" .}: string + validators* {. required desc: "Path to a validator private key, as generated by validator_keygen" diff --git a/beacon_chain/eth2_network.nim b/beacon_chain/eth2_network.nim index 01d96896e..ffdd2a2da 100644 --- a/beacon_chain/eth2_network.nim +++ b/beacon_chain/eth2_network.nim @@ -1,5 +1,5 @@ import - options, chronos, json_serialization, + options, chronos, json_serialization, strutils, spec/digest, version, conf const @@ -19,6 +19,14 @@ when useRLPx: template libp2pProtocol*(name, version: string) {.pragma.} + func parseNat(nat: string): IpAddress = + # TODO we should try to discover the actual external IP, in case we're + # behind a nat / upnp / etc.. + if nat.startsWith("extip:"): + parseIpAddress(nat[6..^1]) + else: + parseIpAddress("127.0.0.1") + proc writeValue*(writer: var JsonWriter, value: BootstrapAddr) {.inline.} = writer.writeValue $value @@ -36,7 +44,7 @@ when useRLPx: let keys = KeyPair(seckey: privKey, pubkey: privKey.getPublicKey()) - address = Address(ip: parseIpAddress("127.0.0.1"), + address = Address(ip: parseNat(conf.nat), tcpPort: Port conf.tcpPort, udpPort: Port conf.udpPort) diff --git a/tests/simulation/run_node.sh b/tests/simulation/run_node.sh index fe1c6ba69..a1662327c 100755 --- a/tests/simulation/run_node.sh +++ b/tests/simulation/run_node.sh @@ -14,6 +14,7 @@ DATA_DIR=$SIMULATION_DIR/node-${1} V_PREFIX="$VALIDATORS_DIR/v$(printf '%06d' ${1})" PORT=$(printf '5%04d' ${1}) +MYIP=$(curl -s ifconfig.me) $BEACON_NODE_BIN \ --network:ephemeralNetwork \ @@ -30,5 +31,6 @@ $BEACON_NODE_BIN \ --validator:${V_PREFIX}9.privkey \ --tcpPort:$PORT \ --udpPort:$PORT \ + --nat:extip:$MYIP \ --stateSnapshot:$SNAPSHOT_FILE \ $BOOTSTRAP_NODES_FLAG