Skip to content

Commit d2097d3

Browse files
committed
fix: do not URL-encode proxy hostnames
1 parent 1219cbe commit d2097d3

2 files changed

Lines changed: 72 additions & 7 deletions

File tree

src/net/proxy.rs

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use tokio_io_timeout::TimeoutStream;
1919
use url::Url;
2020

2121
use crate::config::Config;
22-
use crate::constants::NON_ALPHANUMERIC_WITHOUT_DOT;
2322
use crate::context::Context;
2423
use crate::net::connect_tcp;
2524
use crate::net::session::SessionStream;
@@ -93,13 +92,12 @@ impl HttpConfig {
9392
}
9493

9594
fn to_url(&self, scheme: &str) -> String {
96-
let host = utf8_percent_encode(&self.host, NON_ALPHANUMERIC_WITHOUT_DOT);
9795
if let Some((user, password)) = &self.user_password {
9896
let user = utf8_percent_encode(user, NON_ALPHANUMERIC);
9997
let password = utf8_percent_encode(password, NON_ALPHANUMERIC);
100-
format!("{scheme}://{user}:{password}@{host}:{}", self.port)
98+
format!("{scheme}://{user}:{password}@{}:{}", self.host, self.port)
10199
} else {
102-
format!("{scheme}://{host}:{}", self.port)
100+
format!("{scheme}://{}:{}", self.host, self.port)
103101
}
104102
}
105103
}
@@ -143,13 +141,12 @@ impl Socks5Config {
143141
}
144142

145143
fn to_url(&self) -> String {
146-
let host = utf8_percent_encode(&self.host, NON_ALPHANUMERIC_WITHOUT_DOT);
147144
if let Some((user, password)) = &self.user_password {
148145
let user = utf8_percent_encode(user, NON_ALPHANUMERIC);
149146
let password = utf8_percent_encode(password, NON_ALPHANUMERIC);
150-
format!("socks5://{user}:{password}@{host}:{}", self.port)
147+
format!("socks5://{user}:{password}@{}:{}", self.host, self.port)
151148
} else {
152-
format!("socks5://{host}:{}", self.port)
149+
format!("socks5://{}:{}", self.host, self.port)
153150
}
154151
}
155152
}
@@ -565,6 +562,20 @@ mod tests {
565562
user_password: None
566563
})
567564
);
565+
566+
let proxy_config = ProxyConfig::from_url("socks5://my-proxy.example.org").unwrap();
567+
assert_eq!(
568+
proxy_config,
569+
ProxyConfig::Socks5(Socks5Config {
570+
host: "my-proxy.example.org".to_string(),
571+
port: 1080,
572+
user_password: None
573+
})
574+
);
575+
assert_eq!(
576+
proxy_config.to_url(),
577+
"socks5://my-proxy.example.org:1080".to_string()
578+
);
568579
}
569580

570581
#[test]
@@ -598,6 +609,20 @@ mod tests {
598609
user_password: None
599610
})
600611
);
612+
613+
let proxy_config = ProxyConfig::from_url("http://my-proxy.example.org").unwrap();
614+
assert_eq!(
615+
proxy_config,
616+
ProxyConfig::Http(HttpConfig {
617+
host: "my-proxy.example.org".to_string(),
618+
port: 80,
619+
user_password: None
620+
})
621+
);
622+
assert_eq!(
623+
proxy_config.to_url(),
624+
"http://my-proxy.example.org:80".to_string()
625+
);
601626
}
602627

603628
#[test]
@@ -631,6 +656,20 @@ mod tests {
631656
user_password: None
632657
})
633658
);
659+
660+
let proxy_config = ProxyConfig::from_url("https://my-proxy.example.org").unwrap();
661+
assert_eq!(
662+
proxy_config,
663+
ProxyConfig::Https(HttpConfig {
664+
host: "my-proxy.example.org".to_string(),
665+
port: 443,
666+
user_password: None
667+
})
668+
);
669+
assert_eq!(
670+
proxy_config.to_url(),
671+
"https://my-proxy.example.org:443".to_string()
672+
);
634673
}
635674

636675
#[test]

src/qr/qr_tests.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,32 @@ async fn test_set_proxy_config_from_qr() -> Result<()> {
892892
Ok(())
893893
}
894894

895+
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
896+
async fn test_dont_encode_hyphen_in_proxy_hostnames() -> Result<()> {
897+
let mut tcm = TestContextManager::new();
898+
let t = &tcm.alice().await;
899+
900+
let qr_text = "socks5://my-proxy.example.org";
901+
902+
let qr = check_qr(t, qr_text).await?;
903+
assert_eq!(
904+
qr,
905+
Qr::Proxy {
906+
url: "socks5://my-proxy.example.org".to_string(),
907+
host: "my-proxy.example.org".to_string(),
908+
port: 1080,
909+
}
910+
);
911+
912+
set_config_from_qr(t, "socks5://my-proxy.example.org").await?;
913+
assert_eq!(
914+
t.get_config(Config::ProxyUrl).await?,
915+
Some("socks5://my-proxy.example.org:1080".to_string())
916+
);
917+
918+
Ok(())
919+
}
920+
895921
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
896922
async fn test_decode_shadowsocks() -> Result<()> {
897923
let ctx = TestContext::new().await;

0 commit comments

Comments
 (0)