Skip to content

Commit fbb1904

Browse files
committed
transport: handle ipv6 endpoints when tls is enabled
Fixes #2422
1 parent ff7b540 commit fbb1904

File tree

1 file changed

+45
-1
lines changed
  • tonic/src/transport/channel

1 file changed

+45
-1
lines changed

tonic/src/transport/channel/tls.rs

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,15 @@ impl ClientTlsConfig {
136136
pub(crate) fn into_tls_connector(self, uri: &Uri) -> Result<TlsConnector, crate::BoxError> {
137137
let domain = match &self.domain {
138138
Some(domain) => domain,
139-
None => uri.host().ok_or_else(Error::new_invalid_uri)?,
139+
None => {
140+
let host = uri.host().ok_or_else(Error::new_invalid_uri)?;
141+
// host() returns the host including brackets if it's an IPv6 address
142+
if host.starts_with('[') && host.ends_with(']') {
143+
&host[1..host.len()-1]
144+
} else {
145+
host
146+
}
147+
},
140148
};
141149
TlsConnector::new(
142150
self.certs,
@@ -153,3 +161,39 @@ impl ClientTlsConfig {
153161
)
154162
}
155163
}
164+
165+
#[cfg(test)]
166+
mod tests {
167+
use super::*;
168+
169+
#[test]
170+
fn test_into_tls_connector_with_ipv4() {
171+
let config = ClientTlsConfig::new();
172+
let uri = "https://192.168.1.1:443".parse::<Uri>().unwrap();
173+
config.into_tls_connector(&uri).unwrap();
174+
}
175+
176+
#[test]
177+
fn test_into_tls_connector_with_ipv6_() {
178+
let config = ClientTlsConfig::new();
179+
let uri = "https://[::1]:443".parse::<Uri>().unwrap();
180+
181+
config.into_tls_connector(&uri).unwrap();
182+
}
183+
184+
#[test]
185+
fn test_into_tls_connector_with_domain_name() {
186+
let config = ClientTlsConfig::new();
187+
let uri = "https://example.com:443".parse::<Uri>().unwrap();
188+
189+
config.into_tls_connector(&uri).unwrap();
190+
}
191+
192+
#[test]
193+
fn test_into_tls_connector_with_explicit_domain() {
194+
let config = ClientTlsConfig::new().domain_name("example.com");
195+
let uri = "https://[2001:db8::1]:443".parse::<Uri>().unwrap();
196+
197+
config.into_tls_connector(&uri).unwrap();
198+
}
199+
}

0 commit comments

Comments
 (0)