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

62 lines
1.6 KiB
Rust
Raw Normal View History

2024-02-10 12:29:33 +00:00
use async_trait::async_trait;
use log::warn;
2024-02-10 12:29:33 +00:00
use tokio::sync::mpsc::channel;
use tokio::sync::mpsc::Sender;
use crate::proxynat::udp::ProxyUdpHandler;
use crate::nat::{NatHandler, NatHandlerFactory, NatKey, NatKeyProtocol};
2024-02-10 15:18:12 +00:00
use self::icmp::ProxyIcmpHandler;
mod icmp;
2024-02-10 14:02:54 +00:00
mod udp;
2024-02-10 12:29:33 +00:00
pub struct ProxyNatHandlerFactory {}
impl ProxyNatHandlerFactory {
pub fn new() -> Self {
Self {}
}
}
#[async_trait]
impl NatHandlerFactory for ProxyNatHandlerFactory {
2024-02-10 14:02:54 +00:00
async fn nat(
&self,
key: NatKey,
tx_sender: Sender<Vec<u8>>,
reclaim_sender: Sender<NatKey>,
) -> Option<Box<dyn NatHandler>> {
2024-02-10 12:29:33 +00:00
match key.protocol {
NatKeyProtocol::Udp => {
let (rx_sender, rx_receiver) = channel::<Vec<u8>>(4);
let mut handler = ProxyUdpHandler::new(key, rx_sender);
2024-02-10 14:02:54 +00:00
if let Err(error) = handler.spawn(rx_receiver, tx_sender, reclaim_sender).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 => {
let (rx_sender, rx_receiver) = channel::<Vec<u8>>(4);
let mut handler = ProxyIcmpHandler::new(key, rx_sender);
if let Err(error) = handler.spawn(rx_receiver, tx_sender, reclaim_sender).await {
warn!("unable to spawn icmp proxy handler: {}", error);
None
} else {
Some(Box::new(handler))
}
}
2024-02-10 12:29:33 +00:00
_ => None,
}
}
}