diff --git a/src/proxy.rs b/src/proxy.rs index a467436d1..add809ac6 100644 --- a/src/proxy.rs +++ b/src/proxy.rs @@ -123,7 +123,15 @@ pub trait IntoProxy { impl IntoProxy for S { fn into_proxy(self) -> crate::Result { 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(); @@ -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)); + } + } }