From 09ede4ab5505a72400a1c397857f98149dda0e76 Mon Sep 17 00:00:00 2001 From: Giang Minh Date: Sat, 17 Aug 2024 15:50:43 +0700 Subject: [PATCH] fix: clippy warns --- Cargo.toml | 6 +++ crates/agent/Cargo.toml | 6 --- crates/agent/src/connection/tcp.rs | 2 +- crates/agent/src/main.rs | 2 +- crates/cert_utils/src/main.rs | 4 +- crates/protocol_ed25519/src/lib.rs | 11 ++-- crates/relayer/src/agent_listener/tcp.rs | 2 +- crates/relayer/src/agent_store.rs | 9 +--- crates/relayer/src/lib.rs | 4 +- crates/relayer/src/main.rs | 2 +- crates/relayer/src/proxy_listener/cluster.rs | 4 +- crates/relayer/src/proxy_listener/tcp.rs | 2 +- .../src/proxy_listener/tcp/tls_detector.rs | 50 +++++++++---------- crates/relayer/src/tunnel.rs | 4 +- crates/relayer/src/utils.rs | 2 +- 15 files changed, 52 insertions(+), 58 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 41e879c..4c83b7c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,3 +30,9 @@ rcgen = "0.13" url = "2.5" base64 = "0.22" local-ip-address = "0.6" + +[profile.release] +strip = true # Automatically strip symbols from the binary. +opt-level = "z" # Optimize for size. +lto = true +codegen-units = 1 diff --git a/crates/agent/Cargo.toml b/crates/agent/Cargo.toml index 92546d8..f436e65 100644 --- a/crates/agent/Cargo.toml +++ b/crates/agent/Cargo.toml @@ -24,12 +24,6 @@ rustls = { workspace = true, features = ["ring", "std"] } url = { workspace = true } base64 = { workspace = true } -[profile.release] -strip = true # Automatically strip symbols from the binary. -opt-level = "z" # Optimize for size. -lto = true -codegen-units = 1 - [features] default = ["binary"] binary = ["protocol-ed25519", "tracing-subscriber"] diff --git a/crates/agent/src/connection/tcp.rs b/crates/agent/src/connection/tcp.rs index 36af56a..cbdd0a6 100644 --- a/crates/agent/src/connection/tcp.rs +++ b/crates/agent/src/connection/tcp.rs @@ -138,7 +138,7 @@ where fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { let this = self.get_mut(); match this.connection.poll_new_outbound(cx) { - Poll::Ready(stream) => return Poll::Ready(stream), + Poll::Ready(stream) => Poll::Ready(stream), Poll::Pending => Poll::Pending, } } diff --git a/crates/agent/src/main.rs b/crates/agent/src/main.rs index 199390b..bfc955a 100644 --- a/crates/agent/src/main.rs +++ b/crates/agent/src/main.rs @@ -66,7 +66,7 @@ async fn main() { let server_certs = if let Some(cert) = args.custom_quic_cert_base64 { vec![CertificateDer::from( URL_SAFE - .decode(&cert) + .decode(cert) .expect("Custom cert should in base64 format") .to_vec(), )] diff --git a/crates/cert_utils/src/main.rs b/crates/cert_utils/src/main.rs index fa70e0c..3b7f967 100644 --- a/crates/cert_utils/src/main.rs +++ b/crates/cert_utils/src/main.rs @@ -21,12 +21,12 @@ fn main() { .as_millis(); std::fs::write( format!("./certificate-{}.cert", since_the_epoch), - cert.cert.der().to_vec(), + cert.cert.der(), ) .unwrap(); std::fs::write( format!("./certificate-{}.key", since_the_epoch), - cert.key_pair.serialize_der().to_vec(), + cert.key_pair.serialize_der(), ) .unwrap(); } diff --git a/crates/protocol_ed25519/src/lib.rs b/crates/protocol_ed25519/src/lib.rs index 2d94b09..9e37cb8 100644 --- a/crates/protocol_ed25519/src/lib.rs +++ b/crates/protocol_ed25519/src/lib.rs @@ -49,9 +49,8 @@ impl AgentLocalKey { let key = self .sign_key .to_pkcs8_pem(LineEnding::CRLF) - .ok() .expect("Should ok"); - (&key).to_string() + key.to_string() } } @@ -104,11 +103,11 @@ impl ClusterValidator for ClusterValidatorImpl { fn sign_response_res(&self, m: &RegisterRequest, err: Option) -> Vec { if let Some(err) = err { - return bincode::serialize(&RegisterResponse { response: Err(err) }) + bincode::serialize(&RegisterResponse { response: Err(err) }) .expect("should serialize") - .to_vec(); + .to_vec() } else { - return bincode::serialize(&RegisterResponse { + bincode::serialize(&RegisterResponse { response: Ok(format!( "{}.{}", convert_hex(&m.pub_key.to_bytes()[0..16]), @@ -116,7 +115,7 @@ impl ClusterValidator for ClusterValidatorImpl { )), }) .expect("should serialize") - .to_vec(); + .to_vec() } } } diff --git a/crates/relayer/src/agent_listener/tcp.rs b/crates/relayer/src/agent_listener/tcp.rs index 2a43eb7..1da0136 100644 --- a/crates/relayer/src/agent_listener/tcp.rs +++ b/crates/relayer/src/agent_listener/tcp.rs @@ -169,7 +169,7 @@ where fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { let this = self.get_mut(); match this.connection.poll_new_outbound(cx) { - Poll::Ready(stream) => return Poll::Ready(stream), + Poll::Ready(stream) => Poll::Ready(stream), Poll::Pending => Poll::Pending, } } diff --git a/crates/relayer/src/agent_store.rs b/crates/relayer/src/agent_store.rs index dcf3970..83fc012 100644 --- a/crates/relayer/src/agent_store.rs +++ b/crates/relayer/src/agent_store.rs @@ -7,18 +7,13 @@ use async_std::channel::Sender; use crate::ProxyTunnel; -#[derive(Clone)] +#[derive(Clone, Default)] pub struct AgentStore { + #[allow(clippy::type_complexity)] agents: Arc>>>>, } impl AgentStore { - pub fn new() -> Self { - Self { - agents: Arc::new(RwLock::new(HashMap::new())), - } - } - pub fn add(&self, id: u64, tx: Sender>) { self.agents .write() diff --git a/crates/relayer/src/lib.rs b/crates/relayer/src/lib.rs index 6fa9b9b..6aadec7 100644 --- a/crates/relayer/src/lib.rs +++ b/crates/relayer/src/lib.rs @@ -86,8 +86,8 @@ pub async fn run_agent_connection( let (mut agent_worker, proxy_tunnel_tx) = agent_worker::AgentWorker::::new(agent_connection, agent_rpc_handler); let home_id = home_id_from_domain(&domain); - agents.add(home_id.clone(), proxy_tunnel_tx); - node_alias_sdk.register_alias(home_id.clone()).await; + agents.add(home_id, proxy_tunnel_tx); + node_alias_sdk.register_alias(home_id).await; let agents = agents.clone(); async_std::task::spawn(async move { gauge!(METRICS_AGENT_LIVE).increment(1.0); diff --git a/crates/relayer/src/main.rs b/crates/relayer/src/main.rs index 8b021f1..e4f13ae 100644 --- a/crates/relayer/src/main.rs +++ b/crates/relayer/src/main.rs @@ -146,7 +146,7 @@ async fn main() { ) .await .expect("Should listen rtsps port"); - let agents = AgentStore::new(); + let agents = AgentStore::default(); #[cfg(feature = "expose-metrics")] let app = Route::new() diff --git a/crates/relayer/src/proxy_listener/cluster.rs b/crates/relayer/src/proxy_listener/cluster.rs index e6dd826..ecd8c18 100644 --- a/crates/relayer/src/proxy_listener/cluster.rs +++ b/crates/relayer/src/proxy_listener/cluster.rs @@ -60,7 +60,7 @@ pub async fn run_sdn( builder.set_manual_discovery(vec!["tunnel".to_string()], vec!["tunnel".to_string()]); builder.set_visualization_collector(false); - builder.add_service(Arc::new(service::RelayServiceBuilder::default())); + builder.add_service(Arc::new(service::RelayServiceBuilder)); builder.set_authorization(StaticKeyAuthorization::new(&secret_key)); for seed in seeds { @@ -88,7 +88,7 @@ pub async fn run_sdn( pkt.local_port, pkt.remote, pkt.remote_port, - pkt.data.into(), + pkt.data, pkt.meta, ); controller.send_to( diff --git a/crates/relayer/src/proxy_listener/tcp.rs b/crates/relayer/src/proxy_listener/tcp.rs index b44e1cd..29ff5e2 100644 --- a/crates/relayer/src/proxy_listener/tcp.rs +++ b/crates/relayer/src/proxy_listener/tcp.rs @@ -85,7 +85,7 @@ impl ProxyTunnel for ProxyTcpTunnel { self.domain = self.detector.get_domain(&first_pkt[..first_pkt_size])?; log::info!("[PRoxyTcpTunnel] detected domain {}", self.domain); self.handshake = (&AgentTunnelRequest { - service: self.service.clone(), + service: self.service, tls: self.tls, domain: self.domain.clone(), }) diff --git a/crates/relayer/src/proxy_listener/tcp/tls_detector.rs b/crates/relayer/src/proxy_listener/tcp/tls_detector.rs index 9b31b36..cba3d5d 100644 --- a/crates/relayer/src/proxy_listener/tcp/tls_detector.rs +++ b/crates/relayer/src/proxy_listener/tcp/tls_detector.rs @@ -1,4 +1,6 @@ -use tls_parser::{parse_tls_extensions, parse_tls_plaintext}; +use tls_parser::{ + parse_tls_extensions, parse_tls_plaintext, TlsExtension, TlsMessage, TlsMessageHandshake, +}; use crate::proxy_listener::DomainDetector; @@ -7,7 +9,7 @@ pub struct TlsDomainDetector(); impl DomainDetector for TlsDomainDetector { fn get_domain(&self, packet: &[u8]) -> Option { - let res = match parse_tls_plaintext(&packet) { + let res = match parse_tls_plaintext(packet) { Ok(res) => res, Err(e) => { log::error!("parse_tls_plaintext error {:?}", e); @@ -16,29 +18,27 @@ impl DomainDetector for TlsDomainDetector { }; let tls_message = &res.1.msg[0]; - if let tls_parser::TlsMessage::Handshake(handshake) = tls_message { - if let tls_parser::TlsMessageHandshake::ClientHello(client_hello) = handshake { - // get the extensions - let extensions: &[u8] = client_hello.ext?; - // parse the extensions - let res = match parse_tls_extensions(extensions) { - Ok(res) => res, - Err(e) => { - log::error!("parse_tls_extensions error {:?}", e); - return None; - } - }; - // iterate over the extensions and find the SNI - for extension in res.1 { - if let tls_parser::TlsExtension::SNI(sni) = extension { - // get the hostname - let hostname: &[u8] = sni[0].1; - let s: String = match String::from_utf8(hostname.to_vec()) { - Ok(v) => v, - Err(e) => panic!("Invalid UTF-8 sequence: {}", e), - }; - return Some(s); - } + if let TlsMessage::Handshake(TlsMessageHandshake::ClientHello(client_hello)) = tls_message { + // get the extensions + let extensions: &[u8] = client_hello.ext?; + // parse the extensions + let res = match parse_tls_extensions(extensions) { + Ok(res) => res, + Err(e) => { + log::error!("parse_tls_extensions error {:?}", e); + return None; + } + }; + // iterate over the extensions and find the SNI + for extension in res.1 { + if let TlsExtension::SNI(sni) = extension { + // get the hostname + let hostname: &[u8] = sni[0].1; + let s: String = match String::from_utf8(hostname.to_vec()) { + Ok(v) => v, + Err(e) => panic!("Invalid UTF-8 sequence: {}", e), + }; + return Some(s); } } } diff --git a/crates/relayer/src/tunnel.rs b/crates/relayer/src/tunnel.rs index 4f94fc3..0d8a0ac 100644 --- a/crates/relayer/src/tunnel.rs +++ b/crates/relayer/src/tunnel.rs @@ -32,10 +32,10 @@ impl<'a> TunnelContext<'a> { } } -pub async fn tunnel_task<'a>( +pub async fn tunnel_task( mut proxy_tunnel: Box, agents: AgentStore, - context: TunnelContext<'a>, + context: TunnelContext<'_>, ) { if context.is_local() { counter!(METRICS_PROXY_HTTP_COUNT).increment(1); diff --git a/crates/relayer/src/utils.rs b/crates/relayer/src/utils.rs index 9578996..6169986 100644 --- a/crates/relayer/src/utils.rs +++ b/crates/relayer/src/utils.rs @@ -8,5 +8,5 @@ pub fn home_id_from_domain(domain: &str) -> u64 { let mut parts = domain.split('.'); let mut hasher = DefaultHasher::default(); parts.next().unwrap_or(domain).hash(&mut hasher); - hasher.finish().into() + hasher.finish() }