diff --git a/tuic-client/src/error.rs b/tuic-client/src/error.rs index ddbaad5..013466f 100644 --- a/tuic-client/src/error.rs +++ b/tuic-client/src/error.rs @@ -11,8 +11,6 @@ pub enum Error { #[error(transparent)] Connect(#[from] ConnectError), #[error(transparent)] - Connection(#[from] ConnectionError), - #[error(transparent)] Model(#[from] ModelError), #[error("load native certificates error: {0}")] LoadNativeCerts(IoError), @@ -29,3 +27,9 @@ pub enum Error { #[error("invalid socks5 authentication")] InvalidSocks5Auth, } + +impl From for Error { + fn from(err: ConnectionError) -> Self { + Self::Io(IoError::from(err)) + } +} diff --git a/tuic-server/src/connection/handle_stream.rs b/tuic-server/src/connection/handle_stream.rs index 088147d..8c0a0a9 100644 --- a/tuic-server/src/connection/handle_stream.rs +++ b/tuic-server/src/connection/handle_stream.rs @@ -40,7 +40,7 @@ impl Connection { tokio::select! { () = self.auth.clone() => {} - err = self.inner.closed() => return Err(Error::Connection(err)), + err = self.inner.closed() => return Err(Error::from(err)), }; let same_pkt_src = matches!(task, Task::Packet(_)) @@ -101,7 +101,7 @@ impl Connection { tokio::select! { () = self.auth.clone() => {} - err = self.inner.closed() => return Err(Error::Connection(err)), + err = self.inner.closed() => return Err(Error::from(err)), }; Ok(task) @@ -135,7 +135,7 @@ impl Connection { tokio::select! { () = self.auth.clone() => {} - err = self.inner.closed() => return Err(Error::Connection(err)), + err = self.inner.closed() => return Err(Error::from(err)), }; let same_pkt_src = matches!(task, Task::Packet(_)) diff --git a/tuic-server/src/connection/mod.rs b/tuic-server/src/connection/mod.rs index 3abe1a1..fe083ce 100644 --- a/tuic-server/src/connection/mod.rs +++ b/tuic-server/src/connection/mod.rs @@ -103,7 +103,7 @@ impl Connection { match handle_incoming.await { Ok(()) => {} - Err(err) if err.is_locally_closed() || err.is_timeout_closed() => { + Err(err) if err.is_trivial() => { log::debug!( "[{id:#010x}] [{addr}] [{user}] {err}", id = conn.id(), @@ -118,13 +118,16 @@ impl Connection { } } } - Err(err) if err.is_locally_closed() || err.is_timeout_closed() => { - log::debug!("[{id:#010x}] [{addr}] [unauthenticated] {err}", id = 0); + Err(err) if err.is_trivial() => { + log::debug!( + "[{id:#010x}] [{addr}] [unauthenticated] {err}", + id = u32::MAX, + ); } Err(err) => { log::warn!( - "[{id:#010x}] [{addr}] [unauthenticated] connection establishing error: {err}", - id = usize::MAX, + "[{id:#010x}] [{addr}] [unauthenticated] {err}", + id = u32::MAX, ) } } diff --git a/tuic-server/src/error.rs b/tuic-server/src/error.rs index 663d8ef..dec4e35 100644 --- a/tuic-server/src/error.rs +++ b/tuic-server/src/error.rs @@ -13,8 +13,10 @@ pub enum Error { Rustls(#[from] RustlsError), #[error("invalid max idle time")] InvalidMaxIdleTime, - #[error(transparent)] - Connection(#[from] ConnectionError), + #[error("connection timed out")] + TimedOut, + #[error("connection locally closed")] + LocallyClosed, #[error(transparent)] Model(#[from] ModelError), #[error("duplicated authentication")] @@ -34,11 +36,17 @@ pub enum Error { } impl Error { - pub fn is_locally_closed(&self) -> bool { - matches!(self, Self::Connection(ConnectionError::LocallyClosed)) - } - - pub fn is_timeout_closed(&self) -> bool { - matches!(self, Self::Connection(ConnectionError::TimedOut)) + pub fn is_trivial(&self) -> bool { + matches!(self, Self::TimedOut | Self::LocallyClosed) + } +} + +impl From for Error { + fn from(err: ConnectionError) -> Self { + match err { + ConnectionError::TimedOut => Self::TimedOut, + ConnectionError::LocallyClosed => Self::LocallyClosed, + _ => Self::Io(IoError::from(err)), + } } }