-
Notifications
You must be signed in to change notification settings - Fork 235
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
manage websocket connection dns and dial ourselves
- Loading branch information
Showing
10 changed files
with
186 additions
and
74 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,50 @@ | ||
use nym_http_api_client::dns::HickoryDnsResolver; | ||
use crate::error::ClientCoreError; | ||
|
||
use nym_http_api_client::HickoryDnsResolver; | ||
use tokio::net::TcpStream; | ||
use tokio_tungstenite::{MaybeTlsStream, WebSocketStream}; | ||
use tungstenite::{ | ||
error::{Error, UrlError}, | ||
handshake::client::Response, | ||
}; | ||
use tungstenite::{error::UrlError, handshake::client::Response}; | ||
use url::{Host, Url}; | ||
|
||
use std::net::SocketAddr; | ||
|
||
#[cfg(not(target_arch = "wasm32"))] | ||
pub(crate) async fn connect_async( | ||
endpoint: &str, | ||
) -> Result<(WebSocketStream<MaybeTlsStream<TcpStream>>, Response), Error> { | ||
use std::net::SocketAddr; | ||
|
||
) -> Result<(WebSocketStream<MaybeTlsStream<TcpStream>>, Response), ClientCoreError> { | ||
let resolver = HickoryDnsResolver::default(); | ||
let uri = Url::parse(endpoint).map_err(|_| ClientCoreError::InvalidURL(endpoint.to_owned()))?; | ||
let port: u16 = uri.port_or_known_default().unwrap_or(443); | ||
|
||
let sock_addrs: Vec<SocketAddr> = resolver | ||
.resolve_str(endpoint) | ||
.await | ||
.map_err(|_| UrlError::NoPathOrQuery)? // failed to resolve | ||
.collect(); | ||
let host = uri | ||
.host() | ||
.ok_or(ClientCoreError::InvalidURL(endpoint.to_owned()))?; | ||
|
||
// Get address for tcp connection, if a domain is provided use our preferred resolver rather than | ||
// the default std resolve | ||
let sock_addrs: Vec<SocketAddr> = match host { | ||
Host::Ipv4(addr) => vec![SocketAddr::new(addr.into(), port)], | ||
Host::Ipv6(addr) => vec![SocketAddr::new(addr.into(), port)], | ||
Host::Domain(domain) => { | ||
// Do a DNS lookup for the domain using our custom DNS resolver | ||
resolver | ||
.resolve_str(domain) | ||
.await | ||
.map_err(|_| { | ||
// failed to resolve | ||
ClientCoreError::GatewayConnectionFailure { | ||
source: UrlError::NoPathOrQuery.into(), | ||
} | ||
})? | ||
.into_iter() | ||
.map(|a| SocketAddr::new(a, port)) | ||
.collect() | ||
} | ||
}; | ||
|
||
let stream = TcpStream::connect(&sock_addrs[..]).await?; | ||
|
||
tokio_tungstenite::client_async_tls(endpoint, stream).await | ||
tokio_tungstenite::client_async_tls(endpoint, stream) | ||
.await | ||
.map_err(Into::into) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
common/client-libs/gateway-client/src/client/websockets.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use crate::error::GatewayClientError; | ||
|
||
use nym_http_api_client::HickoryDnsResolver; | ||
use tokio::net::TcpStream; | ||
use tokio_tungstenite::{MaybeTlsStream, WebSocketStream}; | ||
use tungstenite::{error::UrlError, handshake::client::Response}; | ||
use url::{Host, Url}; | ||
|
||
use std::net::SocketAddr; | ||
|
||
#[cfg(not(target_arch = "wasm32"))] | ||
pub(crate) async fn connect_async( | ||
endpoint: &str, | ||
) -> Result<(WebSocketStream<MaybeTlsStream<TcpStream>>, Response), GatewayClientError> { | ||
let resolver = HickoryDnsResolver::default(); | ||
let uri = | ||
Url::parse(endpoint).map_err(|_| GatewayClientError::InvalidURL(endpoint.to_owned()))?; | ||
let port: u16 = uri.port_or_known_default().unwrap_or(443); | ||
|
||
let host = uri | ||
.host() | ||
.ok_or(GatewayClientError::InvalidURL(endpoint.to_owned()))?; | ||
|
||
// Get address for tcp connection, if a domain is provided use our preferred resolver rather than | ||
// the default std resolve | ||
let sock_addrs: Vec<SocketAddr> = match host { | ||
Host::Ipv4(addr) => vec![SocketAddr::new(addr.into(), port)], | ||
Host::Ipv6(addr) => vec![SocketAddr::new(addr.into(), port)], | ||
Host::Domain(domain) => { | ||
// Do a DNS lookup for the domain using our custom DNS resolver | ||
resolver | ||
.resolve_str(domain) | ||
.await | ||
.map_err(|_| { | ||
// failed to resolve | ||
GatewayClientError::NetworkConnectionFailed { | ||
address: endpoint.to_owned(), | ||
source: UrlError::NoPathOrQuery.into(), | ||
} | ||
})? | ||
.into_iter() | ||
.map(|a| SocketAddr::new(a, port)) | ||
.collect() | ||
} | ||
}; | ||
|
||
let stream = TcpStream::connect(&sock_addrs[..]).await.map_err(|error| { | ||
GatewayClientError::NetworkConnectionFailed { | ||
address: endpoint.to_owned(), | ||
source: error.into(), | ||
} | ||
})?; | ||
|
||
tokio_tungstenite::client_async_tls(endpoint, stream) | ||
.await | ||
.map_err(|error| GatewayClientError::NetworkConnectionFailed { | ||
address: endpoint.to_owned(), | ||
source: error, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.