Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hyper-proxy crate #37

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

/target/
**/*.rs.bk
Cargo.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
language: rust
cache: cargo
sudo: false

rust:
- nightly
- beta
- stable

before_script:
- export PATH=$HOME/.cargo/bin:$HOME/.local/bin:$PATH
- pip install -v 'travis-cargo<0.2' --user
- travis-cargo --only nightly install rustfmt-nightly -- --force

script:
- travis-cargo --only nighlty fmt -- --write-mode=diff
- cargo build
- cargo test
- travis-cargo --only stable doc
after_success:
- travis-cargo --only stable doc-upload

env:
global:
- TRAVIS_CARGO_NIGHTLY_FEATURE=""
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[package]
name = "hyper-proxy"
version = "0.5.0"
authors = ["Johann Tuffe <tafia973@gmail.com>"]
description = "A proxy connector for Hyper-based applications"

documentation = "https://docs.rs/hyper-proxy"
repository = "https://github.com/tafia/hyper-proxy"

readme = "README.md"
keywords = ["hyper", "proxy", "tokio", "ssl"]
categories = ["web-programming::http-client", "asynchronous", "authentication"]
license = "MIT"

[dependencies]
http = "0.1"
hyper = "0.12"
futures = "0.1.17"
tokio-io = "0.1.6"
bytes = "0.4.6"
tokio-tls = { version = "0.2", optional=true }
hyper-tls = { version = "0.3", optional=true }
native-tls = { version = "0.2", optional=true }
typed-headers = "0.1"

[dev-dependencies]
tokio = "0.1"
tokio-tcp = "0.1"

[features]
tls = ["tokio-tls", "hyper-tls", "native-tls"]
default = ["tls"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
> Legend:
- feat: A new feature
- fix: A bug fix
- docs: Documentation only changes
- style: White-space, formatting, missing semi-colons, etc
- refactor: A code change that neither fixes a bug nor adds a feature
- perf: A code change that improves performance
- test: Adding missing tests
- chore: Changes to the build process or auxiliary tools/libraries/documentation

## 0.5.0
- feat: upgrade to hyper 0.12

## 0.4.1
- feat: make TLS support configurable

## 0.4.0
- feat: split Proxy into Proxy and ProxyConnector allowing to handle a list of proxies
- doc: add a set_proxy expression for http requests
- doc: fix some wrong comments
- perf: avoid one clone

## 0.3.0
- refactor: add a match_fn macro in tunnel
- fix: add missing '\' in connect message
- feat: do not use connect for pure http request. Else provide headers to update the primary request with.
- feat: have Custom intercept be an opaque struct using `Arc` to be Send + Sync + Clone

## 0.2.0
- feat: Add Intercept::None to never intercept any connection
- fix: Add Send + Sync constraints on Intercept::Custom function (breaking)
- feat: Make Intercept::matches function public
- feat: Add several function to get/modify internal states
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
The MIT License (MIT)

Copyright (c) 2017 Johann Tuffe

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:


The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.


THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# hyper-proxy

[![Travis Build Status](https://travis-ci.org/tafia/hyper-proxy.svg?branch=master)](https://travis-ci.org/tafia/hyper-proxy)
[![MIT licensed](https://img.shields.io/badge/license-MIT-blue.svg)](./LICENSE)
[![crates.io](http://meritbadge.herokuapp.com/hyper-proxy)](https://crates.io/crates/hyper-proxy)

A proxy connector for [hyper][1] based applications.

[Documentation][3]

## Example

```rust,no_run
extern crate hyper;
extern crate http;
extern crate hyper_proxy;
extern crate futures;
extern crate tokio;
extern crate typed_headers;

use hyper::{Chunk, Client, Request, Method, Uri};
use hyper::client::HttpConnector;
use futures::{Future, Stream};
use hyper_proxy::{Proxy, ProxyConnector, Intercept};
use tokio::runtime::current_thread::Runtime;
use typed_headers::Credentials;

fn main() {
let mut core = Runtime::new().unwrap();

let proxy = {
let proxy_uri = "http://my-proxy:8080".parse().unwrap();
let mut proxy = Proxy::new(Intercept::All, proxy_uri);
proxy.set_authorization(Credentials::basic("John Doe", "Agent1234").unwrap());
let connector = HttpConnector::new(4);
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(hyper::Body::from(vec![])).unwrap();
if let Some(headers) = proxy.http_headers(&uri) {
req.headers_mut().extend(headers.clone().into_iter());
}
let client = Client::builder().build(proxy);
let fut_http = client.request(req)
.and_then(|res| res.into_body().concat2())
.map(move |body: Chunk| ::std::str::from_utf8(&body).unwrap().to_string());

// Connecting to an https uri is straightforward (uses 'CONNECT' method underneath)
let uri = "https://my-remote-websitei-secured.com".parse().unwrap();
let fut_https = client
.get(uri)
.and_then(|res| res.into_body().concat2())
.map(move |body: Chunk| ::std::str::from_utf8(&body).unwrap().to_string());

let futs = fut_http.join(fut_https);

let (_http_res, _https_res) = core.block_on(futs).unwrap();
}
```

## Credits

Large part of the code comes from [reqwest][2].
The core part as just been extracted and slightly enhanced.

Main changes are:
- support for authentication
- add non secured tunneling
- add the possibility to add additional headers when connecting to the proxy

[1]: https://crates.io/crates/hyper
[2]: https://github.com/seanmonstar/reqwest
[3]: https://docs.rs/hyper-proxy
Loading