Skip to content

Commit 1752fd6

Browse files
authored
Merge pull request #30 from n0-computer/release-iroh-mainline-content-discovery
chore: publish new iroh-mainline-discovery
2 parents feea978 + c565dc0 commit 1752fd6

File tree

12 files changed

+77
-60
lines changed

12 files changed

+77
-60
lines changed

content-discovery/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ members = [
33
"iroh-mainline-content-discovery",
44
"iroh-mainline-content-discovery-cli",
55
"iroh-mainline-tracker",
6-
"tls",
76
]
87
resolver = "2"
98

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

+29-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "iroh-mainline-content-discovery"
3-
version = "0.5.0"
3+
version = "0.6.0"
44
edition = "2021"
55
description = "Content discovery for iroh, using the bittorrent mainline DHT"
66
license = "MIT OR Apache-2.0"
@@ -22,7 +22,7 @@ hex = "0.4.3"
2222

2323
# Optional features for the client functionality
2424
tracing = { version = "0.1", optional = true }
25-
iroh-quinn = { version = "0.13", optional = true }
25+
quinn = { package = "iroh-quinn", version = "0.13", optional = true }
2626
mainline = { version = "2.0.0", optional = true, features = ["async"] }
2727
anyhow = { version = "1", features = ["backtrace"], optional = true }
2828
postcard = { version = "1", default-features = false, features = ["alloc", "use-std"], optional = true }
@@ -32,8 +32,33 @@ rustls = { version = "0.23", default-features = false, features = ["ring"], opti
3232
genawaiter = { version = "0.99.1", features = ["futures03"], optional = true }
3333
tokio = { workspace = true, optional = true }
3434
flume = "0.11.0"
35-
tls = { path = "../tls", optional = true }
35+
36+
# dependencies for the tls utils
37+
der = { version = "0.7", features = ["alloc", "derive"], optional = true }
38+
webpki = { package = "rustls-webpki", version = "0.102", optional = true }
39+
x509-parser = { version = "0.16", optional = true }
40+
thiserror = { version = "2", optional = true }
41+
ring = { version = "0.17", optional = true }
3642

3743
[features]
38-
client = ["mainline", "iroh-quinn", "tracing", "anyhow", "rcgen", "genawaiter", "rustls", "futures", "postcard", "tokio", "tls"]
44+
client = [
45+
"dep:mainline",
46+
"dep:quinn",
47+
"dep:tracing",
48+
"dep:anyhow",
49+
"dep:rcgen",
50+
"dep:genawaiter",
51+
"dep:rustls",
52+
"dep:futures",
53+
"dep:postcard",
54+
"dep:tokio",
55+
"tls-utils",
56+
]
57+
tls-utils = [
58+
"dep:der",
59+
"dep:webpki",
60+
"dep:x509-parser",
61+
"dep:thiserror",
62+
"dep:ring",
63+
]
3964
default = ["client"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Protocol and client for iroh mainline content discovery
2+
3+
This provides a very minimal protocol for content discovery as well as a
4+
client library for the protocol.
5+
6+
## Features
7+
8+
- client: the client that allows querying content discovery
9+
- tls-utils: utilities to set of quinn connections, used by client

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

+20-15
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,12 @@ use iroh::{
2020
};
2121
use iroh_blobs::HashAndFormat;
2222

23-
use crate::protocol::{
24-
AnnounceKind, Query, QueryResponse, Request, Response, SignedAnnounce, ALPN, REQUEST_SIZE_LIMIT,
23+
use crate::{
24+
protocol::{
25+
AnnounceKind, Query, QueryResponse, Request, Response, SignedAnnounce, ALPN,
26+
REQUEST_SIZE_LIMIT,
27+
},
28+
tls_utils,
2529
};
2630

2731
/// Announce to a tracker.
@@ -33,7 +37,7 @@ use crate::protocol::{
3337
/// `content` is the content to announce.
3438
/// `kind` is the kind of the announcement. We can claim to have the complete data or only some of it.
3539
pub async fn announce_quinn(
36-
connection: iroh_quinn::Connection,
40+
connection: quinn::Connection,
3741
signed_announce: SignedAnnounce,
3842
) -> anyhow::Result<()> {
3943
let (mut send, mut recv) = connection.open_bi().await?;
@@ -119,14 +123,14 @@ async fn query_iroh_one(
119123

120124
/// A connection provider that can be used to connect to a tracker.
121125
///
122-
/// This can either be a [`iroh_quinn::Endpoint`] where connections are created on demand,
126+
/// This can either be a [`quinn::Endpoint`] where connections are created on demand,
123127
/// or some sort of connection pool.
124128
pub trait QuinnConnectionProvider<Addr>: Clone {
125-
fn connect(&self, addr: Addr) -> BoxFuture<anyhow::Result<iroh_quinn::Connection>>;
129+
fn connect(&self, addr: Addr) -> BoxFuture<anyhow::Result<quinn::Connection>>;
126130
}
127131

128-
impl QuinnConnectionProvider<SocketAddr> for iroh_quinn::Endpoint {
129-
fn connect(&self, addr: SocketAddr) -> BoxFuture<anyhow::Result<iroh_quinn::Connection>> {
132+
impl QuinnConnectionProvider<SocketAddr> for quinn::Endpoint {
133+
fn connect(&self, addr: SocketAddr) -> BoxFuture<anyhow::Result<quinn::Connection>> {
130134
async move { Ok(self.connect(addr, "localhost")?.await?) }.boxed()
131135
}
132136
}
@@ -229,7 +233,7 @@ pub async fn query_iroh(
229233

230234
/// Assume an existing connection to a tracker and query it for peers for some content.
231235
pub async fn query_quinn(
232-
connection: iroh_quinn::Connection,
236+
connection: quinn::Connection,
233237
args: Query,
234238
) -> anyhow::Result<QueryResponse> {
235239
tracing::info!("connected to {:?}", connection.remote_address());
@@ -252,12 +256,13 @@ pub fn create_quinn_client(
252256
bind_addr: SocketAddr,
253257
alpn_protocols: Vec<Vec<u8>>,
254258
keylog: bool,
255-
) -> anyhow::Result<iroh_quinn::Endpoint> {
259+
) -> anyhow::Result<quinn::Endpoint> {
256260
let secret_key = iroh::SecretKey::generate(rand::thread_rng());
257-
let tls_client_config = tls::make_client_config(&secret_key, None, alpn_protocols, keylog)?;
258-
let mut client_config = iroh_quinn::ClientConfig::new(Arc::new(tls_client_config));
259-
let mut endpoint = iroh_quinn::Endpoint::client(bind_addr)?;
260-
let mut transport_config = iroh_quinn::TransportConfig::default();
261+
let tls_client_config =
262+
tls_utils::make_client_config(&secret_key, None, alpn_protocols, keylog)?;
263+
let mut client_config = quinn::ClientConfig::new(Arc::new(tls_client_config));
264+
let mut endpoint = quinn::Endpoint::client(bind_addr)?;
265+
let mut transport_config = quinn::TransportConfig::default();
261266
transport_config.keep_alive_interval(Some(Duration::from_secs(1)));
262267
client_config.transport_config(Arc::new(transport_config));
263268
endpoint.set_default_client_config(client_config);
@@ -340,7 +345,7 @@ pub async fn connect(
340345

341346
pub enum Connection {
342347
Iroh(iroh::endpoint::Connection),
343-
Quinn(iroh_quinn::Connection),
348+
Quinn(quinn::Connection),
344349
}
345350

346351
/// Create a iroh endpoint and connect to a tracker using the [crate::protocol::ALPN] protocol.
@@ -363,7 +368,7 @@ async fn connect_iroh(
363368
async fn connect_socket(
364369
tracker: SocketAddr,
365370
local_addr: SocketAddr,
366-
) -> anyhow::Result<iroh_quinn::Connection> {
371+
) -> anyhow::Result<quinn::Connection> {
367372
let endpoint = create_quinn_client(local_addr, vec![ALPN.to_vec()], false)?;
368373
tracing::info!("trying t?o )connect to tracker at {:?}", tracker);
369374
let connection = endpoint.connect(tracker, "localhost")?.await?;

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

+2
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ mod client;
88
pub mod protocol;
99
#[cfg(feature = "client")]
1010
pub use client::*;
11+
#[cfg(feature = "tls-utils")]
12+
pub mod tls_utils;

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ iroh-blobs = { workspace = true }
2323
mainline = { version = "2.0.0", features = ["async"] }
2424
pkarr = { version = "2.0.1", features = ["async"] }
2525
postcard = { version = "1", default-features = false, features = ["alloc", "use-std"] }
26-
iroh-quinn = "0.13"
2726
rand = "0.8"
2827
rcgen = "0.12.0"
2928
redb = "1.5.0"
@@ -42,7 +41,7 @@ url = "2.5.0"
4241
flume = "0.11.0"
4342
genawaiter = { version = "0.99.1", features = ["futures03"] }
4443
iroh-mainline-content-discovery = { path = "../iroh-mainline-content-discovery", features = ["client"] }
45-
tls = { path = "../tls" }
44+
quinn = { package = "iroh-quinn", version = "0.13" }
4645

4746
clap = { version = "4", features = ["derive"], optional = true }
4847
serde-big-array = "0.5.1"

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

+7-7
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use std::{
1212
use clap::Parser;
1313
use iroh::{discovery::pkarr::dht::DhtDiscovery, Endpoint, NodeId};
1414
use iroh_blobs::util::fs::load_secret_key;
15-
use iroh_mainline_content_discovery::protocol::ALPN;
15+
use iroh_mainline_content_discovery::{protocol::ALPN, tls_utils};
1616
use iroh_mainline_tracker::{
1717
io::{
1818
self, load_from_file, setup_logging, tracker_home, tracker_path, CONFIG_DEBUG_FILE,
@@ -130,7 +130,7 @@ async fn server(args: Args) -> anyhow::Result<()> {
130130
let udp_socket = tokio::net::UdpSocket::bind(udp_bind_addr).await?;
131131
let quinn_bind_addr =
132132
SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, options.quinn_port));
133-
let quinn_endpoint = iroh_quinn::Endpoint::server(server_config, quinn_bind_addr)?;
133+
let quinn_endpoint = quinn::Endpoint::server(server_config, quinn_bind_addr)?;
134134
// set the quinn port to the actual port we bound to so the DHT will announce it correctly
135135
options.quinn_port = quinn_endpoint.local_addr()?.port();
136136
let iroh_endpoint = create_endpoint(key.clone(), options.iroh_ipv4_addr, true).await?;
@@ -185,7 +185,7 @@ async fn main() -> anyhow::Result<()> {
185185

186186
/// Returns default server configuration along with its certificate.
187187
#[allow(clippy::field_reassign_with_default)] // https://github.com/rust-lang/rust-clippy/issues/6527
188-
fn configure_server(secret_key: &iroh::SecretKey) -> anyhow::Result<iroh_quinn::ServerConfig> {
188+
fn configure_server(secret_key: &iroh::SecretKey) -> anyhow::Result<quinn::ServerConfig> {
189189
make_server_config(secret_key, 8, 1024, vec![ALPN.to_vec()])
190190
}
191191

@@ -195,10 +195,10 @@ pub fn make_server_config(
195195
max_streams: u64,
196196
max_connections: u32,
197197
alpn_protocols: Vec<Vec<u8>>,
198-
) -> anyhow::Result<iroh_quinn::ServerConfig> {
199-
let tls_server_config = tls::make_server_config(secret_key, alpn_protocols, false)?;
200-
let mut server_config = iroh_quinn::ServerConfig::with_crypto(Arc::new(tls_server_config));
201-
let mut transport_config = iroh_quinn::TransportConfig::default();
198+
) -> anyhow::Result<quinn::ServerConfig> {
199+
let tls_server_config = tls_utils::make_server_config(secret_key, alpn_protocols, false)?;
200+
let mut server_config = quinn::ServerConfig::with_crypto(Arc::new(tls_server_config));
201+
let mut transport_config = quinn::TransportConfig::default();
202202
transport_config
203203
.max_concurrent_bidi_streams(max_streams.try_into()?)
204204
.max_concurrent_uni_streams(0u32.into());

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

+9-9
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use iroh_mainline_content_discovery::{
1818
AbsoluteTime, Announce, AnnounceKind, Query, QueryResponse, Request, Response,
1919
SignedAnnounce, REQUEST_SIZE_LIMIT,
2020
},
21-
to_infohash,
21+
tls_utils, to_infohash,
2222
};
2323
use rand::Rng;
2424
use redb::{ReadableTable, RedbValue};
@@ -883,7 +883,7 @@ impl Tracker {
883883
Ok(())
884884
}
885885

886-
pub async fn quinn_accept_loop(self, endpoint: iroh_quinn::Endpoint) -> std::io::Result<()> {
886+
pub async fn quinn_accept_loop(self, endpoint: quinn::Endpoint) -> std::io::Result<()> {
887887
let local_addr = endpoint.local_addr()?;
888888
println!("quinn listening on {local_addr:?}");
889889
while let Some(incoming) = endpoint.accept().await {
@@ -948,7 +948,7 @@ impl Tracker {
948948
/// Handle a single incoming connection on the tracker ALPN.
949949
pub async fn handle_quinn_connection(
950950
&self,
951-
connection: iroh_quinn::Connection,
951+
connection: quinn::Connection,
952952
) -> anyhow::Result<()> {
953953
tracing::debug!("calling accept_bi");
954954
let (mut send, mut recv) = connection.accept_bi().await?;
@@ -1269,18 +1269,18 @@ impl Tracker {
12691269

12701270
/// Accept an incoming connection and extract the client-provided [`NodeId`] and ALPN protocol.
12711271
async fn accept_conn(
1272-
mut conn: iroh_quinn::Connecting,
1273-
) -> anyhow::Result<(NodeId, String, iroh_quinn::Connection)> {
1272+
mut conn: quinn::Connecting,
1273+
) -> anyhow::Result<(NodeId, String, quinn::Connection)> {
12741274
let alpn = get_alpn(&mut conn).await?;
12751275
let conn = conn.await?;
12761276
let node_id = get_remote_node_id(&conn)?;
12771277
Ok((node_id, alpn, conn))
12781278
}
12791279

12801280
/// Extract the ALPN protocol from the peer's TLS certificate.
1281-
pub async fn get_alpn(connecting: &mut iroh_quinn::Connecting) -> anyhow::Result<String> {
1281+
pub async fn get_alpn(connecting: &mut quinn::Connecting) -> anyhow::Result<String> {
12821282
let data = connecting.handshake_data().await?;
1283-
match data.downcast::<iroh_quinn::crypto::rustls::HandshakeData>() {
1283+
match data.downcast::<quinn::crypto::rustls::HandshakeData>() {
12841284
Ok(data) => match data.protocol {
12851285
Some(protocol) => std::string::String::from_utf8(protocol).map_err(Into::into),
12861286
None => anyhow::bail!("no ALPN protocol available"),
@@ -1289,7 +1289,7 @@ pub async fn get_alpn(connecting: &mut iroh_quinn::Connecting) -> anyhow::Result
12891289
}
12901290
}
12911291

1292-
pub fn get_remote_node_id(connection: &iroh_quinn::Connection) -> anyhow::Result<iroh::NodeId> {
1292+
pub fn get_remote_node_id(connection: &quinn::Connection) -> anyhow::Result<iroh::NodeId> {
12931293
let data = connection.peer_identity();
12941294
match data {
12951295
None => anyhow::bail!("no peer certificate found"),
@@ -1301,7 +1301,7 @@ pub fn get_remote_node_id(connection: &iroh_quinn::Connection) -> anyhow::Result
13011301
certs.len()
13021302
);
13031303
}
1304-
let cert = tls::certificate::parse(&certs[0])?;
1304+
let cert = tls_utils::certificate::parse(&certs[0])?;
13051305
Ok(cert.peer_id())
13061306
}
13071307
Err(_) => anyhow::bail!("invalid peer certificate"),

content-discovery/tls/Cargo.toml

-22
This file was deleted.

0 commit comments

Comments
 (0)