diff --git a/waku/v2/node/waku_node.nim b/waku/v2/node/waku_node.nim index 20aea76db..30954c4f7 100644 --- a/waku/v2/node/waku_node.nim +++ b/waku/v2/node/waku_node.nim @@ -17,6 +17,8 @@ import libp2p/protocols/ping, libp2p/protocols/pubsub/gossipsub, libp2p/protocols/pubsub/rpc/messages, + libp2p/protocols/connectivity/autonat/client, + libp2p/protocols/connectivity/autonat/service, libp2p/nameresolving/nameresolver, libp2p/builders, libp2p/multihash, @@ -204,6 +206,26 @@ proc new*(T: type WakuNode, wakuFlags, enrMultiaddrs) + ## 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 = some(chronos.seconds(120)), + askNewConnectedPeers = false, + numPeersToAsk = 3, + maxQueueSize = 3, + minConfidence = 0.7) + + proc statusAndConfidenceHandler(networkReachability: NetworkReachability, confidence: Option[float]) {.gcsafe, async.} = + if confidence.isSome(): + info "Peer reachability status", networkReachability=networkReachability, confidence=confidence.get() + + autonatService.statusAndConfidenceHandler(statusAndConfidenceHandler) + info "Initializing networking", addrs=announcedAddresses let switch = newWakuSwitch( @@ -220,6 +242,7 @@ proc new*(T: type WakuNode, sendSignedPeerRecord = sendSignedPeerRecord, agentString = agentString, peerStoreCapacity = peerStoreCapacity, + services = @[Service(autonatservice)], ) let wakuNode = WakuNode( diff --git a/waku/v2/node/wakuswitch.nim b/waku/v2/node/wakuswitch.nim index 13e8cda5f..55e449867 100644 --- a/waku/v2/node/wakuswitch.nim +++ b/waku/v2/node/wakuswitch.nim @@ -12,6 +12,7 @@ import libp2p/protocols/pubsub/gossipsub, libp2p/nameresolving/nameresolver, libp2p/builders, + libp2p/switch, libp2p/transports/[transport, tcptransport, wstransport] # override nim-libp2p default value (which is also 1) @@ -74,8 +75,9 @@ proc newWakuSwitch*( wssEnabled: bool = false, secureKeyPath: string = "", secureCertPath: string = "", - agentString = none(string), # defaults to nim-libp2p version, + agentString = none(string), # defaults to nim-libp2p version peerStoreCapacity = none(int), # defaults to nim-libp2p max size + services: seq[switch.Service] = @[], ): Switch {.raises: [Defect, IOError, LPError].} = @@ -111,4 +113,7 @@ proc newWakuSwitch*( else : b = b.withAddress(address) + if services.len > 0: + b = b.withServices(services) + b.build()