From 8274dae7c3770e4f7fe14c74ad3113257eb4a940 Mon Sep 17 00:00:00 2001 From: EAimTY Date: Sat, 3 Jun 2023 21:02:55 +0900 Subject: [PATCH] remove `lib.rs` in binaries --- tuic-client/src/connection/handle_stream.rs | 16 +++++++--------- tuic-client/src/connection/handle_task.rs | 8 ++++---- tuic-client/src/connection/mod.rs | 4 ++-- tuic-client/src/lib.rs | 13 ------------- tuic-client/src/main.rs | 12 +++++++++++- tuic-client/src/socks5/handle_task.rs | 6 +++--- tuic-client/src/socks5/mod.rs | 4 ++-- tuic-client/src/socks5/udp_session.rs | 14 +++++++------- tuic-client/src/utils.rs | 7 ++----- tuic-server/src/connection/authenticated.rs | 8 ++++---- tuic-server/src/connection/handle_stream.rs | 12 ++++-------- tuic-server/src/connection/handle_task.rs | 16 ++++++++-------- tuic-server/src/connection/mod.rs | 6 +++--- tuic-server/src/connection/udp_session.rs | 11 ++++++----- tuic-server/src/error.rs | 2 -- tuic-server/src/lib.rs | 13 ------------- tuic-server/src/main.rs | 11 ++++++++++- tuic-server/src/server.rs | 6 ++++-- tuic-server/src/utils.rs | 4 ++-- 19 files changed, 79 insertions(+), 94 deletions(-) delete mode 100644 tuic-client/src/lib.rs delete mode 100644 tuic-server/src/lib.rs diff --git a/tuic-client/src/connection/handle_stream.rs b/tuic-client/src/connection/handle_stream.rs index 6d7a594..3eed8ca 100644 --- a/tuic-client/src/connection/handle_stream.rs +++ b/tuic-client/src/connection/handle_stream.rs @@ -1,5 +1,5 @@ use super::Connection; -use crate::{utils::UdpRelayMode, Error}; +use crate::{error::Error, utils::UdpRelayMode}; use bytes::Bytes; use quinn::{RecvStream, SendStream, VarInt}; use register_count::Register; @@ -7,7 +7,7 @@ use std::sync::atomic::Ordering; use tuic_quinn::Task; impl Connection { - pub(super) async fn accept_uni_stream(&self) -> Result<(RecvStream, Register), Error> { + pub async fn accept_uni_stream(&self) -> Result<(RecvStream, Register), Error> { let max = self.max_concurrent_uni_streams.load(Ordering::Relaxed); if self.remote_uni_stream_cnt.count() as u32 == max { @@ -23,9 +23,7 @@ impl Connection { Ok((recv, reg)) } - pub(super) async fn accept_bi_stream( - &self, - ) -> Result<(SendStream, RecvStream, Register), Error> { + pub async fn accept_bi_stream(&self) -> Result<(SendStream, RecvStream, Register), Error> { let max = self.max_concurrent_bi_streams.load(Ordering::Relaxed); if self.remote_bi_stream_cnt.count() as u32 == max { @@ -41,11 +39,11 @@ impl Connection { Ok((send, recv, reg)) } - pub(super) async fn accept_datagram(&self) -> Result { + pub async fn accept_datagram(&self) -> Result { Ok(self.conn.read_datagram().await?) } - pub(super) async fn handle_uni_stream(self, recv: RecvStream, _reg: Register) { + pub async fn handle_uni_stream(self, recv: RecvStream, _reg: Register) { log::debug!("[relay] incoming unidirectional stream"); let res = match self.model.accept_uni_stream(recv).await { @@ -65,7 +63,7 @@ impl Connection { } } - pub(super) async fn handle_bi_stream(self, send: SendStream, recv: RecvStream, _reg: Register) { + pub async fn handle_bi_stream(self, send: SendStream, recv: RecvStream, _reg: Register) { log::debug!("[relay] incoming bidirectional stream"); let res = match self.model.accept_bi_stream(send, recv).await { @@ -78,7 +76,7 @@ impl Connection { } } - pub(super) async fn handle_datagram(self, dg: Bytes) { + pub async fn handle_datagram(self, dg: Bytes) { log::debug!("[relay] incoming datagram"); let res = match self.model.accept_datagram(dg) { diff --git a/tuic-client/src/connection/handle_task.rs b/tuic-client/src/connection/handle_task.rs index 442a570..11519ed 100644 --- a/tuic-client/src/connection/handle_task.rs +++ b/tuic-client/src/connection/handle_task.rs @@ -1,5 +1,5 @@ use super::Connection; -use crate::{socks5::UDP_SESSIONS as SOCKS5_UDP_SESSIONS, utils::UdpRelayMode, Error}; +use crate::{error::Error, socks5::UDP_SESSIONS as SOCKS5_UDP_SESSIONS, utils::UdpRelayMode}; use bytes::Bytes; use socks5_proto::Address as Socks5Address; use std::time::Duration; @@ -8,7 +8,7 @@ use tuic::Address; use tuic_quinn::{Connect, Packet}; impl Connection { - pub(super) async fn authenticate(self) { + pub async fn authenticate(self) { log::debug!("[relay] [authenticate] sending authentication"); match self @@ -74,7 +74,7 @@ impl Connection { } } - pub(super) async fn heartbeat(self, heartbeat: Duration) { + pub async fn heartbeat(self, heartbeat: Duration) { loop { time::sleep(heartbeat).await; @@ -93,7 +93,7 @@ impl Connection { } } - pub(super) async fn handle_packet(pkt: Packet) { + pub async fn handle_packet(pkt: Packet) { let assoc_id = pkt.assoc_id(); let pkt_id = pkt.pkt_id(); diff --git a/tuic-client/src/connection/mod.rs b/tuic-client/src/connection/mod.rs index 35f0280..f13d84d 100644 --- a/tuic-client/src/connection/mod.rs +++ b/tuic-client/src/connection/mod.rs @@ -1,7 +1,7 @@ use crate::{ config::Relay, + error::Error, utils::{self, CongestionControl, ServerAddr, UdpRelayMode}, - Error, }; use crossbeam_utils::atomic::AtomicCell; use once_cell::sync::OnceCell; @@ -32,7 +32,7 @@ static ENDPOINT: OnceCell> = OnceCell::new(); static CONNECTION: AsyncOnceCell> = AsyncOnceCell::const_new(); static TIMEOUT: AtomicCell = AtomicCell::new(Duration::from_secs(0)); -pub(crate) const ERROR_CODE: VarInt = VarInt::from_u32(0); +pub const ERROR_CODE: VarInt = VarInt::from_u32(0); const DEFAULT_CONCURRENT_STREAMS: u32 = 32; #[derive(Clone)] diff --git a/tuic-client/src/lib.rs b/tuic-client/src/lib.rs deleted file mode 100644 index 736e783..0000000 --- a/tuic-client/src/lib.rs +++ /dev/null @@ -1,13 +0,0 @@ -mod config; -mod connection; -mod error; -mod socks5; -mod utils; - -pub use crate::{ - config::{Config, ConfigError}, - connection::Connection, - error::Error, - socks5::Server as Socks5Server, - utils::{CongestionControl, ServerAddr, UdpRelayMode}, -}; diff --git a/tuic-client/src/main.rs b/tuic-client/src/main.rs index b31a0c2..3de166a 100644 --- a/tuic-client/src/main.rs +++ b/tuic-client/src/main.rs @@ -1,6 +1,16 @@ +use crate::{ + config::{Config, ConfigError}, + connection::Connection, + socks5::Server as Socks5Server, +}; use env_logger::Builder as LoggerBuilder; use std::{env, process}; -use tuic_client::{Config, ConfigError, Connection, Socks5Server}; + +mod config; +mod connection; +mod error; +mod socks5; +mod utils; #[tokio::main] async fn main() { diff --git a/tuic-client/src/socks5/handle_task.rs b/tuic-client/src/socks5/handle_task.rs index be376ff..78d90ee 100644 --- a/tuic-client/src/socks5/handle_task.rs +++ b/tuic-client/src/socks5/handle_task.rs @@ -10,7 +10,7 @@ use tokio_util::compat::FuturesAsyncReadCompatExt; use tuic::Address as TuicAddress; impl Server { - pub(super) async fn handle_associate( + pub async fn handle_associate( assoc: Associate, assoc_id: u16, dual_stack: Option, @@ -127,7 +127,7 @@ impl Server { } } - pub(super) async fn handle_bind(bind: Bind) { + pub async fn handle_bind(bind: Bind) { let peer_addr = bind.peer_addr().unwrap(); log::warn!("[socks5] [{peer_addr}] [bind] command not supported"); @@ -142,7 +142,7 @@ impl Server { } } - pub(super) async fn handle_connect(conn: Connect, addr: Address) { + pub async fn handle_connect(conn: Connect, addr: Address) { let peer_addr = conn.peer_addr().unwrap(); let target_addr = match addr { Address::DomainAddress(domain, port) => TuicAddress::DomainAddress(domain, port), diff --git a/tuic-client/src/socks5/mod.rs b/tuic-client/src/socks5/mod.rs index cc17e3f..b62bdc5 100644 --- a/tuic-client/src/socks5/mod.rs +++ b/tuic-client/src/socks5/mod.rs @@ -1,4 +1,4 @@ -use crate::{config::Local, Error}; +use crate::{config::Local, error::Error}; use once_cell::sync::OnceCell; use parking_lot::Mutex; use socket2::{Domain, Protocol, SockAddr, Socket, Type}; @@ -19,7 +19,7 @@ use tokio::net::TcpListener; mod handle_task; mod udp_session; -pub(crate) use self::udp_session::UDP_SESSIONS; +pub use self::udp_session::UDP_SESSIONS; static SERVER: OnceCell = OnceCell::new(); diff --git a/tuic-client/src/socks5/udp_session.rs b/tuic-client/src/socks5/udp_session.rs index bf18fed..7033352 100644 --- a/tuic-client/src/socks5/udp_session.rs +++ b/tuic-client/src/socks5/udp_session.rs @@ -1,4 +1,4 @@ -use crate::Error; +use crate::error::Error; use bytes::Bytes; use once_cell::sync::OnceCell; use parking_lot::Mutex; @@ -13,17 +13,17 @@ use std::{ }; use tokio::net::UdpSocket; -pub(crate) static UDP_SESSIONS: OnceCell>> = OnceCell::new(); +pub static UDP_SESSIONS: OnceCell>> = OnceCell::new(); #[derive(Clone)] -pub(crate) struct UdpSession { +pub struct UdpSession { socket: Arc, assoc_id: u16, ctrl_addr: SocketAddr, } impl UdpSession { - pub(super) fn new( + pub fn new( assoc_id: u16, ctrl_addr: SocketAddr, local_ip: IpAddr, @@ -72,7 +72,7 @@ impl UdpSession { }) } - pub(crate) async fn send(&self, pkt: Bytes, src_addr: Address) -> Result<(), Error> { + pub async fn send(&self, pkt: Bytes, src_addr: Address) -> Result<(), Error> { let src_addr_display = src_addr.to_string(); log::debug!( @@ -96,7 +96,7 @@ impl UdpSession { Ok(()) } - pub(crate) async fn recv(&self) -> Result<(Bytes, Address), Error> { + pub async fn recv(&self) -> Result<(Bytes, Address), Error> { let (pkt, frag, dst_addr, src_addr) = self.socket.recv_from().await?; if let Ok(connected_addr) = self.socket.peer_addr() { @@ -126,7 +126,7 @@ impl UdpSession { Ok((pkt, dst_addr)) } - pub(super) fn local_addr(&self) -> Result { + pub fn local_addr(&self) -> Result { self.socket.local_addr() } } diff --git a/tuic-client/src/utils.rs b/tuic-client/src/utils.rs index 25b4d64..0286ea6 100644 --- a/tuic-client/src/utils.rs +++ b/tuic-client/src/utils.rs @@ -1,4 +1,4 @@ -use crate::Error; +use crate::error::Error; use rustls::{Certificate, RootCertStore}; use rustls_pemfile::Item; use std::{ @@ -10,10 +10,7 @@ use std::{ }; use tokio::net; -pub(crate) fn load_certs( - paths: Vec, - disable_native: bool, -) -> Result { +pub fn load_certs(paths: Vec, disable_native: bool) -> Result { let mut certs = RootCertStore::empty(); for path in &paths { diff --git a/tuic-server/src/connection/authenticated.rs b/tuic-server/src/connection/authenticated.rs index b38f4ad..6ea2146 100644 --- a/tuic-server/src/connection/authenticated.rs +++ b/tuic-server/src/connection/authenticated.rs @@ -10,7 +10,7 @@ use std::{ use uuid::Uuid; #[derive(Clone)] -pub(super) struct Authenticated(Arc); +pub struct Authenticated(Arc); struct AuthenticatedInner { uuid: AtomicCell>, @@ -18,14 +18,14 @@ struct AuthenticatedInner { } impl Authenticated { - pub(super) fn new() -> Self { + pub fn new() -> Self { Self(Arc::new(AuthenticatedInner { uuid: AtomicCell::new(None), broadcast: Mutex::new(Vec::new()), })) } - pub(super) fn set(&self, uuid: Uuid) { + pub fn set(&self, uuid: Uuid) { self.0.uuid.store(Some(uuid)); for waker in self.0.broadcast.lock().drain(..) { @@ -33,7 +33,7 @@ impl Authenticated { } } - pub(super) fn get(&self) -> Option { + pub fn get(&self) -> Option { self.0.uuid.load() } } diff --git a/tuic-server/src/connection/handle_stream.rs b/tuic-server/src/connection/handle_stream.rs index 8c0a0a9..f8dc134 100644 --- a/tuic-server/src/connection/handle_stream.rs +++ b/tuic-server/src/connection/handle_stream.rs @@ -1,5 +1,5 @@ use super::Connection; -use crate::{Error, UdpRelayMode}; +use crate::{error::Error, utils::UdpRelayMode}; use bytes::Bytes; use quinn::{RecvStream, SendStream, VarInt}; use register_count::Register; @@ -8,7 +8,7 @@ use tokio::time; use tuic_quinn::Task; impl Connection { - pub(crate) async fn handle_uni_stream(self, recv: RecvStream, _reg: Register) { + pub async fn handle_uni_stream(self, recv: RecvStream, _reg: Register) { log::debug!( "[{id:#010x}] [{addr}] [{user}] incoming unidirectional stream", id = self.id(), @@ -69,11 +69,7 @@ impl Connection { } } - pub(crate) async fn handle_bi_stream( - self, - (send, recv): (SendStream, RecvStream), - _reg: Register, - ) { + pub async fn handle_bi_stream(self, (send, recv): (SendStream, RecvStream), _reg: Register) { log::debug!( "[{id:#010x}] [{addr}] [{user}] incoming bidirectional stream", id = self.id(), @@ -122,7 +118,7 @@ impl Connection { } } - pub(crate) async fn handle_datagram(self, dg: Bytes) { + pub async fn handle_datagram(self, dg: Bytes) { log::debug!( "[{id:#010x}] [{addr}] [{user}] incoming datagram", id = self.id(), diff --git a/tuic-server/src/connection/handle_task.rs b/tuic-server/src/connection/handle_task.rs index 9985801..eb04a0d 100644 --- a/tuic-server/src/connection/handle_task.rs +++ b/tuic-server/src/connection/handle_task.rs @@ -1,5 +1,5 @@ -use super::{UdpSession, ERROR_CODE}; -use crate::{Connection, Error, UdpRelayMode}; +use super::{Connection, UdpSession, ERROR_CODE}; +use crate::{error::Error, utils::UdpRelayMode}; use bytes::Bytes; use std::{ collections::hash_map::Entry, @@ -15,7 +15,7 @@ use tuic::Address; use tuic_quinn::{Authenticate, Connect, Packet}; impl Connection { - pub(super) async fn handle_authenticate(&self, auth: Authenticate) { + pub async fn handle_authenticate(&self, auth: Authenticate) { log::info!( "[{id:#010x}] [{addr}] [{user}] [authenticate] {auth_uuid}", id = self.id(), @@ -25,7 +25,7 @@ impl Connection { ); } - pub(super) async fn handle_connect(&self, conn: Connect) { + pub async fn handle_connect(&self, conn: Connect) { let target_addr = conn.addr().to_string(); log::info!( @@ -79,7 +79,7 @@ impl Connection { } } - pub(super) async fn handle_packet(&self, pkt: Packet, mode: UdpRelayMode) { + pub async fn handle_packet(&self, pkt: Packet, mode: UdpRelayMode) { let assoc_id = pkt.assoc_id(); let pkt_id = pkt.pkt_id(); let frag_id = pkt.frag_id(); @@ -151,7 +151,7 @@ impl Connection { } } - pub(super) async fn handle_dissociate(&self, assoc_id: u16) { + pub async fn handle_dissociate(&self, assoc_id: u16) { log::info!( "[{id:#010x}] [{addr}] [{user}] [dissociate] [{assoc_id:#06x}]", id = self.id(), @@ -164,7 +164,7 @@ impl Connection { } } - pub(super) async fn handle_heartbeat(&self) { + pub async fn handle_heartbeat(&self) { log::info!( "[{id:#010x}] [{addr}] [{user}] [heartbeat]", id = self.id(), @@ -173,7 +173,7 @@ impl Connection { ); } - pub(super) async fn relay_packet(self, pkt: Bytes, addr: Address, assoc_id: u16) { + pub async fn relay_packet(self, pkt: Bytes, addr: Address, assoc_id: u16) { let addr_display = addr.to_string(); log::info!( diff --git a/tuic-server/src/connection/mod.rs b/tuic-server/src/connection/mod.rs index fe083ce..4f1b20e 100644 --- a/tuic-server/src/connection/mod.rs +++ b/tuic-server/src/connection/mod.rs @@ -1,5 +1,5 @@ use self::{authenticated::Authenticated, udp_session::UdpSession}; -use crate::{Error, UdpRelayMode}; +use crate::{error::Error, utils::UdpRelayMode}; use crossbeam_utils::atomic::AtomicCell; use parking_lot::Mutex; use quinn::{Connecting, Connection as QuinnConnection, VarInt}; @@ -18,8 +18,8 @@ mod handle_stream; mod handle_task; mod udp_session; -pub(crate) const ERROR_CODE: VarInt = VarInt::from_u32(0); -pub(crate) const DEFAULT_CONCURRENT_STREAMS: u32 = 32; +pub const ERROR_CODE: VarInt = VarInt::from_u32(0); +pub const DEFAULT_CONCURRENT_STREAMS: u32 = 32; #[derive(Clone)] pub struct Connection { diff --git a/tuic-server/src/connection/udp_session.rs b/tuic-server/src/connection/udp_session.rs index a7c143b..f2bf9c9 100644 --- a/tuic-server/src/connection/udp_session.rs +++ b/tuic-server/src/connection/udp_session.rs @@ -1,4 +1,5 @@ -use crate::{Connection, Error}; +use super::Connection; +use crate::error::Error; use bytes::Bytes; use parking_lot::Mutex; use socket2::{Domain, Protocol, SockAddr, Socket, Type}; @@ -14,7 +15,7 @@ use tokio::{ use tuic::Address; #[derive(Clone)] -pub(super) struct UdpSession(Arc); +pub struct UdpSession(Arc); struct UdpSessionInner { assoc_id: u16, @@ -26,7 +27,7 @@ struct UdpSessionInner { } impl UdpSession { - pub(super) fn new( + pub fn new( conn: Connection, assoc_id: u16, udp_relay_ipv6: bool, @@ -125,7 +126,7 @@ impl UdpSession { Ok(session) } - pub(super) async fn send(&self, pkt: Bytes, addr: SocketAddr) -> Result<(), Error> { + pub async fn send(&self, pkt: Bytes, addr: SocketAddr) -> Result<(), Error> { let socket = match addr { SocketAddr::V4(_) => &self.0.socket_v4, SocketAddr::V6(_) => self @@ -160,7 +161,7 @@ impl UdpSession { } } - pub(super) fn close(&self) { + pub fn close(&self) { let _ = self.0.close.lock().take().unwrap().send(()); } } diff --git a/tuic-server/src/error.rs b/tuic-server/src/error.rs index dec4e35..21129d5 100644 --- a/tuic-server/src/error.rs +++ b/tuic-server/src/error.rs @@ -21,8 +21,6 @@ pub enum Error { Model(#[from] ModelError), #[error("duplicated authentication")] DuplicatedAuth, - #[error("token length too short")] - ExportKeyingMaterial, #[error("authentication failed: {0}")] AuthFailed(Uuid), #[error("received packet from unexpected source")] diff --git a/tuic-server/src/lib.rs b/tuic-server/src/lib.rs deleted file mode 100644 index b7b2a3a..0000000 --- a/tuic-server/src/lib.rs +++ /dev/null @@ -1,13 +0,0 @@ -mod config; -mod connection; -mod error; -mod server; -mod utils; - -pub use crate::{ - config::{Config, ConfigError}, - connection::Connection, - error::Error, - server::Server, - utils::{CongestionControl, UdpRelayMode}, -}; diff --git a/tuic-server/src/main.rs b/tuic-server/src/main.rs index 346855c..a05cdb7 100644 --- a/tuic-server/src/main.rs +++ b/tuic-server/src/main.rs @@ -1,6 +1,15 @@ +use crate::{ + config::{Config, ConfigError}, + server::Server, +}; use env_logger::Builder as LoggerBuilder; use std::{env, process}; -use tuic_server::{Config, ConfigError, Server}; + +mod config; +mod connection; +mod error; +mod server; +mod utils; #[tokio::main] async fn main() { diff --git a/tuic-server/src/server.rs b/tuic-server/src/server.rs index c626ed1..7bdd38d 100644 --- a/tuic-server/src/server.rs +++ b/tuic-server/src/server.rs @@ -1,6 +1,8 @@ use crate::{ - config::Config, connection::DEFAULT_CONCURRENT_STREAMS, utils, CongestionControl, Connection, - Error, + config::Config, + connection::{Connection, DEFAULT_CONCURRENT_STREAMS}, + error::Error, + utils::{self, CongestionControl}, }; use quinn::{ congestion::{BbrConfig, CubicConfig, NewRenoConfig}, diff --git a/tuic-server/src/utils.rs b/tuic-server/src/utils.rs index f32924f..2574217 100644 --- a/tuic-server/src/utils.rs +++ b/tuic-server/src/utils.rs @@ -8,7 +8,7 @@ use std::{ str::FromStr, }; -pub(crate) fn load_certs(path: PathBuf) -> Result, IoError> { +pub fn load_certs(path: PathBuf) -> Result, IoError> { let mut file = BufReader::new(File::open(&path)?); let mut certs = Vec::new(); @@ -25,7 +25,7 @@ pub(crate) fn load_certs(path: PathBuf) -> Result, IoError> { Ok(certs) } -pub(crate) fn load_priv_key(path: PathBuf) -> Result { +pub fn load_priv_key(path: PathBuf) -> Result { let mut file = BufReader::new(File::open(&path)?); let mut priv_key = None;