diff --git a/libp2p/multiaddress.nim b/libp2p/multiaddress.nim index a664684..c046dba 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 ef1279c..55e0183 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 a987ed0..e8f1077 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 19a6f2c..fcdcb3b 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())