Skip to content

Commit

Permalink
Change how daemon is started
Browse files Browse the repository at this point in the history
  • Loading branch information
pinkisemils committed Dec 27, 2024
1 parent e4d44a5 commit 5d51234
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 70 deletions.
60 changes: 32 additions & 28 deletions mullvad-daemon/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -588,30 +588,34 @@ pub struct Daemon {
volume_update_tx: mpsc::UnboundedSender<()>,
location_handler: GeoIpHandler,
}
pub struct DaemonConfig {
pub log_dir: Option<PathBuf>,
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<PathBuf>,
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<Self, Error> {
#[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!(
"{}",
Expand All @@ -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),
Expand All @@ -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!(
Expand All @@ -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());
Expand All @@ -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();
Expand All @@ -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(),
)
Expand All @@ -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();
Expand All @@ -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
Expand All @@ -721,17 +725,17 @@ 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
.map_err(Error::LoadAccountHistory)?;

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"))]
Expand Down Expand Up @@ -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")]
Expand All @@ -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,
)
Expand Down
21 changes: 10 additions & 11 deletions mullvad-daemon/src/main.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -213,16 +212,16 @@ async fn create_daemon(log_dir: Option<PathBuf>) -> Result<Daemon, String> {
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"))
Expand Down
46 changes: 15 additions & 31 deletions mullvad-jni/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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");
Expand All @@ -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,
Expand All @@ -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<tokio::task::JoinHandle<()>, 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 {
Expand Down

0 comments on commit 5d51234

Please sign in to comment.