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.
|
|
|
|
|
2022-12-16 11:32:00 +00:00
|
|
|
{.used.}
|
|
|
|
|
2023-06-07 11:12:49 +00:00
|
|
|
{.push raises: [].}
|
2022-12-16 11:32:00 +00:00
|
|
|
|
|
|
|
import chronos
|
2023-01-06 10:14:38 +00:00
|
|
|
import ../../libp2p/[protocols/connectivity/autonat/client,
|
|
|
|
peerid,
|
|
|
|
multiaddress,
|
|
|
|
switch]
|
2023-04-18 10:50:21 +00:00
|
|
|
from ../../libp2p/protocols/connectivity/autonat/core import NetworkReachability, AutonatUnreachableError, AutonatError
|
2022-12-16 11:32:00 +00:00
|
|
|
|
|
|
|
type
|
2023-01-06 10:14:38 +00:00
|
|
|
AutonatClientStub* = ref object of AutonatClient
|
2022-12-23 15:49:25 +00:00
|
|
|
answer*: Answer
|
2022-12-16 11:32:00 +00:00
|
|
|
dials: int
|
|
|
|
expectedDials: int
|
|
|
|
finished*: Future[void]
|
|
|
|
|
2022-12-23 15:49:25 +00:00
|
|
|
Answer* = enum
|
|
|
|
Reachable,
|
|
|
|
NotReachable,
|
|
|
|
Unknown
|
|
|
|
|
2023-01-06 10:14:38 +00:00
|
|
|
proc new*(T: typedesc[AutonatClientStub], expectedDials: int): T =
|
2022-12-16 11:32:00 +00:00
|
|
|
return T(dials: 0, expectedDials: expectedDials, finished: newFuture[void]())
|
|
|
|
|
|
|
|
method dialMe*(
|
2023-01-06 10:14:38 +00:00
|
|
|
self: AutonatClientStub,
|
|
|
|
switch: Switch,
|
2022-12-16 11:32:00 +00:00
|
|
|
pid: PeerId,
|
|
|
|
addrs: seq[MultiAddress] = newSeq[MultiAddress]()):
|
|
|
|
Future[MultiAddress] {.async.} =
|
|
|
|
|
|
|
|
self.dials += 1
|
|
|
|
|
|
|
|
if self.dials == self.expectedDials:
|
|
|
|
self.finished.complete()
|
2022-12-23 15:49:25 +00:00
|
|
|
case self.answer:
|
|
|
|
of Reachable:
|
|
|
|
return MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet()
|
|
|
|
of NotReachable:
|
|
|
|
raise newException(AutonatUnreachableError, "")
|
|
|
|
of Unknown:
|
|
|
|
raise newException(AutonatError, "")
|