Files
krata/network/src/proxynat/mod.rs

78 lines
2.2 KiB
Rust
Raw Normal View History

2024-02-10 12:29:33 +00:00
use async_trait::async_trait;
2024-02-12 17:01:47 +00:00
use bytes::BytesMut;
use log::warn;
2024-02-10 12:29:33 +00:00
use tokio::sync::mpsc::channel;
use crate::proxynat::udp::ProxyUdpHandler;
2024-02-13 18:01:52 +00:00
use crate::nat::handler::{NatHandler, NatHandlerContext, NatHandlerFactory};
use crate::nat::key::NatKeyProtocol;
2024-02-10 12:29:33 +00:00
2024-02-10 15:18:12 +00:00
use self::icmp::ProxyIcmpHandler;
use self::tcp::ProxyTcpHandler;
2024-02-10 15:18:12 +00:00
mod icmp;
mod tcp;
2024-02-10 14:02:54 +00:00
mod udp;
2024-02-21 20:57:46 +00:00
const RX_CHANNEL_QUEUE_LEN: usize = 1000;
2024-02-12 17:01:47 +00:00
2024-02-10 12:29:33 +00:00
pub struct ProxyNatHandlerFactory {}
2024-02-10 21:13:47 +00:00
impl Default for ProxyNatHandlerFactory {
fn default() -> Self {
Self::new()
}
}
2024-02-10 12:29:33 +00:00
impl ProxyNatHandlerFactory {
pub fn new() -> Self {
Self {}
}
}
#[async_trait]
impl NatHandlerFactory for ProxyNatHandlerFactory {
async fn nat(&self, context: NatHandlerContext) -> Option<Box<dyn NatHandler>> {
match context.key.protocol {
2024-02-10 12:29:33 +00:00
NatKeyProtocol::Udp => {
2024-02-21 20:57:46 +00:00
let (rx_sender, rx_receiver) = channel::<BytesMut>(RX_CHANNEL_QUEUE_LEN);
let mut handler = ProxyUdpHandler::new(rx_sender);
2024-02-10 12:29:33 +00:00
if let Err(error) = handler.spawn(context, rx_receiver).await {
2024-02-10 12:29:33 +00:00
warn!("unable to spawn udp proxy handler: {}", error);
None
} else {
Some(Box::new(handler))
}
}
2024-02-10 15:18:12 +00:00
NatKeyProtocol::Icmp => {
2024-02-21 20:57:46 +00:00
let (rx_sender, rx_receiver) = channel::<BytesMut>(RX_CHANNEL_QUEUE_LEN);
let mut handler = ProxyIcmpHandler::new(rx_sender);
2024-02-10 15:18:12 +00:00
if let Err(error) = handler.spawn(context, rx_receiver).await {
2024-02-10 15:18:12 +00:00
warn!("unable to spawn icmp proxy handler: {}", error);
None
} else {
Some(Box::new(handler))
}
}
NatKeyProtocol::Tcp => {
2024-02-21 20:57:46 +00:00
let (rx_sender, rx_receiver) = channel::<BytesMut>(RX_CHANNEL_QUEUE_LEN);
let mut handler = ProxyTcpHandler::new(rx_sender);
if let Err(error) = handler.spawn(context, rx_receiver).await {
warn!("unable to spawn tcp proxy handler: {}", error);
None
} else {
Some(Box::new(handler))
}
}
2024-02-10 12:29:33 +00:00
}
}
}