feat(cli): 优化 log 子命令并引入 LogLevel 强类型解析,重构 commands.lua 注册表解析与校验逻辑
- 调整 LogLevel 的 FromStr 错误类型为 String,适配 clap 的 value_parser - log 子命令直接绑定 LogLevel 枚举,消除硬编码校验与类型转换 - 规范化日志级别更新与配置读取逻辑 - handler 类型有 Function 改为 String
This commit is contained in:
152
mirror-shim/src/sys/win.rs
Normal file
152
mirror-shim/src/sys/win.rs
Normal file
@@ -0,0 +1,152 @@
|
||||
use std::os::windows::ffi::OsStrExt;
|
||||
use std::{env, mem::size_of, path::Path, ptr::null_mut};
|
||||
|
||||
use std::ffi::{OsStr, OsString};
|
||||
use tracing::error;
|
||||
use windows_sys::Win32::UI::Shell::{SHELLEXECUTEINFOW, ShellExecuteExW};
|
||||
|
||||
use windows_sys::Win32::Foundation::CloseHandle;
|
||||
use windows_sys::{
|
||||
Win32::{
|
||||
Foundation::{FALSE, TRUE},
|
||||
System::{
|
||||
Com::{COINIT_APARTMENTTHREADED, COINIT_DISABLE_OLE1DDE, CoInitializeEx},
|
||||
Console::{
|
||||
CTRL_BREAK_EVENT, CTRL_C_EVENT, CTRL_CLOSE_EVENT, CTRL_LOGOFF_EVENT,
|
||||
CTRL_SHUTDOWN_EVENT, SetConsoleCtrlHandler,
|
||||
},
|
||||
Threading::{GetExitCodeProcess, INFINITE, WaitForSingleObject},
|
||||
},
|
||||
UI::{
|
||||
Shell::{SEE_MASK_NOASYNC, SEE_MASK_NOCLOSEPROCESS},
|
||||
WindowsAndMessaging::SW_NORMAL,
|
||||
},
|
||||
},
|
||||
core::BOOL,
|
||||
};
|
||||
|
||||
pub const EXIT_FAILED_LOAD_SHIM: i32 = 1;
|
||||
pub const EXIT_FAILED_SPAWN_PROG: i32 = 2;
|
||||
pub const EXIT_FAILED_WAIT_PROG: i32 = 3;
|
||||
pub const EXIT_PROG_TERMINATED: i32 = 4;
|
||||
|
||||
pub const ERROR_ELEVATION_REQUIRED: i32 = 740;
|
||||
|
||||
unsafe extern "system" fn console_ctrl_handler(evt: u32) -> BOOL {
|
||||
match evt {
|
||||
CTRL_C_EVENT => TRUE, //eprintln!("ctrl_c handled!"),
|
||||
CTRL_BREAK_EVENT => TRUE, //eprintln!("ctrl_break handled!"),
|
||||
CTRL_CLOSE_EVENT => TRUE, //eprintln!("ctrl_close handled!"),
|
||||
CTRL_LOGOFF_EVENT => TRUE, //eprintln!("ctrl_logoff handled!"),
|
||||
CTRL_SHUTDOWN_EVENT => TRUE, //eprintln!("ctrl_shutdown handled!"),
|
||||
other => {
|
||||
error!("未知的系统事件编号: {},未处理!", other);
|
||||
FALSE
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_console_ctrl_handler() {
|
||||
let res: BOOL = unsafe { SetConsoleCtrlHandler(Some(console_ctrl_handler), TRUE) };
|
||||
if res == FALSE {
|
||||
error!("警告: 注册控制台中断事件处理器失败。");
|
||||
}
|
||||
}
|
||||
|
||||
fn to_wide_null(s: impl AsRef<OsStr>) -> Vec<u16> {
|
||||
s.as_ref().encode_wide().chain(std::iter::once(0)).collect()
|
||||
}
|
||||
|
||||
pub fn execute_elevated(
|
||||
program: &Path,
|
||||
args: &[OsString],
|
||||
env_vars: Option<&std::collections::HashMap<String, OsString>>,
|
||||
) -> i32 {
|
||||
// 若提权启动,在此处将环境变量设置给当前进程(即将弹窗 UAC 的进程,随后会被子进程继承)
|
||||
if let Some(env_map) = env_vars {
|
||||
for (k, v) in env_map {
|
||||
unsafe {
|
||||
env::set_var(k, v);
|
||||
}
|
||||
}
|
||||
}
|
||||
// 2. 将参数列表按 Windows 命令行规则拼装为单个命令行字符串
|
||||
let mut arguments_os = OsString::new();
|
||||
for (i, arg) in args.iter().enumerate() {
|
||||
if i > 0 {
|
||||
arguments_os.push(" ");
|
||||
}
|
||||
let arg_str = arg.to_string_lossy();
|
||||
if arg_str.is_empty() {
|
||||
arguments_os.push("\"\"");
|
||||
} else if !arg_str.contains([' ', '\t', '"']) {
|
||||
arguments_os.push(arg);
|
||||
} else {
|
||||
// 包含空格或引号时进行标准转义包裹
|
||||
arguments_os.push("\"");
|
||||
for c in arg_str.chars() {
|
||||
match c {
|
||||
'\\' => arguments_os.push("\\\\"),
|
||||
'"' => arguments_os.push("\\\""),
|
||||
_ => arguments_os.push(c.to_string()),
|
||||
}
|
||||
}
|
||||
arguments_os.push("\"");
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 准备 Windows 宽字符参数
|
||||
let runas = to_wide_null("runas");
|
||||
let program_wide = to_wide_null(program.as_os_str());
|
||||
let arguments_wide = to_wide_null(&arguments_os);
|
||||
|
||||
let mut info = SHELLEXECUTEINFOW {
|
||||
cbSize: size_of::<SHELLEXECUTEINFOW>() as u32,
|
||||
fMask: SEE_MASK_NOASYNC | SEE_MASK_NOCLOSEPROCESS,
|
||||
hwnd: null_mut(),
|
||||
lpVerb: runas.as_ptr(),
|
||||
lpFile: program_wide.as_ptr(),
|
||||
lpParameters: arguments_wide.as_ptr(),
|
||||
lpDirectory: null_mut(),
|
||||
nShow: SW_NORMAL as i32,
|
||||
hInstApp: null_mut(),
|
||||
lpIDList: null_mut(),
|
||||
lpClass: null_mut(),
|
||||
hkeyClass: null_mut(),
|
||||
dwHotKey: 0,
|
||||
Anonymous: unsafe { std::mem::zeroed() },
|
||||
hProcess: null_mut(),
|
||||
};
|
||||
// let arguments = CString::new(&arguments[..]).unwrap();
|
||||
// let mut info = SHELLEXECUTEINFOA::default();
|
||||
// info.cbSize = size_of::<SHELLEXECUTEINFOA>() as u32;
|
||||
// info.fMask = SEE_MASK_NOASYNC | SEE_MASK_NOCLOSEPROCESS;
|
||||
// info.lpVerb = runas.as_ptr().cast::<u8>();
|
||||
// info.lpFile = program.as_ptr().cast::<u8>();
|
||||
// info.lpParameters = arguments.as_ptr().cast::<u8>();
|
||||
// info.nShow = SW_NORMAL;
|
||||
let res = unsafe {
|
||||
CoInitializeEx(
|
||||
null_mut(),
|
||||
(COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE) as u32,
|
||||
);
|
||||
// ShellExecuteExA(&mut info as *mut _)
|
||||
ShellExecuteExW(&mut info)
|
||||
};
|
||||
if res == FALSE || info.hProcess == null_mut() {
|
||||
return EXIT_FAILED_SPAWN_PROG;
|
||||
}
|
||||
// 5. 等待提权子进程执行结束并获取退出状态码
|
||||
let mut exit_code: u32 = 0;
|
||||
unsafe {
|
||||
WaitForSingleObject(info.hProcess, INFINITE);
|
||||
let ok = GetExitCodeProcess(info.hProcess, &mut exit_code);
|
||||
CloseHandle(info.hProcess); // 释放进程句柄,防止资源泄露
|
||||
|
||||
if ok == FALSE {
|
||||
return EXIT_FAILED_WAIT_PROG;
|
||||
}
|
||||
}
|
||||
|
||||
exit_code as i32
|
||||
}
|
||||
Reference in New Issue
Block a user