Skip to content

Commit e5aa744

Browse files
committed
feat: allow to connect via node id only
1 parent 733cf2f commit e5aa744

File tree

2 files changed

+32
-31
lines changed

2 files changed

+32
-31
lines changed

iroh-net/src/magic_endpoint.rs

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -442,23 +442,8 @@ impl MagicEndpoint {
442442
node_id: &PublicKey,
443443
alpn: &[u8],
444444
) -> Result<quinn::Connection> {
445-
let addr = match self.msock.get_mapping_addr(node_id).await {
446-
Some(addr) => addr,
447-
None => {
448-
let info = self.resolve(node_id).await?;
449-
let peer_addr = NodeAddr {
450-
node_id: *node_id,
451-
info,
452-
};
453-
self.add_node_addr(peer_addr)?;
454-
self.msock.get_mapping_addr(node_id).await.ok_or_else(|| {
455-
anyhow!("Failed to retrieve the mapped address from the magic socket. Unable to dial node {node_id:?}")
456-
})?
457-
}
458-
};
459-
460-
debug!("connecting to {}: (via {})", node_id, addr);
461-
self.connect_inner(node_id, alpn, addr).await
445+
let addr = NodeAddr::new(*node_id);
446+
self.connect(addr, alpn).await
462447
}
463448

464449
/// Connect to a remote endpoint.
@@ -468,21 +453,42 @@ impl MagicEndpoint {
468453
/// connection without involving a DERP server. If no addresses are specified, the endpoint
469454
/// will try to dial the peer through the configured DERP servers.
470455
///
471-
/// If the `derp_url` is not `None` and the configured DERP servers do not include a DERP node from the given `derp_url`, it will error.
456+
/// If the `derp_url` is not `None` and the configured DERP servers do not include a DERP node from the given `derp_url`,
457+
/// it will use the configures [`Discovery`] service to retrieve DERP or direct addresses. If
458+
/// this fails to return any info, it will error.
472459
///
473460
/// If no UDP addresses and no DERP Url is provided, it will error.
461+
//
462+
// TODO: We should try to use the discovery service even if direct or derp addresses are
463+
// provided.
474464
pub async fn connect(&self, node_addr: NodeAddr, alpn: &[u8]) -> Result<quinn::Connection> {
475-
self.add_node_addr(node_addr.clone())?;
465+
if !node_addr.info.is_empty() {
466+
self.add_node_addr(node_addr.clone())?;
467+
}
476468

477469
let NodeAddr { node_id, info } = node_addr;
478-
let addr = self.msock.get_mapping_addr(&node_id).await;
479-
let Some(addr) = addr else {
480-
return Err(match (info.direct_addresses.is_empty(), info.derp_url) {
481-
(true, None) => {
482-
anyhow!("No UDP addresses or DERP Url provided. Unable to dial node {node_id:?}")
470+
471+
let addr = match self.msock.get_mapping_addr(&node_id).await {
472+
Some(addr) => addr,
473+
None => {
474+
// Try to resolve the node address through the configured discovery service
475+
let discovery = self.resolve(&node_id).await;
476+
match discovery {
477+
Ok(info) if !info.is_empty() => {
478+
let node_addr = NodeAddr { node_id, info };
479+
self.add_node_addr(node_addr)?;
480+
self.msock.get_mapping_addr(&node_id).await.ok_or_else(|| {
481+
anyhow!("Failed to retrieve the mapped address from the magic socket. Unable to dial node {node_id:?}")
482+
})?
483+
}
484+
Ok(_info) => {
485+
return Err(anyhow!("Failed to discover Derp or direct adddresses for node {node_id:?}, unable to dial"));
486+
}
487+
Err(_err) => {
488+
return Err(anyhow!("Failed to discover Derp or direct adddresses for node {node_id:?}, unable to dial"));
489+
}
483490
}
484-
_ => anyhow!("Failed to retrieve the mapped address from the magic socket. Unable to dial node {node_id:?}")
485-
});
491+
}
486492
};
487493

488494
debug!(

iroh/src/commands/blob.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,6 @@ impl BlobCommands {
199199
return Err(anyhow::anyhow!("The input arguments refer to a collection of blobs and output is set to STDOUT. Only single blobs may be passed in this case."));
200200
}
201201

202-
if node_addr.info.is_empty() {
203-
return Err(anyhow::anyhow!(
204-
"no Derp url provided and no direct addresses provided"
205-
));
206-
}
207202
let tag = match tag {
208203
Some(tag) => SetTagOption::Named(Tag::from(tag)),
209204
None => SetTagOption::Auto,

0 commit comments

Comments
 (0)