Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: increase autostartup permissions #1221

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ xz2 = { version = "0.1.7", features = ["static"] } # static bind lzma
zip = "2.2.0"
dirs = "5.0.1"
tauri-plugin-process = "2"
planif = "1.0.0"
whoami = "1.5.2"

[target.'cfg(windows)'.dependencies]
winreg = "0.52.0"
Expand Down
82 changes: 82 additions & 0 deletions src-tauri/src/auto_launcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,15 @@ use anyhow::anyhow;
use auto_launch::{AutoLaunch, AutoLaunchBuilder};
use dunce::canonicalize;
use log::{info, warn};
use planif::{
enums::TaskCreationFlags,
schedule::TaskScheduler,
schedule_builder::{Action, ScheduleBuilder},
settings::{LogonType, PrincipalSettings, RunLevel, Settings},
};
use tauri::utils::platform::current_exe;
use tokio::sync::RwLock;
use whoami::username;

const LOG_TARGET: &str = "tari::universe::auto_launcher";

Expand Down Expand Up @@ -123,6 +130,75 @@ impl AutoLauncher {
Ok(())
}

async fn toggle_windows_admin_auto_launcher(
&self,
config_is_auto_launcher_enabled: bool,
) -> Result<(), anyhow::Error> {
if config_is_auto_launcher_enabled {
info!(target: LOG_TARGET, "Enabling admin auto-launcher");
self.create_task_scheduler_for_admin_startup(true)
.await
.map_err(|e| anyhow!("Failed to create task scheduler for admin startup: {}", e));
}

if !config_is_auto_launcher_enabled {
info!(target: LOG_TARGET, "Disabling admin auto-launcher");
self.create_task_scheduler_for_admin_startup(false)
.await
.map_err(|e| anyhow!("Failed to create task scheduler for admin startup: {}", e));
}

Ok(())
}

pub async fn create_task_scheduler_for_admin_startup(
&self,
is_triggered: bool,
) -> Result<(), Box<dyn std::error::Error>> {
let task_scheduler = TaskScheduler::new()?;
let com_runtime = task_scheduler.get_com();
let schedule_builder = ScheduleBuilder::new(&com_runtime)?;

let app_exe = current_exe()?;
let app_exe = canonicalize(&app_exe)?;

let app_path = app_exe
.as_os_str()
.to_str()
.ok_or(anyhow!("Failed to convert path to string"))?
.to_string();

schedule_builder
.create_logon()
.author("Tari Universe")?
.trigger("startup_trigger", is_triggered)?
.action(Action::new("startup_action", &app_path, "", ""))?
.principal(PrincipalSettings {
display_name: "Tari Universe".to_string(),
group_id: None,
user_id: Some(username()),
id: "Tari universe principal".to_string(),
logon_type: LogonType::InteractiveToken,
run_level: RunLevel::Highest,
})?
.settings(Settings {
stop_if_going_on_batteries: Some(false),
start_when_available: Some(true),
run_only_if_network_available: Some(false),
run_only_if_idle: Some(false),
enabled: Some(true),
disallow_start_if_on_batteries: Some(false),
..Default::default()
})?
.build()?
.register(
"Tari Universe startup",
TaskCreationFlags::CreateOrUpdate as i32,
)?;

Ok(())
}

pub async fn initialize_auto_launcher(
&self,
is_auto_launcher_enabled: bool,
Expand All @@ -146,6 +222,9 @@ impl AutoLauncher {
let auto_launcher = AutoLauncher::build_auto_launcher(app_name, &app_path)?;

AutoLauncher::toggle_auto_launcher(&auto_launcher, is_auto_launcher_enabled)?;
#[cfg(target_os = "windows")]
self.toggle_windows_admin_auto_launcher(is_auto_launcher_enabled)
.await?;
Misieq01 marked this conversation as resolved.
Show resolved Hide resolved

let _ = &self.auto_launcher.write().await.replace(auto_launcher);

Expand All @@ -169,6 +248,9 @@ impl AutoLauncher {
match auto_launcher_ref {
Some(auto_launcher) => {
AutoLauncher::toggle_auto_launcher(auto_launcher, is_auto_launcher_enabled)?;
#[cfg(target_os = "windows")]
self.toggle_windows_admin_auto_launcher(is_auto_launcher_enabled)
.await?;
}
None => {
warn!(target: LOG_TARGET, "Could not get auto-launcher reference");
Expand Down
Loading