From ef6f67b97533539fe7056f73e56944022da8badd Mon Sep 17 00:00:00 2001 From: ssrlive <30760636+ssrlive@users.noreply.github.com> Date: Sun, 3 Sep 2023 18:20:02 +0800 Subject: [PATCH] remove_connection refactor --- src/tun2proxy.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/tun2proxy.rs b/src/tun2proxy.rs index a2629c1..a721a86 100644 --- a/src/tun2proxy.rs +++ b/src/tun2proxy.rs @@ -257,9 +257,8 @@ impl<'a> TunToProxy<'a> { .register(&mut exit_receiver, EXIT_TOKEN, Interest::READABLE)?; #[cfg(target_family = "unix")] - #[rustfmt::skip] let config = match tun.capabilities().medium { - Medium::Ethernet => Config::new(smoltcp::wire::EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x01]).into()), + Medium::Ethernet => Config::new(smoltcp::wire::EthernetAddress([0x02, 0, 0, 0, 0, 0x01]).into()), Medium::Ip => Config::new(smoltcp::wire::HardwareAddress::Ip), Medium::Ieee802154 => todo!(), }; @@ -350,16 +349,14 @@ impl<'a> TunToProxy<'a> { /// Destroy connection state machine fn remove_connection(&mut self, info: &ConnectionInfo) -> Result<(), Error> { if let Some(mut state) = self.connection_map.remove(info) { - _ = state.mio_stream.shutdown(Shutdown::Both); + self.expect_smoltcp_send()?; + if let Some(handle) = state.smoltcp_handle { let socket = self.sockets.get_mut::(handle); socket.close(); self.sockets.remove(handle); } - // FIXME: Does this line should be moved up to the beginning of this function? - self.expect_smoltcp_send()?; - if let Err(e) = self.poll.registry().deregister(&mut state.mio_stream) { // FIXME: The function `deregister` will frequently fail for unknown reasons. log::trace!("{}", e); @@ -371,6 +368,10 @@ impl<'a> TunToProxy<'a> { } } + if let Err(err) = state.mio_stream.shutdown(Shutdown::Both) { + log::debug!("Shutdown {} error \"{}\"", info, err); + } + log::info!("Close {}", info); } Ok(())