2024-02-10 12:29:33 +00:00
|
|
|
use async_trait::async_trait;
|
|
|
|
|
2024-02-12 17:01:47 +00:00
|
|
|
use bytes::BytesMut;
|
2024-02-10 14:30:41 +00:00
|
|
|
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;
|
2024-02-11 10:07:47 +00:00
|
|
|
use self::tcp::ProxyTcpHandler;
|
2024-02-10 15:18:12 +00:00
|
|
|
|
|
|
|
mod icmp;
|
2024-02-11 10:07:47 +00:00
|
|
|
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 {
|
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 => {
|
2024-02-21 20:57:46 +00:00
|
|
|
let (rx_sender, rx_receiver) = channel::<BytesMut>(RX_CHANNEL_QUEUE_LEN);
|
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 => {
|
2024-02-21 20:57:46 +00:00
|
|
|
let (rx_sender, rx_receiver) = channel::<BytesMut>(RX_CHANNEL_QUEUE_LEN);
|
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-11 10:07:47 +00:00
|
|
|
NatKeyProtocol::Tcp => {
|
2024-02-21 20:57:46 +00:00
|
|
|
let (rx_sender, rx_receiver) = channel::<BytesMut>(RX_CHANNEL_QUEUE_LEN);
|
2024-02-11 10:07:47 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|