Skip to content

Commit cf12f99

Browse files
committed
Add query_dht cli arg
1 parent 661132d commit cf12f99

File tree

7 files changed

+121
-39
lines changed

7 files changed

+121
-39
lines changed

content-discovery/Cargo.lock

Lines changed: 18 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

content-discovery/iroh-mainline-content-discovery/Cargo.toml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ serde = { version = "1", features = ["derive"] }
1818
# Optional features for the client functionality
1919
tracing = { version = "0.1", optional = true }
2020
quinn = { version = "0.10", optional = true }
21-
iroh-pkarr-node-discovery = { path = "../../iroh-pkarr-node-discovery", optional = true }
21+
iroh-pkarr-node-discovery = { version = "0.1.2", optional = true }
2222
mainline = { version = "1.0.0", optional = true }
23-
pkarr = { version = "1.0.1", features = ["async"], optional = true }
2423
anyhow = { version = "1", features = ["backtrace"], optional = true }
2524
postcard = { version = "1", default-features = false, features = ["alloc", "use-std"], optional = true }
2625
ed25519-dalek = { version = "2.1.0", optional = true }
@@ -37,9 +36,9 @@ tracing-subscriber = { version = "0.3", features = ["env-filter"], optional = tr
3736
tokio = { version = "1", features = ["io-util", "rt"], optional = true }
3837

3938
[features]
40-
client = ["iroh-pkarr-node-discovery", "mainline", "pkarr", "quinn", "tracing", "anyhow", "rcgen", "genawaiter", "rustls", "futures", "postcard"]
39+
client = ["iroh-pkarr-node-discovery", "mainline", "quinn", "tracing", "anyhow", "rcgen", "genawaiter", "rustls", "futures", "postcard"]
4140
cli = ["client", "clap", "tempfile", "derive_more", "tracing-subscriber", "tokio"]
42-
default = []
41+
default = ["client", "cli"]
4342

4443
[[bin]]
4544
name = "iroh-mainline-content-discovery"

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

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub struct Args {
1515
pub enum Commands {
1616
Announce(AnnounceArgs),
1717
Query(QueryArgs),
18+
QueryDht(QueryDhtArgs),
1819
}
1920

2021
/// Various ways to specify content.
@@ -80,10 +81,6 @@ pub struct AnnounceArgs {
8081
#[clap(long)]
8182
pub tracker: NodeId,
8283

83-
/// the port to use for announcing
84-
#[clap(long)]
85-
pub port: Option<u16>,
86-
8784
/// The host to announce. Not needed if content is a ticket.
8885
#[clap(long)]
8986
pub host: Option<NodeId>,
@@ -98,17 +95,36 @@ pub struct AnnounceArgs {
9895
/// Announce that the peer has only partial data.
9996
#[clap(long)]
10097
pub partial: bool,
98+
99+
/// the port to use for announcing
100+
#[clap(long)]
101+
pub magic_port: Option<u16>,
101102
}
102103

103104
#[derive(Parser, Debug)]
104105
pub struct QueryArgs {
106+
/// the tracker to query
105107
#[clap(long)]
106108
pub tracker: TrackerId,
107109

110+
/// The content to find hosts for.
111+
pub content: ContentArg,
112+
113+
/// Ask for hosts that were announced as having just partial data
114+
#[clap(long)]
115+
pub partial: bool,
116+
117+
/// Ask for hosts that were recently checked and found to have some data
118+
#[clap(long)]
119+
pub verified: bool,
120+
108121
/// the port to use for querying
109122
#[clap(long)]
110-
pub port: Option<u16>,
123+
pub magic_port: Option<u16>,
124+
}
111125

126+
#[derive(Parser, Debug)]
127+
pub struct QueryDhtArgs {
112128
/// The content to find hosts for.
113129
pub content: ContentArg,
114130

@@ -119,4 +135,12 @@ pub struct QueryArgs {
119135
/// Ask for hosts that were recently checked and found to have some data
120136
#[clap(long)]
121137
pub verified: bool,
138+
139+
/// Parallelism for querying the dht
140+
#[clap(long)]
141+
pub query_parallelism: Option<usize>,
142+
143+
/// the port to use for querying
144+
#[clap(long)]
145+
pub quinn_port: Option<u16>,
122146
}

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use iroh_bytes::HashAndFormat;
1111
use iroh_net::{MagicEndpoint, NodeId};
1212
use iroh_pkarr_node_discovery::PkarrNodeDiscovery;
1313
use mainline::common::{GetPeerResponse, StoreQueryMetdata};
14-
use pkarr::PkarrClient;
1514

1615
use crate::protocol::{
1716
Announce, Query, QueryResponse, Request, Response, ALPN, REQUEST_SIZE_LIMIT,
@@ -52,7 +51,7 @@ fn unique_tracker_addrs(
5251
Gen::new(|co| async move {
5352
let mut found = HashSet::new();
5453
while let Some(response) = response.next_async().await {
55-
println!("got get_peers response: {:?}", response);
54+
tracing::info!("got get_peers response: {:?}", response);
5655
let tracker = response.peer;
5756
if !found.insert(tracker) {
5857
continue;
@@ -185,7 +184,7 @@ pub async fn query(connection: quinn::Connection, args: Query) -> anyhow::Result
185184
}
186185

187186
/// Create a quinn client endpoint.
188-
fn create_quinn_client(
187+
pub fn create_quinn_client(
189188
bind_addr: SocketAddr,
190189
alpn_protocols: Vec<Vec<u8>>,
191190
keylog: bool,
@@ -207,9 +206,11 @@ async fn create_endpoint(
207206
port: u16,
208207
publish: bool,
209208
) -> anyhow::Result<MagicEndpoint> {
210-
let pkarr = PkarrClient::new();
211-
let discovery_key = if publish { Some(&key) } else { None };
212-
let mainline_discovery = PkarrNodeDiscovery::new(pkarr, discovery_key);
209+
let mainline_discovery = if publish {
210+
PkarrNodeDiscovery::builder().secret_key(&key).build()
211+
} else {
212+
PkarrNodeDiscovery::default()
213+
};
213214
iroh_net::MagicEndpoint::builder()
214215
.secret_key(key)
215216
.discovery(Box::new(mainline_discovery))

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

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
pub mod args;
22

3-
use std::collections::BTreeSet;
3+
use std::{
4+
collections::BTreeSet,
5+
net::{Ipv4Addr, SocketAddr, SocketAddrV4},
6+
};
47

58
use anyhow::Context;
9+
use args::QueryDhtArgs;
610
use clap::Parser;
7-
use iroh_mainline_content_discovery::protocol::{Announce, AnnounceKind, Query, QueryFlags, ALPN};
11+
use futures::StreamExt;
12+
use iroh_mainline_content_discovery::{
13+
create_quinn_client,
14+
protocol::{Announce, AnnounceKind, Query, QueryFlags, ALPN},
15+
to_infohash,
16+
};
817
use iroh_net::{
918
magic_endpoint::{get_alpn, get_remote_node_id},
1019
MagicEndpoint, NodeId,
1120
};
1221
use iroh_pkarr_node_discovery::PkarrNodeDiscovery;
13-
use pkarr::PkarrClient;
1422
use tokio::io::AsyncWriteExt;
1523
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter};
1624

@@ -21,9 +29,11 @@ async fn create_endpoint(
2129
port: u16,
2230
publish: bool,
2331
) -> anyhow::Result<MagicEndpoint> {
24-
let pkarr = PkarrClient::new();
25-
let discovery_key = if publish { Some(&key) } else { None };
26-
let mainline_discovery = PkarrNodeDiscovery::new(pkarr, discovery_key);
32+
let mainline_discovery = if publish {
33+
PkarrNodeDiscovery::builder().secret_key(&key).build()
34+
} else {
35+
PkarrNodeDiscovery::default()
36+
};
2737
iroh_net::MagicEndpoint::builder()
2838
.secret_key(key)
2939
.discovery(Box::new(mainline_discovery))
@@ -68,7 +78,7 @@ async fn announce(args: AnnounceArgs) -> anyhow::Result<()> {
6878
for content in &content {
6979
println!(" {}", content);
7080
}
71-
let endpoint = create_endpoint(key, 11112, false).await?;
81+
let endpoint = create_endpoint(key, args.magic_port.unwrap_or_default(), false).await?;
7282
let connection = endpoint.connect_by_node_id(&args.tracker, ALPN).await?;
7383
println!("connected to {:?}", connection.remote_address());
7484
let kind = if args.partial {
@@ -87,9 +97,11 @@ async fn announce(args: AnnounceArgs) -> anyhow::Result<()> {
8797
}
8898

8999
async fn query(args: QueryArgs) -> anyhow::Result<()> {
90-
let connection =
91-
iroh_mainline_content_discovery::connect(&args.tracker, args.port.unwrap_or_default())
92-
.await?;
100+
let connection = iroh_mainline_content_discovery::connect(
101+
&args.tracker,
102+
args.magic_port.unwrap_or_default(),
103+
)
104+
.await?;
93105
let q = Query {
94106
content: args.content.hash_and_format(),
95107
flags: QueryFlags {
@@ -108,6 +120,36 @@ async fn query(args: QueryArgs) -> anyhow::Result<()> {
108120
Ok(())
109121
}
110122

123+
async fn query_dht(args: QueryDhtArgs) -> anyhow::Result<()> {
124+
let bind_addr = SocketAddr::V4(SocketAddrV4::new(
125+
Ipv4Addr::UNSPECIFIED,
126+
args.quinn_port.unwrap_or_default(),
127+
));
128+
let endpoint = create_quinn_client(bind_addr, vec![ALPN.to_vec()], false)?;
129+
let dht = mainline::Dht::default();
130+
let q = Query {
131+
content: args.content.hash_and_format(),
132+
flags: QueryFlags {
133+
complete: !args.partial,
134+
verified: args.verified,
135+
},
136+
};
137+
println!("content corresponds to infohash {}", to_infohash(q.content));
138+
let mut stream = iroh_mainline_content_discovery::query_dht(
139+
endpoint,
140+
dht,
141+
q,
142+
args.query_parallelism.unwrap_or(4),
143+
);
144+
while let Some(item) = stream.next().await {
145+
match item {
146+
Ok(provider) => println!("found provider {}", provider),
147+
Err(e) => println!("error: {}", e),
148+
}
149+
}
150+
Ok(())
151+
}
152+
111153
// set the RUST_LOG env var to one of {debug,info,warn} to see logging info
112154
pub fn setup_logging() {
113155
tracing_subscriber::registry()
@@ -124,6 +166,7 @@ async fn main() -> anyhow::Result<()> {
124166
match args.command {
125167
Commands::Announce(args) => announce(args).await,
126168
Commands::Query(args) => query(args).await,
169+
Commands::QueryDht(args) => query_dht(args).await,
127170
}
128171
}
129172

content-discovery/iroh-mainline-tracker/Cargo.toml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
[package]
22
name = "iroh-mainline-tracker"
3-
version = "0.3.0"
3+
version = "0.1.0"
44
edition = "2021"
5-
description = "Content discovery for iroh, using the bittorrent mainline DHT"
5+
description = "Content tracker for iroh, using the bittorrent mainline DHT"
66
license = "MIT OR Apache-2.0"
77

88
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
@@ -11,7 +11,6 @@ license = "MIT OR Apache-2.0"
1111
anyhow = { version = "1", features = ["backtrace"] }
1212
bao-tree = { version = "0.9.1", features = ["tokio_fsm"], default-features = false }
1313
bytes = "1"
14-
clap = { version = "4", features = ["derive"], optional = true }
1514
derive_more = { version = "1.0.0-beta.1", features = ["debug", "display", "from", "try_into"] }
1615
dirs-next = "2"
1716
ed25519-dalek = "2.1.0"
@@ -20,10 +19,10 @@ hex = "0.4.3"
2019
humantime = "2.1.0"
2120
iroh-net = "0.12.0"
2221
iroh-bytes = "0.12.0"
23-
iroh-pkarr-node-discovery = { path = "../../iroh-pkarr-node-discovery" }
22+
iroh-pkarr-node-discovery = { path = "../../iroh-pkarr-node-discovery", default-features = false }
2423
mainline = "1.0.0"
2524
pkarr = { version = "1.0.1", features = ["async"] }
26-
postcard = { version = "1", default-features = false, features = ["alloc", "use-std", "experimental-derive"] }
25+
postcard = { version = "1", default-features = false, features = ["alloc", "use-std"] }
2726
quinn = "0.10"
2827
rand = "0.8"
2928
rcgen = "0.12.0"
@@ -44,6 +43,8 @@ flume = "0.11.0"
4443
genawaiter = { version = "0.99.1", features = ["futures03"] }
4544
iroh-mainline-content-discovery = { path = "../iroh-mainline-content-discovery", features = ["client"] }
4645

46+
clap = { version = "4", features = ["derive"], optional = true }
47+
4748
[features]
4849
cli = ["clap"]
4950
default = ["cli"]

content-discovery/iroh-mainline-tracker/src/main.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ use iroh_net::{
2525
MagicEndpoint, NodeId,
2626
};
2727
use iroh_pkarr_node_discovery::PkarrNodeDiscovery;
28-
use pkarr::PkarrClient;
2928
use tokio::io::AsyncWriteExt;
3029
use tokio_util::task::LocalPoolHandle;
3130

@@ -71,9 +70,11 @@ async fn create_endpoint(
7170
port: u16,
7271
publish: bool,
7372
) -> anyhow::Result<MagicEndpoint> {
74-
let pkarr = PkarrClient::new();
75-
let discovery_key = if publish { Some(&key) } else { None };
76-
let mainline_discovery = PkarrNodeDiscovery::new(pkarr, discovery_key);
73+
let mainline_discovery = if publish {
74+
PkarrNodeDiscovery::builder().secret_key(&key).build()
75+
} else {
76+
PkarrNodeDiscovery::default()
77+
};
7778
iroh_net::MagicEndpoint::builder()
7879
.secret_key(key)
7980
.discovery(Box::new(mainline_discovery))

0 commit comments

Comments
 (0)