Skip to content
Merged
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
31 changes: 30 additions & 1 deletion src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,15 @@ pub trait IntoProxy {
impl<S: IntoUrl> IntoProxy for S {
fn into_proxy(self) -> crate::Result<Url> {
match self.as_str().into_url() {
Ok(ok) => Ok(ok),
Ok(mut url) => {
// If the scheme is a SOCKS protocol and no port is specified, set the default
if url.port().is_none()
&& matches!(url.scheme(), "socks4" | "socks4a" | "socks5" | "socks5h")
{
let _ = url.set_port(Some(1080));
}
Ok(url)
}
Err(e) => {
let mut presumed_to_have_scheme = true;
let mut source = e.source();
Expand Down Expand Up @@ -910,4 +918,25 @@ mod tests {
.into_matcher();
assert!(m.maybe_has_http_auth(), "http forwards");
}

#[test]
fn test_socks_proxy_default_port() {
{
let m = Proxy::all("socks5://example.com").unwrap().into_matcher();

let http = "http://hyper.rs";
let https = "https://hyper.rs";

assert_eq!(intercepted_uri(&m, http).port_u16(), Some(1080));
assert_eq!(intercepted_uri(&m, https).port_u16(), Some(1080));

// custom port
let m = Proxy::all("socks5://example.com:1234")
.unwrap()
.into_matcher();

assert_eq!(intercepted_uri(&m, http).port_u16(), Some(1234));
assert_eq!(intercepted_uri(&m, https).port_u16(), Some(1234));
}
}
}
Loading