From 50c2125346ae0ff11715a66b8b3f81fb783d0588 Mon Sep 17 00:00:00 2001 From: pentaoa Date: Tue, 10 Nov 2026 14:05:38 +0800 Subject: [PATCH] Persist the mode `terc mode` switches, and add `terc core start|stop` The TUI writes the mode it switches to back into config.yaml (main.rs cycle_routing_mode), because a reload or a core restart applies the file's mode. The CLI only PATCHed /configs, so a scripted `terc mode rule` reverted to whatever the file held on the next restart - the failure looks like "the proxy works until the core restarts". `terc core start|stop` gives the same headless control the TUI has, so a core can be brought up (or taken down, which also releases a system proxy pointing at it) without an interactive terminal. --- src/cli.rs | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/src/cli.rs b/src/cli.rs index 75e5fbe..776f73b 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -31,6 +31,7 @@ Usage: terc dns [--type A] [--json] terc explain [--json] terc sysproxy on|off [--yes] [--json] + terc core start|stop [--json] start or stop the managed core terc notify-test [--json] verify desktop notifications terc help @@ -59,6 +60,7 @@ pub fn dispatch(args: &[String]) -> Option { "dns" => dns(rest, json), "explain" => explain(rest, json), "sysproxy" => sysproxy(rest, json), + "core" => core(rest, json), "notify-test" => notify_test(json), "help" | "?" => { print!("{USAGE}"); @@ -307,8 +309,16 @@ fn mode(args: &[String], json: bool) -> i32 { }; match mihomo::switch_routing_mode(&config.controller, normalized) { Ok(()) => { + // A reload or a core restart reads the file's mode, not the running + // one, so record the switch the way the TUI's mode key does. + let persisted = mihomo::persist_mode(&config.controller, &normalized.to_lowercase()); + if let Err(err) = &persisted { + eprintln!("terc: warning: mode {normalized} not saved to config.yaml: {err}"); + } if json { - print_json(&json!({ "ok": true, "mode": normalized })); + print_json( + &json!({ "ok": true, "mode": normalized, "persisted": persisted.is_ok() }), + ); } else { println!("mode {normalized}"); } @@ -321,6 +331,38 @@ fn mode(args: &[String], json: bool) -> i32 { } } +fn core(args: &[String], json: bool) -> i32 { + let Some(action) = args + .iter() + .find(|arg| !arg.starts_with("--")) + .map(|arg| arg.to_lowercase()) + else { + return usage_error("core needs start or stop"); + }; + let Ok(config) = load_config() else { + return 1; + }; + let result = match action.as_str() { + "start" => mihomo::start_core_service(&config.controller), + "stop" => mihomo::stop_core_service(&config.controller), + _ => return usage_error("core takes start or stop"), + }; + match result { + Ok(message) => { + if json { + print_json(&json!({ "ok": true, "action": action, "message": message })); + } else { + println!("{message}"); + } + 0 + } + Err(err) => { + eprintln!("terc: {err}"); + 1 + } + } +} + fn test(args: &[String], json: bool) -> i32 { let Some(group) = args.iter().find(|arg| !arg.starts_with("--")) else { return usage_error("test needs a group name");