From 65a5f7362022dee96d9d1abd50fa6f43ae529cee Mon Sep 17 00:00:00 2001 From: zonyitoo Date: Wed, 16 Mar 2022 23:33:40 +0800 Subject: [PATCH] renamed Password auth request to shorter name --- .../src/local/socks/server/mod.rs | 17 ++++----- .../src/local/socks/server/socks5/tcprelay.rs | 20 +++++----- crates/shadowsocks/src/relay/socks5.rs | 38 +++++++++++-------- 3 files changed, 40 insertions(+), 35 deletions(-) diff --git a/crates/shadowsocks-service/src/local/socks/server/mod.rs b/crates/shadowsocks-service/src/local/socks/server/mod.rs index c9ce3cb0..bc7cf47d 100644 --- a/crates/shadowsocks-service/src/local/socks/server/mod.rs +++ b/crates/shadowsocks-service/src/local/socks/server/mod.rs @@ -141,15 +141,14 @@ impl Socks { let mode = self.mode; let socks5_auth = self.socks5_auth.clone(); - tokio::spawn(Socks::handle_tcp_client( - context, - udp_bind_addr, - stream, - balancer, - peer_addr, - mode, - socks5_auth, - )); + tokio::spawn(async move { + if let Err(err) = + Socks::handle_tcp_client(context, udp_bind_addr, stream, balancer, peer_addr, mode, socks5_auth) + .await + { + error!("socks5 tcp client handler error: {}", err); + } + }); } } diff --git a/crates/shadowsocks-service/src/local/socks/server/socks5/tcprelay.rs b/crates/shadowsocks-service/src/local/socks/server/socks5/tcprelay.rs index 8663d5b2..25762e55 100644 --- a/crates/shadowsocks-service/src/local/socks/server/socks5/tcprelay.rs +++ b/crates/shadowsocks-service/src/local/socks/server/socks5/tcprelay.rs @@ -17,8 +17,8 @@ use shadowsocks::{ Error as Socks5Error, HandshakeRequest, HandshakeResponse, - PasswordAuthenticationInitialRequest, - PasswordAuthenticationResponse, + PasswdAuthRequest, + PasswdAuthResponse, Reply, TcpRequestHeader, TcpResponseHeader, @@ -112,10 +112,10 @@ impl Socks5TcpHandler { // Read initiation negociation - let init = match PasswordAuthenticationInitialRequest::read_from(stream).await { + let req = match PasswdAuthRequest::read_from(stream).await { Ok(i) => i, Err(err) => { - let rsp = PasswordAuthenticationResponse::new(err.as_reply().as_u8()); + let rsp = PasswdAuthResponse::new(err.as_reply().as_u8()); let _ = rsp.write_to(stream).await; return Err(Error::new( @@ -125,10 +125,10 @@ impl Socks5TcpHandler { } }; - let user_name = match str::from_utf8(&init.uname) { + let user_name = match str::from_utf8(&req.uname) { Ok(u) => u, Err(..) => { - let rsp = PasswordAuthenticationResponse::new(PASSWORD_AUTH_STATUS_FAILURE); + let rsp = PasswdAuthResponse::new(PASSWORD_AUTH_STATUS_FAILURE); let _ = rsp.write_to(stream).await; return Err(Error::new( @@ -138,10 +138,10 @@ impl Socks5TcpHandler { } }; - let password = match str::from_utf8(&init.passwd) { + let password = match str::from_utf8(&req.passwd) { Ok(u) => u, Err(..) => { - let rsp = PasswordAuthenticationResponse::new(PASSWORD_AUTH_STATUS_FAILURE); + let rsp = PasswdAuthResponse::new(PASSWORD_AUTH_STATUS_FAILURE); let _ = rsp.write_to(stream).await; return Err(Error::new( @@ -158,12 +158,12 @@ impl Socks5TcpHandler { password ); - let rsp = PasswordAuthenticationResponse::new(0); + let rsp = PasswdAuthResponse::new(0); rsp.write_to(stream).await?; Ok(()) } else { - let rsp = PasswordAuthenticationResponse::new(PASSWORD_AUTH_STATUS_FAILURE); + let rsp = PasswdAuthResponse::new(PASSWORD_AUTH_STATUS_FAILURE); rsp.write_to(stream).await?; error!( diff --git a/crates/shadowsocks/src/relay/socks5.rs b/crates/shadowsocks/src/relay/socks5.rs index 9c3eb2f0..a7ddf01c 100644 --- a/crates/shadowsocks/src/relay/socks5.rs +++ b/crates/shadowsocks/src/relay/socks5.rs @@ -167,6 +167,10 @@ pub enum Error { UnsupportedSocksVersion(u8), #[error("unsupported command {0:#x}")] UnsupportedCommand(u8), + #[error("unsupported username/password authentication version {0:#x}")] + UnsupportedPasswdAuthVersion(u8), + #[error("username/password authentication invalid request")] + PasswdAuthInvalidRequest, #[error("{0}")] Reply(Reply), } @@ -192,6 +196,8 @@ impl Error { Error::AddressDomainInvalidEncoding => Reply::GeneralFailure, Error::UnsupportedSocksVersion(..) => Reply::GeneralFailure, Error::UnsupportedCommand(..) => Reply::CommandNotSupported, + Error::UnsupportedPasswdAuthVersion(..) => Reply::GeneralFailure, + Error::PasswdAuthInvalidRequest => Reply::GeneralFailure, Error::Reply(r) => r, } } @@ -805,14 +811,14 @@ impl UdpAssociateHeader { /// | 1 | 1 | 1 to 255 | 1 | 1 to 255 | /// +----+------+----------+------+----------+ /// ``` -pub struct PasswordAuthenticationInitialRequest { +pub struct PasswdAuthRequest { pub uname: Vec, pub passwd: Vec, } -impl PasswordAuthenticationInitialRequest { +impl PasswdAuthRequest { /// Create a Username/Password Authentication Request - pub fn new(uname: U, passwd: P) -> PasswordAuthenticationInitialRequest + pub fn new(uname: U, passwd: P) -> PasswdAuthRequest where U: Into>, P: Into>, @@ -823,11 +829,11 @@ impl PasswordAuthenticationInitialRequest { uname.len() > 0 && uname.len() <= u8::MAX as usize && passwd.len() > 0 && passwd.len() <= u8::MAX as usize ); - PasswordAuthenticationInitialRequest { uname, passwd } + PasswdAuthRequest { uname, passwd } } /// Read from a reader - pub async fn read_from(r: &mut R) -> Result + pub async fn read_from(r: &mut R) -> Result where R: AsyncRead + Unpin, { @@ -836,7 +842,7 @@ impl PasswordAuthenticationInitialRequest { // The only valid subnegociation version if ver_buf[0] != 0x01 { - return Err(Error::Reply(Reply::GeneralFailure)); + return Err(Error::UnsupportedPasswdAuthVersion(ver_buf[0])); } let mut ulen_buf = [0u8; 1]; @@ -844,7 +850,7 @@ impl PasswordAuthenticationInitialRequest { let ulen = ulen_buf[0] as usize; if ulen == 0 { - return Err(Error::Reply(Reply::GeneralFailure)); + return Err(Error::PasswdAuthInvalidRequest); } let mut uname = vec![0u8; ulen]; @@ -857,7 +863,7 @@ impl PasswordAuthenticationInitialRequest { let plen = plen_buf[0] as usize; if plen == 0 { - return Err(Error::Reply(Reply::GeneralFailure)); + return Err(Error::PasswdAuthInvalidRequest); } let mut passwd = vec![0u8; plen]; @@ -865,7 +871,7 @@ impl PasswordAuthenticationInitialRequest { let _ = r.read_exact(&mut passwd).await?; } - Ok(PasswordAuthenticationInitialRequest { uname, passwd }) + Ok(PasswdAuthRequest { uname, passwd }) } /// Write to a writer @@ -894,17 +900,17 @@ impl PasswordAuthenticationInitialRequest { } } -pub struct PasswordAuthenticationResponse { +pub struct PasswdAuthResponse { pub status: u8, } -impl PasswordAuthenticationResponse { - pub fn new(status: u8) -> PasswordAuthenticationResponse { - PasswordAuthenticationResponse { status } +impl PasswdAuthResponse { + pub fn new(status: u8) -> PasswdAuthResponse { + PasswdAuthResponse { status } } /// Read from a reader - pub async fn read_from(r: &mut R) -> Result + pub async fn read_from(r: &mut R) -> Result where R: AsyncRead + Unpin, { @@ -912,10 +918,10 @@ impl PasswordAuthenticationResponse { let _ = r.read_exact(&mut buf); if buf[0] != 0x01 { - return Err(Error::Reply(Reply::GeneralFailure)); + return Err(Error::UnsupportedPasswdAuthVersion(buf[0])); } - Ok(PasswordAuthenticationResponse { status: buf[1] }) + Ok(PasswdAuthResponse { status: buf[1] }) } /// Write to a writer