diff --git a/src/client/legacy/connect/proxy/socks/mod.rs b/src/client/legacy/connect/proxy/socks/mod.rs index d6077b94..a6e43f49 100644 --- a/src/client/legacy/connect/proxy/socks/mod.rs +++ b/src/client/legacy/connect/proxy/socks/mod.rs @@ -4,6 +4,11 @@ pub use v5::{SocksV5, SocksV5Error}; mod v4; pub use v4::{SocksV4, SocksV4Error}; +use pin_project_lite::pin_project; +use std::future::Future; +use std::pin::Pin; +use std::task::{Context, Poll}; + use bytes::BytesMut; use hyper::rt::Read; @@ -119,3 +124,31 @@ impl From for SocksError { Self::V5(err) } } + +pin_project! { + // Not publicly exported (so missing_docs doesn't trigger). + // + // We return this `Future` instead of the `Pin>` directly + // so that users don't rely on it fitting in a `Pin>` slot + // (and thus we can change the type in the future). + #[must_use = "futures do nothing unless polled"] + #[allow(missing_debug_implementations)] + pub struct Handshaking { + #[pin] + fut: BoxHandshaking, + _marker: std::marker::PhantomData + } +} + +type BoxHandshaking = Pin>> + Send>>; + +impl Future for Handshaking +where + F: Future>, +{ + type Output = Result>; + + fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { + self.project().fut.poll(cx) + } +} diff --git a/src/client/legacy/connect/proxy/socks/v4/mod.rs b/src/client/legacy/connect/proxy/socks/v4/mod.rs index bee7e6dc..bfe79099 100644 --- a/src/client/legacy/connect/proxy/socks/v4/mod.rs +++ b/src/client/legacy/connect/proxy/socks/v4/mod.rs @@ -4,11 +4,8 @@ pub use errors::*; mod messages; use messages::*; -use std::future::Future; -use std::pin::Pin; -use std::task::{Context, Poll}; - use std::net::{IpAddr, SocketAddr, SocketAddrV4, ToSocketAddrs}; +use std::task::{Context, Poll}; use http::Uri; use hyper::rt::{Read, Write}; @@ -16,7 +13,7 @@ use tower_service::Service; use bytes::BytesMut; -use pin_project_lite::pin_project; +use super::{Handshaking, SocksError}; /// Tunnel Proxy via SOCKSv4 /// @@ -35,23 +32,6 @@ struct SocksConfig { local_dns: bool, } -pin_project! { - // Not publicly exported (so missing_docs doesn't trigger). - // - // We return this `Future` instead of the `Pin>` directly - // so that users don't rely on it fitting in a `Pin>` slot - // (and thus we can change the type in the future). - #[must_use = "futures do nothing unless polled"] - #[allow(missing_debug_implementations)] - pub struct Handshaking { - #[pin] - fut: BoxHandshaking, - _marker: std::marker::PhantomData - } -} - -type BoxHandshaking = Pin>> + Send>>; - impl SocksV4 { /// Create a new SOCKSv4 handshake service /// @@ -86,12 +66,7 @@ impl SocksConfig { } } - async fn execute( - self, - mut conn: T, - host: String, - port: u16, - ) -> Result> + async fn execute(self, mut conn: T, host: String, port: u16) -> Result> where T: Read + Write + Unpin, { @@ -109,7 +84,7 @@ impl SocksConfig { None } }) - .ok_or(super::SocksError::DnsFailure)? + .ok_or(SocksError::DnsFailure)? } else { Address::Domain(host, port) } @@ -142,11 +117,11 @@ where C::Error: Send + 'static, { type Response = C::Response; - type Error = super::SocksError; + type Error = SocksError; type Future = Handshaking; fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { - self.inner.poll_ready(cx).map_err(super::SocksError::Inner) + self.inner.poll_ready(cx).map_err(SocksError::Inner) } fn call(&mut self, dst: Uri) -> Self::Future { @@ -155,12 +130,9 @@ where let fut = async move { let port = dst.port().map(|p| p.as_u16()).unwrap_or(443); - let host = dst - .host() - .ok_or(super::SocksError::MissingHost)? - .to_string(); + let host = dst.host().ok_or(SocksError::MissingHost)?.to_string(); - let conn = connecting.await.map_err(super::SocksError::Inner)?; + let conn = connecting.await.map_err(SocksError::Inner)?; config.execute(conn, host, port).await }; @@ -170,14 +142,3 @@ where } } } - -impl Future for Handshaking -where - F: Future>, -{ - type Output = Result>; - - fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { - self.project().fut.poll(cx) - } -} diff --git a/src/client/legacy/connect/proxy/socks/v5/mod.rs b/src/client/legacy/connect/proxy/socks/v5/mod.rs index caf2446b..b64b4323 100644 --- a/src/client/legacy/connect/proxy/socks/v5/mod.rs +++ b/src/client/legacy/connect/proxy/socks/v5/mod.rs @@ -4,11 +4,8 @@ pub use errors::*; mod messages; use messages::*; -use std::future::Future; -use std::pin::Pin; -use std::task::{Context, Poll}; - use std::net::{IpAddr, SocketAddr, ToSocketAddrs}; +use std::task::{Context, Poll}; use http::Uri; use hyper::rt::{Read, Write}; @@ -16,7 +13,7 @@ use tower_service::Service; use bytes::BytesMut; -use pin_project_lite::pin_project; +use super::{Handshaking, SocksError}; /// Tunnel Proxy via SOCKSv5 /// @@ -48,23 +45,6 @@ enum State { ReadingProxyRes, } -pin_project! { - // Not publicly exported (so missing_docs doesn't trigger). - // - // We return this `Future` instead of the `Pin>` directly - // so that users don't rely on it fitting in a `Pin>` slot - // (and thus we can change the type in the future). - #[must_use = "futures do nothing unless polled"] - #[allow(missing_debug_implementations)] - pub struct Handshaking { - #[pin] - fut: BoxHandshaking, - _marker: std::marker::PhantomData - } -} - -type BoxHandshaking = Pin>> + Send>>; - impl SocksV5 { /// Create a new SOCKSv5 handshake service. /// @@ -126,12 +106,7 @@ impl SocksConfig { } } - async fn execute( - self, - mut conn: T, - host: String, - port: u16, - ) -> Result> + async fn execute(self, mut conn: T, host: String, port: u16) -> Result> where T: Read + Write + Unpin, { @@ -142,7 +117,7 @@ impl SocksConfig { let socket = (host, port) .to_socket_addrs()? .next() - .ok_or(super::SocksError::DnsFailure)?; + .ok_or(SocksError::DnsFailure)?; Address::Socket(socket) } else { @@ -272,11 +247,11 @@ where C::Error: Send + 'static, { type Response = C::Response; - type Error = super::SocksError; + type Error = SocksError; type Future = Handshaking; fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { - self.inner.poll_ready(cx).map_err(super::SocksError::Inner) + self.inner.poll_ready(cx).map_err(SocksError::Inner) } fn call(&mut self, dst: Uri) -> Self::Future { @@ -285,12 +260,9 @@ where let fut = async move { let port = dst.port().map(|p| p.as_u16()).unwrap_or(443); - let host = dst - .host() - .ok_or(super::SocksError::MissingHost)? - .to_string(); + let host = dst.host().ok_or(SocksError::MissingHost)?.to_string(); - let conn = connecting.await.map_err(super::SocksError::Inner)?; + let conn = connecting.await.map_err(SocksError::Inner)?; config.execute(conn, host, port).await }; @@ -300,14 +272,3 @@ where } } } - -impl Future for Handshaking -where - F: Future>, -{ - type Output = Result>; - - fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { - self.project().fut.poll(cx) - } -}