refactor(validators,main): 重构 Lua 配置解析与校验逻辑,提取 LuaValidator 模块; 抽离进程执行与 Win32 提权逻辑
- 重构 Lua 配置解析与校验逻辑 - 将控制台事件监听与 UAC 提权执行逻辑迁移至 win.rs 模块 - 简化 main 函数,提升代码可读性与模块化程度 - 清理废弃的注释代码
This commit is contained in:
254
src/main.rs
254
src/main.rs
@@ -2,7 +2,7 @@ use rshim::Shim;
|
||||
use std::os::windows::ffi::OsStrExt;
|
||||
use std::{env, ffi::CString, mem::size_of, path::Path, process::exit, ptr::null_mut};
|
||||
use tracing_subscriber::{EnvFilter, fmt};
|
||||
|
||||
use rshim::execute_elevated;
|
||||
use std::ffi::{OsStr, OsString};
|
||||
|
||||
use windows_sys::Win32::UI::Shell::{SHELLEXECUTEINFOW, ShellExecuteExW};
|
||||
@@ -107,129 +107,129 @@ fn main() {
|
||||
exit(status.code().unwrap_or(EXIT_PROG_TERMINATED))
|
||||
}
|
||||
// 辅助函数:将任意 OsStr 转换为以 \0 结尾的 UTF-16 宽字符向量 (Vec<u16>)
|
||||
fn to_wide_null(s: impl AsRef<OsStr>) -> Vec<u16> {
|
||||
s.as_ref().encode_wide().chain(std::iter::once(0)).collect()
|
||||
}
|
||||
|
||||
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("\"");
|
||||
}
|
||||
}
|
||||
// let runas = CString::new("runas").unwrap();
|
||||
// let program = CString::new(program.to_str().unwrap()).unwrap();
|
||||
// let mut arguments = String::new();
|
||||
// for arg in args.iter() {
|
||||
// arguments.push(' ');
|
||||
// if arg.len() == 0 {
|
||||
// arguments.push_str("\"\"");
|
||||
// } else if arg.find(&[' ', '\t', '"'][..]).is_none() {
|
||||
// arguments.push_str(&arg);
|
||||
// } else {
|
||||
// arguments.push('"');
|
||||
// for c in arg.chars() {
|
||||
// match c {
|
||||
// '\\' => arguments.push_str("\\\\"),
|
||||
// '"' => arguments.push_str("\\\""),
|
||||
// c => arguments.push(c),
|
||||
// }
|
||||
// }
|
||||
// arguments.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
|
||||
|
||||
// let mut code: u32 = 0;
|
||||
// unsafe {
|
||||
// WaitForSingleObject(info.hProcess, INFINITE);
|
||||
// if GetExitCodeProcess(info.hProcess, &mut code as *mut _) == FALSE {
|
||||
// return EXIT_FAILED_WAIT_PROG;
|
||||
// }
|
||||
// }
|
||||
// code as i32
|
||||
}
|
||||
// fn to_wide_null(s: impl AsRef<OsStr>) -> Vec<u16> {
|
||||
// s.as_ref().encode_wide().chain(std::iter::once(0)).collect()
|
||||
// }
|
||||
//
|
||||
// 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("\"");
|
||||
// }
|
||||
// }
|
||||
// // let runas = CString::new("runas").unwrap();
|
||||
// // let program = CString::new(program.to_str().unwrap()).unwrap();
|
||||
// // let mut arguments = String::new();
|
||||
// // for arg in args.iter() {
|
||||
// // arguments.push(' ');
|
||||
// // if arg.len() == 0 {
|
||||
// // arguments.push_str("\"\"");
|
||||
// // } else if arg.find(&[' ', '\t', '"'][..]).is_none() {
|
||||
// // arguments.push_str(&arg);
|
||||
// // } else {
|
||||
// // arguments.push('"');
|
||||
// // for c in arg.chars() {
|
||||
// // match c {
|
||||
// // '\\' => arguments.push_str("\\\\"),
|
||||
// // '"' => arguments.push_str("\\\""),
|
||||
// // c => arguments.push(c),
|
||||
// // }
|
||||
// // }
|
||||
// // arguments.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
|
||||
//
|
||||
// // let mut code: u32 = 0;
|
||||
// // unsafe {
|
||||
// // WaitForSingleObject(info.hProcess, INFINITE);
|
||||
// // if GetExitCodeProcess(info.hProcess, &mut code as *mut _) == FALSE {
|
||||
// // return EXIT_FAILED_WAIT_PROG;
|
||||
// // }
|
||||
// // }
|
||||
// // code as i32
|
||||
// }
|
||||
|
||||
Reference in New Issue
Block a user