use crate::auth_client::AuthClient; use crate::bufferpool::BufferPool; use crate::metrics::{ACTIVE_CONNECTIONS, PACKETS_RECEIVED}; use crate::packet::Packet; use dotenv::dotenv; use std::collections::HashMap; use std::env; use std::error::Error; use std::net::ToSocketAddrs; use std::str::FromStr; use std::sync::Arc; use tokio::io::AsyncReadExt; use tokio::net::{TcpListener, TcpStream}; use tokio::sync::{Mutex, Semaphore}; use tokio::{select, signal}; use tracing::Level; use tracing::{debug, error, info, warn}; use utils::consul_registration; use utils::service_discovery::get_service_address; use warp::Filter; use crate::connection_service::ConnectionService; mod packet_type; mod packet; mod router; mod packets; mod enums; mod dataconsts; mod types; mod handlers; mod bufferpool; mod metrics; mod auth_client; mod connection_state; mod connection_service; pub mod auth { tonic::include_proto!("auth"); // Path matches the package name in auth.proto } const BUFFER_POOL_SIZE: usize = 1000; const MAX_CONCURRENT_CONNECTIONS: usize = 100; async fn handle_connection(stream: &mut TcpStream, pool: Arc, auth_client: Arc>, connection_service: Arc, connection_id: String) -> Result<(), Box> { ACTIVE_CONNECTIONS.inc(); while let Some(mut buffer) = pool.acquire().await { // Read data into the buffer let n = stream.read(&mut buffer).await?; if n == 0 { break; // Connection closed } PACKETS_RECEIVED.inc(); // Process the packet match Packet::from_raw(&buffer[..n]) { Ok(packet) => { debug!("Parsed Packet: {:?}", packet); // Handle the parsed packet (route it, process it, etc.) router::route_packet(stream, packet, auth_client.clone(), connection_service.clone(), connection_id.clone()).await?; } Err(e) => warn!("Failed to parse packet: {}", e), } pool.release(buffer).await; } if let Some(state) = connection_service.get_connection(&connection_id) { let session_id = state.session_id.unwrap(); let mut auth_client = auth_client.lock().await; auth_client.logout(&session_id).await?; } ACTIVE_CONNECTIONS.dec(); Ok(()) } #[tokio::main] async fn main() -> Result<(), Box> { dotenv().ok(); tracing_subscriber::fmt() .with_max_level(Level::from_str(&env::var("LOG_LEVEL").unwrap_or_else(|_| "info".to_string())).unwrap_or_else(|_| Level::INFO)) .init(); // Set the gRPC server address let addr = env::var("LISTEN_ADDR").unwrap_or_else(|_| "0.0.0.0".to_string()); let port = env::var("PACKET_SERVICE_PORT").unwrap_or_else(|_| "4000".to_string()); let health_port = env::var("HEALTH_CHECK_PORT").unwrap_or_else(|_| "8082".to_string()); let consul_url = env::var("CONSUL_URL").unwrap_or_else(|_| "http://127.0.0.1:8500".to_string()); let service_name = env::var("SERVICE_NAME").unwrap_or_else(|_| "packet-service".to_string()); let service_address = env::var("PACKET_SERVICE_ADDR").unwrap_or_else(|_| "127.0.0.1".to_string()); let service_port = port.clone(); let health_check_url = format!("http://{}:{}/health", service_address, health_port); let health_check_endpoint_addr = format!("{}:{}", service_address, health_port); let auth_node = get_service_address(&consul_url, "auth-service").await?; // Register service with Consul let service_id = consul_registration::get_or_generate_service_id(); let tags = vec!["version-1.0".to_string()]; let mut meta = HashMap::new(); consul_registration::register_service( &consul_url, service_id.as_str(), service_name.as_str(), service_address.as_str(), service_port.parse().unwrap_or(50052), tags, meta, &health_check_url, ) .await?; // Start health-check endpoint consul_registration::start_health_check(addr.as_str()).await?; let auth_address = auth_node.get(0).unwrap(); let auth_url = format!("http://{}:{}", auth_address.ServiceAddress, auth_address.ServicePort); let auth_client = Arc::new(Mutex::new(AuthClient::connect(&auth_url).await?)); let full_addr = format!("{}:{}", &addr, port); tokio::spawn(async move { let semaphore = Arc::new(Semaphore::new(MAX_CONCURRENT_CONNECTIONS)); let listener = TcpListener::bind(full_addr.clone()).await.unwrap(); let buffer_pool = BufferPool::new(BUFFER_POOL_SIZE); let connection_service = Arc::new(ConnectionService::new()); info!("Packet service listening on {}", full_addr); loop { let (mut socket, addr) = listener.accept().await.unwrap(); let auth_client = auth_client.clone(); let connection_service = connection_service.clone(); info!("New connection from {}", addr); let pool = buffer_pool.clone(); let permit = semaphore.clone().acquire_owned().await.unwrap(); // Spawn a new task for each connection tokio::spawn(async move { let _permit = permit; let connection_id = connection_service.add_connection(); if let Err(e) = handle_connection(&mut socket, pool, auth_client, connection_service.clone(), connection_id.clone()).await { error!("Error handling connection: {}", e); } connection_service.remove_connection(&connection_id); }); } }); let mut sigterm_stream = signal::unix::signal(signal::unix::SignalKind::terminate())?; select! { _ = signal::ctrl_c() => { info!("Received SIGINT (Ctrl+C), shutting down..."); }, _ = sigterm_stream.recv() => { info!("Received SIGTERM, shutting down..."); }, } consul_registration::deregister_service(&consul_url, service_id.as_str()).await.expect(""); info!("service {} deregistered", service_name); Ok(()) }