2023-01-06 10:14:38 +00:00
|
|
|
# Nim-LibP2P
|
2023-01-20 14:47:40 +00:00
|
|
|
# Copyright (c) 2023 Status Research & Development GmbH
|
2023-01-06 10:14:38 +00:00
|
|
|
# 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.
|
|
|
|
|
2023-06-07 11:12:49 +00:00
|
|
|
{.push raises: [].}
|
2023-01-06 10:14:38 +00:00
|
|
|
|
2023-05-18 08:24:17 +00:00
|
|
|
import std/options
|
|
|
|
import stew/results
|
2023-01-06 10:14:38 +00:00
|
|
|
import chronos, chronicles
|
|
|
|
import ../../../switch,
|
|
|
|
../../../multiaddress,
|
|
|
|
../../../peerid
|
|
|
|
import core
|
|
|
|
|
|
|
|
logScope:
|
|
|
|
topics = "libp2p autonat"
|
|
|
|
|
|
|
|
type
|
|
|
|
AutonatClient* = ref object of RootObj
|
|
|
|
|
|
|
|
proc sendDial(conn: Connection, pid: PeerId, addrs: seq[MultiAddress]) {.async.} =
|
|
|
|
let pb = AutonatDial(peerInfo: some(AutonatPeerInfo(
|
|
|
|
id: some(pid),
|
|
|
|
addrs: addrs
|
|
|
|
))).encode()
|
|
|
|
await conn.writeLp(pb.buffer)
|
|
|
|
|
|
|
|
method dialMe*(self: AutonatClient, switch: Switch, pid: PeerId, addrs: seq[MultiAddress] = newSeq[MultiAddress]()):
|
|
|
|
Future[MultiAddress] {.base, async.} =
|
|
|
|
|
2023-06-07 11:12:49 +00:00
|
|
|
proc getResponseOrRaise(autonatMsg: Option[AutonatMsg]): AutonatDialResponse {.raises: [AutonatError].} =
|
2023-01-06 10:14:38 +00:00
|
|
|
if autonatMsg.isNone() or
|
|
|
|
autonatMsg.get().msgType != DialResponse or
|
|
|
|
autonatMsg.get().response.isNone() or
|
|
|
|
(autonatMsg.get().response.get().status == Ok and
|
|
|
|
autonatMsg.get().response.get().ma.isNone()):
|
|
|
|
raise newException(AutonatError, "Unexpected response")
|
|
|
|
else:
|
|
|
|
autonatMsg.get().response.get()
|
|
|
|
|
|
|
|
let conn =
|
|
|
|
try:
|
|
|
|
if addrs.len == 0:
|
|
|
|
await switch.dial(pid, @[AutonatCodec])
|
|
|
|
else:
|
|
|
|
await switch.dial(pid, addrs, AutonatCodec)
|
|
|
|
except CatchableError as err:
|
2023-02-09 15:53:46 +00:00
|
|
|
raise newException(AutonatError, "Unexpected error when dialling: " & err.msg, err)
|
2023-01-06 10:14:38 +00:00
|
|
|
|
2023-01-24 16:04:42 +00:00
|
|
|
# To bypass maxConnectionsPerPeer
|
2023-02-21 16:49:41 +00:00
|
|
|
let incomingConnection = switch.connManager.expectConnection(pid, In)
|
|
|
|
if incomingConnection.failed() and incomingConnection.error of AlreadyExpectingConnectionError:
|
|
|
|
raise newException(AutonatError, incomingConnection.error.msg)
|
2023-01-24 16:04:42 +00:00
|
|
|
defer:
|
|
|
|
await conn.close()
|
|
|
|
incomingConnection.cancel() # Safer to always try to cancel cause we aren't sure if the peer dialled us or not
|
2023-02-21 16:49:41 +00:00
|
|
|
if incomingConnection.completed():
|
2023-03-08 11:30:19 +00:00
|
|
|
await (await incomingConnection).connection.close()
|
2023-02-07 17:51:17 +00:00
|
|
|
trace "sending Dial", addrs = switch.peerInfo.addrs
|
2023-01-06 10:14:38 +00:00
|
|
|
await conn.sendDial(switch.peerInfo.peerId, switch.peerInfo.addrs)
|
|
|
|
let response = getResponseOrRaise(AutonatMsg.decode(await conn.readLp(1024)))
|
|
|
|
return case response.status:
|
|
|
|
of ResponseStatus.Ok:
|
|
|
|
response.ma.get()
|
|
|
|
of ResponseStatus.DialError:
|
2023-02-07 17:51:17 +00:00
|
|
|
raise newException(AutonatUnreachableError, "Peer could not dial us back: " & response.text.get(""))
|
2023-01-06 10:14:38 +00:00
|
|
|
else:
|
|
|
|
raise newException(AutonatError, "Bad status " & $response.status & " " & response.text.get(""))
|