From 2eeaec66d42ca5afef1a4872c4c9b26dbaa7748f Mon Sep 17 00:00:00 2001 From: EAimTY Date: Fri, 3 Feb 2023 00:48:23 +0900 Subject: [PATCH] filtering packet source --- tuic-client/src/connection.rs | 58 +++++++++++++++++++---------------- tuic-client/src/error.rs | 2 ++ 2 files changed, 34 insertions(+), 26 deletions(-) diff --git a/tuic-client/src/connection.rs b/tuic-client/src/connection.rs index 2a62e6c..38787d7 100644 --- a/tuic-client/src/connection.rs +++ b/tuic-client/src/connection.rs @@ -284,19 +284,22 @@ impl Connection { async fn handle_uni_stream(self, recv: RecvStream, _reg: Register) { let res = match self.model.accept_uni_stream(recv).await { Err(err) => Err(Error::from(err)), - Ok(Task::Packet(pkt)) => match pkt.accept().await { - Ok(Some((pkt, addr, assoc_id))) => { - let addr = match addr { - Address::None => unreachable!(), - Address::DomainAddress(domain, port) => { - Socks5Address::DomainAddress(domain, port) - } - Address::SocketAddress(addr) => Socks5Address::SocketAddress(addr), - }; - Socks5Server::recv_pkt(pkt, addr, assoc_id).await - } - Ok(None) => Ok(()), - Err(err) => Err(Error::from(err)), + Ok(Task::Packet(pkt)) => match self.udp_relay_mode { + UdpRelayMode::Quic => match pkt.accept().await { + Ok(Some((pkt, addr, assoc_id))) => { + let addr = match addr { + Address::None => unreachable!(), + Address::DomainAddress(domain, port) => { + Socks5Address::DomainAddress(domain, port) + } + Address::SocketAddress(addr) => Socks5Address::SocketAddress(addr), + }; + Socks5Server::recv_pkt(pkt, addr, assoc_id).await + } + Ok(None) => Ok(()), + Err(err) => Err(Error::from(err)), + }, + UdpRelayMode::Native => Err(Error::WrongPacketSource), }, _ => unreachable!(), }; @@ -322,19 +325,22 @@ impl Connection { async fn handle_datagram(self, dg: Bytes) { let res = match self.model.accept_datagram(dg) { Err(err) => Err(Error::from(err)), - Ok(Task::Packet(pkt)) => match pkt.accept().await { - Ok(Some((pkt, addr, assoc_id))) => { - let addr = match addr { - Address::None => unreachable!(), - Address::DomainAddress(domain, port) => { - Socks5Address::DomainAddress(domain, port) - } - Address::SocketAddress(addr) => Socks5Address::SocketAddress(addr), - }; - Socks5Server::recv_pkt(pkt, addr, assoc_id).await - } - Ok(None) => Ok(()), - Err(err) => Err(Error::from(err)), + Ok(Task::Packet(pkt)) => match self.udp_relay_mode { + UdpRelayMode::Native => match pkt.accept().await { + Ok(Some((pkt, addr, assoc_id))) => { + let addr = match addr { + Address::None => unreachable!(), + Address::DomainAddress(domain, port) => { + Socks5Address::DomainAddress(domain, port) + } + Address::SocketAddress(addr) => Socks5Address::SocketAddress(addr), + }; + Socks5Server::recv_pkt(pkt, addr, assoc_id).await + } + Ok(None) => Ok(()), + Err(err) => Err(Error::from(err)), + }, + UdpRelayMode::Quic => Err(Error::WrongPacketSource), }, _ => unreachable!(), }; diff --git a/tuic-client/src/error.rs b/tuic-client/src/error.rs index 8988b01..c541e32 100644 --- a/tuic-client/src/error.rs +++ b/tuic-client/src/error.rs @@ -20,6 +20,8 @@ pub enum Error { Timeout, #[error("cannot resolve the server name")] DnsResolve, + #[error("received packet from an unexpected source")] + WrongPacketSource, #[error("invalid socks5 authentication")] InvalidSocks5Auth, }