11//! # Pkarr based node discovery for iroh-net
22//!
3- //! Node discovery is being able to find information about an iroh node based on just its node id.
3+ //! Node discovery is being able to find connecting information about an iroh node based on just its node id.
44//!
55//! This crate implements a discovery mechanism for iroh-net based on https://https://pkarr.org/.
66//!
7- //! Each node publishes its address to the mainline DHT as a DNS packet, signed with its private key.
7+ //! TLDR: Each node publishes its address to the mainline DHT as a DNS packet, signed with its private key.
88//! The DNS packet contains the node's direct addresses and optionally a DERP URL.
99use std:: {
1010 collections:: BTreeSet ,
@@ -25,26 +25,39 @@ use pkarr::{
2525 Keypair , PkarrClient , SignedPacket ,
2626} ;
2727
28+ /// The key for the DERP URL TXT record.
2829const DERP_URL_KEY : & str = "_derp_url.iroh." ;
30+ /// Republish delay for the DHT. This is only for when the info does not change.
31+ /// If the info changes, it will be published immediately.
2932const REPUBLISH_DELAY : Duration = Duration :: from_secs ( 60 * 60 ) ;
3033
3134/// A discovery mechanism for iroh-net based on https://https://pkarr.org/
3235///
3336/// TLDR: it stores node addresses in DNS records, signed by the node's private key,
3437/// and publishes them to the bittorrent mainline DHT.
3538///
36- /// Calling publish will start a background task that periodically publishes the node address
39+ /// Calling publish will start a background task that periodically publishes the node address.
3740#[ derive( Debug , Clone ) ]
3841pub struct PkarrNodeDiscovery ( Arc < Inner > ) ;
3942
4043#[ derive( Debug ) ]
4144struct Inner {
42- keypair : Option < pkarr :: Keypair > ,
45+ /// Pkarr client for interacting with the DHT.
4346 pkarr : PkarrClient ,
47+ /// The background task that periodically publishes the node address.
48+ /// Due to AbortingJoinHandle, this will be aborted when the discovery is dropped.
4449 task : Mutex < Option < AbortingJoinHandle < ( ) > > > ,
50+ /// Optional keypair for signing the DNS packets.
51+ ///
52+ /// If this is None, the node will not publish its address to the DHT.
53+ keypair : Option < pkarr:: Keypair > ,
4554}
4655
4756impl PkarrNodeDiscovery {
57+ /// Create a new discovery mechanism.
58+ ///
59+ /// If a secret key is provided, the node will publish its address to the DHT.
60+ /// If no secret key is provided, publish will be a no-op, but resolving other nodes will still work.
4861 pub fn new ( pkarr : PkarrClient , secret_key : Option < & SecretKey > ) -> Self {
4962 let keypair =
5063 secret_key. map ( |secret_key| pkarr:: Keypair :: from_secret_key ( & secret_key. to_bytes ( ) ) ) ;
0 commit comments