1
0

filtering packet source

This commit is contained in:
EAimTY 2023-02-03 00:48:23 +09:00
parent 9806c62fe7
commit 2eeaec66d4
2 changed files with 34 additions and 26 deletions

View File

@ -284,19 +284,22 @@ impl Connection {
async fn handle_uni_stream(self, recv: RecvStream, _reg: Register) { async fn handle_uni_stream(self, recv: RecvStream, _reg: Register) {
let res = match self.model.accept_uni_stream(recv).await { let res = match self.model.accept_uni_stream(recv).await {
Err(err) => Err(Error::from(err)), Err(err) => Err(Error::from(err)),
Ok(Task::Packet(pkt)) => match pkt.accept().await { Ok(Task::Packet(pkt)) => match self.udp_relay_mode {
Ok(Some((pkt, addr, assoc_id))) => { UdpRelayMode::Quic => match pkt.accept().await {
let addr = match addr { Ok(Some((pkt, addr, assoc_id))) => {
Address::None => unreachable!(), let addr = match addr {
Address::DomainAddress(domain, port) => { Address::None => unreachable!(),
Socks5Address::DomainAddress(domain, port) Address::DomainAddress(domain, port) => {
} Socks5Address::DomainAddress(domain, port)
Address::SocketAddress(addr) => Socks5Address::SocketAddress(addr), }
}; Address::SocketAddress(addr) => Socks5Address::SocketAddress(addr),
Socks5Server::recv_pkt(pkt, addr, assoc_id).await };
} Socks5Server::recv_pkt(pkt, addr, assoc_id).await
Ok(None) => Ok(()), }
Err(err) => Err(Error::from(err)), Ok(None) => Ok(()),
Err(err) => Err(Error::from(err)),
},
UdpRelayMode::Native => Err(Error::WrongPacketSource),
}, },
_ => unreachable!(), _ => unreachable!(),
}; };
@ -322,19 +325,22 @@ impl Connection {
async fn handle_datagram(self, dg: Bytes) { async fn handle_datagram(self, dg: Bytes) {
let res = match self.model.accept_datagram(dg) { let res = match self.model.accept_datagram(dg) {
Err(err) => Err(Error::from(err)), Err(err) => Err(Error::from(err)),
Ok(Task::Packet(pkt)) => match pkt.accept().await { Ok(Task::Packet(pkt)) => match self.udp_relay_mode {
Ok(Some((pkt, addr, assoc_id))) => { UdpRelayMode::Native => match pkt.accept().await {
let addr = match addr { Ok(Some((pkt, addr, assoc_id))) => {
Address::None => unreachable!(), let addr = match addr {
Address::DomainAddress(domain, port) => { Address::None => unreachable!(),
Socks5Address::DomainAddress(domain, port) Address::DomainAddress(domain, port) => {
} Socks5Address::DomainAddress(domain, port)
Address::SocketAddress(addr) => Socks5Address::SocketAddress(addr), }
}; Address::SocketAddress(addr) => Socks5Address::SocketAddress(addr),
Socks5Server::recv_pkt(pkt, addr, assoc_id).await };
} Socks5Server::recv_pkt(pkt, addr, assoc_id).await
Ok(None) => Ok(()), }
Err(err) => Err(Error::from(err)), Ok(None) => Ok(()),
Err(err) => Err(Error::from(err)),
},
UdpRelayMode::Quic => Err(Error::WrongPacketSource),
}, },
_ => unreachable!(), _ => unreachable!(),
}; };

View File

@ -20,6 +20,8 @@ pub enum Error {
Timeout, Timeout,
#[error("cannot resolve the server name")] #[error("cannot resolve the server name")]
DnsResolve, DnsResolve,
#[error("received packet from an unexpected source")]
WrongPacketSource,
#[error("invalid socks5 authentication")] #[error("invalid socks5 authentication")]
InvalidSocks5Auth, InvalidSocks5Auth,
} }