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

Enable new uniffi functions for user agent #615

Merged
merged 7 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions nym-vpn-core/crates/nym-vpn-api-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ pub use helpers::{
get_countries, get_entry_countries, get_entry_gateways, get_exit_countries, get_exit_gateways,
get_gateways,
};
pub use responses::Country;
pub use responses::Gateway;
1 change: 1 addition & 0 deletions nym-vpn-core/nym-vpn-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ nym-topology.workspace = true
nym-validator-client.workspace = true
nym-wireguard-types.workspace = true

nym-vpn-api-client = { path = "../crates/nym-vpn-api-client" }
nym-connection-monitor = { path = "../crates/nym-connection-monitor" }
nym-gateway-directory = { path = "../crates/nym-gateway-directory" }
nym-ip-packet-client = { path = "../crates/nym-ip-packet-client" }
Expand Down
11 changes: 11 additions & 0 deletions nym-vpn-core/nym-vpn-lib/src/platform/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ pub enum FFIError {
#[error("Invalid credential passed in uniffi")]
InvalidCredential,

#[error("{inner}")]
VpnApiClientError { inner: String },

#[error("Invalid path")]
InvalidPath,

Expand Down Expand Up @@ -53,3 +56,11 @@ impl From<nym_gateway_directory::Error> for FFIError {
}
}
}

impl From<nym_vpn_api_client::VpnApiClientError> for FFIError {
fn from(value: nym_vpn_api_client::VpnApiClientError) -> Self {
Self::VpnApiClientError {
inner: value.to_string(),
}
}
}
76 changes: 37 additions & 39 deletions nym-vpn-core/nym-vpn-lib/src/platform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,24 +269,23 @@ pub fn getGatewayCountries(
))
}

// TODO: generate uniffi
// #[allow(non_snake_case)]
// #[uniffi::export]
// pub fn getGatewayCountriesUserAgent(
// api_url: Url,
// explorer_url: Url,
// harbour_master_url: Option<Url>,
// exit_only: bool,
// user_agent: UserAgent,
// ) -> Result<Vec<Location>, FFIError> {
// RUNTIME.block_on(get_gateway_countries(
// api_url,
// explorer_url,
// harbour_master_url,
// exit_only,
// Some(user_agent),
// ))
// }
#[allow(non_snake_case)]
#[uniffi::export]
pub fn getGatewayCountriesUserAgent(
api_url: Url,
explorer_url: Url,
harbour_master_url: Option<Url>,
exit_only: bool,
user_agent: UserAgent,
) -> Result<Vec<Location>, FFIError> {
RUNTIME.block_on(get_gateway_countries(
api_url,
explorer_url,
harbour_master_url,
exit_only,
Some(user_agent),
))
}

async fn get_gateway_countries(
api_url: Url,
Expand All @@ -295,21 +294,21 @@ async fn get_gateway_countries(
exit_only: bool,
user_agent: Option<UserAgent>,
) -> Result<Vec<Location>, FFIError> {
let config = nym_gateway_directory::Config {
//TODO support other envs in the future
let _config = nym_gateway_directory::Config {
api_url,
explorer_url: Some(explorer_url),
harbour_master_url,
};
let user_agent = user_agent
.map(nym_sdk::UserAgent::from)
.unwrap_or_else(|| nym_bin_common::bin_info!().into());
let gateway_client = GatewayClient::new(config, user_agent)?;

let locations = if !exit_only {
gateway_client.lookup_all_countries_iso().await?
nym_vpn_api_client::get_countries(user_agent).await
} else {
gateway_client.lookup_all_exit_countries_iso().await?
};
nym_vpn_api_client::get_exit_countries(user_agent).await
}?;
Ok(locations.into_iter().map(Into::into).collect())
}

Expand All @@ -328,22 +327,21 @@ pub fn getLowLatencyEntryCountry(
))
}

// TODO: generate uniffi
//#[allow(non_snake_case)]
//#[uniffi::export]
//pub fn getLowLatencyEntryCountryUserAgent(
// api_url: Url,
// explorer_url: Url,
// harbour_master_url: Option<Url>,
// user_agent: UserAgent,
//) -> Result<Location, FFIError> {
// RUNTIME.block_on(get_low_latency_entry_country(
// api_url,
// explorer_url,
// harbour_master_url,
// Some(user_agent),
// ))
//}
#[allow(non_snake_case)]
#[uniffi::export]
pub fn getLowLatencyEntryCountryUserAgent(
api_url: Url,
explorer_url: Url,
harbour_master_url: Option<Url>,
user_agent: UserAgent,
) -> Result<Location, FFIError> {
RUNTIME.block_on(get_low_latency_entry_country(
api_url,
explorer_url,
harbour_master_url,
Some(user_agent),
))
}

async fn get_low_latency_entry_country(
api_url: Url,
Expand Down
19 changes: 10 additions & 9 deletions nym-vpn-core/nym-vpn-lib/src/uniffi_custom_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use ipnetwork::IpNetwork;
use nym_explorer_client::Location as ExpLocation;
use nym_gateway_directory::{EntryPoint as GwEntryPoint, ExitPoint as GwExitPoint};
use nym_sdk::UserAgent as NymUserAgent;
use nym_vpn_api_client::Country;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
use std::path::PathBuf;
use std::str::FromStr;
Expand Down Expand Up @@ -167,26 +168,26 @@ impl UniffiCustomTypeConverter for PresharedKey {
#[derive(uniffi::Record)]
pub struct Location {
pub two_letter_iso_country_code: String,
pub three_letter_iso_country_code: String,
pub country_name: String,
pub latitude: Option<f64>,
pub longitude: Option<f64>,
}

impl From<ExpLocation> for Location {
fn from(value: ExpLocation) -> Self {
Location {
two_letter_iso_country_code: value.two_letter_iso_country_code,
three_letter_iso_country_code: value.three_letter_iso_country_code,
country_name: value.country_name,
latitude: value.latitude,
longitude: value.longitude,
}
}
}

impl From<Country> for Location {
fn from(value: Country) -> Self {
Location {
two_letter_iso_country_code: value.iso_code().to_string(),
}
}
}

// TODO: generate uniffi
// #[derive(uniffi::Record)]
#[derive(uniffi::Record)]
pub struct UserAgent {
// The name of the application
// Example: nym-vpnd
Expand Down
Loading