From f89bd0c77cad93b5cba54562e67e713f68cc16d3 Mon Sep 17 00:00:00 2001 From: diegomrsantos Date: Thu, 9 Feb 2023 16:53:46 +0100 Subject: [PATCH] Autonat dials dns addrs (#856) --- libp2p/multiaddress.nim | 1 + .../protocols/connectivity/autonat/client.nim | 2 +- .../protocols/connectivity/autonat/server.nim | 2 +- tests/testautonat.nim | 33 +++++++++++++++++-- 4 files changed, 33 insertions(+), 5 deletions(-) diff --git a/libp2p/multiaddress.nim b/libp2p/multiaddress.nim index a664684a0..c046dba06 100644 --- a/libp2p/multiaddress.nim +++ b/libp2p/multiaddress.nim @@ -462,6 +462,7 @@ const IP6* = mapEq("ip6") DNS* = mapOr(DNSANY, DNS4, DNS6, DNSADDR) IP* = mapOr(IP4, IP6) + DNS_OR_IP* = mapOr(DNS, IP) TCP* = mapOr(mapAnd(DNS, mapEq("tcp")), mapAnd(IP, mapEq("tcp"))) UDP* = mapOr(mapAnd(DNS, mapEq("udp")), mapAnd(IP, mapEq("udp"))) UTP* = mapAnd(UDP, mapEq("utp")) diff --git a/libp2p/protocols/connectivity/autonat/client.nim b/libp2p/protocols/connectivity/autonat/client.nim index ef1279c0b..55e0183c4 100644 --- a/libp2p/protocols/connectivity/autonat/client.nim +++ b/libp2p/protocols/connectivity/autonat/client.nim @@ -55,7 +55,7 @@ method dialMe*(self: AutonatClient, switch: Switch, pid: PeerId, addrs: seq[Mult else: await switch.dial(pid, addrs, AutonatCodec) except CatchableError as err: - raise newException(AutonatError, "Unexpected error when dialling", err) + raise newException(AutonatError, "Unexpected error when dialling: " & err.msg, err) # To bypass maxConnectionsPerPeer let incomingConnection = switch.connManager.expectConnection(pid) diff --git a/libp2p/protocols/connectivity/autonat/server.nim b/libp2p/protocols/connectivity/autonat/server.nim index a987ed023..e8f1077e8 100644 --- a/libp2p/protocols/connectivity/autonat/server.nim +++ b/libp2p/protocols/connectivity/autonat/server.nim @@ -117,7 +117,7 @@ proc handleDial(autonat: Autonat, conn: Connection, msg: AutonatMsg): Future[voi if isRelayed.isErr() or isRelayed.get(): continue let maFirst = ma[0] - if maFirst.isErr() or not IP.match(maFirst.get()): + if maFirst.isErr() or not DNS_OR_IP.match(maFirst.get()): continue try: diff --git a/tests/testautonat.nim b/tests/testautonat.nim index 19a6f2c12..fcdcb3b4b 100644 --- a/tests/testautonat.nim +++ b/tests/testautonat.nim @@ -7,18 +7,24 @@ import builders, protocols/connectivity/autonat/client, protocols/connectivity/autonat/server, + nameresolving/nameresolver, + nameresolving/mockresolver, ], ./helpers -proc createAutonatSwitch(): Switch = - result = SwitchBuilder.new() +proc createAutonatSwitch(nameResolver: NameResolver = nil): Switch = + var builder = SwitchBuilder.new() .withRng(newRng()) .withAddresses(@[ MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet() ]) .withTcpTransport() .withMplex() .withAutonat() .withNoise() - .build() + + if nameResolver != nil: + builder = builder.withNameResolver(nameResolver) + + return builder.build() proc makeAutonatServicePrivate(): Switch = var autonatProtocol = new LPProtocol @@ -88,3 +94,24 @@ suite "Autonat": response.text.get() == "Dial timeout" response.ma.isNone() await allFutures(doesNothingListener.stop(), src.stop(), dst.stop()) + + asyncTest "dialMe dials dns and returns public address": + let resolver = MockResolver.new() + resolver.ipResponses[("localhost", false)] = @["127.0.0.1"] + resolver.ipResponses[("localhost", true)] = @["::1"] + + let + src = newStandardSwitch() + dst = createAutonatSwitch(nameResolver = resolver) + + await src.start() + await dst.start() + + let testAddr = MultiAddress.init("/dns4/localhost/").tryGet() & + dst.peerInfo.addrs[0][1].tryGet() + + await src.connect(dst.peerInfo.peerId, dst.peerInfo.addrs) + let ma = await AutonatClient.new().dialMe(src, dst.peerInfo.peerId, @[testAddr]) + + check ma in src.peerInfo.addrs + await allFutures(src.stop(), dst.stop())