Skip to content

Commit

Permalink
chore: Test that the legacy /derp route keeps working
Browse files Browse the repository at this point in the history
  • Loading branch information
matheus23 committed Jul 1, 2024
1 parent 18f1533 commit d0de17f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 71 deletions.
28 changes: 17 additions & 11 deletions iroh-net/src/relay/http/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ struct Actor {
ping_tasks: JoinSet<()>,
dns_resolver: DnsResolver,
proxy_url: Option<Url>,
relay_path: &'static str,
}

#[derive(Default, Debug)]
Expand All @@ -203,12 +204,14 @@ impl PingTracker {
}

/// Build a Client.
#[derive(derive_more::Debug)]
pub struct ClientBuilder {
/// Default is false
can_ack_pings: bool,
/// Default is false
is_preferred: bool,
/// Default is None
#[debug("address family selector callback")]
address_family_selector: Option<Box<dyn Fn() -> BoxFuture<bool> + Send + Sync + 'static>>,
/// Default is false
is_prober: bool,
Expand All @@ -221,16 +224,8 @@ pub struct ClientBuilder {
insecure_skip_cert_verify: bool,
/// HTTP Proxy
proxy_url: Option<Url>,
}

impl std::fmt::Debug for ClientBuilder {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let address_family_selector_txt = match self.address_family_selector {
Some(_) => "Some(Box<dyn Fn() -> BoxFuture<'static, bool> + Send + Sync + 'static>)",
None => "None",
};
write!(f, "ClientBuilder {{ can_ack_pings: {}, is_preferred: {}, address_family_selector: {address_family_selector_txt} }}", self.can_ack_pings, self.is_preferred)
}
/// Default is "/relay"
relay_path: &'static str,
}

impl ClientBuilder {
Expand All @@ -246,6 +241,7 @@ impl ClientBuilder {
#[cfg(any(test, feature = "test-utils"))]
insecure_skip_cert_verify: false,
proxy_url: None,
relay_path: RELAY_HTTP_PATH,
}
}

Expand Down Expand Up @@ -303,6 +299,15 @@ impl ClientBuilder {
self
}

/// Set the relay endpoint path used for making connections.
///
/// Private for now, as it's only used in tests so far.
#[cfg(test)]
pub(crate) fn relay_path(mut self, relay_path: &'static str) -> Self {
self.relay_path = relay_path;
self
}

