Skip to content

Commit

Permalink
add relay_map::QuicConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
“ramfox” committed Nov 25, 2024
1 parent 10bbdf5 commit ec4ddb3
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 18 deletions.
30 changes: 23 additions & 7 deletions iroh-base/src/relay_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl RelayMap {
url,
stun_only: false,
stun_port,
quic_port: DEFAULT_QUIC_PORT,
quic: Some(QuicConfig::default()),
}
.into(),
);
Expand Down Expand Up @@ -133,15 +133,31 @@ pub struct RelayNode {
///
/// Setting this to `0` means the default STUN port is used.
pub stun_port: u16,
/// The QUIC endpoint port of the relay server.
/// Configuration to speak to the QUIC endpoint on the relay server.
///
/// Setting this to `0` means the default QUIC port is used.
#[serde(default = "quic_port")]
pub quic_port: u16,
/// When `None`, we will not attempt to do QUIC address discovery
/// with this relay server.
#[serde(default = "quic_config")]
pub quic: Option<QuicConfig>,
}

fn quic_port() -> u16 {
DEFAULT_QUIC_PORT
fn quic_config() -> Option<QuicConfig> {
Some(QuicConfig::default())
}

/// Configuration for speaking to the QUIC endpoint on the relay
/// server to do QUIC address discovery.
#[derive(Debug, Deserialize, Serialize, Clone, Eq, PartialEq, PartialOrd, Ord)]
pub struct QuicConfig {
pub port: u16,
}

impl Default for QuicConfig {
fn default() -> Self {
Self {
port: DEFAULT_QUIC_PORT,
}
}
}

impl fmt::Display for RelayNode {
Expand Down
11 changes: 6 additions & 5 deletions iroh-net-report/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,7 @@ mod test_utils {

use std::sync::Arc;

use iroh_base::relay_map::QuicConfig;
use iroh_relay::server;

use crate::RelayNode;
Expand All @@ -791,14 +792,14 @@ mod test_utils {
let server = server::Server::spawn(server::testing::server_config())
.await
.expect("should serve relay");
let quic = Some(QuicConfig {
port: server.quic_addr().expect("server sould run quic").port(),
});
let node_desc = RelayNode {
url: server.https_url().expect("should work as relay"),
stun_only: false, // the checks above and below guarantee both stun and relay
stun_port: server.stun_addr().expect("server should serve stun").port(),
quic_port: server
.quic_addr()
.expect("server should server_stun")
.port(),
quic,
};

(server, Arc::new(node_desc))
Expand Down Expand Up @@ -883,7 +884,7 @@ mod tests {
url,
stun_port: port,
stun_only,
quic_port: 0,
quic: None,
}
});
RelayMap::from_nodes(nodes).expect("generated invalid nodes")
Expand Down
13 changes: 8 additions & 5 deletions iroh-net/src/defaults.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Default values used in [`iroh-net`][`crate`]

use iroh_base::relay_map::QuicConfig;
/// The default QUIC port used by the Relay server to accept QUIC connections
/// for QUIC address discovery
///
Expand All @@ -25,6 +26,8 @@ pub const DEFAULT_METRICS_PORT: u16 = 9090;

/// Production configuration.
pub mod prod {
use iroh_base::relay_map::QuicConfig;

use super::*;

/// Hostname of the default NA relay.
Expand Down Expand Up @@ -54,7 +57,7 @@ pub mod prod {
url: url.into(),
stun_only: false,
stun_port: DEFAULT_STUN_PORT,
quic_port: DEFAULT_QUIC_PORT,
quic: Some(QuicConfig::default()),
}
}

Expand All @@ -68,7 +71,7 @@ pub mod prod {
url: url.into(),
stun_only: false,
stun_port: DEFAULT_STUN_PORT,
quic_port: DEFAULT_QUIC_PORT,
quic: Some(QuicConfig::default()),
}
}

Expand All @@ -82,7 +85,7 @@ pub mod prod {
url: url.into(),
stun_only: false,
stun_port: DEFAULT_STUN_PORT,
quic_port: DEFAULT_QUIC_PORT,
quic: Some(QuicConfig::default()),
}
}
}
Expand Down Expand Up @@ -116,7 +119,7 @@ pub mod staging {
url: url.into(),
stun_only: false,
stun_port: DEFAULT_STUN_PORT,
quic_port: DEFAULT_QUIC_PORT,
quic: Some(QuicConfig::default()),
}
}

Expand All @@ -130,7 +133,7 @@ pub mod staging {
url: url.into(),
stun_only: false,
stun_port: DEFAULT_STUN_PORT,
quic_port: DEFAULT_QUIC_PORT,
quic: Some(QuicConfig::default()),
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion iroh-net/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,14 @@ pub async fn run_relay_server_with(
let url: RelayUrl = format!("https://{}", server.https_addr().expect("configured"))
.parse()
.unwrap();
let quic = Some(iroh_base::relay_map::QuicConfig {
port: server.quic_addr().map_or(DEFAULT_QUIC_PORT, |s| s.port()),
});
let m = RelayMap::from_nodes([RelayNode {
url: url.clone(),
stun_only: false,
stun_port: server.stun_addr().map_or(DEFAULT_STUN_PORT, |s| s.port()),
quic_port: server.quic_addr().map_or(DEFAULT_QUIC_PORT, |s| s.port()),
quic,
}])
.unwrap();
Ok((m, url, server))
Expand Down

0 comments on commit ec4ddb3

Please sign in to comment.