From 9352c9cf13030a0664dbc264486065a54ad57d40 Mon Sep 17 00:00:00 2001 From: elenaf9 Date: Wed, 6 Jul 2022 00:15:09 +0200 Subject: [PATCH 1/4] rust-client: add api-key & listen_multi_addresses --- rust-client/src/main.rs | 68 ++++++++++++++++++++++++++--------------- 1 file changed, 43 insertions(+), 25 deletions(-) diff --git a/rust-client/src/main.rs b/rust-client/src/main.rs index 37f909b..5fd9494 100644 --- a/rust-client/src/main.rs +++ b/rust-client/src/main.rs @@ -47,14 +47,21 @@ struct Opt { #[clap(long)] secret_key_seed: u8, + /// If set to `true`, use relay-v1 protocol. + /// Per default relay-v2 is used. #[clap(long)] relay_v1: bool, + + /// Api-key used to authenticate our client at the server. + #[clap(long)] + api_key: String, } #[tokio::main] async fn main() -> Result<(), Box> { let _ = env_logger::try_init(); let opt = Opt::parse(); + let api_key = opt.api_key; let mut client = grpc::punchr_service_client::PunchrServiceClient::connect(opt.url.clone()).await?; @@ -85,6 +92,7 @@ async fn main() -> Result<(), Box> { client_id: local_peer_id.to_bytes(), agent_version: agent_version(), protocols: protocols.clone().unwrap(), + api_key: api_key.clone(), }); client.register(request).await?; @@ -92,6 +100,7 @@ async fn main() -> Result<(), Box> { let request = tonic::Request::new(grpc::GetAddrInfoRequest { host_id: local_peer_id.to_bytes(), all_host_ids: vec![local_peer_id.to_bytes()], + api_key: api_key.clone(), }); let response = client.get_addr_info(request).await?.into_inner(); @@ -104,29 +113,19 @@ async fn main() -> Result<(), Box> { .map(Multiaddr::try_from) .collect::, libp2p::multiaddr::Error>>()?; + let state = HolePunchState::new( + local_peer_id, + swarm.listeners(), + remote_peer_id, + remote_addrs.clone(), + api_key.clone(), + ); + if remote_addrs .iter() .all(|a| a.iter().any(|p| p == libp2p::multiaddr::Protocol::Quic)) { - info!( - "Skipping hole punch through to {:?} via {:?} because the Quic transport is not supported.", - remote_peer_id, remote_addrs - ); - let unix_time_now = unix_time_now(); - let request = grpc::TrackHolePunchRequest { - client_id: local_peer_id.into(), - remote_id: response.remote_id, - remote_multi_addresses: remote_addrs.into_iter().map(|a| a.to_vec()).collect(), - open_multi_addresses: Vec::new(), - has_direct_conns: false, - connect_started_at: unix_time_now, - connect_ended_at: unix_time_now, - hole_punch_attempts: Vec::new(), - error: Some("rust-lib2p doesn't support quic transport yet.".into()), - outcome: grpc::HolePunchOutcome::Cancelled.into(), - ended_at: unix_time_now - }; - + let request = state.cancel(); client .track_hole_punch(tonic::Request::new(request)) .await?; @@ -140,13 +139,11 @@ async fn main() -> Result<(), Box> { swarm.dial( DialOpts::peer_id(remote_peer_id) - .addresses(remote_addrs.clone()) + .addresses(remote_addrs) .build(), )?; - let request = HolePunchState::new(local_peer_id, remote_peer_id, remote_addrs) - .drive_hole_punch(&mut swarm) - .await; + let request = state.drive_hole_punch(&mut swarm).await; client .track_hole_punch(tonic::Request::new(request)) @@ -242,7 +239,13 @@ struct HolePunchState { } impl HolePunchState { - fn new(client_id: PeerId, remote_id: PeerId, remote_multi_addresses: Vec) -> Self { + fn new<'a>( + client_id: PeerId, + client_listen_addrs: impl Iterator, + remote_id: PeerId, + remote_multi_addresses: Vec, + api_key: String, + ) -> Self { let request = grpc::TrackHolePunchRequest { client_id: client_id.into(), remote_id: remote_id.into(), @@ -258,6 +261,8 @@ impl HolePunchState { error: None, outcome: grpc::HolePunchOutcome::Unknown.into(), ended_at: 0, + listen_multi_addresses: client_listen_addrs.map(|a| a.to_vec()).collect(), + api_key, }; HolePunchState { request, @@ -266,6 +271,19 @@ impl HolePunchState { } } + fn cancel(mut self) -> grpc::TrackHolePunchRequest { + info!( + "Skipping hole punch through to {:?} via {:?} because the Quic transport is not supported.", + self.request.remote_id, self.request.remote_multi_addresses + ); + let unix_time_now = unix_time_now(); + self.request.connect_ended_at = unix_time_now; + self.request.ended_at = unix_time_now; + self.request.error = Some("rust-lib2p doesn't support quic transport yet.".into()); + self.request.outcome = grpc::HolePunchOutcome::Cancelled.into(); + self.request + } + async fn drive_hole_punch( mut self, swarm: &mut libp2p::swarm::Swarm, @@ -465,7 +483,7 @@ impl HolePunchAttemptState { ); grpc::HolePunchAttempt { opened_at: self.opened_at, - started_at: self.started_at, + started_at: Some(self.started_at), ended_at, start_rtt: None, elapsed_time: (ended_at - self.started_at) as f32 / 1000f32, From 1ea1d34ddc1ef554369f0f680fce206de2c70dc3 Mon Sep 17 00:00:00 2001 From: elenaf9 Date: Wed, 6 Jul 2022 00:31:27 +0200 Subject: [PATCH 2/4] .github/workflows: enable ci again --- .github/workflows/ci.yml | 62 +++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 32 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 71d847a..554b819 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,35 +1,33 @@ # Based on https://github.com/actions-rs/meta/blob/master/recipes/quickstart.md -# commented out until this works again +name: Cargo check + +on: + push: + branches: + - main + pull_request: + branches: + - main + +jobs: + check: + name: Check + runs-on: ubuntu-latest + steps: + - name: Checkout sources + uses: actions/checkout@v2 + + - name: Install stable toolchain + uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: stable + override: true + + - name: Run cargo check + uses: actions-rs/cargo@v1 + with: + command: check + args: --manifest-path=rust-client/Cargo.toml -# name: Cargo check -# -# on: -# push: -# branches: -# - main -# pull_request: -# branches: -# - main -# -# jobs: -# check: -# name: Check -# runs-on: ubuntu-latest -# steps: -# - name: Checkout sources -# uses: actions/checkout@v2 -# -# - name: Install stable toolchain -# uses: actions-rs/toolchain@v1 -# with: -# profile: minimal -# toolchain: stable -# override: true -# -# - name: Run cargo check -# uses: actions-rs/cargo@v1 -# with: -# command: check -# args: --manifest-path=rust-client/Cargo.toml -# From 50253b0445355179d088fea669962d33c6b5298a Mon Sep 17 00:00:00 2001 From: elenaf9 Date: Sat, 9 Jul 2022 19:21:29 +0200 Subject: [PATCH 3/4] rust-client: read API_KEY from env variable --- rust-client/src/main.rs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/rust-client/src/main.rs b/rust-client/src/main.rs index 5fd9494..4c9dbdc 100644 --- a/rust-client/src/main.rs +++ b/rust-client/src/main.rs @@ -21,8 +21,9 @@ use libp2p::swarm::{ use libp2p::tcp::TcpConfig; use libp2p::Transport; use libp2p::{identity, PeerId}; -use log::info; +use log::{info, warn}; use std::convert::TryInto; +use std::env; use std::net::Ipv4Addr; use std::num::NonZeroU32; use std::ops::ControlFlow; @@ -51,17 +52,23 @@ struct Opt { /// Per default relay-v2 is used. #[clap(long)] relay_v1: bool, - - /// Api-key used to authenticate our client at the server. - #[clap(long)] - api_key: String, } #[tokio::main] async fn main() -> Result<(), Box> { let _ = env_logger::try_init(); + + // Api-key used to authenticate our client at the server. + let api_key = match env::var("API_KEY") { + Ok(k) => k, + Err(env::VarError::NotPresent) => { + warn!("No value for env variable \"API_KEY\" found. If the server enforces authorization it will reject our requests."); + String::new() + } + Err(e) => return Err(e.into()), + }; + let opt = Opt::parse(); - let api_key = opt.api_key; let mut client = grpc::punchr_service_client::PunchrServiceClient::connect(opt.url.clone()).await?; From fa50278187bb2c97363484e18581f5c0d92f28b9 Mon Sep 17 00:00:00 2001 From: elenaf9 Date: Sat, 9 Jul 2022 19:22:24 +0200 Subject: [PATCH 4/4] rust-client: make rounds configurable --- rust-client/src/main.rs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/rust-client/src/main.rs b/rust-client/src/main.rs index 4c9dbdc..683f897 100644 --- a/rust-client/src/main.rs +++ b/rust-client/src/main.rs @@ -29,8 +29,6 @@ use std::num::NonZeroU32; use std::ops::ControlFlow; use std::time::{SystemTime, UNIX_EPOCH}; -const ROUNDS: u8 = 50; - pub mod grpc { tonic::include_proto!("_"); } @@ -52,6 +50,11 @@ struct Opt { /// Per default relay-v2 is used. #[clap(long)] relay_v1: bool, + + /// Number of iterations to run. + /// Will loop eternally if set to `None`. + #[clap(long)] + rounds: Option, } #[tokio::main] @@ -81,7 +84,15 @@ async fn main() -> Result<(), Box> { let mut protocols = None; - for _ in 0..ROUNDS { + let mut iterations = opt.rounds.map(|r| (0, r)); + while iterations + .as_mut() + .map(|(i, r)| { + *i += 1; + i <= r + }) + .unwrap_or(true) + { let mut swarm = init_swarm(local_key.clone(), opt.relay_v1).await?; if protocols.is_none() { let supported_protocols = swarm