Skip to content

Commit

Permalink
Add network discovery and selection capability to nym-vpnd (#1361)
Browse files Browse the repository at this point in the history
  • Loading branch information
octol authored Oct 22, 2024
1 parent 4c804d1 commit f68ea13
Show file tree
Hide file tree
Showing 18 changed files with 665 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,7 @@ where
Some(command) = self.command_rx.recv() => {
if let Err(err) = self.handle_command(command).await {
tracing::error!("{err}");
tracing::debug!("{err:#?}");
}
}
_ = update_shared_account_state_timer.tick() => {
Expand Down
2 changes: 1 addition & 1 deletion nym-vpn-core/crates/nym-vpn-lib/src/mixnet/connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ pub(crate) async fn setup_mixnet_client(
let storage = VpnClientOnDiskStorage::new(path.clone());
match storage.is_mnemonic_stored().await {
Ok(is_stored) if !is_stored => {
tracing::error!("No credential stored");
tracing::error!("No account stored");
task_client.disarm();
return Err(MixnetError::InvalidCredential);
}
Expand Down
7 changes: 7 additions & 0 deletions nym-vpn-core/crates/nym-vpnc/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub(crate) enum Command {
Disconnect,
Status,
Info,
SetNetwork(SetNetworkArgs),
StoreAccount(StoreAccountArgs),
RemoveAccount,
GetLocalAccountState,
Expand Down Expand Up @@ -134,6 +135,12 @@ pub(crate) struct CliExit {
pub(crate) exit_gateway_random: bool,
}

#[derive(Args)]
pub(crate) struct SetNetworkArgs {
/// The network to be set.
pub(crate) network: String,
}

#[derive(Args)]
pub(crate) struct StoreAccountArgs {
/// The account mnemonic to be stored.
Expand Down
14 changes: 13 additions & 1 deletion nym-vpn-core/crates/nym-vpnc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use nym_vpn_proto::{
ConnectRequest, DisconnectRequest, Empty, GetAccountSummaryRequest, GetDeviceZkNymsRequest,
GetDevicesRequest, GetLocalAccountStateRequest, InfoRequest, InfoResponse,
IsReadyToConnectRequest, ListCountriesRequest, ListGatewaysRequest, RegisterDeviceRequest,
RemoveAccountRequest, RequestZkNymRequest, StatusRequest, StoreAccountRequest, UserAgent,
RemoveAccountRequest, RequestZkNymRequest, SetNetworkRequest, StatusRequest,
StoreAccountRequest, UserAgent,
};
use protobuf_conversion::{into_gateway_type, into_threshold};
use sysinfo::System;
Expand Down Expand Up @@ -39,6 +40,7 @@ async fn main() -> Result<()> {
Command::Disconnect => disconnect(client_type).await?,
Command::Status => status(client_type).await?,
Command::Info => info(client_type).await?,
Command::SetNetwork(ref args) => set_network(client_type, args).await?,
Command::StoreAccount(ref store_args) => store_account(client_type, store_args).await?,
Command::RemoveAccount => remove_account(client_type).await?,
Command::GetLocalAccountState => get_local_account_state(client_type).await?,
Expand Down Expand Up @@ -164,6 +166,16 @@ async fn info(client_type: ClientType) -> Result<()> {
Ok(())
}

async fn set_network(client_type: ClientType, args: &cli::SetNetworkArgs) -> Result<()> {
let mut client = vpnd_client::get_client(client_type).await?;
let request = tonic::Request::new(SetNetworkRequest {
network: args.network.clone(),
});
let response = client.set_network(request).await?.into_inner();
println!("{:#?}", response);
Ok(())
}

async fn store_account(client_type: ClientType, store_args: &cli::StoreAccountArgs) -> Result<()> {
let mut client = vpnd_client::get_client(client_type).await?;
let request = tonic::Request::new(StoreAccountRequest {
Expand Down
1 change: 1 addition & 0 deletions nym-vpn-core/crates/nym-vpnd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ parity-tokio-ipc.workspace = true
prost-types.workspace = true
prost.workspace = true
reqwest = { workspace = true, default-features = false, features = [
"blocking",
"rustls-tls",
] }
serde.workspace = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use nym_vpn_lib::gateway_directory::{EntryPoint, ExitPoint, GatewayClient, Gatew

use crate::{
service::{
AccountError, ConnectArgs, ConnectOptions, VpnServiceCommand, VpnServiceConnectError,
VpnServiceDisconnectError, VpnServiceInfo, VpnServiceStatus,
AccountError, ConnectArgs, ConnectOptions, SetNetworkError, VpnServiceCommand,
VpnServiceConnectError, VpnServiceDisconnectError, VpnServiceInfo, VpnServiceStatus,
},
types::gateway,
};
Expand Down Expand Up @@ -77,6 +77,14 @@ impl CommandInterfaceConnectionHandler {
self.send_and_wait(VpnServiceCommand::Info, ()).await
}

pub(crate) async fn handle_set_network(
&self,
network: String,
) -> Result<Result<(), SetNetworkError>, VpnCommandSendError> {
self.send_and_wait(VpnServiceCommand::SetNetwork, network)
.await
}

pub(crate) async fn handle_status(&self) -> Result<VpnServiceStatus, VpnCommandSendError> {
self.send_and_wait(VpnServiceCommand::Status, ()).await
}
Expand Down
23 changes: 21 additions & 2 deletions nym-vpn-core/crates/nym-vpnd/src/command_interface/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ use nym_vpn_proto::{
InfoResponse, IsAccountStoredRequest, IsAccountStoredResponse, IsReadyToConnectRequest,
IsReadyToConnectResponse, ListCountriesRequest, ListCountriesResponse, ListGatewaysRequest,
ListGatewaysResponse, RegisterDeviceRequest, RegisterDeviceResponse, RemoveAccountRequest,
RemoveAccountResponse, RequestZkNymRequest, RequestZkNymResponse, StatusRequest,
StatusResponse, StoreAccountRequest, StoreAccountResponse,
RemoveAccountResponse, RequestZkNymRequest, RequestZkNymResponse, SetNetworkRequest,
SetNetworkResponse, StatusRequest, StatusResponse, StoreAccountRequest, StoreAccountResponse,
};

use super::{
Expand Down Expand Up @@ -123,6 +123,25 @@ impl NymVpnd for CommandInterface {
Ok(tonic::Response::new(response))
}

async fn set_network(
&self,
request: tonic::Request<SetNetworkRequest>,
) -> Result<tonic::Response<SetNetworkResponse>, tonic::Status> {
let network = request.into_inner().network;

let status = CommandInterfaceConnectionHandler::new(self.vpn_command_tx.clone())
.handle_set_network(network)
.await?;

let response = nym_vpn_proto::SetNetworkResponse {
error: status
.err()
.map(nym_vpn_proto::SetNetworkRequestError::from),
};
tracing::debug!("Returning set network response: {:?}", response);
Ok(tonic::Response::new(response))
}

async fn vpn_connect(
&self,
request: tonic::Request<ConnectRequest>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use maplit::hashmap;
use nym_vpn_account_controller::ReadyToConnect;
use nym_vpn_proto::{account_error::AccountErrorType, error::ErrorType, Error as ProtoError};

use crate::service::{AccountError, ConnectionFailedError, VpnServiceConnectError};
use crate::service::{
AccountError, ConnectionFailedError, SetNetworkError, VpnServiceConnectError,
};

impl From<VpnServiceConnectError> for nym_vpn_proto::ConnectRequestError {
fn from(err: VpnServiceConnectError) -> Self {
Expand Down Expand Up @@ -538,3 +540,24 @@ impl From<VpnCommandSendError> for tonic::Status {
}
}
}

impl From<SetNetworkError> for nym_vpn_proto::SetNetworkRequestError {
fn from(err: SetNetworkError) -> Self {
match err {
SetNetworkError::NetworkNotFound(ref err) => nym_vpn_proto::SetNetworkRequestError {
kind: nym_vpn_proto::set_network_request_error::SetNetworkRequestErrorType::InvalidNetworkName as i32,
message: err.to_string(),
},
SetNetworkError::ReadConfig { .. } => nym_vpn_proto::SetNetworkRequestError {
kind: nym_vpn_proto::set_network_request_error::SetNetworkRequestErrorType::Internal
as i32,
message: err.to_string(),
},
SetNetworkError::WriteConfig { .. } => nym_vpn_proto::SetNetworkRequestError {
kind: nym_vpn_proto::set_network_request_error::SetNetworkRequestErrorType::Internal
as i32,
message: err.to_string(),
},
}
}
}
Loading

0 comments on commit f68ea13

Please sign in to comment.