Skip to content

Commit

Permalink
fix: clippy warns
Browse files Browse the repository at this point in the history
  • Loading branch information
giangndm committed Aug 17, 2024
1 parent 3352b6e commit 09ede4a
Show file tree
Hide file tree
Showing 15 changed files with 52 additions and 58 deletions.
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 0 additions & 6 deletions crates/agent/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
2 changes: 1 addition & 1 deletion crates/agent/src/connection/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ where
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
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,
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/agent/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
)]
Expand Down
4 changes: 2 additions & 2 deletions crates/cert_utils/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
11 changes: 5 additions & 6 deletions crates/protocol_ed25519/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

Expand Down Expand Up @@ -104,19 +103,19 @@ impl ClusterValidator<RegisterRequest> for ClusterValidatorImpl {

fn sign_response_res(&self, m: &RegisterRequest, err: Option<String>) -> Vec<u8> {
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]),
self.root_domain
)),
})
.expect("should serialize")
.to_vec();
.to_vec()
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/relayer/src/agent_listener/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ where
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
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,
}
}
Expand Down
9 changes: 2 additions & 7 deletions crates/relayer/src/agent_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<RwLock<HashMap<u64, Sender<Box<dyn ProxyTunnel>>>>>,
}

impl AgentStore {
pub fn new() -> Self {
Self {
agents: Arc::new(RwLock::new(HashMap::new())),
}
}

pub fn add(&self, id: u64, tx: Sender<Box<dyn ProxyTunnel>>) {
self.agents
.write()
Expand Down
4 changes: 2 additions & 2 deletions crates/relayer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ pub async fn run_agent_connection<AG, S, R, W>(
let (mut agent_worker, proxy_tunnel_tx) =
agent_worker::AgentWorker::<AG, S, R, W>::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);
Expand Down
2 changes: 1 addition & 1 deletion crates/relayer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions crates/relayer/src/proxy_listener/cluster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion crates/relayer/src/proxy_listener/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
})
Expand Down
50 changes: 25 additions & 25 deletions crates/relayer/src/proxy_listener/tcp/tls_detector.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -7,7 +9,7 @@ pub struct TlsDomainDetector();

impl DomainDetector for TlsDomainDetector {
fn get_domain(&self, packet: &[u8]) -> Option<String> {
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);
Expand All @@ -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);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/relayer/src/tunnel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ impl<'a> TunnelContext<'a> {
}
}

pub async fn tunnel_task<'a>(
pub async fn tunnel_task(
mut proxy_tunnel: Box<dyn ProxyTunnel>,
agents: AgentStore,
context: TunnelContext<'a>,
context: TunnelContext<'_>,
) {
if context.is_local() {
counter!(METRICS_PROXY_HTTP_COUNT).increment(1);
Expand Down
2 changes: 1 addition & 1 deletion crates/relayer/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

0 comments on commit 09ede4a

Please sign in to comment.