1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
//! A Proxy Connector crate for Hyper based applications
//!
//! # Example
//! ```rust,no_run
//! use hyper::{Request, Uri, body::Body};
//! use hyper_util::client::legacy::Client;
//! use hyper_util::client::legacy::connect::HttpConnector;
//! use hyper_util::rt::TokioExecutor;
//! use bytes::Bytes;
//! use futures_util::{TryFutureExt, TryStreamExt};
//! use http_body_util::{BodyExt, Empty};
//! use hyper_proxy2::{Proxy, ProxyConnector, Intercept};
//! use headers::Authorization;
//! use std::error::Error;
//! use tokio::io::{stdout, AsyncWriteExt as _};
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn Error>> {
//! let proxy = {
//! let proxy_uri = "http://my-proxy:8080".parse().unwrap();
//! let mut proxy = Proxy::new(Intercept::All, proxy_uri);
//! proxy.set_authorization(Authorization::basic("John Doe", "Agent1234"));
//! let connector = HttpConnector::new();
//! # #[cfg(not(any(feature = "tls", feature = "rustls-base", feature = "openssl-tls")))]
//! # let proxy_connector = ProxyConnector::from_proxy_unsecured(connector, proxy);
//! # #[cfg(any(feature = "tls", feature = "rustls-base", feature = "openssl"))]
//! let proxy_connector = ProxyConnector::from_proxy(connector, proxy).unwrap();
//! proxy_connector
//! };
//!
//! // Connecting to http will trigger regular GETs and POSTs.
//! // We need to manually append the relevant headers to the request
//! let uri: Uri = "http://my-remote-website.com".parse().unwrap();
//! let mut req = Request::get(uri.clone()).body(Empty::<Bytes>::new()).unwrap();
//!
//! if let Some(headers) = proxy.http_headers(&uri) {
//! req.headers_mut().extend(headers.clone().into_iter());
//! }
//!
//! let client = Client::builder(TokioExecutor::new()).build(proxy);
//! let mut resp = client.request(req).await?;
//! println!("Response: {}", resp.status());
//! while let Some(chunk) = resp.body_mut().collect().await.ok().map(|c| c.to_bytes()) {
//! stdout().write_all(&chunk).await?;
//! }
//!
//! // Connecting to an https uri is straightforward (uses 'CONNECT' method underneath)
//! let uri = "https://my-remote-websitei-secured.com".parse().unwrap();
//! let mut resp = client.get(uri).await?;
//! println!("Response: {}", resp.status());
//! while let Some(chunk) = resp.body_mut().collect().await.ok().map(|c| c.to_bytes()) {
//! stdout().write_all(&chunk).await?;
//! }
//!
//! Ok(())
//! }
//! ```
#![allow(missing_docs)]
mod rt;
mod stream;
mod tunnel;
use std::{fmt, io, sync::Arc};
use std::{
future::Future,
pin::Pin,
task::{Context, Poll},
};
use futures_util::future::TryFutureExt;
use headers::{authorization::Credentials, Authorization, HeaderMapExt, ProxyAuthorization};
use http::header::{HeaderMap, HeaderName, HeaderValue};
use hyper::rt::{Read, Write};
use hyper::Uri;
use tower_service::Service;
pub use stream::ProxyStream;
#[cfg(feature = "tls")]
use native_tls::TlsConnector as NativeTlsConnector;
#[cfg(feature = "tls")]
use tokio_native_tls::TlsConnector;
#[cfg(feature = "rustls-base")]
use hyper_rustls::ConfigBuilderExt;
#[cfg(feature = "rustls-base")]
use tokio_rustls::TlsConnector;
#[cfg(feature = "rustls-base")]
use tokio_rustls::rustls::pki_types::ServerName;
#[cfg(feature = "openssl-tls")]
use openssl::ssl::{SslConnector as OpenSslConnector, SslMethod};
#[cfg(feature = "openssl-tls")]
use tokio_openssl::SslStream;
type BoxError = Box<dyn std::error::Error + Send + Sync>;
/// The Intercept enum to filter connections
#[derive(Debug, Clone)]
pub enum Intercept {
/// All incoming connection will go through proxy
All,
/// Only http connections will go through proxy
Http,
/// Only https connections will go through proxy
Https,
/// No connection will go through this proxy
None,
/// A custom intercept
Custom(Custom),
}
/// A trait for matching between Destination and Uri
pub trait Dst {
/// Returns the connection scheme, e.g. "http" or "https"
fn scheme(&self) -> Option<&str>;
/// Returns the host of the connection
fn host(&self) -> Option<&str>;
/// Returns the port for the connection
fn port(&self) -> Option<u16>;
}
impl Dst for Uri {
fn scheme(&self) -> Option<&str> {
self.scheme_str()
}
fn host(&self) -> Option<&str> {
self.host()
}
fn port(&self) -> Option<u16> {
self.port_u16()
}
}
#[inline]
pub(crate) fn io_err<E: Into<Box<dyn std::error::Error + Send + Sync>>>(e: E) -> io::Error {
io::Error::new(io::ErrorKind::Other, e)
}
/// A Custom struct to proxy custom uris
#[derive(Clone)]
pub struct Custom(Arc<dyn Fn(Option<&str>, Option<&str>, Option<u16>) -> bool + Send + Sync>);
impl fmt::Debug for Custom {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(f, "_")
}
}
impl<F: Fn(Option<&str>, Option<&str>, Option<u16>) -> bool + Send + Sync + 'static> From<F>
for Custom
{
fn from(f: F) -> Custom {
Custom(Arc::new(f))
}
}
impl Intercept {
/// A function to check if given `Uri` is proxied
pub fn matches<D: Dst>(&self, uri: &D) -> bool {
match (self, uri.scheme()) {
(&Intercept::All, _)
| (&Intercept::Http, Some("http"))
| (&Intercept::Https, Some("https")) => true,
(&Intercept::Custom(Custom(ref f)), _) => f(uri.scheme(), uri.host(), uri.port()),
_ => false,
}
}
}
impl<F: Fn(Option<&str>, Option<&str>, Option<u16>) -> bool + Send + Sync + 'static> From<F>
for Intercept
{
fn from(f: F) -> Intercept {
Intercept::Custom(f.into())
}
}
/// A Proxy struct
#[derive(Clone, Debug)]
pub struct Proxy {
intercept: Intercept,
force_connect: bool,
headers: HeaderMap,
uri: Uri,
}
impl Proxy {
/// Create a new `Proxy`
pub fn new<I: Into<Intercept>>(intercept: I, uri: Uri) -> Proxy {
Proxy {
intercept: intercept.into(),
uri: uri,
headers: HeaderMap::new(),
force_connect: false,
}
}
/// Set `Proxy` authorization
pub fn set_authorization<C: Credentials + Clone>(&mut self, credentials: Authorization<C>) {
match self.intercept {
Intercept::Http => {
self.headers.typed_insert(Authorization(credentials.0));
}
Intercept::Https => {
self.headers.typed_insert(ProxyAuthorization(credentials.0));
}
_ => {
self.headers
.typed_insert(Authorization(credentials.0.clone()));
self.headers.typed_insert(ProxyAuthorization(credentials.0));
}
}
}
/// Forces the use of the CONNECT method.
pub fn force_connect(&mut self) {
self.force_connect = true;
}
/// Set a custom header
pub fn set_header(&mut self, name: HeaderName, value: HeaderValue) {
self.headers.insert(name, value);
}
/// Get current intercept
pub fn intercept(&self) -> &Intercept {
&self.intercept
}
/// Get current `Headers` which must be sent to proxy
pub fn headers(&self) -> &HeaderMap {
&self.headers
}
/// Get proxy uri
pub fn uri(&self) -> &Uri {
&self.uri
}
}
/// A wrapper around `Proxy`s with a connector.
#[derive(Clone)]
pub struct ProxyConnector<C> {
proxies: Vec<Proxy>,
connector: C,
#[cfg(feature = "tls")]
tls: Option<NativeTlsConnector>,
#[cfg(feature = "rustls-base")]
tls: Option<TlsConnector>,
#[cfg(feature = "openssl-tls")]
tls: Option<OpenSslConnector>,
#[cfg(not(any(feature = "tls", feature = "rustls-base", feature = "openssl-tls")))]
tls: Option<()>,
}
impl<C: fmt::Debug> fmt::Debug for ProxyConnector<C> {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(
f,
"ProxyConnector {}{{ proxies: {:?}, connector: {:?} }}",
if self.tls.is_some() {
""
} else {
"(unsecured)"
},
self.proxies,
self.connector
)
}
}
impl<C> ProxyConnector<C> {
/// Create a new secured Proxies
#[cfg(feature = "tls")]
pub fn new(connector: C) -> Result<Self, io::Error> {
let tls = NativeTlsConnector::builder()
.build()
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
Ok(ProxyConnector {
proxies: Vec::new(),
connector: connector,
tls: Some(tls),
})
}
/// Create a new secured Proxies
#[cfg(feature = "rustls-base")]
pub fn new(connector: C) -> Result<Self, io::Error> {
let config = tokio_rustls::rustls::ClientConfig::builder();
#[cfg(feature = "rustls")]
let config = config.with_native_roots()?;
#[cfg(feature = "rustls-webpki")]
let config = config.with_webpki_roots();
let cfg = Arc::new(config.with_no_client_auth());
let tls = TlsConnector::from(cfg);
Ok(ProxyConnector {
proxies: Vec::new(),
connector,
tls: Some(tls),
})
}
#[allow(missing_docs)]
#[cfg(feature = "openssl-tls")]
pub fn new(connector: C) -> Result<Self, io::Error> {
let builder = OpenSslConnector::builder(SslMethod::tls())
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
let tls = builder.build();
Ok(ProxyConnector {
proxies: Vec::new(),
connector: connector,
tls: Some(tls),
})
}
/// Create a new unsecured Proxy
pub fn unsecured(connector: C) -> Self {
ProxyConnector {
proxies: Vec::new(),
connector: connector,
tls: None,
}
}
/// Create a proxy connector and attach a particular proxy
#[cfg(any(feature = "tls", feature = "rustls-base", feature = "openssl-tls"))]
pub fn from_proxy(connector: C, proxy: Proxy) -> Result<Self, io::Error> {
let mut c = ProxyConnector::new(connector)?;
c.proxies.push(proxy);
Ok(c)
}
/// Create a proxy connector and attach a particular proxy
pub fn from_proxy_unsecured(connector: C, proxy: Proxy) -> Self {
let mut c = ProxyConnector::unsecured(connector);
c.proxies.push(proxy);
c
}
/// Change proxy connector
pub fn with_connector<CC>(self, connector: CC) -> ProxyConnector<CC> {
ProxyConnector {
connector: connector,
proxies: self.proxies,
tls: self.tls,
}
}
/// Set or unset tls when tunneling
#[cfg(any(feature = "tls"))]
pub fn set_tls(&mut self, tls: Option<NativeTlsConnector>) {
self.tls = tls;
}
/// Set or unset tls when tunneling
#[cfg(any(feature = "rustls-base"))]
pub fn set_tls(&mut self, tls: Option<TlsConnector>) {
self.tls = tls;
}
/// Set or unset tls when tunneling
#[cfg(any(feature = "openssl-tls"))]
pub fn set_tls(&mut self, tls: Option<OpenSslConnector>) {
self.tls = tls;
}
/// Get the current proxies
pub fn proxies(&self) -> &[Proxy] {
&self.proxies
}
/// Add a new additional proxy
pub fn add_proxy(&mut self, proxy: Proxy) {
self.proxies.push(proxy);
}
/// Extend the list of proxies
pub fn extend_proxies<I: IntoIterator<Item = Proxy>>(&mut self, proxies: I) {
self.proxies.extend(proxies)
}
/// Get http headers for a matching uri
///
/// These headers must be appended to the hyper Request for the proxy to work properly.
/// This is needed only for http requests.
pub fn http_headers(&self, uri: &Uri) -> Option<&HeaderMap> {
if uri.scheme_str().map_or(true, |s| s != "http") {
return None;
}
self.match_proxy(uri).map(|p| &p.headers)
}
fn match_proxy<D: Dst>(&self, uri: &D) -> Option<&Proxy> {
self.proxies.iter().find(|p| p.intercept.matches(uri))
}
}
macro_rules! mtry {
($e:expr) => {
match $e {
Ok(v) => v,
Err(e) => break Err(e.into()),
}
};
}
impl<C> Service<Uri> for ProxyConnector<C>
where
C: Service<Uri>,
C::Response: Read + Write + Send + Unpin + 'static,
C::Future: Send + 'static,
C::Error: Into<BoxError>,
{
type Response = ProxyStream<C::Response>;
type Error = io::Error;
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>;
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
match self.connector.poll_ready(cx) {
Poll::Ready(Ok(())) => Poll::Ready(Ok(())),
Poll::Ready(Err(e)) => Poll::Ready(Err(io_err(e.into()))),
Poll::Pending => Poll::Pending,
}
}
fn call(&mut self, uri: Uri) -> Self::Future {
if let (Some(p), Some(host)) = (self.match_proxy(&uri), uri.host()) {
if uri.scheme() == Some(&http::uri::Scheme::HTTPS) || p.force_connect {
let host = host.to_owned();
let port =
uri.port_u16()
.unwrap_or(if uri.scheme() == Some(&http::uri::Scheme::HTTP) {
80
} else {
443
});
let tunnel = tunnel::new(&host, port, &p.headers);
let connection =
proxy_dst(&uri, &p.uri).map(|proxy_url| self.connector.call(proxy_url));
let tls = if uri.scheme() == Some(&http::uri::Scheme::HTTPS) {
self.tls.clone()
} else {
None
};
Box::pin(async move {
loop {
// this hack will gone once `try_blocks` will eventually stabilized
let proxy_stream = mtry!(mtry!(connection).await.map_err(io_err));
let tunnel_stream = mtry!(tunnel.with_stream(proxy_stream).await);
break match tls {
#[cfg(feature = "tls")]
Some(tls) => {
use hyper_util::rt::TokioIo;
let tls = TlsConnector::from(tls);
let secure_stream = mtry!(tls
.connect(&host, TokioIo::new(tunnel_stream))
.await
.map_err(io_err));
Ok(ProxyStream::Secured(TokioIo::new(secure_stream)))
}
#[cfg(feature = "rustls-base")]
Some(tls) => {
use hyper_util::rt::TokioIo;
let server_name =
mtry!(ServerName::try_from(host.to_string()).map_err(io_err));
let tls = TlsConnector::from(tls);
let secure_stream = mtry!(tls
.connect(server_name, TokioIo::new(tunnel_stream))
.await
.map_err(io_err));
Ok(ProxyStream::Secured(TokioIo::new(secure_stream)))
}
#[cfg(feature = "openssl-tls")]
Some(tls) => {
use hyper_util::rt::TokioIo;
let config = tls.configure().map_err(io_err)?;
let ssl = config.into_ssl(&host).map_err(io_err)?;
let mut stream =
mtry!(SslStream::new(ssl, TokioIo::new(tunnel_stream)));
mtry!(Pin::new(&mut stream).connect().await.map_err(io_err));
Ok(ProxyStream::Secured(TokioIo::new(stream)))
}
#[cfg(not(any(
feature = "tls",
feature = "rustls-base",
feature = "openssl-tls"
)))]
Some(_) => panic!("hyper-proxy was not built with TLS support"),
None => Ok(ProxyStream::Regular(tunnel_stream)),
};
}
})
} else {
match proxy_dst(&uri, &p.uri) {
Ok(proxy_uri) => Box::pin(
self.connector
.call(proxy_uri)
.map_ok(ProxyStream::Regular)
.map_err(|err| io_err(err.into())),
),
Err(err) => Box::pin(futures_util::future::err(io_err(err))),
}
}
} else {
Box::pin(
self.connector
.call(uri)
.map_ok(ProxyStream::NoProxy)
.map_err(|err| io_err(err.into())),
)
}
}
}
fn proxy_dst(dst: &Uri, proxy: &Uri) -> io::Result<Uri> {
Uri::builder()
.scheme(
proxy
.scheme_str()
.ok_or_else(|| io_err(format!("proxy uri missing scheme: {}", proxy)))?,
)
.authority(
proxy
.authority()
.ok_or_else(|| io_err(format!("proxy uri missing host: {}", proxy)))?
.clone(),
)
.path_and_query(dst.path_and_query().unwrap().clone())
.build()
.map_err(|err| io_err(format!("other error: {}", err)))
}