use std::sync::Weak;
use std::sync::atomic::AtomicUsize;
use crate::{configuration::Backend, tcp_proxy::ReverseTcpProxyTarget};
#[derive(Debug)]
pub struct ProxyLiveStats {
pub total_accepted_tcp_connections : AtomicUsize,
pub active_connections : dashmap::DashMap<ConnectionKey,ProxyActiveTCPConnection>,
pub lb_access_count_per_hostname : dashmap::DashMap<String,AtomicUsize>,
}
pub type ConnectionKey = u64;
use serde::Serialize;
use super::connection_type::ConnectionType;
#[derive(Debug,Clone,Serialize)]
pub enum OutgoingTunnelType {
TLS(ReverseTcpProxyTarget,Backend),
Raw(ReverseTcpProxyTarget,Backend)
}
#[derive(Debug,Clone,Serialize)]
#[allow(dead_code)]
pub struct ProxyActiveTCPConnection {
pub incoming_sni : Option<String>,
pub client_socket_address : Option<std::net::SocketAddr>,
pub odd_box_socket : Option<std::net::SocketAddr>,
#[serde(skip)]
pub connection_key_pointer : Weak<ConnectionKey>,
pub connection_key: ConnectionKey,
pub client_addr_string : String,
pub incoming_connection_uses_tls: bool,
pub tls_terminated: bool,
pub http_terminated: bool,
pub version : u64,
pub resolved_connection_type: Option<ConnectionType>,
pub resolved_connection_type_description: Option<String>,
pub is_grpc : Option<bool>,
pub http_version : Option<bool>,
pub is_websocket : Option<bool>,
pub outgoing_tunnel_type: Option<OutgoingTunnelType>
}
impl ProxyActiveTCPConnection {
pub fn get_connection_type(&self) -> ConnectionType {
if self.version == 1 {
return ConnectionType::PendingInit
}
match (
self.incoming_connection_uses_tls,
self.tls_terminated,
self.http_terminated,
&self.outgoing_tunnel_type,
) {
(false,_,_,Some(OutgoingTunnelType::Raw(target,backend))) => ConnectionType::HttpPassthru(target.clone(),backend.clone()),
(false,_,_,Some(OutgoingTunnelType::TLS(target,backend))) => ConnectionType::HttpWithOutgoingTLS(target.clone(),backend.clone()),
(false,false,true,None) => ConnectionType::HttpTermination,
(true, true, false, Some(OutgoingTunnelType::TLS(target,backend))) => ConnectionType::TlsTerminatedWithOutgoingTLS(target.clone(),backend.clone()),
(true, true, false, Some(OutgoingTunnelType::Raw(target,backend))) => ConnectionType::TlsTerminatedWithOutgoingClearText(target.clone(),backend.clone()),
(true,false,false,Some(OutgoingTunnelType::Raw(target,backend))) => ConnectionType::TlsPassthru(target.clone(),backend.clone()),
(true,true,true,None) => ConnectionType::TlsTermination,
_ => ConnectionType::Invalid(format!("odd-box bug - invalid contype: {incoming_connection_uses_tls} + {tls_terminated} + {http_terminated} x + {outgoing_tunnel_type:?}",
incoming_connection_uses_tls=self.incoming_connection_uses_tls,
tls_terminated = self.tls_terminated,
http_terminated = self.http_terminated,
outgoing_tunnel_type = self.outgoing_tunnel_type
).to_string())
}
}
}