/// Build the [`Client`]
pub fn build(self, key: SecretKey, dns_resolver: DnsResolver) -> (Client, ClientReceiver) {
// TODO: review TLS config
Expand Down Expand Up @@ -345,6 +350,7 @@ impl ClientBuilder {
tls_connector,
dns_resolver,
proxy_url: self.proxy_url,
relay_path: self.relay_path,
};

let (msg_sender, inbox) = mpsc::channel(64);
Expand Down Expand Up @@ -621,7 +627,7 @@ impl Actor {

async fn connect_ws(&self) -> Result<(ConnReader, ConnWriter), ClientError> {
let mut dial_url = (*self.url).clone();
dial_url.set_path(RELAY_HTTP_PATH);
dial_url.set_path(self.relay_path);

debug!(%dial_url, "Dialing relay by websocket");

Expand Down
96 changes: 36 additions & 60 deletions iroh-net/src/relay/iroh_relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,20 @@ mod tests {

use super::*;

async fn spawn_local_relay() -> Result<Server> {
Server::spawn(ServerConfig::<(), ()> {
relay: Some(RelayConfig {
secret_key: SecretKey::generate(),
http_bind_addr: (Ipv4Addr::LOCALHOST, 0).into(),
tls: None,
limits: Default::default(),
}),
stun: None,
metrics_addr: None,
})
.await
}

#[tokio::test]
async fn test_no_services() {
let _guard = iroh_test::logging::setup();
Expand Down Expand Up @@ -749,18 +763,7 @@ mod tests {
#[tokio::test]
async fn test_root_handler() {
let _guard = iroh_test::logging::setup();
let server = Server::spawn(ServerConfig::<(), ()> {
relay: Some(RelayConfig {
secret_key: SecretKey::generate(),
http_bind_addr: (Ipv4Addr::LOCALHOST, 0).into(),
tls: None,
limits: Default::default(),
}),
stun: None,
metrics_addr: None,
})
.await
.unwrap();
let server = spawn_local_relay().await.unwrap();
let url = format!("http://{}", server.http_addr().unwrap());

let response = reqwest::get(&url).await.unwrap();
Expand All @@ -772,18 +775,7 @@ mod tests {
#[tokio::test]
async fn test_captive_portal_service() {
let _guard = iroh_test::logging::setup();
let server = Server::spawn(ServerConfig::<(), ()> {
relay: Some(RelayConfig {
secret_key: SecretKey::generate(),
http_bind_addr: (Ipv4Addr::LOCALHOST, 0).into(),
tls: None,
limits: Default::default(),
}),
stun: None,
metrics_addr: None,
})
.await
.unwrap();
let server = spawn_local_relay().await.unwrap();
let url = format!("http://{}/generate_204", server.http_addr().unwrap());
let challenge = "123az__.";

Expand All @@ -801,21 +793,27 @@ mod tests {
assert!(body.is_empty());
}

#[tokio::test]
async fn test_relay_client_legacy_route() {
let _guard = iroh_test::logging::setup();
let server = spawn_local_relay().await.unwrap();
let relay_url = format!("http://{}", server.http_addr().unwrap());
let relay_url: RelayUrl = relay_url.parse().unwrap();

// set up client a
let secret_key = SecretKey::generate();
let resolver = crate::dns::default_resolver().clone();
let (client, _) = ClientBuilder::new(relay_url)
.relay_path("/derp") // Try the legacy relay path for backwards compatibility
.build(secret_key, resolver);

client.ping().await.unwrap();
}

#[tokio::test]
async fn test_relay_clients_both_derp() {
let _guard = iroh_test::logging::setup();
let server = Server::spawn(ServerConfig::<(), ()> {
relay: Some(RelayConfig {
secret_key: SecretKey::generate(),
http_bind_addr: (Ipv4Addr::LOCALHOST, 0).into(),
tls: None,
limits: Default::default(),
}),
stun: None,
metrics_addr: None,
})
.await
.unwrap();
let server = spawn_local_relay().await.unwrap();
let relay_url = format!("http://{}", server.http_addr().unwrap());
let relay_url: RelayUrl = relay_url.parse().unwrap();

Expand Down Expand Up @@ -880,18 +878,7 @@ mod tests {
#[tokio::test]
async fn test_relay_clients_both_websockets() {
let _guard = iroh_test::logging::setup();
let server = Server::spawn(ServerConfig::<(), ()> {
relay: Some(RelayConfig {
secret_key: SecretKey::generate(),
http_bind_addr: (Ipv4Addr::LOCALHOST, 0).into(),
tls: None,
limits: Default::default(),
}),
stun: None,
metrics_addr: None,
})
.await
.unwrap();
let server = spawn_local_relay().await.unwrap();
// NOTE: Using `ws://` URL scheme to trigger websockets.
let relay_url = format!("ws://{}", server.http_addr().unwrap());
let relay_url: RelayUrl = relay_url.parse().unwrap();
Expand Down Expand Up @@ -957,18 +944,7 @@ mod tests {
#[tokio::test]
async fn test_relay_clients_websocket_and_derp() {
let _guard = iroh_test::logging::setup();
let server = Server::spawn(ServerConfig::<(), ()> {
relay: Some(RelayConfig {
secret_key: SecretKey::generate(),
http_bind_addr: (Ipv4Addr::LOCALHOST, 0).into(),
tls: None,
limits: Default::default(),
}),
stun: None,
metrics_addr: None,
})
.await
.unwrap();
let server = spawn_local_relay().await.unwrap();

let derp_relay_url = format!("http://{}", server.http_addr().unwrap());
let derp_relay_url: RelayUrl = derp_relay_url.parse().unwrap();
Expand Down

0 comments on commit d0de17f

Please sign in to comment.