diff options
| author | Linus Färnstrand <linus@mullvad.net> | 2017-08-25 09:46:01 +0200 |
|---|---|---|
| committer | Linus Färnstrand <linus@mullvad.net> | 2017-08-25 09:46:01 +0200 |
| commit | 1a7830e8fb177d80f2ad9da4a967943160c362eb (patch) | |
| tree | f5484c35ad22f61a0bd7657fab0b8e490c2c4181 | |
| parent | 2363152298aa9671abde9b0fa11a68930cff5e13 (diff) | |
| parent | 154a97178a0eff6e64a6c5584148b1760ed10fef (diff) | |
| download | mullvadvpn-1a7830e8fb177d80f2ad9da4a967943160c362eb.tar.xz mullvadvpn-1a7830e8fb177d80f2ad9da4a967943160c362eb.zip | |
Merge branch 'get-expiry-from-master'
| -rw-r--r-- | mullvad-cli/src/cmds/account.rs | 13 | ||||
| -rw-r--r-- | mullvad-daemon/Cargo.toml | 5 | ||||
| -rw-r--r-- | mullvad-daemon/src/main.rs | 31 | ||||
| -rw-r--r-- | mullvad-daemon/src/management_interface.rs | 21 | ||||
| -rw-r--r-- | mullvad-daemon/src/master.rs | 17 |
5 files changed, 69 insertions, 18 deletions
diff --git a/mullvad-cli/src/cmds/account.rs b/mullvad-cli/src/cmds/account.rs index fcfe9f9901..553458e11a 100644 --- a/mullvad-cli/src/cmds/account.rs +++ b/mullvad-cli/src/cmds/account.rs @@ -1,6 +1,8 @@ use Command; use Result; use clap; + +use mullvad_types::account::{AccountData, AccountToken}; use rpc; pub struct Account; @@ -45,10 +47,13 @@ impl Account { } fn get(&self) -> Result<()> { - let token: Option<String> = rpc::call("get_account", &[] as &[u8; 0])?; - match token { - Some(token) => println!("Mullvad account: {:?}", token), - None => println!("No account configured"), + let account_token: Option<AccountToken> = rpc::call("get_account", &[] as &[u8; 0])?; + if let Some(account_token) = account_token { + let expiry: AccountData = rpc::call("get_account_data", &[&account_token])?; + println!("Mullvad account: {}", account_token); + println!("Expires at : {}", expiry.expiry); + } else { + println!("No account configured"); } Ok(()) } diff --git a/mullvad-daemon/Cargo.toml b/mullvad-daemon/Cargo.toml index 026a044460..a7a20af859 100644 --- a/mullvad-daemon/Cargo.toml +++ b/mullvad-daemon/Cargo.toml @@ -10,10 +10,11 @@ path = "src/main.rs" [dependencies] app_dirs = "1.1" -chrono = "0.4" +chrono = { version = "0.4", features = ["serde"] } clap = "2.25" error-chain = "0.10" fern = "0.4" +futures = "0.1" serde = "1.0" serde_derive = "1.0" log = "0.3" @@ -21,6 +22,8 @@ jsonrpc-core = { git = "https://github.com/paritytech/jsonrpc" } jsonrpc-macros = { git = "https://github.com/paritytech/jsonrpc" } jsonrpc-pubsub = { git = "https://github.com/paritytech/jsonrpc" } jsonrpc-ws-server = { git = "https://github.com/paritytech/jsonrpc" } +jsonrpc-client-core = { git = "https://github.com/mullvad/jsonrpc-client-rs", branch = "rewrite-to-async" } +jsonrpc-client-http = { git = "https://github.com/mullvad/jsonrpc-client-rs", branch = "rewrite-to-async" } uuid = { version = "0.5", features = ["v4"] } lazy_static = "0.2" toml = "0.4" diff --git a/mullvad-daemon/src/main.rs b/mullvad-daemon/src/main.rs index 4b7b0f5745..9bf95f2e51 100644 --- a/mullvad-daemon/src/main.rs +++ b/mullvad-daemon/src/main.rs @@ -6,11 +6,15 @@ extern crate log; #[macro_use] extern crate error_chain; extern crate fern; +extern crate futures; extern crate serde; #[macro_use] extern crate serde_derive; +#[macro_use] +extern crate jsonrpc_client_core; +extern crate jsonrpc_client_http; extern crate jsonrpc_core; extern crate jsonrpc_pubsub; #[macro_use] @@ -26,17 +30,22 @@ extern crate talpid_ipc; mod cli; mod management_interface; +mod master; mod rpc_info; mod settings; mod shutdown; use error_chain::ChainedError; +use futures::{BoxFuture, Future}; +use jsonrpc_client_http::{Error as HttpError, HttpHandle}; use jsonrpc_core::futures::sync::oneshot::Sender as OneshotSender; use management_interface::{ManagementInterfaceServer, TunnelCommand}; +use master::AccountsProxy; +use mullvad_types::account::{AccountData, AccountToken}; use mullvad_types::states::{DaemonState, SecurityState, TargetState}; + use std::io; use std::net::Ipv4Addr; - use std::path::PathBuf; use std::sync::{Arc, Mutex, mpsc}; use std::thread; @@ -151,6 +160,7 @@ struct Daemon { tx: mpsc::Sender<DaemonEvent>, management_interface_broadcaster: management_interface::EventBroadcaster, settings: settings::Settings, + accounts_proxy: AccountsProxy<HttpError, HttpHandle>, firewall: FirewallProxy, remote_endpoint: Option<Endpoint>, @@ -178,9 +188,10 @@ impl Daemon { rx, tx, management_interface_broadcaster, - firewall: FirewallProxy::new() - .chain_err(|| ErrorKind::FirewallError)?, settings: settings::Settings::load().chain_err(|| "Unable to read settings")?, + accounts_proxy: master::create_account_proxy() + .chain_err(|| "Unable to connect to master")?, + firewall: FirewallProxy::new().chain_err(|| ErrorKind::FirewallError)?, remote_endpoint: None, remote_iter: REMOTES.iter().cloned().cycle(), }, @@ -280,6 +291,7 @@ impl Daemon { match event { SetTargetState(state) => self.on_set_target_state(state), GetState(tx) => Ok(self.on_get_state(tx)), + GetAccountData(tx, account_token) => Ok(self.on_get_account_data(tx, account_token)), SetAccount(tx, account_token) => self.on_set_account(tx, account_token), GetAccount(tx) => Ok(self.on_get_account(tx)), } @@ -298,6 +310,17 @@ impl Daemon { Self::oneshot_send(tx, self.last_broadcasted_state, "current state"); } + fn on_get_account_data(&mut self, + tx: OneshotSender<BoxFuture<AccountData, jsonrpc_client_core::Error>>, + account_token: AccountToken) { + let rpc_call = self.accounts_proxy + .get_expiry(account_token) + .map(|expiry| AccountData { expiry }) + .boxed(); + Self::oneshot_send(tx, rpc_call, "account data") + } + + fn on_set_account(&mut self, tx: OneshotSender<()>, account_token: Option<String>) @@ -528,9 +551,11 @@ fn init_logger(log_level: log::LogLevelFilter, log_file: Option<&PathBuf>) -> Re let silenced_crates = [ "jsonrpc_core", "tokio_core", + "tokio_proto", "jsonrpc_ws_server", "ws", "mio", + "hyper", ]; let mut config = fern::Dispatch::new() .format( diff --git a/mullvad-daemon/src/management_interface.rs b/mullvad-daemon/src/management_interface.rs index 9401f4ad5e..62b1994773 100644 --- a/mullvad-daemon/src/management_interface.rs +++ b/mullvad-daemon/src/management_interface.rs @@ -1,5 +1,6 @@ use error_chain; +use jsonrpc_client_core; use jsonrpc_core::{Error, ErrorCode, Metadata}; use jsonrpc_core::futures::{BoxFuture, Future, future, sync}; use jsonrpc_core::futures::sync::oneshot::Sender as OneshotSender; @@ -28,8 +29,8 @@ build_rpc_trait! { /// Fetches and returns metadata about an account. Returns an error on non-existing /// accounts. - #[rpc(name = "get_account_data")] - fn get_account_data(&self, AccountToken) -> Result<AccountData, Error>; + #[rpc(async, name = "get_account_data")] + fn get_account_data(&self, AccountToken) -> BoxFuture<AccountData, Error>; /// Returns available countries. #[rpc(name = "get_countries")] @@ -98,12 +99,13 @@ build_rpc_trait! { /// Enum representing commands coming in on the management interface. -#[derive(Debug)] pub enum TunnelCommand { /// Change target state. SetTargetState(TargetState), /// Request the current state. GetState(OneshotSender<DaemonState>), + /// Request the metadata for an account. + GetAccountData(OneshotSender<BoxFuture<AccountData, jsonrpc_client_core::Error>>, AccountToken), /// Set which account token to use for subsequent connection attempts. SetAccount(OneshotSender<()>, Option<AccountToken>), /// Request the current account token being used. @@ -244,14 +246,13 @@ impl<T: From<TunnelCommand> + 'static + Send> ManagementInterface<T> { impl<T: From<TunnelCommand> + 'static + Send> ManagementInterfaceApi for ManagementInterface<T> { type Metadata = Meta; - fn get_account_data(&self, _account_token: AccountToken) -> Result<AccountData, Error> { + fn get_account_data(&self, account_token: AccountToken) -> BoxFuture<AccountData, Error> { trace!("get_account_data"); - // Just mock implementation, so locally importing temporarily. - use chrono::DateTime; - use chrono::offset::Utc; - use std::str::FromStr; - let expiry: DateTime<Utc> = DateTime::from_str("2018-12-31T16:00:00.000Z").unwrap(); - Ok(AccountData { expiry }) + let (tx, rx) = sync::oneshot::channel(); + self.send_command_to_daemon(TunnelCommand::GetAccountData(tx, account_token)) + .and_then(|_| rx.map_err(|_| Error::internal_error())) + .and_then(|rpc_future| rpc_future.map_err(|_| Error::internal_error())) + .boxed() } fn get_countries(&self) -> Result<HashMap<CountryCode, String>, Error> { diff --git a/mullvad-daemon/src/master.rs b/mullvad-daemon/src/master.rs new file mode 100644 index 0000000000..96bf4778f2 --- /dev/null +++ b/mullvad-daemon/src/master.rs @@ -0,0 +1,17 @@ +use chrono::DateTime; +use chrono::offset::Utc; +use jsonrpc_client_http::{Error as HttpError, HttpCore, HttpHandle}; + +use mullvad_types::account::AccountToken; + +static MASTER_API_URI: &str = "https://api.mullvad.net/rpc/"; + +pub fn create_account_proxy() -> Result<AccountsProxy<HttpError, HttpHandle>, HttpError> { + let core = HttpCore::standalone()?; + let transport = core.handle(MASTER_API_URI)?; + Ok(AccountsProxy::new(transport)) +} + +jsonrpc_client!(pub struct AccountsProxy { + pub fn get_expiry(&mut self, account_token: AccountToken) -> RpcRequest<DateTime<Utc>>; +}); |
