Skip to content

Commit

Permalink
cargo
Browse files Browse the repository at this point in the history
  • Loading branch information
Rohit Kulkarni committed Feb 14, 2025
1 parent 93ef34b commit bf2bec2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ pub enum TargetAddr<'a> {
Domain(Cow<'a, str>, u16),
}

#[allow(clippy::needless_lifetimes)]
impl<'a> fmt::Display for TargetAddr<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Expand All @@ -123,6 +124,7 @@ impl<'a> fmt::Display for TargetAddr<'a> {
}
}

#[allow(clippy::needless_lifetimes)]
impl<'a> TargetAddr<'a> {
/// Creates owned `TargetAddr` by cloning. It is usually used to eliminate
/// the lifetime bound.
Expand All @@ -134,6 +136,7 @@ impl<'a> TargetAddr<'a> {
}
}

#[allow(clippy::needless_lifetimes)]
impl<'a> ToSocketAddrs for TargetAddr<'a> {
type Iter = Either<std::option::IntoIter<SocketAddr>, std::vec::IntoIter<SocketAddr>>;

Expand Down Expand Up @@ -264,6 +267,7 @@ enum Authentication<'a> {
None,
}

#[allow(clippy::needless_lifetimes)]
impl<'a> Authentication<'a> {
fn id(&self) -> u8 {
match self {
Expand Down
47 changes: 29 additions & 18 deletions src/tcp/socks5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@ use tokio::net::TcpStream;
use crate::ToProxyAddrs;
use crate::{
io::{AsyncSocket, AsyncSocketExt},
Authentication,
Error,
IntoTargetAddr,
Result,
TargetAddr,
Authentication, Error, IntoTargetAddr, Result, TargetAddr,
};

#[repr(u8)]
Expand Down Expand Up @@ -149,7 +145,8 @@ impl Socks5Stream<TcpStream> {
}

impl<S> Socks5Stream<S>
where S: AsyncSocket + Unpin
where
S: AsyncSocket + Unpin,
{
/// Connects to a target server through a SOCKS5 proxy given a socket to it.
///
Expand All @@ -158,7 +155,9 @@ where S: AsyncSocket + Unpin
/// It propagates the error that occurs in the conversion from `T` to
/// `TargetAddr`.
pub async fn connect_with_socket<'t, T>(socket: S, target: T) -> Result<Socks5Stream<S>>
where T: IntoTargetAddr<'t> {
where
T: IntoTargetAddr<'t>,
{
Self::execute_command_with_socket(socket, target, Authentication::None, Command::Connect).await
}

Expand Down Expand Up @@ -190,11 +189,11 @@ where S: AsyncSocket + Unpin
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 All @@ -208,7 +207,9 @@ where S: AsyncSocket + Unpin
/// Resolve the domain name to an ip using special Tor Resolve command, by
/// connecting to a Tor compatible proxy given a socket to it.
pub async fn tor_resolve_with_socket<'t, T>(socket: S, target: T) -> Result<TargetAddr<'static>>
where T: IntoTargetAddr<'t> {
where
T: IntoTargetAddr<'t>,
{
let sock = Self::execute_command_with_socket(socket, target, Authentication::None, Command::TorResolve).await?;

Ok(sock.target_addr().to_owned())
Expand All @@ -219,7 +220,9 @@ where S: AsyncSocket + Unpin
/// PTR command, by connecting to a Tor compatible proxy given a socket
/// to it.
pub async fn tor_resolve_ptr_with_socket<'t, T>(socket: S, target: T) -> Result<TargetAddr<'static>>
where T: IntoTargetAddr<'t> {
where
T: IntoTargetAddr<'t>,
{
let sock =
Self::execute_command_with_socket(socket, target, Authentication::None, Command::TorResolvePtr).await?;

Expand Down Expand Up @@ -274,7 +277,8 @@ pub struct SocksConnector<'a, 't, S> {
}

impl<'a, 't, S> SocksConnector<'a, 't, S>
where S: Stream<Item = Result<SocketAddr>> + Unpin
where
S: Stream<Item = Result<SocketAddr>> + Unpin,
{
fn new(auth: Authentication<'a>, command: Command, proxy: Fuse<S>, target: TargetAddr<'t>) -> Self {
SocksConnector {
Expand Down Expand Up @@ -585,7 +589,8 @@ impl Socks5Listener<TcpStream> {
}

impl<S> Socks5Listener<S>
where S: AsyncSocket + Unpin
where
S: AsyncSocket + Unpin,
{
/// Initiates a BIND request to the specified proxy using the given socket
/// to it.
Expand All @@ -598,7 +603,9 @@ where S: AsyncSocket + Unpin
/// It propagates the error that occurs in the conversion from `T` to
/// `TargetAddr`.
pub async fn bind_with_socket<'t, T>(socket: S, target: T) -> Result<Socks5Listener<S>>
where T: IntoTargetAddr<'t> {
where
T: IntoTargetAddr<'t>,
{
Self::bind_with_auth_and_socket(Authentication::None, socket, target).await
}

Expand Down Expand Up @@ -674,7 +681,8 @@ where S: AsyncSocket + Unpin

#[cfg(feature = "tokio")]
impl<T> tokio::io::AsyncRead for Socks5Stream<T>
where T: tokio::io::AsyncRead + Unpin
where
T: tokio::io::AsyncRead + Unpin,
{
fn poll_read(
mut self: Pin<&mut Self>,
Expand All @@ -687,7 +695,8 @@ where T: tokio::io::AsyncRead + Unpin

#[cfg(feature = "tokio")]
impl<T> tokio::io::AsyncWrite for Socks5Stream<T>
where T: tokio::io::AsyncWrite + Unpin
where
T: tokio::io::AsyncWrite + Unpin,
{
fn poll_write(mut self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<io::Result<usize>> {
tokio::io::AsyncWrite::poll_write(Pin::new(&mut self.socket), cx, buf)
Expand All @@ -704,7 +713,8 @@ where T: tokio::io::AsyncWrite + Unpin

#[cfg(feature = "futures-io")]
impl<T> futures_io::AsyncRead for Socks5Stream<T>
where T: futures_io::AsyncRead + Unpin
where
T: futures_io::AsyncRead + Unpin,
{
fn poll_read(mut self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut [u8]) -> Poll<io::Result<usize>> {
futures_io::AsyncRead::poll_read(Pin::new(&mut self.socket), cx, buf)
Expand All @@ -713,7 +723,8 @@ where T: futures_io::AsyncRead + Unpin

#[cfg(feature = "futures-io")]
impl<T> futures_io::AsyncWrite for Socks5Stream<T>
where T: futures_io::AsyncWrite + Unpin
where
T: futures_io::AsyncWrite + Unpin,
{
fn poll_write(mut self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<io::Result<usize>> {
futures_io::AsyncWrite::poll_write(Pin::new(&mut self.socket), cx, buf)
Expand Down

0 comments on commit bf2bec2

Please sign in to comment.