From 882fbcf66940c6286079db06b3ffd5418e78446a Mon Sep 17 00:00:00 2001 From: EAimTY Date: Mon, 29 May 2023 18:37:27 +0900 Subject: [PATCH] log info on relay tasks --- tuic-client/src/connection.rs | 54 ++++++++++++++++++++++++++++------- tuic-client/src/socks5.rs | 9 ++++-- 2 files changed, 49 insertions(+), 14 deletions(-) diff --git a/tuic-client/src/connection.rs b/tuic-client/src/connection.rs index 7eb0c4c..118cba5 100644 --- a/tuic-client/src/connection.rs +++ b/tuic-client/src/connection.rs @@ -36,6 +36,7 @@ static ENDPOINT: OnceCell> = OnceCell::new(); static CONNECTION: AsyncOnceCell> = AsyncOnceCell::const_new(); static TIMEOUT: AtomicCell = AtomicCell::new(Duration::from_secs(0)); +pub const CONNECTION_CLOSE_ERROR_CODE: VarInt = VarInt::from_u32(0); const DEFAULT_CONCURRENT_STREAMS: usize = 32; pub struct Endpoint { @@ -299,21 +300,54 @@ impl Connection { } pub async fn connect(&self, addr: Address) -> Result { - Ok(self.model.connect(addr).await?) + let addr_display = addr.to_string(); + log::info!("[relay] [connect] {addr_display}"); + + match self.model.connect(addr).await { + Ok(conn) => Ok(conn), + Err(err) => { + log::warn!("[relay] [connect] failed initializing relay to {addr_display}: {err}"); + Err(Error::Model(err)) + } + } } pub async fn packet(&self, pkt: Bytes, addr: Address, assoc_id: u16) -> Result<(), Error> { - match self.udp_relay_mode { - UdpRelayMode::Native => self.model.packet_native(pkt, addr, assoc_id)?, - UdpRelayMode::Quic => self.model.packet_quic(pkt, addr, assoc_id).await?, - } + let addr_display = addr.to_string(); - Ok(()) + match self.udp_relay_mode { + UdpRelayMode::Native => { + log::info!("[relay] [packet] [{assoc_id:#06x}] [to-native] {addr_display}"); + match self.model.packet_native(pkt, addr, assoc_id) { + Ok(()) => Ok(()), + Err(err) => { + log::warn!("[relay] [packet] [{assoc_id:#06x}] [to-native] failed relaying packet to {addr_display}: {err}"); + Err(Error::Model(err)) + } + } + } + UdpRelayMode::Quic => { + log::info!("[relay] [packet] [{assoc_id:#06x}] [to-quic] {addr_display}"); + match self.model.packet_quic(pkt, addr, assoc_id).await { + Ok(()) => Ok(()), + Err(err) => { + log::warn!("[relay] [packet] [{assoc_id:#06x}] [to-quic] failed relaying packet to {addr_display}: {err}"); + Err(Error::Model(err)) + } + } + } + } } pub async fn dissociate(&self, assoc_id: u16) -> Result<(), Error> { - self.model.dissociate(assoc_id).await?; - Ok(()) + log::info!("[relay] [dissociate] [{assoc_id:#06x}]"); + match self.model.dissociate(assoc_id).await { + Ok(()) => Ok(()), + Err(err) => { + log::warn!("[relay] [dissociate] [{assoc_id:#06x}] failed dissociating: {err}"); + Err(Error::Model(err)) + } + } } fn is_closed(&self) -> bool { @@ -464,9 +498,7 @@ impl Connection { match pkt.accept().await { Ok(Some((pkt, addr, _))) => { - log::info!( - "[relay] [packet] [{assoc_id:#06x}] [from-native] [{pkt_id:#06x}] {addr}", - ); + log::info!("[relay] [packet] [{assoc_id:#06x}] [from-native] [{pkt_id:#06x}] {addr}"); let addr = match addr { Address::None => unreachable!(), diff --git a/tuic-client/src/socks5.rs b/tuic-client/src/socks5.rs index d1db0c3..142dd2d 100644 --- a/tuic-client/src/socks5.rs +++ b/tuic-client/src/socks5.rs @@ -1,8 +1,11 @@ -use crate::{config::Local, connection::Connection as TuicConnection, Error}; +use crate::{ + config::Local, + connection::{Connection as TuicConnection, CONNECTION_CLOSE_ERROR_CODE}, + Error, +}; use bytes::Bytes; use once_cell::sync::OnceCell; use parking_lot::Mutex; -use quinn::VarInt; use socket2::{Domain, Protocol, SockAddr, Socket, Type}; use socks5_proto::{Address, Reply}; use socks5_server::{ @@ -311,7 +314,7 @@ impl Server { Ok(_) => {} Err(err) => { let _ = conn.shutdown().await; - let _ = relay.get_mut().reset(VarInt::from_u32(0)); + let _ = relay.get_mut().reset(CONNECTION_CLOSE_ERROR_CODE); log::warn!("[socks5] [{peer_addr}] [connect] [{target_addr}] TCP stream relaying error: {err}"); } },