diff options
| author | David Lönnhager <david.l@mullvad.net> | 2020-06-16 15:13:32 +0200 |
|---|---|---|
| committer | David Lönnhager <david.l@mullvad.net> | 2020-08-20 14:41:41 +0200 |
| commit | 8cf02b29a718a7856c80323ee0cf496b9ee24648 (patch) | |
| tree | 16f378f2cc3d3101d01d58435bf54824e57683a6 /mullvad-setup/src | |
| parent | c2e9303cc7aff29df7941fc08df19b8ffcffa48f (diff) | |
| download | mullvadvpn-8cf02b29a718a7856c80323ee0cf496b9ee24648.tar.xz mullvadvpn-8cf02b29a718a7856c80323ee0cf496b9ee24648.zip | |
Use gRPC for management interface in backend and CLI
Diffstat (limited to 'mullvad-setup/src')
| -rw-r--r-- | mullvad-setup/src/main.rs | 53 |
1 files changed, 39 insertions, 14 deletions
diff --git a/mullvad-setup/src/main.rs b/mullvad-setup/src/main.rs index f8fda169b5..6f80be9df8 100644 --- a/mullvad-setup/src/main.rs +++ b/mullvad-setup/src/main.rs @@ -1,18 +1,28 @@ use clap::{crate_authors, crate_description, crate_name, SubCommand}; -use mullvad_ipc_client::{new_standalone_ipc_client, DaemonRpcClient}; -use std::{io, process}; +use parity_tokio_ipc::Endpoint as IpcEndpoint; +use std::process; use talpid_core::firewall::{self, Firewall, FirewallArguments}; use talpid_types::ErrorExt; +use tonic::{ + self, + transport::{Endpoint, Uri}, +}; +use tower::service_fn; + +mod proto { + tonic::include_proto!("mullvad_daemon.management_interface"); +} +use proto::management_service_client::ManagementServiceClient; pub const PRODUCT_VERSION: &str = include_str!(concat!(env!("OUT_DIR"), "/product-version.txt")); #[derive(err_derive::Error, Debug)] pub enum Error { #[error(display = "Failed to connect to daemon")] - DaemonConnect(#[error(source)] io::Error), + DaemonConnect(#[error(source)] tonic::transport::Error), #[error(display = "RPC call failed")] - DaemonRpcError(#[error(source)] mullvad_ipc_client::Error), + DaemonRpcError(#[error(source)] tonic::Status), #[error(display = "This command cannot be run if the daemon is active")] DaemonIsRunning, @@ -21,7 +31,8 @@ pub enum Error { FirewallError(#[error(source)] firewall::Error), } -fn main() { +#[tokio::main] +async fn main() { env_logger::init(); let subcommands = vec![ @@ -44,8 +55,8 @@ fn main() { let matches = app.get_matches(); let result = match matches.subcommand_name().expect("Subcommand has no name") { - "prepare-restart" => prepare_restart(), - "reset-firewall" => reset_firewall(), + "prepare-restart" => prepare_restart().await, + "reset-firewall" => reset_firewall().await, _ => unreachable!("No command matched"), }; @@ -55,14 +66,17 @@ fn main() { } } -fn prepare_restart() -> Result<(), Error> { - let mut rpc = new_rpc_client()?; - rpc.prepare_restart().map_err(Error::DaemonRpcError) +async fn prepare_restart() -> Result<(), Error> { + let mut rpc = new_grpc_client().await?; + rpc.prepare_restart(()) + .await + .map_err(Error::DaemonRpcError)?; + Ok(()) } -fn reset_firewall() -> Result<(), Error> { +async fn reset_firewall() -> Result<(), Error> { // Ensure that the daemon isn't running - if let Ok(_) = new_rpc_client() { + if let Ok(_) = new_grpc_client().await { return Err(Error::DaemonIsRunning); } @@ -75,6 +89,17 @@ fn reset_firewall() -> Result<(), Error> { firewall.reset_policy().map_err(Error::FirewallError) } -fn new_rpc_client() -> Result<DaemonRpcClient, Error> { - new_standalone_ipc_client(&mullvad_paths::get_rpc_socket_path()).map_err(Error::DaemonConnect) +pub async fn new_grpc_client() -> Result<ManagementServiceClient<tonic::transport::Channel>, Error> +{ + let ipc_path = mullvad_paths::get_rpc_socket_path(); + + // The URI will be ignored + let channel = Endpoint::from_static("lttp://[::]:50051") + .connect_with_connector(service_fn(move |_: Uri| { + IpcEndpoint::connect(ipc_path.clone()) + })) + .await + .map_err(Error::DaemonConnect)?; + + Ok(ManagementServiceClient::new(channel)) } |
