send_udp_packet

This commit is contained in:
ssrlive 2023-08-11 22:54:20 +08:00
parent da87fa8d5a
commit 30420059cc
2 changed files with 20 additions and 21 deletions

View file

@ -158,11 +158,11 @@ impl SocksProxyImpl {
return Err("SOCKS5 server requires an unsupported authentication method.".into()); return Err("SOCKS5 server requires an unsupported authentication method.".into());
} }
if auth_method == AuthMethod::UserPass { self.state = if auth_method == AuthMethod::UserPass {
self.state = SocksState::SendAuthData; SocksState::SendAuthData
} else { } else {
self.state = SocksState::SendRequest; SocksState::SendRequest
} };
self.state_change() self.state_change()
} }
@ -344,12 +344,8 @@ impl ConnectionManager for SocksProxyManager {
fn new_tcp_proxy(&self, info: &ConnectionInfo, udp_associate: bool) -> Result<Box<dyn TcpProxy>> { fn new_tcp_proxy(&self, info: &ConnectionInfo, udp_associate: bool) -> Result<Box<dyn TcpProxy>> {
use socks5_impl::protocol::Command::{Connect, UdpAssociate}; use socks5_impl::protocol::Command::{Connect, UdpAssociate};
let command = if udp_associate { UdpAssociate } else { Connect }; let command = if udp_associate { UdpAssociate } else { Connect };
Ok(Box::new(SocksProxyImpl::new( let credentials = self.credentials.clone();
info, Ok(Box::new(SocksProxyImpl::new(info, credentials, self.version, command)?))
self.credentials.clone(),
self.version,
command,
)?))
} }
fn close_connection(&self, _: &ConnectionInfo) {} fn close_connection(&self, _: &ConnectionInfo) {}

View file

@ -484,17 +484,7 @@ impl<'a> TunToProxy<'a> {
let payload = &frame[payload_offset..payload_offset + payload_size]; let payload = &frame[payload_offset..payload_offset + payload_size];
if let (Some(virtual_dns), true) = (&mut self.options.virtual_dns, port == 53) { if let (Some(virtual_dns), true) = (&mut self.options.virtual_dns, port == 53) {
let response = virtual_dns.receive_query(payload)?; let response = virtual_dns.receive_query(payload)?;
{ self.send_udp_packet(dst, connection_info.src, response.as_slice())?;
let rx_buffer = udp::PacketBuffer::new(vec![udp::PacketMetadata::EMPTY], vec![0; 4096]);
let tx_buffer = udp::PacketBuffer::new(vec![udp::PacketMetadata::EMPTY], vec![0; 4096]);
let mut socket = udp::Socket::new(rx_buffer, tx_buffer);
socket.bind(dst)?;
let meta = UdpMetadata::from(connection_info.src);
socket.send_slice(response.as_slice(), meta)?;
let handle = self.sockets.add(socket);
self.expect_smoltcp_send()?;
self.sockets.remove(handle);
}
} else { } else {
// Another UDP packet // Another UDP packet
if !self.connection_map.contains_key(&connection_info) { if !self.connection_map.contains_key(&connection_info) {
@ -516,6 +506,7 @@ impl<'a> TunToProxy<'a> {
if let Some(udp_associate) = state.tcp_proxy_handler.get_udp_associate() { if let Some(udp_associate) = state.tcp_proxy_handler.get_udp_associate() {
log::debug!("UDP associate address: {}", udp_associate); log::debug!("UDP associate address: {}", udp_associate);
// Send packets via UDP associate... // Send packets via UDP associate...
// self.send_udp_packet(connection_info.src, udp_associate, &s5_udp_data)?;
} else { } else {
// UDP associate tunnel not ready yet, we must cache the packet... // UDP associate tunnel not ready yet, we must cache the packet...
} }
@ -562,6 +553,18 @@ impl<'a> TunToProxy<'a> {
Ok(state) Ok(state)
} }
fn send_udp_packet(&mut self, src: SocketAddr, dst: SocketAddr, data: &[u8]) -> Result<()> {
let rx_buffer = udp::PacketBuffer::new(vec![udp::PacketMetadata::EMPTY], vec![0; 4096]);
let tx_buffer = udp::PacketBuffer::new(vec![udp::PacketMetadata::EMPTY], vec![0; 4096]);
let mut socket = udp::Socket::new(rx_buffer, tx_buffer);
socket.bind(src)?;
socket.send_slice(data, UdpMetadata::from(dst))?;
let handle = self.sockets.add(socket);
self.expect_smoltcp_send()?;
self.sockets.remove(handle);
Ok(())
}
fn write_to_server(&mut self, info: &ConnectionInfo) -> Result<(), Error> { fn write_to_server(&mut self, info: &ConnectionInfo) -> Result<(), Error> {
if let Some(state) = self.connection_map.get_mut(info) { if let Some(state) = self.connection_map.get_mut(info) {
let event = state.tcp_proxy_handler.peek_data(OutgoingDirection::ToServer); let event = state.tcp_proxy_handler.peek_data(OutgoingDirection::ToServer);