2024-10-28 09:17:46 +01:00
|
|
|
import
|
|
|
|
|
chronos,
|
|
|
|
|
chronicles,
|
|
|
|
|
bearssl/rand,
|
|
|
|
|
libp2p/protocols/connectivity/autonat/client,
|
|
|
|
|
libp2p/protocols/connectivity/autonat/service,
|
|
|
|
|
libp2p/protocols/connectivity/autonat/core
|
|
|
|
|
|
|
|
|
|
const AutonatCheckInterval = Opt.some(chronos.seconds(30))
|
|
|
|
|
|
|
|
|
|
proc getAutonatService*(rng: ref HmacDrbgContext): AutonatService =
|
|
|
|
|
## AutonatService request other peers to dial us back
|
|
|
|
|
## flagging us as Reachable or NotReachable.
|
|
|
|
|
## minConfidence is used as threshold to determine the state.
|
|
|
|
|
## If maxQueueSize > numPeersToAsk past samples are considered
|
|
|
|
|
## in the calculation.
|
|
|
|
|
let autonatService = AutonatService.new(
|
|
|
|
|
autonatClient = AutonatClient.new(),
|
|
|
|
|
rng = rng,
|
|
|
|
|
scheduleInterval = AutonatCheckInterval,
|
|
|
|
|
askNewConnectedPeers = false,
|
|
|
|
|
numPeersToAsk = 3,
|
|
|
|
|
maxQueueSize = 3,
|
|
|
|
|
minConfidence = 0.7,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
proc statusAndConfidenceHandler(
|
|
|
|
|
networkReachability: NetworkReachability, confidence: Opt[float]
|
2025-05-26 21:58:02 +02:00
|
|
|
): Future[void] {.gcsafe, async: (raises: [CancelledError]).} =
|
2024-10-28 09:17:46 +01:00
|
|
|
if confidence.isSome():
|
|
|
|
|
info "Peer reachability status",
|
|
|
|
|
networkReachability = networkReachability, confidence = confidence.get()
|
|
|
|
|
|
|
|
|
|
autonatService.statusAndConfidenceHandler(statusAndConfidenceHandler)
|
|
|
|
|
|
|
|
|
|
return autonatService
|