Skip to content

Commit

Permalink
upgrade to support hyper1
Browse files Browse the repository at this point in the history
  • Loading branch information
aviramha committed Jan 21, 2024
1 parent d7789e3 commit a2b689c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
19 changes: 11 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,27 @@ edition = "2018"
github-actions = { repository = "https://github.com/ark0f/hyper-socks2", workflow = "CI" }

[dependencies]
hyper = "0.14"
hyper = { version = "1", features = ["server", "http1", "client"] }
async-socks5 = "0.5"
futures = "0.3"
tokio = "1.0"
thiserror = "1.0"
http = "0.2"

http = "1"
tower-service = "0.3"
hyper-util = { version = "0.1", features = ["tokio"]}
# `tls` feature
hyper-tls = { version = "0.5", optional = true }
hyper-tls = { version = "0.6", optional = true }

# `rustls` feature
hyper-rustls = { version = "0.24", optional = true }
rusttls = { package = "rustls", version = "0.21", optional = true }
rustls-native-certs = { version = "0.6", optional = true }
hyper-rustls = { version = "0.26", optional = true }
rusttls = { package = "rustls", version = "0.22", optional = true }
rustls-native-certs = { version = "0.7", optional = true }

[dev-dependencies]
hyper = { version = "0.14", features = ["http1"] }
tokio = { version = "1.0", features = ["macros"] }
hyper-util = { version = "0.1", features = ["http1", "client", "client-legacy"]}
http-body-util = "0.1"
bytes = "1"

[features]
default = ["tls"]
Expand Down
29 changes: 20 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,17 @@ use futures::{
task::{Context, Poll},
};
use http::uri::Scheme;
use hyper::{service::Service, Uri};
use hyper::{
rt::{Read, Write},
Uri,
};
#[cfg(feature = "rustls")]
use hyper_rustls::HttpsConnector;
#[cfg(feature = "tls")]
use hyper_tls::HttpsConnector;
use hyper_util::rt::TokioIo;
use std::{future::Future, io, pin::Pin};
use tokio::io::{AsyncRead, AsyncWrite, BufStream};
use tower_service::Service;

pub use async_socks5::Auth;

Expand Down Expand Up @@ -137,7 +141,7 @@ impl<C> SocksConnector<C> {
impl<C> SocksConnector<C>
where
C: Service<Uri>,
C::Response: AsyncRead + AsyncWrite + Send + Unpin,
C::Response: Read + Write + Send + Unpin,
C::Error: Into<BoxedError>,
{
async fn call_async(mut self, target_addr: Uri) -> Result<C::Response, Error> {
Expand All @@ -160,7 +164,7 @@ where
.call(self.proxy_addr)
.await
.map_err(Into::<BoxedError>::into)?;
let mut buf_stream = BufStream::new(stream); // fixes issue #3
let mut buf_stream = TokioIo::new(stream); // fixes issue #3
let _ = async_socks5::connect(&mut buf_stream, target_addr, self.auth).await?;
Ok(buf_stream.into_inner())
}
Expand All @@ -169,7 +173,7 @@ where
impl<C> Service<Uri> for SocksConnector<C>
where
C: Service<Uri> + Clone + Send + 'static,
C::Response: AsyncRead + AsyncWrite + Send + Unpin,
C::Response: Read + Write + Send + Unpin,
C::Error: Into<BoxedError>,
C::Future: Send,
{
Expand All @@ -191,7 +195,12 @@ where
#[cfg(test)]
mod tests {
use super::*;
use hyper::{client::HttpConnector, Body, Client};
use bytes::Bytes;
use http_body_util::Empty;
use hyper_util::{
client::legacy::{connect::HttpConnector, Client},
rt::TokioExecutor,
};

const PROXY_ADDR: &str = "socks5://127.0.0.1:1080";
const PROXY_USERNAME: &str = "hyper";
Expand Down Expand Up @@ -245,10 +254,12 @@ mod tests {
};

let fut = if (self.uri.scheme() == Some(&Scheme::HTTP)) ^ self.swap_connector {
Client::builder().build::<_, Body>(socks).get(self.uri)
Client::builder(TokioExecutor::new())
.build::<_, Empty<Bytes>>(socks)
.get(self.uri)
} else {
Client::builder()
.build::<_, Body>(socks.with_tls().unwrap())
Client::builder(TokioExecutor::new())
.build::<_, Empty<Bytes>>(socks.with_tls().unwrap())
.get(self.uri)
};
let _ = fut.await.unwrap();
Expand Down

0 comments on commit a2b689c

Please sign in to comment.