From c1bf1424846ce3a1297ed3be7b8401cef6904bee Mon Sep 17 00:00:00 2001 From: Jacob Fullerton Date: Mon, 29 Jan 2024 16:33:01 -0800 Subject: [PATCH] Add endpoint connection mode query param (#392) * add endpoint query param * Revert "add endpoint query param" This reverts commit 76b489c2e80d2f51a9c9b551e1310ac8fa26e1e7. * use tostring * update types * fix delete * fix build * update port request * fix syntax * fix build again * fix build * compilation error, build connections in ci * run clippy and cargo fmt --------- Co-authored-by: Connor Peet --- .github/workflows/rust.yml | 6 +-- rs/src/connections/relay_tunnel_host.rs | 7 ++-- rs/src/connections/ws.rs | 3 +- rs/src/contracts/tunnel.rs | 2 +- .../contracts/tunnel_access_control_entry.rs | 2 +- rs/src/contracts/tunnel_constraints.rs | 2 +- rs/src/contracts/tunnel_port.rs | 2 +- rs/src/management/http_client.rs | 37 ++++++++----------- 8 files changed, 27 insertions(+), 34 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index b598ec76..671db906 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -17,7 +17,7 @@ jobs: steps: - uses: actions/checkout@v3 - name: Build - run: cargo build --verbose --manifest-path=rs/Cargo.toml + run: cargo build --features connections --verbose --manifest-path=rs/Cargo.toml - name: Run tests - run: cargo test --verbose --manifest-path=rs/Cargo.toml - + run: cargo test --features connections --verbose --manifest-path=rs/Cargo.toml + diff --git a/rs/src/connections/relay_tunnel_host.rs b/rs/src/connections/relay_tunnel_host.rs index 9c573251..e52e27fe 100644 --- a/rs/src/connections/relay_tunnel_host.rs +++ b/rs/src/connections/relay_tunnel_host.rs @@ -239,8 +239,7 @@ impl RelayTunnelHost { self.mgmt .delete_tunnel_endpoints( &self.locator, - &self.host_id.to_string(), - None, + &format!("{}-relay", &self.host_id.to_string()), NO_REQUEST_OPTIONS, ) .await @@ -326,7 +325,7 @@ impl RelayTunnelHost { connection_timeout: None, auth_rejection_time: std::time::Duration::from_secs(5), keys: vec![keypair], - window_size: 1024 * 1024 * 1, + window_size: 1024 * 1024, preferred: russh::Preferred::COMPRESSED, limits: russh::Limits { rekey_read_limit: usize::MAX, @@ -390,7 +389,7 @@ impl RelayTunnelHost { &self.locator, &TunnelRelayTunnelEndpoint { base: TunnelEndpoint { - id: Some(uuid::Uuid::new_v4().to_string()), + id: format!("{}-relay", self.host_id), connection_mode: TunnelConnectionMode::TunnelRelay, host_id: self.host_id.to_string(), host_public_keys: vec![], diff --git a/rs/src/connections/ws.rs b/rs/src/connections/ws.rs index 380969b3..12460197 100644 --- a/rs/src/connections/ws.rs +++ b/rs/src/connections/ws.rs @@ -368,8 +368,7 @@ mod test { } }); - let mut output = Vec::new(); - output.resize(input_len, 0); + let mut output = vec![0; input_len]; read.read_exact(&mut output) .await .expect("expected to read"); diff --git a/rs/src/contracts/tunnel.rs b/rs/src/contracts/tunnel.rs index 16a78133..3e71632f 100644 --- a/rs/src/contracts/tunnel.rs +++ b/rs/src/contracts/tunnel.rs @@ -2,12 +2,12 @@ // Licensed under the MIT license. // Generated from ../../../cs/src/Contracts/Tunnel.cs -use chrono::{DateTime, Utc}; use crate::contracts::TunnelAccessControl; use crate::contracts::TunnelEndpoint; use crate::contracts::TunnelOptions; use crate::contracts::TunnelPort; use crate::contracts::TunnelStatus; +use chrono::{DateTime, Utc}; use serde::{Deserialize, Serialize}; use std::collections::HashMap; diff --git a/rs/src/contracts/tunnel_access_control_entry.rs b/rs/src/contracts/tunnel_access_control_entry.rs index b36863a9..fa7f985a 100644 --- a/rs/src/contracts/tunnel_access_control_entry.rs +++ b/rs/src/contracts/tunnel_access_control_entry.rs @@ -2,8 +2,8 @@ // Licensed under the MIT license. // Generated from ../../../cs/src/Contracts/TunnelAccessControlEntry.cs -use chrono::{DateTime, Utc}; use crate::contracts::TunnelAccessControlEntryType; +use chrono::{DateTime, Utc}; use serde::{Deserialize, Serialize}; // Data contract for an access control entry on a `Tunnel` or `TunnelPort`. diff --git a/rs/src/contracts/tunnel_constraints.rs b/rs/src/contracts/tunnel_constraints.rs index fd4eb0e5..142bc27f 100644 --- a/rs/src/contracts/tunnel_constraints.rs +++ b/rs/src/contracts/tunnel_constraints.rs @@ -110,7 +110,7 @@ pub const TUNNEL_ALIAS_PATTERN: &str = r#"[0123456789bcdfghjklmnpqrstvwxz]{3,60} pub const TUNNEL_NAME_PATTERN: &str = r#"([a-z0-9][a-z0-9-]{1,58}[a-z0-9])|(^$)"#; // Regular expression that can match or validate tunnel or port labels. -pub const LABEL_PATTERN: &str = r#"[\w-=]{1,50}"#; +pub const LABEL_PATTERN: &str = r"[\w-=]{1,50}"; // Regular expression that can match or validate tunnel domains. // diff --git a/rs/src/contracts/tunnel_port.rs b/rs/src/contracts/tunnel_port.rs index 1fd3e41a..89611508 100644 --- a/rs/src/contracts/tunnel_port.rs +++ b/rs/src/contracts/tunnel_port.rs @@ -43,7 +43,7 @@ pub struct TunnelPort { // A client that connects to a tunnel (by ID or name) without specifying a port number // will connect to the default port for the tunnel, if a default is configured. Or if // the tunnel has only one port then the single port is the implicit default. - // + // // Selection of a default port for a connection also depends on matching the // connection to the port `TunnelPort.Protocol`, so it is possible to configure // separate defaults for distinct protocols like `TunnelProtocol.Http` and diff --git a/rs/src/management/http_client.rs b/rs/src/management/http_client.rs index 216432b3..0ad685c9 100644 --- a/rs/src/management/http_client.rs +++ b/rs/src/management/http_client.rs @@ -14,9 +14,8 @@ use url::Url; use rand::Rng; use crate::contracts::{ - env_production, NamedRateStatus, Tunnel, TunnelConnectionMode, TunnelEndpoint, - TunnelListByRegionResponse, TunnelPort, TunnelPortListResponse, TunnelRelayTunnelEndpoint, - TunnelServiceProperties, + env_production, NamedRateStatus, Tunnel, TunnelEndpoint, TunnelListByRegionResponse, + TunnelPort, TunnelPortListResponse, TunnelRelayTunnelEndpoint, TunnelServiceProperties, }; use super::{ @@ -161,13 +160,12 @@ impl TunnelManagementClient { endpoint: &TunnelEndpoint, options: &TunnelRequestOptions, ) -> HttpResult { - let url = self.build_tunnel_uri( + let mut url = self.build_tunnel_uri( locator, - Some(&format!( - "{}/{}/{}", - ENDPOINTS_API_SUB_PATH, endpoint.host_id, endpoint.connection_mode - )), + Some(&format!("{}/{}", ENDPOINTS_API_SUB_PATH, endpoint.id)), ); + url.query_pairs_mut() + .append_pair("connectionMode", &endpoint.connection_mode.to_string()); let mut request = self.make_tunnel_request(Method::PUT, url, options).await?; json_body(&mut request, endpoint); self.execute_json("update_tunnel_endpoints", request).await @@ -180,13 +178,12 @@ impl TunnelManagementClient { endpoint: &TunnelRelayTunnelEndpoint, options: &TunnelRequestOptions, ) -> HttpResult { - let url = self.build_tunnel_uri( + let mut url = self.build_tunnel_uri( locator, - Some(&format!( - "{}/{}", - ENDPOINTS_API_SUB_PATH, endpoint.base.id - )), + Some(&format!("{}/{}", ENDPOINTS_API_SUB_PATH, endpoint.base.id)), ); + url.query_pairs_mut() + .append_pair("connectionMode", &endpoint.base.connection_mode.to_string()); let mut request = self.make_tunnel_request(Method::PUT, url, options).await?; json_body(&mut request, endpoint); self.execute_json("update_tunnel_relay_endpoints", request) @@ -197,15 +194,10 @@ impl TunnelManagementClient { pub async fn delete_tunnel_endpoints( &self, locator: &TunnelLocator, - host_id: &str, - connection_mode: Option, + id: &str, options: &TunnelRequestOptions, ) -> HttpResult { - let path = if let Some(cm) = connection_mode { - format!("{}/{}/{}", ENDPOINTS_API_SUB_PATH, host_id, cm) - } else { - format!("{}/{}", ENDPOINTS_API_SUB_PATH, host_id) - }; + let path = format!("{}/{}", ENDPOINTS_API_SUB_PATH, id); let url = self.build_tunnel_uri(locator, Some(&path)); let request = self @@ -250,7 +242,10 @@ impl TunnelManagementClient { port: &TunnelPort, options: &TunnelRequestOptions, ) -> HttpResult { - let url = self.build_tunnel_uri(locator, Some(PORTS_API_SUB_PATH)); + let url = self.build_tunnel_uri( + locator, + Some(&format!("{}/{}", PORTS_API_SUB_PATH, port.port_number)), + ); let mut request = self.make_tunnel_request(Method::PUT, url, options).await?; json_body(&mut request, port); self.execute_json("create_tunnel_port", request).await