Skip to content

Commit

Permalink
feat: add discovery option to doctor (#2182)
Browse files Browse the repository at this point in the history
## Description

`iroh doctor accept` and `iroh doctor connect` will both run with
discovery services enabled by default.

Use the `--disable-discovery` flag to disable discovery.

This PR also cleans up some of the output for `iroh doctor accept`


## Change checklist

- [x] Self-review.
- [x] Documentation updates if relevant.
  • Loading branch information
ramfox authored Apr 15, 2024
1 parent 70db5fb commit 2c1eca9
Showing 1 changed file with 68 additions and 7 deletions.
75 changes: 68 additions & 7 deletions iroh-cli/src/commands/doctor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ use iroh::{
},
net::{
defaults::DEFAULT_RELAY_STUN_PORT,
discovery::{
dns::DnsDiscovery, pkarr_publish::PkarrPublisher, ConcurrentDiscovery, Discovery,
},
dns::default_resolver,
key::{PublicKey, SecretKey},
magic_endpoint,
Expand Down Expand Up @@ -97,6 +100,15 @@ pub enum Commands {
/// Use a local relay
#[clap(long)]
local_relay_server: bool,

/// Do not allow the node to dial and be dialed by id only.
///
/// This disables DNS discovery, which would allow the node to dial other nodes by id only.
/// And it disables Pkarr Publishing, which would allow the node to announce its address for dns discovery.
///
/// Default is `false`
#[clap(long, default_value_t = false)]
disable_discovery: bool,
},
/// Connect to an iroh doctor accept node.
Connect {
Expand Down Expand Up @@ -126,6 +138,15 @@ pub enum Commands {
/// Default is `None`.
#[clap(long)]
relay_url: Option<RelayUrl>,

/// Do not allow the node to dial and be dialed by id only.
///
/// This disables DNS discovery, which would allow the node to dial other nodes by id only.
/// And it disables Pkarr Publishing, which would allow the node to announce its address for dns discovery.
///
/// Default is `false`
#[clap(long, default_value_t = false)]
disable_discovery: bool,
},
/// Probe the port mapping protocols.
PortMapProbe {
Expand Down Expand Up @@ -591,6 +612,7 @@ const DR_RELAY_ALPN: [u8; 11] = *b"n0/drderp/1";
async fn make_endpoint(
secret_key: SecretKey,
relay_map: Option<RelayMap>,
discovery: Option<Box<dyn Discovery>>,
) -> anyhow::Result<MagicEndpoint> {
tracing::info!(
"public key: {}",
Expand All @@ -607,6 +629,11 @@ async fn make_endpoint(
.alpns(vec![DR_RELAY_ALPN.to_vec()])
.transport_config(transport_config);

let endpoint = match discovery {
Some(discovery) => endpoint.discovery(discovery),
None => endpoint,
};

let endpoint = match relay_map {
Some(relay_map) => endpoint.relay_mode(RelayMode::Custom(relay_map)),
None => endpoint,
Expand All @@ -627,8 +654,9 @@ async fn connect(
direct_addresses: Vec<SocketAddr>,
relay_url: Option<RelayUrl>,
relay_map: Option<RelayMap>,
discovery: Option<Box<dyn Discovery>>,
) -> anyhow::Result<()> {
let endpoint = make_endpoint(secret_key, relay_map).await?;
let endpoint = make_endpoint(secret_key, relay_map, discovery).await?;

tracing::info!("dialing {:?}", node_id);
let node_addr = NodeAddr::from_parts(node_id, relay_url, direct_addresses);
Expand Down Expand Up @@ -660,8 +688,9 @@ async fn accept(
secret_key: SecretKey,
config: TestConfig,
relay_map: Option<RelayMap>,
discovery: Option<Box<dyn Discovery>>,
) -> anyhow::Result<()> {
let endpoint = make_endpoint(secret_key.clone(), relay_map).await?;
let endpoint = make_endpoint(secret_key.clone(), relay_map, discovery).await?;
let endpoints = endpoint
.local_endpoints()
.next()
Expand All @@ -672,19 +701,25 @@ async fn accept(
.map(|endpoint| format!("--remote-endpoint {}", format_addr(endpoint.addr)))
.collect::<Vec<_>>()
.join(" ");
println!("Connect to this node using one of the following commands to connect either directly by address or indirectly by relay url:");
println!("Connect to this node using one of the following commands:\n");
println!(
"iroh doctor connect {} {}",
"\tUsing the relay url and direct connections:\niroh doctor connect {} {}\n",
secret_key.public(),
remote_addrs,
);
if let Some(relay_url) = endpoint.my_relay() {
println!(
"iroh doctor connect {} --relay-url {}",
"\tUsing just the relay url:\niroh doctor connect {} --relay-url {}\n",
secret_key.public(),
relay_url,
);
}
if endpoint.discovery().is_some() {
println!(
"\tUsing just the node id:\niroh doctor connect {}\n",
secret_key.public(),
);
}
let connections = Arc::new(AtomicU64::default());
while let Some(connecting) = endpoint.accept().await {
let connections = connections.clone();
Expand Down Expand Up @@ -911,6 +946,19 @@ fn create_secret_key(secret_key: SecretKeyOption) -> anyhow::Result<SecretKey> {
})
}

fn create_discovery(disable_discovery: bool, secret_key: &SecretKey) -> Option<Box<dyn Discovery>> {
if disable_discovery {
None
} else {
Some(Box::new(ConcurrentDiscovery::from_services(vec![
// Enable DNS discovery by default
Box::new(DnsDiscovery::n0_dns()),
// Enable pkarr publishing by default
Box::new(PkarrPublisher::n0_dns(secret_key.clone())),
])))
}
}

fn inspect_ticket(ticket: &str) -> anyhow::Result<()> {
if ticket.starts_with(iroh::ticket::BlobTicket::KIND) {
let ticket =
Expand Down Expand Up @@ -945,6 +993,7 @@ pub async fn run(command: Commands, config: &NodeConfig) -> anyhow::Result<()> {
local_relay_server,
relay_url,
remote_endpoint,
disable_discovery,
} => {
let (relay_map, relay_url) = if local_relay_server {
let dm = configure_local_relay_map();
Expand All @@ -954,13 +1003,24 @@ pub async fn run(command: Commands, config: &NodeConfig) -> anyhow::Result<()> {
(config.relay_map()?, relay_url)
};
let secret_key = create_secret_key(secret_key)?;
connect(dial, secret_key, remote_endpoint, relay_url, relay_map).await

let discovery = create_discovery(disable_discovery, &secret_key);
connect(
dial,
secret_key,
remote_endpoint,
relay_url,
relay_map,
discovery,
)
.await
}
Commands::Accept {
secret_key,
local_relay_server,
size,
iterations,
disable_discovery,
} => {
let relay_map = if local_relay_server {
Some(configure_local_relay_map())
Expand All @@ -969,7 +1029,8 @@ pub async fn run(command: Commands, config: &NodeConfig) -> anyhow::Result<()> {
};
let secret_key = create_secret_key(secret_key)?;
let config = TestConfig { size, iterations };
accept(secret_key, config, relay_map).await
let discovery = create_discovery(disable_discovery, &secret_key);
accept(secret_key, config, relay_map, discovery).await
}
Commands::PortMap {
protocol,
Expand Down

0 comments on commit 2c1eca9

Please sign in to comment.