From 389c12119a05bb460e582eddc71f7a62433082f2 Mon Sep 17 00:00:00 2001 From: Ara Sadoyan Date: Wed, 8 Apr 2026 17:00:06 +0200 Subject: [PATCH] code cleanup and improvements. --- src/utils/auth.rs | 17 +++++++++++----- src/utils/metrics.rs | 45 ------------------------------------------ src/utils/parceyaml.rs | 4 ++-- src/utils/tools.rs | 14 ++++++------- src/web/proxyhttp.rs | 3 +-- 5 files changed, 22 insertions(+), 61 deletions(-) diff --git a/src/utils/auth.rs b/src/utils/auth.rs index c2f6cad..2a636c9 100644 --- a/src/utils/auth.rs +++ b/src/utils/auth.rs @@ -16,10 +16,14 @@ struct JwtAuth<'a>(&'a str); impl AuthValidator for BasicAuth<'_> { fn validate(&self, session: &Session) -> bool { if let Some(header) = session.get_header("authorization") { - if let Some((_, val)) = header.to_str().ok().unwrap().split_once(' ') { - let decoded = STANDARD.decode(val).ok().unwrap(); - let decoded_str = String::from_utf8(decoded).ok().unwrap(); - return decoded_str == self.0; + if let Some(h) = header.to_str().ok() { + if let Some((_, val)) = h.split_once(' ') { + if let Some(decoded) = STANDARD.decode(val).ok() { + if let Some(decoded_str) = String::from_utf8(decoded).ok() { + return decoded_str == self.0; + } + } + } } } false @@ -29,7 +33,10 @@ impl AuthValidator for BasicAuth<'_> { impl AuthValidator for ApiKeyAuth<'_> { fn validate(&self, session: &Session) -> bool { if let Some(header) = session.get_header("x-api-key") { - return header.to_str().ok().unwrap() == self.0; + if let Some(header) = header.to_str().ok() { + return header == self.0; + } + // return header.to_str().ok().unwrap() == self.0; } false } diff --git a/src/utils/metrics.rs b/src/utils/metrics.rs index d745e23..19f9613 100644 --- a/src/utils/metrics.rs +++ b/src/utils/metrics.rs @@ -12,48 +12,6 @@ pub struct MetricTypes { pub latency: Duration, pub version: Version, } -/* -lazy_static::lazy_static! { - pub static ref REQUEST_COUNT: IntCounter = register_int_counter!( - "aralez_requests_total", - "Total number of requests handled by Aralez" - ).unwrap(); - pub static ref RESPONSE_CODES: IntCounterVec = register_int_counter_vec!( - "aralez_responses_total", - "Responses grouped by status code", - &["status"] - ).unwrap(); - pub static ref REQUEST_LATENCY: Histogram = register_histogram!( - "aralez_request_latency_seconds", - "Request latency in seconds", - vec![0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1.0, 2.5, 5.0] - ).unwrap(); - pub static ref RESPONSE_LATENCY: Histogram = register_histogram!( - "aralez_response_latency_seconds", - "Response latency in seconds", - vec![0.01, 0.05, 0.1, 0.25, 0.5, 1.0, 2.0, 5.0] - ).unwrap(); - pub static ref REQUESTS_BY_METHOD: IntCounterVec = register_int_counter_vec!( - "aralez_requests_by_method_total", - "Number of requests by HTTP method", - &["method"] - ).unwrap(); - pub static ref REQUESTS_BY_UPSTREAM: IntCounterVec = register_int_counter_vec!( - "aralez_requests_by_upstream", - "Number of requests by UPSTREAM server", - &["upstream"] - ).unwrap(); - pub static ref REQUESTS_BY_VERSION: IntCounterVec = register_int_counter_vec!( - "aralez_requests_by_version_total", - "Number of requests by HTTP versions", - &["version"] - ).unwrap(); - pub static ref ERROR_COUNT: IntCounter = register_int_counter!( - "aralez_errors_total", - "Total number of errors" - ).unwrap(); -} -*/ use std::sync::LazyLock; @@ -89,11 +47,8 @@ pub static REQUESTS_BY_UPSTREAM: LazyLock = pub static REQUESTS_BY_VERSION: LazyLock = LazyLock::new(|| register_int_counter_vec!("aralez_requests_by_version_total", "Number of requests by HTTP versions", &["version"]).unwrap()); -pub static ERROR_COUNT: LazyLock = LazyLock::new(|| register_int_counter!("aralez_errors_total", "Total number of errors").unwrap()); - pub fn calc_metrics(metric_types: &MetricTypes) { REQUEST_COUNT.inc(); - ERROR_COUNT.inc(); let timer = REQUEST_LATENCY.start_timer(); timer.observe_duration(); diff --git a/src/utils/parceyaml.rs b/src/utils/parceyaml.rs index 77ecbcb..aa74714 100644 --- a/src/utils/parceyaml.rs +++ b/src/utils/parceyaml.rs @@ -176,7 +176,7 @@ async fn populate_file_upstreams(config: &mut Configuration, parsed: &Config) { server_list.push(Arc::from(InnerMap { address: Arc::from(ip), port, - is_ssl: true, + is_ssl: false, is_http2: false, to_https: path_config.to_https.unwrap_or(false), rate_limit: path_config.rate_limit, @@ -265,7 +265,7 @@ fn parce_tls_grades(what: Option) -> Option { }, None => { warn!("TLS grade not set, defaulting to: medium"); - Some("b".to_string()) + Some("medium".to_string()) } } } diff --git a/src/utils/tools.rs b/src/utils/tools.rs index 3e5787b..3e566e0 100644 --- a/src/utils/tools.rs +++ b/src/utils/tools.rs @@ -227,13 +227,13 @@ pub fn listdir(dir: String) -> Vec { certificate_configs.push(y); } } - for (_, v) in f.iter() { - let y = CertificateConfig { - cert_path: v[0].clone(), - key_path: v[1].clone(), - }; - certificate_configs.push(y); - } + // for (_, v) in f.iter() { + // let y = CertificateConfig { + // cert_path: v[0].clone(), + // key_path: v[1].clone(), + // }; + // certificate_configs.push(y); + // } certificate_configs } diff --git a/src/web/proxyhttp.rs b/src/web/proxyhttp.rs index d9fe7d3..1a4ad20 100644 --- a/src/web/proxyhttp.rs +++ b/src/web/proxyhttp.rs @@ -70,7 +70,6 @@ impl ProxyHttp for LB { let hostname = return_header_host_from_upstream(session, &self.ump_upst); _ctx.hostname = hostname; let mut backend_id = None; - if _ctx.extraparams.sticky_sessions { if let Some(cookies) = session.req_header().headers.get("cookie") { if let Ok(cookie_str) = cookies.to_str() { @@ -239,7 +238,7 @@ impl ProxyHttp for LB { let mut buf = buffer.borrow_mut(); buf.clear(); write!(buf, "{}", client_ip).unwrap_or(()); - upstream_request.append_header("X-Forward-For", buf.as_str()).unwrap_or(false); + upstream_request.append_header("X-Forwarded-For", buf.as_str()).unwrap_or(false); }); }