Skip to content

Instantly share code, notes, and snippets.

@ianoc
Created May 31, 2018 16:55
Show Gist options
  • Save ianoc/0030bab39c1757b4ea31b2109cf8fcfb to your computer and use it in GitHub Desktop.
Save ianoc/0030bab39c1757b4ea31b2109cf8fcfb to your computer and use it in GitHub Desktop.
use tokio::io::AsyncRead;
use tokio::io::AsyncWrite;
use hyper::client::connect::Connected;
use hyper::client::connect::Destination;
use http::Uri;
use hyper::client::connect::Connect;
use futures::Future;
use std::io;
/// A wrapper around `Proxy`s with a connector.
#[derive(Clone)]
#[derive(Debug)]
pub struct ProxyConnector<C> {
proxy: Uri,
connector: C,
}
impl<C, T: 'static> Connect for ProxyConnector<C>
where
C: Connect<
Error = io::Error,
Transport = T,
Future = Box<
Future<
Item = (T,
Connected),
Error = io::Error,
>
+ Send,
>,
>,
T: AsyncWrite + Send + AsyncRead,
{
type Transport = T;
type Error = io::Error;
type Future = Box<Future<Item = (T, Connected), Error = io::Error> + Send>;
fn connect(&self, _dst: Destination) -> Self::Future {
let proxy = self.proxy.clone();
Box::new(self.connector.connect(Destination::new(&self.proxy)).map(
move |(s, c)| (s, c.proxy(true)),
))
}
}
impl<C> ProxyConnector<C> {
pub fn new(connector: C, proxy: Uri) -> Result<Self, io::Error> {
Ok(ProxyConnector {
proxy: proxy,
connector: connector,
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment