process_incoming_tcp_packets

This commit is contained in:
ssrlive 2023-09-03 23:04:54 +08:00
parent c723adce4f
commit cc46526af0

View file

@ -690,29 +690,22 @@ impl<'a> TunToProxy<'a> {
Ok(())
}
// A raw packet was received on the tunnel interface.
fn receive_tun(&mut self, frame: &mut [u8]) -> Result<(), Error> {
let mut handler = || -> Result<(), Error> {
let result = connection_tuple(frame);
if let Err(error) = result {
log::debug!("{}, ignored", error);
return Ok(());
}
let (info, _first_packet, payload_offset, payload_size) = result?;
let origin_dst = SocketAddr::try_from(&info.dst)?;
let info = self.preprocess_origin_connection_info(info)?;
let manager = self.get_connection_manager().ok_or("get connection manager")?;
if info.protocol == IpProtocol::Tcp {
if _first_packet {
let proxy_handler = manager.new_proxy_handler(&info, false)?;
fn process_incoming_tcp_packets(
&mut self,
first_packet: bool,
manager: &Rc<dyn ConnectionManager>,
info: &ConnectionInfo,
origin_dst: SocketAddr,
frame: &[u8],
) -> Result<()> {
if first_packet {
let proxy_handler = manager.new_proxy_handler(info, false)?;
let server = manager.get_server_addr();
let state = self.create_new_tcp_connection_state(server, origin_dst, proxy_handler, false)?;
self.connection_map.insert(info.clone(), state);
log::info!("Connect done {} ({})", info, origin_dst);
} else if !self.connection_map.contains_key(&info) {
} else if !self.connection_map.contains_key(info) {
log::trace!("Drop middle session {} ({})", info, origin_dst);
return Ok(());
} else {
@ -728,11 +721,30 @@ impl<'a> TunToProxy<'a> {
self.expect_smoltcp_send()?;
// Read from the smoltcp socket and push the data to the connection handler.
self.tunsocket_read_and_forward(&info)?;
self.tunsocket_read_and_forward(info)?;
// The connection handler builds up the connection or encapsulates the data.
// Therefore, we now expect it to write data to the server.
self.write_to_server(&info)?;
self.write_to_server(info)?;
Ok(())
}
// A raw packet was received on the tunnel interface.
fn receive_tun(&mut self, frame: &mut [u8]) -> Result<(), Error> {
let mut handler = || -> Result<(), Error> {
let result = connection_tuple(frame);
if let Err(error) = result {
log::debug!("{}, ignored", error);
return Ok(());
}
let (info, first_packet, payload_offset, payload_size) = result?;
let origin_dst = SocketAddr::try_from(&info.dst)?;
let info = self.preprocess_origin_connection_info(info)?;
let manager = self.get_connection_manager().ok_or("get connection manager")?;
if info.protocol == IpProtocol::Tcp {
self.process_incoming_tcp_packets(first_packet, &manager, &info, origin_dst, frame)?;
} else if info.protocol == IpProtocol::Udp {
let port = info.dst.port();
let payload = &frame[payload_offset..payload_offset + payload_size];