Skip to content

Commit

Permalink
Merge branch 'main' into flub/messing-around-with-rtt
Browse files Browse the repository at this point in the history
  • Loading branch information
flub committed May 6, 2024
2 parents 008d470 + 38bdaef commit 3518b49
Show file tree
Hide file tree
Showing 23 changed files with 670 additions and 296 deletions.
344 changes: 205 additions & 139 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions iroh-base/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ serde_test = "1.0.176"

[features]
default = ["hash", "base32"]
hash = ["bao-tree", "data-encoding", "postcard"]
base32 = ["data-encoding"]
hash = ["dep:bao-tree", "dep:data-encoding", "dep:postcard", "dep:derive_more"]
base32 = ["dep:data-encoding"]
redb = ["dep:redb"]
key = ["dep:ed25519-dalek", "dep:once_cell", "dep:rand", "dep:rand_core", "dep:ssh-key", "dep:ttl_cache", "dep:aead", "dep:crypto_box", "dep:zeroize", "dep:url", "dep:derive_more"]
1 change: 1 addition & 0 deletions iroh-base/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ impl MaxSize for Hash {
Debug,
MaxSize,
Hash,
derive_more::Display,
)]
pub enum BlobFormat {
/// Raw blob
Expand Down
1 change: 1 addition & 0 deletions iroh-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ indicatif = { version = "0.17", features = ["tokio"] }
iroh = { version = "0.15.0", path = "../iroh", features = ["metrics"] }
iroh-metrics = { version = "0.15.0", path = "../iroh-metrics" }
parking_lot = "0.12.1"
pkarr = { version = "1.1.5", default-features = false }
portable-atomic = "1"
postcard = "1.0.8"
quic-rpc = { version = "0.9.0", features = ["flume-transport", "quinn-transport"] }
Expand Down
9 changes: 7 additions & 2 deletions iroh-cli/src/commands/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -605,8 +605,13 @@ impl DocCommands {
Origin::Connect(_) => "we initiated",
};
match event.result {
Ok(()) => {
println!("synced peer {} ({origin})", fmt_short(event.peer))
Ok(details) => {
println!(
"synced peer {} ({origin}, received {}, sent {}",
fmt_short(event.peer),
details.entries_received,
details.entries_sent
)
}
Err(err) => println!(
"failed to sync with peer {} ({origin}): {err}",
Expand Down
60 changes: 54 additions & 6 deletions iroh-cli/src/commands/doctor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ use crate::config::{iroh_data_root, NodeConfig};

use anyhow::Context;
use clap::Subcommand;
use console::style;
use derive_more::Display;
use futures_lite::StreamExt;
use indicatif::{HumanBytes, MultiProgress, ProgressBar};
use iroh::{
Expand All @@ -34,6 +36,7 @@ use iroh::{
util::AbortingJoinHandle,
MagicEndpoint, NodeAddr, NodeId,
},
sync::Capability,
util::{path::IrohPaths, progress::ProgressWriter},
};
use portable_atomic::AtomicU64;
Expand Down Expand Up @@ -178,7 +181,11 @@ pub enum Commands {
count: usize,
},
/// Inspect a ticket.
TicketInspect { ticket: String },
TicketInspect {
ticket: String,
#[clap(long)]
zbase32: bool,
},
/// Perform a metadata consistency check on a blob store.
BlobConsistencyCheck {
/// Path of the blob store to validate. For iroh, this is the blobs subdirectory
Expand Down Expand Up @@ -957,19 +964,60 @@ fn create_discovery(disable_discovery: bool, secret_key: &SecretKey) -> Option<B
}
}

fn inspect_ticket(ticket: &str) -> anyhow::Result<()> {
fn bold<T: Display>(x: T) -> String {
style(x).bold().to_string()
}

fn to_z32(node_id: NodeId) -> String {
pkarr::PublicKey::try_from(*node_id.as_bytes())
.unwrap()
.to_z32()
}

fn print_node_addr(prefix: &str, node_addr: &NodeAddr, zbase32: bool) {
let node = if zbase32 {
to_z32(node_addr.node_id)
} else {
node_addr.node_id.to_string()
};
println!("{}node-id: {}", prefix, bold(node));
if let Some(relay_url) = node_addr.relay_url() {
println!("{}relay-url: {}", prefix, bold(relay_url));
}
for addr in node_addr.direct_addresses() {
println!("{}addr: {}", prefix, bold(addr.to_string()));
}
}

fn inspect_ticket(ticket: &str, zbase32: bool) -> anyhow::Result<()> {
if ticket.starts_with(iroh::ticket::BlobTicket::KIND) {
let ticket =
iroh::ticket::BlobTicket::from_str(ticket).context("failed parsing blob ticket")?;
println!("Blob ticket:\n{ticket:#?}");
println!("BlobTicket");
println!(" hash: {}", bold(ticket.hash()));
println!(" format: {}", bold(ticket.format()));
println!(" NodeInfo");
print_node_addr(" ", ticket.node_addr(), zbase32);
} else if ticket.starts_with(iroh::ticket::DocTicket::KIND) {
let ticket =
iroh::ticket::DocTicket::from_str(ticket).context("failed parsing doc ticket")?;
println!("Document ticket:\n{ticket:#?}");
println!("DocTicket:\n");
match ticket.capability {
Capability::Read(namespace) => {
println!(" read: {}", bold(namespace));
}
Capability::Write(secret) => {
println!(" write: {}", bold(secret));
}
}
for node in &ticket.nodes {
print_node_addr(" ", node, zbase32);
}
} else if ticket.starts_with(iroh::ticket::NodeTicket::KIND) {
let ticket =
iroh::ticket::NodeTicket::from_str(ticket).context("failed parsing node ticket")?;
println!("Node ticket:\n{ticket:#?}");
println!("NodeTicket");
print_node_addr(" ", ticket.node_addr(), zbase32);
} else {
println!("Unknown ticket type");
}
Expand Down Expand Up @@ -1052,7 +1100,7 @@ pub async fn run(command: Commands, config: &NodeConfig) -> anyhow::Result<()> {
let config = NodeConfig::load(None).await?;
relay_urls(count, config).await
}
Commands::TicketInspect { ticket } => inspect_ticket(&ticket),
Commands::TicketInspect { ticket, zbase32 } => inspect_ticket(&ticket, zbase32),
Commands::BlobConsistencyCheck { path, repair } => {
let blob_store = iroh::bytes::store::fs::Store::load(path).await?;
let (send, recv) = flume::bounded(1);
Expand Down
7 changes: 4 additions & 3 deletions iroh-net/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ rust-version = "1.75"
workspace = true

[dependencies]
axum = { version = "0.7.4", optional = true }
aead = { version = "0.5.2", features = ["bytes"] }
anyhow = { version = "1" }
backoff = "0.4.0"
bytes = "1"
default-net = "0.20"
netdev = "0.25"
der = { version = "0.7", features = ["alloc", "derive"] }
derive_more = { version = "1.0.0-beta.1", features = ["debug", "display", "from", "try_into", "deref"] }
flume = "0.11"
Expand Down Expand Up @@ -98,6 +99,7 @@ wmi = "0.13"
windows = { version = "0.51", features = ["Win32_NetworkManagement_IpHelper", "Win32_Foundation", "Win32_NetworkManagement_Ndis", "Win32_Networking_WinSock"] }

[dev-dependencies]
axum = { version = "0.7.4" }
clap = { version = "4", features = ["derive"] }
criterion = "0.5.1"
crypto_box = { version = "0.9.1", features = ["serde", "chacha20"] }
Expand All @@ -110,7 +112,6 @@ tokio = { version = "1", features = ["io-util", "sync", "rt", "net", "fs", "macr
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
iroh-test = { path = "../iroh-test" }
serde_json = "1.0.107"
axum = "0.7.4"

[[bench]]
name = "key"
Expand All @@ -123,7 +124,7 @@ duct = "0.13.6"
default = ["metrics"]
iroh-relay = ["clap", "toml", "rustls-pemfile", "regex", "serde_with", "tracing-subscriber"]
metrics = ["iroh-metrics/metrics"]
test-utils = []
test-utils = ["axum"]

[[bin]]
name = "iroh-relay"
Expand Down
55 changes: 20 additions & 35 deletions iroh-net/src/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -558,22 +558,19 @@ mod tests {
/// publish to. The DNS and pkarr servers share their state.
#[cfg(test)]
mod test_dns_pkarr {
use std::net::SocketAddr;
use std::time::Duration;

use anyhow::Result;
use iroh_base::key::SecretKey;
use url::Url;

use crate::{
discovery::{dns::DnsDiscovery, pkarr_publish::PkarrPublisher, ConcurrentDiscovery},
discovery::pkarr_publish::PkarrPublisher,
dns::node_info::{lookup_by_id, NodeInfo},
relay::{RelayMap, RelayMode},
test_utils::{
dns_and_pkarr_servers::run_dns_and_pkarr_servers,
dns_server::{create_dns_resolver, run_dns_server},
pkarr_dns_state::State,
run_relay_server,
run_relay_server, DnsPkarrServer,
},
AddrInfo, MagicEndpoint, NodeAddr,
};
Expand Down Expand Up @@ -610,8 +607,7 @@ mod test_dns_pkarr {
let origin = "testdns.example".to_string();
let timeout = Duration::from_secs(2);

let (nameserver, pkarr_url, state, _dns_drop_guard, _pkarr_drop_guard) =
run_dns_and_pkarr_servers(origin.clone()).await?;
let dns_pkarr_server = DnsPkarrServer::run_with_origin(origin.clone()).await?;

let secret_key = SecretKey::generate();
let node_id = secret_key.public();
Expand All @@ -621,12 +617,12 @@ mod test_dns_pkarr {
..Default::default()
};

let resolver = create_dns_resolver(nameserver)?;
let publisher = PkarrPublisher::new(secret_key, pkarr_url);
let resolver = create_dns_resolver(dns_pkarr_server.nameserver)?;
let publisher = PkarrPublisher::new(secret_key, dns_pkarr_server.pkarr_url.clone());
// does not block, update happens in background task
publisher.update_addr_info(&addr_info);
// wait until our shared state received the update from pkarr publishing
state.on_node(&node_id, timeout).await?;
dns_pkarr_server.on_node(&node_id, timeout).await?;
let resolved = lookup_by_id(&resolver, &node_id, &origin).await?;

let expected = NodeAddr {
Expand All @@ -644,18 +640,16 @@ mod test_dns_pkarr {
async fn pkarr_publish_dns_discover() -> Result<()> {
let _logging_guard = iroh_test::logging::setup();

let origin = "testdns.example".to_string();
let timeout = Duration::from_secs(2);

let (nameserver, pkarr_url, state, _dns_drop_guard, _pkarr_drop_guard) =
run_dns_and_pkarr_servers(&origin).await?;
let dns_pkarr_server = DnsPkarrServer::run().await?;
let (relay_map, _relay_url, _relay_guard) = run_relay_server().await?;

let ep1 = ep_with_discovery(relay_map.clone(), nameserver, &origin, &pkarr_url).await?;
let ep2 = ep_with_discovery(relay_map, nameserver, &origin, &pkarr_url).await?;
let ep1 = ep_with_discovery(&relay_map, &dns_pkarr_server).await?;
let ep2 = ep_with_discovery(&relay_map, &dns_pkarr_server).await?;

// wait until our shared state received the update from pkarr publishing
state.on_node(&ep1.node_id(), timeout).await?;
dns_pkarr_server.on_node(&ep1.node_id(), timeout).await?;

// we connect only by node id!
let res = ep2.connect(ep1.node_id().into(), TEST_ALPN).await;
Expand All @@ -667,18 +661,16 @@ mod test_dns_pkarr {
async fn pkarr_publish_dns_discover_empty_node_addr() -> Result<()> {
let _logging_guard = iroh_test::logging::setup();

let origin = "testdns.example".to_string();
let timeout = Duration::from_secs(2);

let (nameserver, pkarr_url, state, _dns_drop_guard, _pkarr_drop_guard) =
run_dns_and_pkarr_servers(&origin).await?;
let dns_pkarr_server = DnsPkarrServer::run().await?;
let (relay_map, _relay_url, _relay_guard) = run_relay_server().await?;

let ep1 = ep_with_discovery(relay_map.clone(), nameserver, &origin, &pkarr_url).await?;
let ep2 = ep_with_discovery(relay_map, nameserver, &origin, &pkarr_url).await?;
let ep1 = ep_with_discovery(&relay_map, &dns_pkarr_server).await?;
let ep2 = ep_with_discovery(&relay_map, &dns_pkarr_server).await?;

// wait until our shared state received the update from pkarr publishing
state.on_node(&ep1.node_id(), timeout).await?;
dns_pkarr_server.on_node(&ep1.node_id(), timeout).await?;

let node_addr = NodeAddr::new(ep1.node_id());

Expand All @@ -692,24 +684,17 @@ mod test_dns_pkarr {
}

async fn ep_with_discovery(
relay_map: RelayMap,
nameserver: SocketAddr,
node_origin: &str,
pkarr_relay: &Url,
relay_map: &RelayMap,
dns_pkarr_server: &DnsPkarrServer,
) -> Result<MagicEndpoint> {
let secret_key = SecretKey::generate();
let resolver = create_dns_resolver(nameserver)?;
let discovery = ConcurrentDiscovery::from_services(vec![
Box::new(DnsDiscovery::new(node_origin.to_string())),
Box::new(PkarrPublisher::new(secret_key.clone(), pkarr_relay.clone())),
]);
let ep = MagicEndpoint::builder()
.relay_mode(RelayMode::Custom(relay_map))
.relay_mode(RelayMode::Custom(relay_map.clone()))
.insecure_skip_relay_cert_verify(true)
.secret_key(secret_key)
.dns_resolver(resolver)
.secret_key(secret_key.clone())
.alpns(vec![TEST_ALPN.to_vec()])
.discovery(Box::new(discovery))
.dns_resolver(dns_pkarr_server.dns_resolver())
.discovery(dns_pkarr_server.discovery(secret_key))
.bind(0)
.await?;
Ok(ep)
Expand Down
2 changes: 2 additions & 0 deletions iroh-net/src/dns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ pub(crate) mod tests {
use super::*;

#[tokio::test]
#[cfg_attr(target_os = "windows", ignore = "flaky")]
async fn test_dns_lookup_basic() {
let _logging = iroh_test::logging::setup();
let resolver = default_resolver();
Expand All @@ -165,6 +166,7 @@ pub(crate) mod tests {
}

#[tokio::test]
#[cfg_attr(target_os = "windows", ignore = "flaky")]
async fn test_dns_lookup_ipv4_ipv6() {
let _logging = iroh_test::logging::setup();
let resolver = default_resolver();
Expand Down
3 changes: 2 additions & 1 deletion iroh-net/src/magic_endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ impl MagicEndpointBuilder {

/// Optionally set a custom DNS resolver to use for this endpoint.
///
/// The DNS resolver is used to resolve relay hostnames.
/// The DNS resolver is used to resolve relay hostnames, and node addresses if
/// [`crate::discovery::dns::DnsDiscovery`] is configured.
///
/// By default, all magic endpoints share a DNS resolver, which is configured to use the
/// host system's DNS configuration. You can pass a custom instance of [`DnsResolver`]
Expand Down
2 changes: 1 addition & 1 deletion iroh-net/src/net.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Networking related utilities
pub mod interfaces;
pub(crate) mod interfaces;
pub mod ip;
mod ip_family;
pub mod netmon;
Expand Down
Loading

0 comments on commit 3518b49

Please sign in to comment.