Skip to content

Commit

Permalink
Merge pull request #2466 from eidenar/reqwest-backend-socks-support
Browse files Browse the repository at this point in the history
Add socks5 support for reqwest-backend
  • Loading branch information
kinnison authored Sep 3, 2020
2 parents 94362b5 + ccb103b commit 2869670
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 10 deletions.
98 changes: 89 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion download/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ url = "2.1"
curl = { version = "0.4.11", optional = true }
env_proxy = { version = "0.4.1", optional = true }
lazy_static = { version = "1.0", optional = true }
reqwest = { version = "0.10", features = ["blocking", "gzip"], optional = true }
reqwest = { version = "0.10", features = ["blocking", "gzip", "socks"], optional = true }

[dev-dependencies]
hyper = { version = "0.13", default-features = false, features = ["tcp"] }
Expand Down
53 changes: 53 additions & 0 deletions download/tests/read-proxy-env.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
#![cfg(feature = "reqwest-backend")]

use std::env::{remove_var, set_var};
use std::error::Error;
use std::net::TcpListener;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Mutex;
use std::thread;
use std::time::Duration;

use env_proxy::for_url;
use lazy_static::lazy_static;
use reqwest::{blocking::Client, Proxy};
use url::Url;

lazy_static! {
static ref SERIALISE_TESTS: Mutex<()> = Mutex::new(());
}

fn scrub_env() {
remove_var("http_proxy");
remove_var("https_proxy");
Expand All @@ -20,6 +32,9 @@ fn scrub_env() {
// Tests for correctly retrieving the proxy (host, port) tuple from $https_proxy
#[test]
fn read_basic_proxy_params() {
let _guard = SERIALISE_TESTS
.lock()
.expect("Unable to lock the test guard");
scrub_env();
set_var("https_proxy", "http://proxy.example.com:8080");
let u = Url::parse("https://www.example.org").ok().unwrap();
Expand All @@ -28,3 +43,41 @@ fn read_basic_proxy_params() {
Some(("proxy.example.com".to_string(), 8080))
);
}

// Tests to verify if socks feature is available and being used
#[test]
fn socks_proxy_request() {
static CALL_COUNT: AtomicUsize = AtomicUsize::new(0);
let _guard = SERIALISE_TESTS
.lock()
.expect("Unable to lock the test guard");

scrub_env();
set_var("all_proxy", "socks5://127.0.0.1:1080");

thread::spawn(move || {
let listener = TcpListener::bind("127.0.0.1:1080").unwrap();
let incoming = listener.incoming();
for _ in incoming {
CALL_COUNT.fetch_add(1, Ordering::SeqCst);
}
});

let env_proxy = |url: &Url| for_url(&url).to_url();
let url = Url::parse("http://example.org").unwrap();

let client = Client::builder()
.proxy(Proxy::custom(env_proxy))
.timeout(Duration::from_secs(1))
.build()
.unwrap();
let res = client.get(url.as_str()).send();

if let Err(e) = res {
let s = e.source().unwrap();
assert_eq!(CALL_COUNT.load(Ordering::SeqCst), 1);
assert!(s.to_string().contains("socks connect error"));
} else {
panic!("Socks proxy was ignored")
}
}

0 comments on commit 2869670

Please sign in to comment.