Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion src/client/legacy/connect/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ where
let config = &self.config;

let (host, port) = get_host_port(config, &dst)?;
let host = host.trim_start_matches('[').trim_end_matches(']');
let host = crate::client::strip_ipv6_brackets(host);

// If the host is already an IP addr (v4 or v6),
// skip resolving the dns and start connecting right away.
Expand Down
6 changes: 1 addition & 5 deletions src/client/legacy/connect/proxy/socks/v4/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,7 @@ where
// Whilst SOCKSv4 does not support IPv6, this bracket stripping will enable an IPv6
// destination to be correctly rejected down the line. Without it, we would end up with
// a DNS resolution failure which would be confusing.
let host = host_from_uri
.strip_prefix('[')
.and_then(|s| s.strip_suffix(']'))
.unwrap_or(host_from_uri)
.to_string();
let host = crate::client::strip_ipv6_brackets(host_from_uri).to_string();

let conn = connecting.await.map_err(SocksError::Inner)?;
config.execute(conn, host, port).await
Expand Down
6 changes: 1 addition & 5 deletions src/client/legacy/connect/proxy/socks/v5/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,11 +265,7 @@ where

// Note: host() returns the host part of the URI which could be a square bracketed IPv6
// address. We need to remove any brackets to make it a valid IP address.
let host = host_from_uri
.strip_prefix('[')
.and_then(|s| s.strip_suffix(']'))
.unwrap_or(host_from_uri)
.to_string();
let host = crate::client::strip_ipv6_brackets(host_from_uri).to_string();

let conn = connecting.await.map_err(SocksError::Inner)?;
config.execute(conn, host, port).await
Expand Down
21 changes: 21 additions & 0 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,24 @@ pub mod pool;

#[cfg(feature = "client-proxy")]
pub mod proxy;

#[cfg(any(feature = "client-legacy", feature = "client-proxy"))]
fn strip_ipv6_brackets(host: &str) -> &str {
host.strip_prefix('[')
.and_then(|host| host.strip_suffix(']'))
.unwrap_or(host)
}

#[cfg(all(test, any(feature = "client-legacy", feature = "client-proxy")))]
mod tests {
use super::strip_ipv6_brackets;

#[test]
fn test_strip_ipv6_brackets() {
assert_eq!(strip_ipv6_brackets("[::1]"), "::1");
assert_eq!(strip_ipv6_brackets("::1"), "::1");
assert_eq!(strip_ipv6_brackets("example.com"), "example.com");
assert_eq!(strip_ipv6_brackets("[example.com"), "[example.com");
assert_eq!(strip_ipv6_brackets("example.com]"), "example.com]");
}
}
7 changes: 1 addition & 6 deletions src/client/proxy/matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,12 +470,7 @@ impl NoProxy {
pub fn contains(&self, host: &str) -> bool {
// According to RFC3986, raw IPv6 hosts will be wrapped in []. So we need to strip those off
// the end in order to parse correctly
let host = if host.starts_with('[') {
let x: &[_] = &['[', ']'];
host.trim_matches(x)
} else {
host
};
let host = crate::client::strip_ipv6_brackets(host);
match host.parse::<IpAddr>() {
// If we can parse an IP addr, then use it, otherwise, assume it is a domain
Ok(ip) => self.ips.contains(ip),
Expand Down