Skip to content

Commit f186803

Browse files
authored
fix(proxy): restore default port 1080 for SOCKS proxies without explicit port (#2771)
1 parent e684d0e commit f186803

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

src/proxy.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,15 @@ pub trait IntoProxy {
123123
impl<S: IntoUrl> IntoProxy for S {
124124
fn into_proxy(self) -> crate::Result<Url> {
125125
match self.as_str().into_url() {
126-
Ok(ok) => Ok(ok),
126+
Ok(mut url) => {
127+
// If the scheme is a SOCKS protocol and no port is specified, set the default
128+
if url.port().is_none()
129+
&& matches!(url.scheme(), "socks4" | "socks4a" | "socks5" | "socks5h")
130+
{
131+
let _ = url.set_port(Some(1080));
132+
}
133+
Ok(url)
134+
}
127135
Err(e) => {
128136
let mut presumed_to_have_scheme = true;
129137
let mut source = e.source();
@@ -910,4 +918,25 @@ mod tests {
910918
.into_matcher();
911919
assert!(m.maybe_has_http_auth(), "http forwards");
912920
}
921+
922+
#[test]
923+
fn test_socks_proxy_default_port() {
924+
{
925+
let m = Proxy::all("socks5://example.com").unwrap().into_matcher();
926+
927+
let http = "http://hyper.rs";
928+
let https = "https://hyper.rs";
929+
930+
assert_eq!(intercepted_uri(&m, http).port_u16(), Some(1080));
931+
assert_eq!(intercepted_uri(&m, https).port_u16(), Some(1080));
932+
933+
// custom port
934+
let m = Proxy::all("socks5://example.com:1234")
935+
.unwrap()
936+
.into_matcher();
937+
938+
assert_eq!(intercepted_uri(&m, http).port_u16(), Some(1234));
939+
assert_eq!(intercepted_uri(&m, https).port_u16(), Some(1234));
940+
}
941+
}
913942
}

0 commit comments

Comments
 (0)