diff options
| author | Linus Färnstrand <linus@mullvad.net> | 2017-07-03 12:49:15 +0200 |
|---|---|---|
| committer | Linus Färnstrand <linus@mullvad.net> | 2017-07-03 12:49:15 +0200 |
| commit | e0ba08fb3ac8198b7184bd73fb82d35fae704e09 (patch) | |
| tree | 384c09ff013f72dfd2958f354357b44558c84663 /mullvad_daemon/src | |
| parent | 39e704f76cd74f1700db2efe7622110d52f82951 (diff) | |
| parent | adb58cdfbd60c9779db9e42aee018595f7eff9da (diff) | |
| download | mullvadvpn-e0ba08fb3ac8198b7184bd73fb82d35fae704e09.tar.xz mullvadvpn-e0ba08fb3ac8198b7184bd73fb82d35fae704e09.zip | |
Merge branch 'support-user-pass-auth'
Diffstat (limited to 'mullvad_daemon/src')
| -rw-r--r-- | mullvad_daemon/src/main.rs | 23 | ||||
| -rw-r--r-- | mullvad_daemon/src/management_interface.rs | 29 |
2 files changed, 45 insertions, 7 deletions
diff --git a/mullvad_daemon/src/main.rs b/mullvad_daemon/src/main.rs index 62c09608a5..682d6b1290 100644 --- a/mullvad_daemon/src/main.rs +++ b/mullvad_daemon/src/main.rs @@ -50,6 +50,10 @@ error_chain!{ description("Error in the management interface") display("Management interface error: {}", msg) } + InvalidSettings(msg: &'static str) { + description("Invalid settings") + display("Invalid Settings: {}", msg) + } } } @@ -118,6 +122,8 @@ struct Daemon { // Just for testing. A cyclic iterator iterating over the hardcoded remotes, // picking a new one for each retry. remote_iter: std::iter::Cycle<std::iter::Cloned<std::slice::Iter<'static, Endpoint>>>, + // The current account token for now. Should be moved into the settings later. + account_token: Option<String>, } impl Daemon { @@ -135,6 +141,7 @@ impl Daemon { tunnel_close_handle: None, management_interface_broadcaster, remote_iter: REMOTES.iter().cloned().cycle(), + account_token: None, }, ) } @@ -226,6 +233,12 @@ impl Daemon { warn!("Unable to send current state to management interface client",); } } + TunnelCommand::SetAccount(account_token) => self.account_token = account_token, + TunnelCommand::GetAccount(tx) => { + if let Err(_) = tx.send(self.account_token.clone()) { + warn!("Unable to send current account to management interface client"); + } + } } Ok(()) } @@ -306,7 +319,11 @@ impl Daemon { ErrorKind::InvalidState ); let remote = self.remote_iter.next().unwrap(); - let tunnel_monitor = self.spawn_tunnel_monitor(remote)?; + let account_token = self.account_token + .as_ref() + .ok_or(ErrorKind::InvalidSettings("No account token"))? + .clone(); + let tunnel_monitor = self.spawn_tunnel_monitor(remote, &account_token)?; self.tunnel_close_handle = Some(tunnel_monitor.close_handle()); self.spawn_tunnel_monitor_wait_thread(tunnel_monitor); @@ -314,13 +331,13 @@ impl Daemon { Ok(()) } - fn spawn_tunnel_monitor(&self, remote: Endpoint) -> Result<TunnelMonitor> { + fn spawn_tunnel_monitor(&self, remote: Endpoint, account_token: &str) -> Result<TunnelMonitor> { // Must wrap the channel in a Mutex because TunnelMonitor forces the closure to be Sync let event_tx = Arc::new(Mutex::new(self.tx.clone())); let on_tunnel_event = move |event| { let _ = event_tx.lock().unwrap().send(DaemonEvent::TunnelEvent(event)); }; - TunnelMonitor::new(remote, on_tunnel_event) + TunnelMonitor::new(remote, account_token, on_tunnel_event) .chain_err(|| ErrorKind::TunnelError("Unable to start tunnel monitor")) } diff --git a/mullvad_daemon/src/management_interface.rs b/mullvad_daemon/src/management_interface.rs index fccdae2414..4b5a104d5e 100644 --- a/mullvad_daemon/src/management_interface.rs +++ b/mullvad_daemon/src/management_interface.rs @@ -48,9 +48,13 @@ build_rpc_trait! { #[rpc(name = "get_countries")] fn get_countries(&self) -> Result<HashMap<CountryCode, String>, Error>; - /// Set which account to connect with + /// Set which account to connect with. #[rpc(name = "set_account")] - fn set_account(&self, AccountToken) -> Result<(), Error>; + fn set_account(&self, Option<AccountToken>) -> Result<(), Error>; + + /// Get which account is configured. + #[rpc(async, name = "get_account")] + fn get_account(&self) -> BoxFuture<Option<AccountToken>, Error>; /// Set which country to connect to #[rpc(name = "set_country")] @@ -113,6 +117,10 @@ pub enum TunnelCommand { SetTargetState(TargetState), /// Request the current state. GetState(sync::oneshot::Sender<SecurityState>), + /// Set which account token to use for subsequent connection attempts. + SetAccount(Option<AccountToken>), + /// Request the current account token being used. + GetAccount(sync::oneshot::Sender<Option<AccountToken>>), } #[derive(Default)] @@ -252,9 +260,22 @@ impl<T: From<TunnelCommand> + 'static + Send> ManagementInterfaceApi for Managem Ok(HashMap::new()) } - fn set_account(&self, _account_token: AccountToken) -> Result<(), Error> { + fn set_account(&self, account_token: Option<AccountToken>) -> Result<(), Error> { trace!("set_account"); - Ok(()) + self.tx + .lock() + .unwrap() + .send(TunnelCommand::SetAccount(account_token)) + .map_err(|_| Error::internal_error()) + } + + fn get_account(&self) -> BoxFuture<Option<AccountToken>, Error> { + trace!("get_account"); + let (tx, rx) = sync::oneshot::channel(); + match self.tx.lock().unwrap().send(TunnelCommand::GetAccount(tx)) { + Ok(()) => rx.map_err(|_| Error::internal_error()).boxed(), + Err(_) => future::err(Error::internal_error()).boxed(), + } } fn set_country(&self, _country_code: CountryCode) -> Result<(), Error> { |
