-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
173 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,77 @@ | ||
use anyhow::Result; | ||
use windows::core::w; | ||
use windows::core::PCWSTR; | ||
use windows::core::{w, PCWSTR}; | ||
|
||
use crate::utils::get_exe_path; | ||
use crate::utils::RegKey; | ||
use crate::utils::{get_exe_path, RegKey, ScheduleTask}; | ||
|
||
const TASK_NAME: &str = "WindowSwitcher"; | ||
const HKEY_RUN: PCWSTR = w!("Software\\Microsoft\\Windows\\CurrentVersion\\Run"); | ||
const HKEY_NAME: PCWSTR = w!("Window Switcher"); | ||
|
||
#[derive(Default)] | ||
pub struct Startup { | ||
pub is_enable: bool, | ||
pub task: Option<ScheduleTask>, | ||
pub exe_path: Vec<u16>, | ||
} | ||
|
||
impl Startup { | ||
pub fn init() -> Result<Self> { | ||
let enable = Self::detect()?; | ||
Ok(Self { is_enable: enable }) | ||
pub fn init(is_admin: bool) -> Result<Self> { | ||
let exe_path = get_exe_path(); | ||
let (task, is_enable) = if is_admin { | ||
let exe_path_str = String::from_utf16_lossy(&exe_path); | ||
let task = ScheduleTask::new(TASK_NAME, &exe_path_str); | ||
let is_enable = task.exist()?; | ||
(Some(task), is_enable) | ||
} else { | ||
(None, reg_is_enable(&exe_path)?) | ||
}; | ||
Ok(Self { | ||
is_enable, | ||
exe_path, | ||
task, | ||
}) | ||
} | ||
|
||
pub fn toggle(&mut self) -> Result<()> { | ||
let is_enable = self.is_enable; | ||
if is_enable { | ||
Self::disable()?; | ||
if self.is_enable { | ||
match &self.task { | ||
Some(task) => task.delete()?, | ||
None => reg_disable()?, | ||
} | ||
self.is_enable = false; | ||
} else { | ||
Self::enable()?; | ||
match &self.task { | ||
Some(task) => task.create()?, | ||
None => reg_enable(&self.exe_path)?, | ||
}; | ||
self.is_enable = true; | ||
} | ||
Ok(()) | ||
} | ||
} | ||
|
||
fn detect() -> Result<bool> { | ||
let key = win_run_key()?; | ||
let value = match key.get_value()? { | ||
Some(value) => value, | ||
None => return Ok(false), | ||
}; | ||
let path = get_exe_path(); | ||
Ok(value == path) | ||
} | ||
fn reg_key() -> Result<RegKey> { | ||
RegKey::new_hkcu(HKEY_RUN, HKEY_NAME) | ||
} | ||
|
||
fn enable() -> Result<()> { | ||
let key = win_run_key()?; | ||
let path = get_exe_path(); | ||
let path = unsafe { path.align_to::<u8>().1 }; | ||
key.set_value(path)?; | ||
Ok(()) | ||
} | ||
fn reg_is_enable(exe_path: &[u16]) -> Result<bool> { | ||
let key = reg_key()?; | ||
let value = match key.get_value()? { | ||
Some(value) => value, | ||
None => return Ok(false), | ||
}; | ||
Ok(value == exe_path) | ||
} | ||
|
||
fn disable() -> Result<()> { | ||
let key = win_run_key()?; | ||
key.delete_value()?; | ||
Ok(()) | ||
} | ||
fn reg_enable(exe_path: &[u16]) -> Result<()> { | ||
let key = reg_key()?; | ||
let path = unsafe { exe_path.align_to::<u8>().1 }; | ||
key.set_value(path)?; | ||
Ok(()) | ||
} | ||
|
||
fn win_run_key() -> Result<RegKey> { | ||
RegKey::new_hkcu(HKEY_RUN, HKEY_NAME) | ||
fn reg_disable() -> Result<()> { | ||
let key = reg_key()?; | ||
key.delete_value()?; | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use anyhow::{anyhow, Result}; | ||
use windows::Win32::{ | ||
Foundation::{CloseHandle, HANDLE}, | ||
Security::{GetTokenInformation, TokenElevation, TOKEN_ELEVATION, TOKEN_QUERY}, | ||
System::Threading::{GetCurrentProcess, OpenProcessToken}, | ||
}; | ||
|
||
pub fn is_running_as_admin() -> Result<bool> { | ||
is_running_as_admin_impl() | ||
.map_err(|err| anyhow!("Failed to verify if the program is running as admin, {err}")) | ||
} | ||
|
||
fn is_running_as_admin_impl() -> Result<bool> { | ||
let is_elevated = unsafe { | ||
let mut token_handle: HANDLE = HANDLE(0); | ||
let mut elevation = TOKEN_ELEVATION::default(); | ||
let mut returned_length = 0; | ||
OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &mut token_handle)?; | ||
|
||
let token_information = GetTokenInformation( | ||
token_handle, | ||
TokenElevation, | ||
Some(&mut elevation as *mut _ as *mut _), | ||
std::mem::size_of::<TOKEN_ELEVATION>() as u32, | ||
&mut returned_length, | ||
); | ||
|
||
CloseHandle(token_handle)?; | ||
|
||
token_information?; | ||
elevation.TokenIsElevated != 0 | ||
}; | ||
Ok(is_elevated) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
use std::{os::windows::process::CommandExt, process::Command}; | ||
|
||
use anyhow::{bail, Result}; | ||
use windows::Win32::System::Threading::CREATE_NO_WINDOW; | ||
|
||
#[derive(Debug)] | ||
pub struct ScheduleTask { | ||
name: String, | ||
exe_path: String, | ||
} | ||
|
||
impl ScheduleTask { | ||
pub fn new(name: &str, exe_path: &str) -> Self { | ||
Self { | ||
name: name.to_string(), | ||
exe_path: exe_path.to_string(), | ||
} | ||
} | ||
|
||
pub fn create(&self) -> Result<()> { | ||
let output = Command::new("schtasks") | ||
.creation_flags(CREATE_NO_WINDOW.0) // CREATE_NO_WINDOW flag | ||
.args([ | ||
"/create", | ||
"/tn", | ||
&self.name, | ||
"/tr", | ||
&self.exe_path, | ||
"/sc", | ||
"onlogon", | ||
"/rl", | ||
"highest", | ||
"/it", | ||
"/f", | ||
]) | ||
.output()?; | ||
if !output.status.success() { | ||
bail!( | ||
"Fail to create scheduled task, {}", | ||
String::from_utf8_lossy(&output.stderr) | ||
); | ||
} | ||
Ok(()) | ||
} | ||
|
||
pub fn delete(&self) -> Result<()> { | ||
let output = Command::new("schtasks") | ||
.creation_flags(CREATE_NO_WINDOW.0) // CREATE_NO_WINDOW flag | ||
.args(["/delete", "/tn", &self.name, "/f"]) | ||
.output()?; | ||
if !output.status.success() { | ||
bail!( | ||
"Fail to delete scheduled task, {}", | ||
String::from_utf8_lossy(&output.stderr) | ||
); | ||
} | ||
Ok(()) | ||
} | ||
|
||
pub fn exist(&self) -> Result<bool> { | ||
let output = Command::new("schtasks") | ||
.creation_flags(CREATE_NO_WINDOW.0) // CREATE_NO_WINDOW flag | ||
.args(["/query", "/tn", &self.name]) | ||
.output()?; | ||
if output.status.success() { | ||
Ok(true) | ||
} else { | ||
Ok(false) | ||
} | ||
} | ||
} |