2024-02-10 12:29:33 +00:00
|
|
|
use async_trait::async_trait;
|
|
|
|
|
2024-02-10 14:30:41 +00:00
|
|
|
use log::warn;
|
2024-02-10 12:29:33 +00:00
|
|
|
|
|
|
|
use tokio::sync::mpsc::channel;
|
|
|
|
|
2024-02-11 06:43:09 +00:00
|
|
|
use crate::nat::NatHandlerContext;
|
2024-02-10 12:29:33 +00:00
|
|
|
use crate::proxynat::udp::ProxyUdpHandler;
|
|
|
|
|
2024-02-11 06:43:09 +00:00
|
|
|
use crate::nat::{NatHandler, NatHandlerFactory, NatKeyProtocol};
|
2024-02-10 12:29:33 +00:00
|
|
|
|
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 {}
|
|
|
|
|
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 {
|
2024-02-11 06:43:09 +00:00
|
|
|
async fn nat(&self, context: NatHandlerContext) -> Option<Box<dyn NatHandler>> {
|
|
|
|
match context.key.protocol {
|
2024-02-10 12:29:33 +00:00
|
|
|
NatKeyProtocol::Udp => {
|
|
|
|
let (rx_sender, rx_receiver) = channel::<Vec<u8>>(4);
|
2024-02-11 06:43:09 +00:00
|
|
|
let mut handler = ProxyUdpHandler::new(rx_sender);
|
2024-02-10 12:29:33 +00:00
|
|
|
|
2024-02-11 06:43:09 +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 => {
|
|
|
|
let (rx_sender, rx_receiver) = channel::<Vec<u8>>(4);
|
2024-02-11 06:43:09 +00:00
|
|
|
let mut handler = ProxyIcmpHandler::new(rx_sender);
|
2024-02-10 15:18:12 +00:00
|
|
|
|
2024-02-11 06:43:09 +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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-10 12:29:33 +00:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|