diff --git a/src/utils/httpclient.rs b/src/utils/httpclient.rs index 9b00f06..8c45492 100644 --- a/src/utils/httpclient.rs +++ b/src/utils/httpclient.rs @@ -1,5 +1,5 @@ use crate::utils::kuberconsul::{match_path, ConsulService, KubeEndpoints}; -use crate::utils::structs::{InnerMap, ServiceMapping}; +use crate::utils::structs::{GlobalServiceMapping, InnerMap}; use axum::http::{HeaderMap, HeaderValue}; use dashmap::DashMap; use reqwest::Client; @@ -7,7 +7,7 @@ use std::sync::atomic::AtomicUsize; use std::sync::Arc; use std::time::Duration; -pub async fn for_consul(url: String, token: Option, conf: &ServiceMapping) -> Option, (Vec>, AtomicUsize)>> { +pub async fn for_consul(url: String, token: Option, conf: &GlobalServiceMapping) -> Option, (Vec>, AtomicUsize)>> { let client = Client::builder().timeout(Duration::from_secs(2)).danger_accept_invalid_certs(true).build().ok()?; let mut headers = HeaderMap::new(); if let Some(token) = token { @@ -27,7 +27,7 @@ pub async fn for_consul(url: String, token: Option, conf: &ServiceMappin // let prt = subsets.tagged_addresses.get("lan_ipv4").unwrap().port.clone(); let addr = subsets.tagged_addresses.get("lan_ipv4").unwrap().address.clone(); let prt = subsets.tagged_addresses.get("lan_ipv4").unwrap().port.clone(); - let redirect_link = conf.redirect_to.as_ref().map(|www| Arc::from(www.as_str())); + // let redirect_link = conf.redirect_to.as_ref().map(|www| Arc::from(www.as_str())); let to_add = Arc::from(InnerMap { address: Arc::from(&*addr), port: prt, @@ -35,7 +35,7 @@ pub async fn for_consul(url: String, token: Option, conf: &ServiceMappin is_http2: false, to_https: conf.to_https.unwrap_or(false), rate_limit: conf.rate_limit, - redirect_to: redirect_link, + redirect_to: None, healthcheck: None, authorization: None, }); @@ -45,7 +45,7 @@ pub async fn for_consul(url: String, token: Option, conf: &ServiceMappin Some(upstreams) } -pub async fn for_kuber(url: &str, token: &str, conf: &ServiceMapping) -> Option, (Vec>, AtomicUsize)>> { +pub async fn for_kuber(url: &str, token: &str, conf: &GlobalServiceMapping) -> Option, (Vec>, AtomicUsize)>> { let to = Duration::from_secs(10); let client = Client::builder().timeout(Duration::from_secs(10)).danger_accept_invalid_certs(true).build().ok()?; let resp = client.get(url).timeout(to).bearer_auth(token).send().await.ok()?; @@ -63,7 +63,7 @@ pub async fn for_kuber(url: &str, token: &str, conf: &ServiceMapping) -> Option< let mut inner_vec = Vec::new(); for addr in addresses { for port in &ports { - let redirect_link = conf.redirect_to.as_ref().map(|www| Arc::from(www.as_str())); + // let redirect_link = conf.redirect_to.as_ref().map(|www| Arc::from(www.as_str())); let to_add = Arc::from(InnerMap { address: Arc::from(addr.ip.clone()), port: port.port.clone(), @@ -72,7 +72,7 @@ pub async fn for_kuber(url: &str, token: &str, conf: &ServiceMapping) -> Option< to_https: conf.to_https.unwrap_or(false), rate_limit: conf.rate_limit, healthcheck: None, - redirect_to: redirect_link, + redirect_to: None, authorization: None, }); inner_vec.push(to_add); diff --git a/src/utils/kuberconsul.rs b/src/utils/kuberconsul.rs index 7aef457..03a9e66 100644 --- a/src/utils/kuberconsul.rs +++ b/src/utils/kuberconsul.rs @@ -1,6 +1,6 @@ use crate::utils::httpclient; use crate::utils::parceyaml::build_headers; -use crate::utils::structs::{Configuration, InnerMap, ServiceMapping, UpstreamsDashMap}; +use crate::utils::structs::{Configuration, GlobalServiceMapping, InnerMap, UpstreamsDashMap}; use crate::utils::tools::{clone_dashmap_into, compare_dashmaps, print_upstreams}; use async_trait::async_trait; use dashmap::DashMap; @@ -52,7 +52,7 @@ pub struct ConsulTaggedAddress { #[serde(rename = "Port")] pub port: u16, } -pub fn list_to_upstreams(lt: Option, (Vec>, AtomicUsize)>>, upstreams: &UpstreamsDashMap, i: &ServiceMapping) { +pub fn list_to_upstreams(lt: Option, (Vec>, AtomicUsize)>>, upstreams: &UpstreamsDashMap, i: &GlobalServiceMapping) { if let Some(list) = lt { match upstreams.get(&*i.hostname.clone()) { Some(upstr) => { @@ -67,7 +67,7 @@ pub fn list_to_upstreams(lt: Option, (Vec>, Atomi } } -pub fn match_path(conf: &ServiceMapping, upstreams: &DashMap, (Vec>, AtomicUsize)>, values: Vec>) { +pub fn match_path(conf: &GlobalServiceMapping, upstreams: &DashMap, (Vec>, AtomicUsize)>, values: Vec>) { match conf.path { Some(ref p) => { upstreams.insert(Arc::from(p.clone()), (values, AtomicUsize::new(0))); diff --git a/src/utils/structs.rs b/src/utils/structs.rs index 67bf329..13e2609 100644 --- a/src/utils/structs.rs +++ b/src/utils/structs.rs @@ -18,14 +18,13 @@ pub struct Extraparams { } #[derive(Debug, Default, Clone, Serialize, Deserialize)] -pub struct ServiceMapping { +pub struct GlobalServiceMapping { pub upstream: String, pub hostname: String, pub path: Option, pub to_https: Option, pub sticky_sessions: Option, pub rate_limit: Option, - pub redirect_to: Option, pub client_headers: Option>, pub server_headers: Option>, } @@ -33,14 +32,14 @@ pub struct ServiceMapping { #[derive(Clone, Default, Debug, Serialize, Deserialize)] pub struct Kubernetes { pub servers: Option>, - pub services: Option>, + pub services: Option>, pub tokenpath: Option, } #[derive(Clone, Default, Debug, Serialize, Deserialize)] pub struct Consul { pub servers: Option>, - pub services: Option>, + pub services: Option>, pub token: Option, } #[derive(Debug, Default, Serialize, Deserialize)] diff --git a/src/web/proxyhttp.rs b/src/web/proxyhttp.rs index 49e06fb..9aeb290 100644 --- a/src/web/proxyhttp.rs +++ b/src/web/proxyhttp.rs @@ -43,7 +43,6 @@ pub struct LB { pub struct Context { backend_id: Option, - // to_https: bool, sticky_sessions: bool, redirect_to: Option, start_time: Instant, @@ -59,7 +58,6 @@ impl ProxyHttp for LB { fn new_ctx(&self) -> Self::CTX { Context { backend_id: None, - // to_https: false, sticky_sessions: false, redirect_to: None, start_time: Instant::now(), @@ -163,13 +161,13 @@ impl ProxyHttp for LB { peer.options.tcp_recv_buf = Some(128 * 1024); End of experimental options */ + if let Some(redirect_to) = &innermap.redirect_to { let uri = session.req_header().uri.path(); let capacity = redirect_to.len() + uri.len(); let mut s = String::with_capacity(capacity); s.push_str(redirect_to); s.push_str(uri); - // ctx.to_https = true; ctx.redirect_to = Some(s); } @@ -185,7 +183,6 @@ impl ProxyHttp for LB { s.push_str(host); s.push_str(port.as_str()); s.push_str(uri); - // ctx.to_https = true; ctx.redirect_to = Some(s); } } @@ -271,7 +268,7 @@ impl ProxyHttp for LB { } Ok(()) } - async fn response_filter(&self, session: &mut Session, _upstream_response: &mut ResponseHeader, ctx: &mut Self::CTX) -> Result<()> { + async fn response_filter(&self, _session: &mut Session, _upstream_response: &mut ResponseHeader, ctx: &mut Self::CTX) -> Result<()> { if ctx.sticky_sessions { if let Some(bid) = &ctx.backend_id { let tt = if let Some(existing) = REVERSE_STORE.get(bid) { @@ -290,20 +287,13 @@ impl ProxyHttp for LB { } } - if let Some(_) = &ctx.redirect_to { - let mut redirect_response = ResponseHeader::build(StatusCode::MOVED_PERMANENTLY, None)?; - redirect_response.insert_header("Location", ctx.redirect_to.clone().unwrap_or(String::from("/")))?; - redirect_response.insert_header("Content-Length", "0")?; - session.write_response_header(Box::new(redirect_response), false).await?; + if let Some(redirect_to) = &ctx.redirect_to { + *_upstream_response = ResponseHeader::build(StatusCode::MOVED_PERMANENTLY, None)?; + _upstream_response.insert_header("Location", redirect_to)?; + _upstream_response.insert_header("Content-Length", "0")?; + return Ok(()); } - // if ctx.to_https { - // let mut redirect_response = ResponseHeader::build(StatusCode::MOVED_PERMANENTLY, None)?; - // redirect_response.insert_header("Location", ctx.redirect_to.clone().unwrap_or(String::from("/")))?; - // redirect_response.insert_header("Content-Length", "0")?; - // session.write_response_header(Box::new(redirect_response), false).await?; - // } - // ALLOCATIONS ! if let Some(client_headers) = &ctx.client_headers { for (k, v) in client_headers.iter() {