Skip to content

Commit

Permalink
Merge branch 'master' into feature_gssapi_auth_support_socks5
Browse files Browse the repository at this point in the history
  • Loading branch information
Rohit Kulkarni committed Feb 22, 2025
2 parents a8fb578 + 8737caf commit 24e49dc
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 10 deletions.
87 changes: 79 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use async_trait::async_trait;
use std::{
borrow::Cow,
fmt::Debug,
fmt::{self, Debug},
io::Result as IoResult,
net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6, ToSocketAddrs},
pin::Pin,
Expand Down Expand Up @@ -50,7 +50,6 @@ trivial_impl_to_proxy_addrs!((Ipv6Addr, u16));
trivial_impl_to_proxy_addrs!(SocketAddrV4);
trivial_impl_to_proxy_addrs!(SocketAddrV6);

#[allow(clippy::unnecessary_to_owned)]
impl<'a> ToProxyAddrs for &'a [SocketAddr] {

Check failure on line 53 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (stable)

the following explicit lifetimes could be elided: 'a

Check failure on line 53 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (beta)

the following explicit lifetimes could be elided: 'a

Check failure on line 53 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (nightly)

the following explicit lifetimes could be elided: 'a
type Output = ProxyAddrsStream;

Expand All @@ -68,15 +67,15 @@ impl ToProxyAddrs for str {
}
}

impl<'a> ToProxyAddrs for (&'a str, u16) {
impl ToProxyAddrs for (&str, u16) {
type Output = ProxyAddrsStream;

fn to_proxy_addrs(&self) -> Self::Output {
ProxyAddrsStream(Some(self.to_socket_addrs()))
}
}

impl<'a, T: ToProxyAddrs + ?Sized> ToProxyAddrs for &'a T {
impl<T: ToProxyAddrs + ?Sized> ToProxyAddrs for &T {
type Output = T::Output;

fn to_proxy_addrs(&self) -> Self::Output {
Expand All @@ -102,7 +101,7 @@ impl Stream for ProxyAddrsStream {
}

/// A SOCKS connection target.
#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum TargetAddr<'a> {
/// Connect to an IP address.
Ip(SocketAddr),
Expand All @@ -114,7 +113,16 @@ pub enum TargetAddr<'a> {
Domain(Cow<'a, str>, u16),
}

impl<'a> TargetAddr<'a> {
impl fmt::Display for TargetAddr<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
TargetAddr::Ip(addr) => write!(f, "{}", addr),
TargetAddr::Domain(domain, port) => write!(f, "{}:{}", domain, port),
}
}
}

impl TargetAddr<'_> {
/// Creates owned `TargetAddr` by cloning. It is usually used to eliminate
/// the lifetime bound.
pub fn to_owned(&self) -> TargetAddr<'static> {
Expand All @@ -125,7 +133,7 @@ impl<'a> TargetAddr<'a> {
}
}

impl<'a> ToSocketAddrs for TargetAddr<'a> {
impl ToSocketAddrs for TargetAddr<'_> {
type Iter = Either<std::option::IntoIter<SocketAddr>, std::vec::IntoIter<SocketAddr>>;

fn to_socket_addrs(&self) -> IoResult<Self::Iter> {
Expand Down Expand Up @@ -285,7 +293,30 @@ impl<'a> Debug for GssapiAuthenticator<'a> {
}
}

impl<'a> Authentication<'a> {
#[cfg(feature = "gssapi")]
pub struct GssapiAuthenticator<'a> {
gssapi_authenticator: &'a dyn GssapiAuthentication,
renegotiate_sec_token: bool,
}

#[cfg(feature = "gssapi")]
impl<'a> GssapiAuthenticator<'a> {
pub fn new(gssapi_authenticator: &'a dyn GssapiAuthentication, renegotiate_sec_token: bool) -> Self {
GssapiAuthenticator {
gssapi_authenticator,
renegotiate_sec_token,
}
}
}

#[cfg(feature = "gssapi")]
impl<'a> Debug for GssapiAuthenticator<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "GssapiAuthenticator")
}
}

impl Authentication<'_> {
fn id(&self) -> u8 {
match self {
Authentication::Password { .. } => 0x02,
Expand Down Expand Up @@ -323,6 +354,46 @@ mod tests {
Ok(block_on(t.to_proxy_addrs().map(Result::unwrap).collect()))
}

#[test]
fn test_clone_ip() {
let addr = TargetAddr::Ip(SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080));
let addr_clone = addr.clone();
assert_eq!(addr, addr_clone);
assert_eq!(addr.to_string(), addr_clone.to_string());
}

#[test]
fn test_clone_domain() {
let addr = TargetAddr::Domain(Cow::Borrowed("example.com"), 80);
let addr_clone = addr.clone();
assert_eq!(addr, addr_clone);
assert_eq!(addr.to_string(), addr_clone.to_string());
}

#[test]
fn test_display_ip() {
let addr = TargetAddr::Ip(SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080));
assert_eq!(format!("{}", addr), "127.0.0.1:8080");
}

#[test]
fn test_display_domain() {
let addr = TargetAddr::Domain(Cow::Borrowed("example.com"), 80);
assert_eq!(format!("{}", addr), "example.com:80");
}

#[test]
fn test_to_string_ip() {
let addr = TargetAddr::Ip(SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080));
assert_eq!(addr.to_string(), "127.0.0.1:8080");
}

#[test]
fn test_to_string_domain() {
let addr = TargetAddr::Domain(Cow::Borrowed("example.com"), 80);
assert_eq!(addr.to_string(), "example.com:80");
}

#[test]
fn converts_socket_addr_to_proxy_addrs() -> Result<()> {
let addr = SocketAddr::from(([1, 1, 1, 1], 443));
Expand Down
4 changes: 2 additions & 2 deletions src/tcp/socks5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,11 @@ where
fn validate_auth(auth: &Authentication<'_>) -> Result<()> {
match auth {
Authentication::Password { username, password } => {
let username_len = username.as_bytes().len();
let username_len = username.len();
if !(1..=255).contains(&username_len) {
Err(Error::InvalidAuthValues("username length should between 1 to 255"))?
}
let password_len = password.as_bytes().len();
let password_len = password.len();
if !(1..=255).contains(&password_len) {
Err(Error::InvalidAuthValues("password length should between 1 to 255"))?
}
Expand Down

0 comments on commit 24e49dc

Please sign in to comment.