From 74dc916b0db8ff48a4204555598f176818636bc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Em=C4=ABls?= Date: Wed, 18 Dec 2024 18:01:18 +0100 Subject: [PATCH] Change how daemon is started --- mullvad-daemon/src/lib.rs | 60 ++++++++++++++++++++------------------ mullvad-daemon/src/main.rs | 21 +++++++------ mullvad-jni/src/lib.rs | 46 ++++++++++------------------- 3 files changed, 57 insertions(+), 70 deletions(-) diff --git a/mullvad-daemon/src/lib.rs b/mullvad-daemon/src/lib.rs index e155ba792247..b313e274bc2c 100644 --- a/mullvad-daemon/src/lib.rs +++ b/mullvad-daemon/src/lib.rs @@ -588,30 +588,34 @@ pub struct Daemon { volume_update_tx: mpsc::UnboundedSender<()>, location_handler: GeoIpHandler, } +pub struct DaemonConfig { + pub log_dir: Option, + pub resource_dir: PathBuf, + pub settings_dir: PathBuf, + pub cache_dir: PathBuf, + pub rpc_socket_path: PathBuf, + pub endpoint: ApiEndpoint, + #[cfg(target_os = "android")] + pub android_context: AndroidContext, +} impl Daemon { pub async fn start( - log_dir: Option, - resource_dir: PathBuf, - settings_dir: PathBuf, - cache_dir: PathBuf, - rpc_socket_path: PathBuf, + config: DaemonConfig, daemon_command_channel: DaemonCommandChannel, - endpoint: ApiEndpoint, - #[cfg(target_os = "android")] android_context: AndroidContext, ) -> Result { #[cfg(target_os = "macos")] macos::bump_filehandle_limit(); let command_sender = daemon_command_channel.sender(); let management_interface = - ManagementInterfaceServer::start(command_sender, rpc_socket_path) + ManagementInterfaceServer::start(command_sender, config.rpc_socket_path) .map_err(Error::ManagementInterfaceError)?; let (internal_event_tx, internal_event_rx) = daemon_command_channel.destructure(); #[cfg(target_os = "android")] - let connectivity_listener = ConnectivityListener::new(android_context.clone()) + let connectivity_listener = ConnectivityListener::new(config.android_context.clone()) .inspect_err(|error| { log::error!( "{}", @@ -620,10 +624,10 @@ impl Daemon { }) .map_err(|_| Error::DaemonUnavailable)?; - mullvad_api::proxy::ApiConnectionMode::try_delete_cache(&cache_dir).await; + mullvad_api::proxy::ApiConnectionMode::try_delete_cache(&config.cache_dir).await; let api_runtime = mullvad_api::Runtime::with_cache( - &endpoint, - &cache_dir, + &config.endpoint, + &config.cache_dir, true, #[cfg(target_os = "android")] api::create_bypass_tx(&internal_event_tx), @@ -634,7 +638,7 @@ impl Daemon { let api_availability = api_runtime.availability_handle(); api_availability.suspend(); - let migration_data = migrations::migrate_all(&cache_dir, &settings_dir) + let migration_data = migrations::migrate_all(&config.cache_dir, &config.settings_dir) .await .unwrap_or_else(|error| { log::error!( @@ -645,7 +649,7 @@ impl Daemon { }); let settings_event_listener = management_interface.notifier().clone(); - let mut settings = SettingsPersister::load(&settings_dir).await; + let mut settings = SettingsPersister::load(&config.settings_dir).await; settings.register_change_listener(move |settings| { // Notify management interface server of changes to the settings settings_event_listener.notify_settings(settings.to_owned()); @@ -654,8 +658,8 @@ impl Daemon { let initial_selector_config = SelectorConfig::from_settings(&settings); let relay_selector = RelaySelector::new( initial_selector_config, - resource_dir.join(RELAYS_FILENAME), - cache_dir.join(RELAYS_FILENAME), + config.resource_dir.join(RELAYS_FILENAME), + config.cache_dir.join(RELAYS_FILENAME), ); let settings_relay_selector = relay_selector.clone(); @@ -667,11 +671,11 @@ impl Daemon { }); let (access_mode_handler, access_mode_provider) = api::AccessModeSelector::spawn( - cache_dir.clone(), + config.cache_dir.clone(), relay_selector.clone(), settings.api_access_methods.clone(), #[cfg(feature = "api-override")] - endpoint.clone(), + config.endpoint.clone(), internal_event_tx.to_specialized_sender(), api_runtime.address_cache().clone(), ) @@ -685,7 +689,7 @@ impl Daemon { api_runtime.address_cache().clone(), api_handle.clone(), #[cfg(feature = "api-override")] - endpoint, + config.endpoint.clone(), )); let access_method_handle = access_mode_handler.clone(); @@ -709,7 +713,7 @@ impl Daemon { let (account_manager, data) = device::AccountManager::spawn( api_handle.clone(), - &settings_dir, + &config.settings_dir, settings .tunnel_options .wireguard @@ -721,7 +725,7 @@ impl Daemon { .map_err(Error::LoadAccountManager)?; let account_history = account_history::AccountHistory::new( - &settings_dir, + &config.settings_dir, data.device().map(|device| device.account_number.clone()), ) .await @@ -729,9 +733,9 @@ impl Daemon { let target_state = if settings.auto_connect { log::info!("Automatically connecting since auto-connect is turned on"); - PersistentTargetState::new_secured(&cache_dir).await + PersistentTargetState::new_secured(&config.cache_dir).await } else { - PersistentTargetState::new(&cache_dir).await + PersistentTargetState::new(&config.cache_dir).await }; #[cfg(any(windows, target_os = "android", target_os = "macos"))] @@ -790,14 +794,14 @@ impl Daemon { exclude_paths, }, parameters_generator.clone(), - log_dir, - resource_dir.clone(), + config.log_dir, + config.resource_dir.clone(), internal_event_tx.to_specialized_sender(), offline_state_tx, #[cfg(target_os = "windows")] volume_update_rx, #[cfg(target_os = "android")] - android_context, + config.android_context, #[cfg(target_os = "android")] connectivity_listener.clone(), #[cfg(target_os = "linux")] @@ -819,14 +823,14 @@ impl Daemon { let mut relay_list_updater = RelayListUpdater::spawn( relay_selector.clone(), api_handle.clone(), - &cache_dir, + &config.cache_dir, on_relay_list_update, ); let version_updater_handle = version_check::VersionUpdater::spawn( api_handle.clone(), api_availability.clone(), - cache_dir.clone(), + config.cache_dir.clone(), internal_event_tx.to_specialized_sender(), settings.show_beta_releases, ) diff --git a/mullvad-daemon/src/main.rs b/mullvad-daemon/src/main.rs index 98910f76f864..c2146752c7d5 100644 --- a/mullvad-daemon/src/main.rs +++ b/mullvad-daemon/src/main.rs @@ -1,11 +1,10 @@ use std::{path::PathBuf, thread, time::Duration}; -use mullvad_api::ApiEndpoint; #[cfg(not(windows))] use mullvad_daemon::cleanup_old_rpc_socket; use mullvad_daemon::{ exception_logging, logging, rpc_uniqueness_check, runtime, version, Daemon, - DaemonCommandChannel, + DaemonCommandChannel, DaemonConfig, }; use talpid_types::ErrorExt; @@ -213,16 +212,16 @@ async fn create_daemon(log_dir: Option) -> Result { let cache_dir = mullvad_paths::cache_dir() .map_err(|e| e.display_chain_with_msg("Unable to get cache dir"))?; - let daemon_command_channel = DaemonCommandChannel::new(); - Daemon::start( - log_dir, - resource_dir, - settings_dir, - cache_dir, - rpc_socket_path, - daemon_command_channel, - ApiEndpoint::from_env_vars(), + DaemonConfig { + log_dir, + resource_dir, + settings_dir, + cache_dir, + rpc_socket_path, + endpoint: mullvad_api::ApiEndpoint::from_env_vars(), + }, + DaemonCommandChannel::new(), ) .await .map_err(|e| e.display_chain_with_msg("Unable to initialize daemon")) diff --git a/mullvad-jni/src/lib.rs b/mullvad-jni/src/lib.rs index 2d2561403f00..fd35396fd00f 100644 --- a/mullvad-jni/src/lib.rs +++ b/mullvad-jni/src/lib.rs @@ -15,7 +15,7 @@ use jnix::{ use mullvad_api::ApiEndpoint; use mullvad_daemon::{ cleanup_old_rpc_socket, exception_logging, logging, runtime::new_multi_thread, version, Daemon, - DaemonCommandChannel, DaemonCommandSender, + DaemonCommandChannel, DaemonCommandSender, DaemonConfig, }; use std::{ io, @@ -139,13 +139,6 @@ fn start( start_logging(&files_dir).map_err(Error::InitializeLogging)?; version::log_version(); - #[cfg(feature = "api-override")] - if let Some(api_endpoint) = api_endpoint { - log::debug!("Overriding API endpoint: {api_endpoint:?}"); - if mullvad_api::API.override_init(api_endpoint).is_err() { - log::warn!("Ignoring API settings (already initialized)"); - } - } #[cfg(not(feature = "api-override"))] if api_endpoint.is_some() { log::warn!("api_endpoint will be ignored since 'api-override' is not enabled"); @@ -172,14 +165,18 @@ fn spawn_daemon( let runtime = new_multi_thread().build().map_err(Error::InitTokio)?; - let running_daemon = runtime.block_on(spawn_daemon_inner( - rpc_socket, - files_dir, + let daemon_config = DaemonConfig { + rpc_socket_path: rpc_socket, + log_dir: Some(files_dir.clone()), + resource_dir: files_dir.clone(), + settings_dir: files_dir, cache_dir, - daemon_command_channel, android_context, endpoint, - ))?; + }; + + let running_daemon = + runtime.block_on(spawn_daemon_inner(daemon_config, daemon_command_channel))?; Ok(DaemonContext { runtime, @@ -189,27 +186,14 @@ fn spawn_daemon( } async fn spawn_daemon_inner( - rpc_socket: PathBuf, - files_dir: PathBuf, - cache_dir: PathBuf, + daemon_config: DaemonConfig, daemon_command_channel: DaemonCommandChannel, - android_context: AndroidContext, - endpoint: ApiEndpoint, ) -> Result, Error> { - cleanup_old_rpc_socket(&rpc_socket).await; + cleanup_old_rpc_socket(&daemon_config.rpc_socket_path).await; - let daemon = Daemon::start( - Some(files_dir.clone()), - files_dir.clone(), - files_dir, - cache_dir, - rpc_socket, - daemon_command_channel, - endpoint, - android_context, - ) - .await - .map_err(Error::InitializeDaemon)?; + let daemon = Daemon::start(daemon_config, daemon_command_channel) + .await + .map_err(Error::InitializeDaemon)?; let running_daemon = tokio::spawn(async move { match daemon.run().await {