Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lookup gateway IP using vpn api #1481

Merged
merged 1 commit into from
Nov 6, 2024
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
13 changes: 13 additions & 0 deletions nym-vpn-core/crates/nym-gateway-directory/src/entries/gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,19 @@ impl Gateway {
self.authenticator_address.is_some()
}

pub fn host(&self) -> Option<&nym_topology::NetworkAddress> {
self.host.as_ref()
}

pub async fn lookup_ip(&self) -> Option<IpAddr> {
match self.host.clone()? {
nym_topology::NetworkAddress::IpAddr(ip) => Some(ip),
nym_topology::NetworkAddress::Hostname(hostname) => {
crate::helpers::try_resolve_hostname(&hostname).await.ok()
}
}
}

pub fn clients_address_no_tls(&self) -> Option<String> {
match (&self.host, &self.clients_ws_port) {
(Some(host), Some(port)) => Some(format!("ws://{}:{}", host, port)),
Expand Down
3 changes: 3 additions & 0 deletions nym-vpn-core/crates/nym-gateway-directory/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ pub enum Error {

#[error("gateway {0} doesn't have a description available")]
NoGatewayDescriptionAvailable(String),

#[error("failed to lookup gateway ip for gateway {0}")]
FailedToLookupIp(String),
}

// Result type based on our error type
Expand Down
30 changes: 29 additions & 1 deletion nym-vpn-core/crates/nym-gateway-directory/src/gateway_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ impl GatewayClient {
gateways.random_low_latency_gateway().await
}

pub async fn lookup_gateway_ip(&self, gateway_identity: &str) -> Result<IpAddr> {
pub async fn lookup_gateway_ip_from_nym_api(&self, gateway_identity: &str) -> Result<IpAddr> {
info!("Fetching gateway ip from nym-api...");
let mut ips = self
.api_client
.get_all_described_nodes()
Expand Down Expand Up @@ -262,6 +263,33 @@ impl GatewayClient {
.map(GatewayList::into_vpn_gateways)
}

pub async fn lookup_gateway_ip(&self, gateway_identity: &str) -> Result<IpAddr> {
if let Some(nym_vpn_api_client) = &self.nym_vpn_api_client {
info!("Fetching gateway ip from nym-vpn-api...");
let gateway = nym_vpn_api_client
.get_gateways(None)
.await?
.into_iter()
.find_map(|gw| {
if gw.identity_key != gateway_identity {
None
} else {
Gateway::try_from(gw)
.inspect_err(|err| error!("Failed to parse gateway: {err}"))
.ok()
}
})
.ok_or_else(|| Error::RequestedGatewayIdNotFound(gateway_identity.to_string()))?;
gateway
.lookup_ip()
.await
.ok_or(Error::FailedToLookupIp(gateway_identity.to_string()))
} else {
warn!("OPERATING IN FALLBACK MODE WITHOUT NYM-VPN-API!");
self.lookup_gateway_ip_from_nym_api(gateway_identity).await
}
}

pub async fn lookup_all_gateways(&self) -> Result<GatewayList> {
if let Some(nym_vpn_api_client) = &self.nym_vpn_api_client {
info!("Fetching all gateways from nym-vpn-api...");
Expand Down
Loading