Skip to content

Commit 06d33ad

Browse files
committed
fix: Not write ctx.bin in auto mode
1 parent d110112 commit 06d33ad

File tree

2 files changed

+26
-32
lines changed

2 files changed

+26
-32
lines changed

src/loader/args.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ impl Args {
1515

1616
while let Some(arg) = args.next() {
1717
match arg.as_str() {
18-
"--name" => {
18+
"-n" | "--name" => {
1919
if let Some(val) = args.next() {
2020
name = val;
2121
}
2222
}
23-
"--disable" => {
23+
"-d" | "--disable" => {
2424
disable = true;
2525
}
2626
"--auto" => {

src/loader/mod.rs

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,38 +25,31 @@ pub fn is_winserver() -> bool {
2525
!unsafe { VerifyVersionInfoA(&mut osvi, VER_PRODUCT_TYPE, cond_mask).is_ok() }
2626
}
2727

28-
#[allow(dead_code)]
29-
fn alloc_console_if_needed() {
30-
unsafe {
31-
if AttachConsole(ATTACH_PARENT_PROCESS).is_err() {
32-
AllocConsole().expect("[Error]: Failed to allocate console");
33-
}
34-
}
35-
}
36-
37-
#[allow(dead_code)]
38-
fn free_console_if_needed() {
39-
unsafe {
40-
FreeConsole().ok();
41-
}
42-
}
43-
4428
pub fn run() {
45-
let args = args::Args::parse();
46-
#[cfg(not(debug_assertions))]
47-
if !args.auto {
48-
alloc_console_if_needed();
49-
}
50-
let mut ctx = Ctx::default_with_name(&args.name);
51-
if args.disable {
52-
ctx.state = 0; // OFF
53-
}
29+
// 首先设置一下环境
5430
let current_path = std::env::current_exe().unwrap();
5531
let current_dir = current_path.parent().unwrap();
5632
unsafe { std::env::set_var("DEFENDER_RS_PATH", current_dir.to_str().unwrap()) };
57-
let ctx_path = current_dir.join("ctx.bin");
58-
ctx.serialize(&ctx_path);
59-
println!("[Info]: Write context");
33+
// 解析命令行参数
34+
let args = args::Args::parse();
35+
36+
if !args.auto {
37+
// 只在非 auto 模式下分配控制台
38+
unsafe {
39+
if AttachConsole(ATTACH_PARENT_PROCESS).is_err() {
40+
AllocConsole().expect("[Error]: Failed to allocate console");
41+
}
42+
}
43+
44+
// 只在非 auto 模式下写入配置
45+
let mut ctx = Ctx::default_with_name(&args.name);
46+
if args.disable {
47+
ctx.state = 0; // OFF
48+
}
49+
let ctx_path = current_dir.join("ctx.bin");
50+
ctx.serialize(&ctx_path);
51+
println!("[Info]: Write context");
52+
}
6053

6154
// 环境准备, 确保 wscsvc 服务已启动
6255
if let Err(e) = wsc::ensure_wsc() {
@@ -121,8 +114,9 @@ pub fn run() {
121114
CoUninitialize();
122115
}
123116

124-
#[cfg(not(debug_assertions))]
125117
if !args.auto {
126-
free_console_if_needed();
118+
unsafe {
119+
FreeConsole().ok();
120+
}
127121
}
128122
}

0 commit comments

Comments
 (0)