@@ -442,23 +442,8 @@ impl MagicEndpoint {
442
442
node_id : & PublicKey ,
443
443
alpn : & [ u8 ] ,
444
444
) -> 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
462
447
}
463
448
464
449
/// Connect to a remote endpoint.
@@ -468,21 +453,42 @@ impl MagicEndpoint {
468
453
/// connection without involving a DERP server. If no addresses are specified, the endpoint
469
454
/// will try to dial the peer through the configured DERP servers.
470
455
///
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.
472
459
///
473
460
/// 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.
474
464
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
+ }
476
468
477
469
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
+ }
483
490
}
484
- _ => anyhow ! ( "Failed to retrieve the mapped address from the magic socket. Unable to dial node {node_id:?}" )
485
- } ) ;
491
+ }
486
492
} ;
487
493
488
494
debug ! (
0 commit comments