Skip to content

Commit

Permalink
Lookup gateway IP using vpn-api (#1481)
Browse files Browse the repository at this point in the history
  • Loading branch information
octol authored Nov 6, 2024
1 parent b30a6c9 commit 9b4fe63
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
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

0 comments on commit 9b4fe63

Please sign in to comment.