diff options
| author | Linus Färnstrand <linus@mullvad.net> | 2017-08-24 15:11:53 +0200 |
|---|---|---|
| committer | Linus Färnstrand <linus@mullvad.net> | 2017-08-24 16:29:18 +0200 |
| commit | 820fe7f2a03374d9c111f28fb1edba6640995f2f (patch) | |
| tree | 17cdc94a7b520a33275f1ef0f657c887d4115b65 | |
| parent | 7f13ec23b33a375e5932eee56547f04f50fe48b5 (diff) | |
| download | mullvadvpn-820fe7f2a03374d9c111f28fb1edba6640995f2f.tar.xz mullvadvpn-820fe7f2a03374d9c111f28fb1edba6640995f2f.zip | |
Use AccountsProxy to fetch data from master
| -rw-r--r-- | mullvad-daemon/Cargo.toml | 1 | ||||
| -rw-r--r-- | mullvad-daemon/src/main.rs | 23 | ||||
| -rw-r--r-- | mullvad-daemon/src/management_interface.rs | 21 |
3 files changed, 34 insertions, 11 deletions
diff --git a/mullvad-daemon/Cargo.toml b/mullvad-daemon/Cargo.toml index e754cb1980..a7a20af859 100644 --- a/mullvad-daemon/Cargo.toml +++ b/mullvad-daemon/Cargo.toml @@ -14,6 +14,7 @@ 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" diff --git a/mullvad-daemon/src/main.rs b/mullvad-daemon/src/main.rs index c186bc58a7..fdbb64735a 100644 --- a/mullvad-daemon/src/main.rs +++ b/mullvad-daemon/src/main.rs @@ -6,6 +6,7 @@ extern crate log; #[macro_use] extern crate error_chain; extern crate fern; +extern crate futures; extern crate serde; #[macro_use] @@ -29,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; @@ -154,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>, @@ -182,6 +189,8 @@ impl Daemon { tx, management_interface_broadcaster, 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(), @@ -282,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)), } @@ -300,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>) 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> { |
