Skip to content

Commit b02b2e8

Browse files
authored
feat(iroh-pkarr-node-discovery): allow configuring if direct addresse… (#57)
* feat(iroh-pkarr-node-discovery): allow configuring if direct addresses will be included * increase version number * Make sure the default is sane. Default should be dht=true, relay=None.
1 parent fd03fd0 commit b02b2e8

File tree

5 files changed

+39
-36
lines changed

5 files changed

+39
-36
lines changed

content-discovery/Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

content-discovery/iroh-mainline-content-discovery/src/protocol.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ impl SignedAnnounce {
136136
}
137137
}
138138

139-
///
139+
/// Flags for a query.
140140
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
141141
pub struct QueryFlags {
142142
/// Only return peers that supposedly have the complete data.

iroh-pkarr-node-discovery/Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

iroh-pkarr-node-discovery/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "iroh-pkarr-node-discovery"
3-
version = "0.2.0"
3+
version = "0.3.0"
44
edition = "2021"
55
authors = ["Rüdiger Klaehn <rklaehn@protonmail.com>", "n0 team"]
66
keywords = ["network", "p2p"]

iroh-pkarr-node-discovery/src/lib.rs

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,15 @@ const INITIAL_PUBLISH_DELAY: Duration = Duration::from_millis(500);
3737
/// and publishes them to the bittorrent mainline DHT.
3838
///
3939
/// Calling publish will start a background task that periodically publishes the node address.
40-
#[derive(Debug, Clone, Default)]
40+
#[derive(Debug, Clone)]
4141
pub struct PkarrNodeDiscovery(Arc<Inner>);
4242

43+
impl Default for PkarrNodeDiscovery {
44+
fn default() -> Self {
45+
Self::builder().build().expect("valid builder")
46+
}
47+
}
48+
4349
#[derive(Debug, Default)]
4450
struct Inner {
4551
/// Pkarr client for interacting with the DHT.
@@ -57,6 +63,8 @@ struct Inner {
5763
dht: bool,
5864
/// Time-to-live value for the DNS packets.
5965
ttl: u32,
66+
/// True to include the direct addresses in the DNS packet.
67+
include_direct_addresses: bool,
6068
}
6169

6270
/// Builder for PkarrNodeDiscovery.
@@ -69,6 +77,7 @@ pub struct Builder {
6977
ttl: Option<u32>,
7078
pkarr_relay: Option<Url>,
7179
dht: bool,
80+
include_direct_addresses: bool,
7281
}
7382

7483
impl Default for Builder {
@@ -79,6 +88,7 @@ impl Default for Builder {
7988
ttl: None,
8089
pkarr_relay: None,
8190
dht: true,
91+
include_direct_addresses: false,
8292
}
8393
}
8494
}
@@ -111,7 +121,7 @@ impl Builder {
111121
}
112122

113123
/// Use the default pkarr relay URL.
114-
pub fn iroh_pkarr_relay(mut self) -> Self {
124+
pub fn n0_dns_pkarr_relay(mut self) -> Self {
115125
self.pkarr_relay = Some(N0_DNS_PKARR_RELAY.parse().expect("valid URL"));
116126
self
117127
}
@@ -122,23 +132,33 @@ impl Builder {
122132
self
123133
}
124134

135+
/// Set whether to include the direct addresses in the DNS packet.
136+
pub fn include_direct_addresses(mut self, include_direct_addresses: bool) -> Self {
137+
self.include_direct_addresses = include_direct_addresses;
138+
self
139+
}
140+
125141
/// Build the discovery mechanism.
126142
pub fn build(self) -> anyhow::Result<PkarrNodeDiscovery> {
127-
let client = self.client.unwrap_or_default();
143+
let pkarr = self.client.unwrap_or_default();
128144
let ttl = self.ttl.unwrap_or(DEFAULT_PKARR_TTL);
129-
let pkarr_relay = self.pkarr_relay;
145+
let relay_url = self.pkarr_relay;
130146
let dht = self.dht;
147+
let include_direct_addresses = self.include_direct_addresses;
131148
anyhow::ensure!(
132-
dht || pkarr_relay.is_some(),
149+
dht || relay_url.is_some(),
133150
"at least one of DHT or relay must be enabled"
134151
);
135-
Ok(PkarrNodeDiscovery::new(
136-
client,
137-
self.secret_key,
152+
153+
Ok(PkarrNodeDiscovery(Arc::new(Inner {
154+
pkarr,
155+
secret_key: self.secret_key,
138156
ttl,
139-
pkarr_relay,
157+
relay_url,
140158
dht,
141-
))
159+
include_direct_addresses,
160+
task: Default::default(),
161+
})))
142162
}
143163
}
144164

@@ -148,27 +168,6 @@ impl PkarrNodeDiscovery {
148168
Builder::default()
149169
}
150170

151-
/// Create a new discovery mechanism.
152-
///
153-
/// If a secret key is provided, the node will publish its address to the DHT.
154-
/// If no secret key is provided, publish will be a no-op, but resolving other nodes will still work.
155-
fn new(
156-
pkarr: PkarrClient,
157-
secret_key: Option<SecretKey>,
158-
ttl: u32,
159-
relay_url: Option<Url>,
160-
dht: bool,
161-
) -> Self {
162-
Self(Arc::new(Inner {
163-
secret_key,
164-
pkarr,
165-
ttl,
166-
relay_url,
167-
dht,
168-
task: Default::default(),
169-
}))
170-
}
171-
172171
/// Periodically publish the node address to the DHT and relay.
173172
async fn publish_loop(self, keypair: SecretKey, signed_packet: SignedPacket) {
174173
let this = self;
@@ -325,7 +324,11 @@ impl Discovery for PkarrNodeDiscovery {
325324
let info = NodeInfo {
326325
node_id: keypair.public(),
327326
relay_url: info.relay_url.clone().map(Url::from),
328-
direct_addresses: Default::default(),
327+
direct_addresses: if self.0.include_direct_addresses {
328+
info.direct_addresses.clone()
329+
} else {
330+
Default::default()
331+
},
329332
};
330333
let Ok(signed_packet) = info.to_pkarr_signed_packet(keypair, self.0.ttl) else {
331334
tracing::warn!("failed to create signed packet");

0 commit comments

Comments
 (0)