Skip to content

Commit

Permalink
Merge pull request #325 from nymtech/jon/import-credential-file
Browse files Browse the repository at this point in the history
Import credential file in vpnd
  • Loading branch information
octol authored Apr 24, 2024
2 parents d663d6e + 599d440 commit 3b481ee
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 69 deletions.
13 changes: 6 additions & 7 deletions nym-vpnc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,13 @@ async fn import_credential(
import_args: &ImportCredentialArgs,
) -> anyhow::Result<()> {
let import_type: ImportCredentialTypeEnum = import_args.credential_type.clone().into();
let request = match import_type {
ImportCredentialTypeEnum::Path(_path) => todo!(),
ImportCredentialTypeEnum::Data(data) => {
let bin = parse_encoded_credential_data(&data)?;
tonic::Request::new(ImportUserCredentialRequest { credential: bin })
}
let raw_credential = match import_type {
ImportCredentialTypeEnum::Path(path) => std::fs::read(path)?,
ImportCredentialTypeEnum::Data(data) => parse_encoded_credential_data(&data)?,
};

let request = tonic::Request::new(ImportUserCredentialRequest {
credential: raw_credential,
});
let mut client = get_client(args).await?;
let response = client.import_user_credential(request).await?.into_inner();
println!("{:?}", response);
Expand Down
25 changes: 15 additions & 10 deletions nym-vpnd/src/service/vpn_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,19 +187,26 @@ impl NymVpnService {
*self.shared_vpn_state.lock().unwrap() = state;
}

async fn handle_disconnect(&mut self) -> VpnServiceDisconnectResult {
// To handle the mutable borrow we set the state separate from the sending the stop
// message, including the logical check for the ctrl sender twice.
let is_running = self.vpn_ctrl_sender.is_some();
fn is_running(&self) -> bool {
self.vpn_ctrl_sender
.as_ref()
.map(|s| !s.is_closed())
.unwrap_or(false)
}

if is_running {
async fn handle_disconnect(&mut self) -> VpnServiceDisconnectResult {
// To handle the mutable borrow we set the state separate from the sending the stop message
if self.is_running() {
self.set_shared_state(VpnState::Disconnecting);
} else {
return VpnServiceDisconnectResult::NotRunning;
}

if let Some(ref mut vpn_ctrl_sender) = self.vpn_ctrl_sender {
let _ = vpn_ctrl_sender
vpn_ctrl_sender
.send(nym_vpn_lib::NymVpnCtrlMessage::Stop)
.await;
.await
.ok();
VpnServiceDisconnectResult::Success
} else {
VpnServiceDisconnectResult::NotRunning
Expand All @@ -220,9 +227,7 @@ impl NymVpnService {
&mut self,
credential: Vec<u8>,
) -> VpnServiceImportUserCredentialResult {
// BUG: this is not correct after a connect/disconnect cycle
let is_running = self.vpn_ctrl_sender.is_some();
if is_running {
if self.is_running() {
return VpnServiceImportUserCredentialResult::Fail(
"Can't import credential while VPN is running".to_string(),
);
Expand Down
100 changes: 48 additions & 52 deletions proto/nym/vpn.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@ syntax = "proto3";

package nym.vpn;

message Empty {}

message Gateway {
string id = 1;
Location location = 2;
}

message Location {
string two_letter_country_code = 1;
}

message Node {
oneof node {
Location location = 1;
Gateway gateway = 2;
Empty fastest = 3;
}
}
// message Empty {}

// message Gateway {
// string id = 1;
// Location location = 2;
// }
//
// message Location {
// string two_letter_country_code = 1;
// }
//
// message Node {
// oneof node {
// Location location = 1;
// Gateway gateway = 2;
// Empty fastest = 3;
// }
// }

message ConnectRequest {
// Node entry = 1;
Expand All @@ -36,12 +36,12 @@ message DisconnectResponse {
bool success = 1;
}

enum VpnMode {
MODE_UNSPECIFIED = 0;
MIXNET_FIVE_HOP = 1;
MIXNET_TWO_HOP = 2;
WIREGUARD_TWO_HOP = 3;
}
// enum VpnMode {
// MODE_UNSPECIFIED = 0;
// MIXNET_FIVE_HOP = 1;
// MIXNET_TWO_HOP = 2;
// WIREGUARD_TWO_HOP = 3;
// }

enum ConnectionStatus {
STATUS_UNSPECIFIED = 0;
Expand All @@ -53,48 +53,44 @@ enum ConnectionStatus {
CONNECTION_FAILED = 6;
}

message LocationListResponse {
repeated Location location = 1;
}

message SetVpnModeRequest {
VpnMode mode = 1;
}

message GetVpnModeResponse {
VpnMode mode = 1;
}

message GatewayResponse {
Gateway gateways = 1;
}
// message LocationListResponse {
// repeated Location location = 1;
// }
//
// message SetVpnModeRequest {
// VpnMode mode = 1;
// }
//
// message GetVpnModeResponse {
// VpnMode mode = 1;
// }
//
// message GatewayResponse {
// Gateway gateways = 1;
// }

message StatusRequest {}
message StatusResponse {
ConnectionStatus status = 1;
Error error = 2;
}

message ConnectionStatusUpdate {
ConnectionStatus status = 1;
repeated ConnectionProgress connection_progress = 2;
// optional Error error = 3;
}

message ConnectionProgress {
string message = 1;
}
// message ConnectionStatusUpdate {
// ConnectionStatus status = 1;
// repeated ConnectionProgress connection_progress = 2;
// // optional Error error = 3;
// }
//
// message ConnectionProgress {
// string message = 1;
// }

message Error {
string message = 1;
// ErrorType type = 2; // TOBE implemented
// optional string source = 3;
}

message SetUserCredentialsRequest {
string key = 1;
}

message ImportUserCredentialRequest {
bytes credential = 1;
}
Expand Down

0 comments on commit 3b481ee

Please sign in to comment.