windows: refuse loopback through the proxy; Clash API always has a secret

Per-app rules naming an outbound that is not running are left out with a
warning instead of failing the start, and failover renders as sing-box's
own urltest until the service drives it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
faumarayandClaude Opus 5.5 committed 2026-10-26 22:57:06 +03:00
1 parent 3665f7f5a0
commit 130c020151
3 files changed
+24 -14

No files matched your search

+18 -9
View File
@@ -139,15 +139,21 @@ pub(super) fn direct_match(input: &Input) -> RouteMatch {
/// The rules that pick an outbound by program, and the loopback hops' guard: kept in every /// The rules that pick an outbound by program, and the loopback hops' guard: kept in every
/// route mode, ahead of everything that picks by destination, as upstream's pin rules are. /// route mode, ahead of everything that picks by destination, as upstream's pin rules are.
fn always_rules(input: &Input) -> Vec<Value> { fn always_rules(input: &Input, known: &dyn Fn(&str) -> bool, warnings: &mut Vec<String>) -> Vec<Value> {
let cfg = input.cfg; let cfg = input.cfg;
let mut rules = Vec::new(); let mut rules = Vec::new();
if !input.env.direct_processes.is_empty() { if !input.env.direct_processes.is_empty() {
rules.push(json!({ "process_path": input.env.direct_processes, "outbound": "direct" })); rules.push(json!({ "process_path": input.env.direct_processes, "outbound": "direct" }));
} }
if cfg.per_app_routing.enable { if cfg.per_app_routing.enable {
for (i, prof) in cfg.per_app_routing.profiles.iter().enumerate() { for prof in &cfg.per_app_routing.profiles {
let _ = i; if !derived::BUILTIN_TAGS.contains(&prof.outbound.as_str()) && !known(&prof.outbound) {
warnings.push(format!(
"per-app profile '{}': no outbound or group named '{}' is running; its programs follow the routing",
prof.name, prof.outbound
));
continue;
}
let inbound = per_app_inbound(&prof.name); let inbound = per_app_inbound(&prof.name);
rules.push(target(json!({ "inbound": [inbound] }), &prof.outbound)); rules.push(target(json!({ "inbound": [inbound] }), &prof.outbound));
if !prof.process_names.is_empty() { if !prof.process_names.is_empty() {
@@ -187,12 +193,15 @@ pub(super) fn route(input: &Input, hop_ports: &[u16], known: &dyn Fn(&str) -> bo
if onion { if onion {
common.push(json!({ "domain_suffix": ["onion"], "outbound": derived::TOR_OUTBOUND_TAG })); common.push(json!({ "domain_suffix": ["onion"], "outbound": derived::TOR_OUTBOUND_TAG }));
} }
// A client asking for a loopback hop would have sing-box dial the helpers' listeners // Nothing reaches this host's loopback through the proxy: the helpers' listeners, the
// for it, past the routing. // Clash API and whatever else listens there would be a client's for the asking, a LAN
if !hop_ports.is_empty() { // client's too when the listener is exposed. Upstream guards only the hops (its firewall
common.push(json!({ "ip_cidr": LOOPBACK, "port": hop_ports, "action": "reject" })); // keeps the rest from other users); Windows has no such firewall here, so every port. A
} // name that resolves to loopback on its own is not caught: keep the listener on loopback.
common.extend(always_rules(input)); let _ = hop_ports;
common.push(json!({ "ip_cidr": LOOPBACK, "action": "reject" }));
common.push(json!({ "domain": ["localhost"], "domain_suffix": [".localhost"], "action": "reject" }));
common.extend(always_rules(input, known, warnings));
// The custom rules: each outbound's own routing, then routing.rules, at their priority. // The custom rules: each outbound's own routing, then routing.rules, at their priority.
let mut sections = Vec::new(); let mut sections = Vec::new();
@@ -174,10 +174,11 @@ pub(super) fn config(input: &Input, asm: &Assembly, warnings: &mut Vec<String>)
route_obj["auto_detect_interface"] = true.into(); route_obj["auto_detect_interface"] = true.into();
} }
let mut clash = json!({ "external_controller": format!("127.0.0.1:{}", p.sing_box.clash_api_port) }); // Any local program could switch selectors or read the connections otherwise.
if let Some(secret) = &env.clash_secret { let Some(secret) = env.clash_secret.as_ref().filter(|s| !s.is_empty()) else {
clash["secret"] = secret.clone().into(); return Err(RenderError("the Clash API needs a secret".into()));
} };
let clash = json!({ "external_controller": format!("127.0.0.1:{}", p.sing_box.clash_api_port), "secret": secret });
let mut experimental = json!({ "clash_api": clash }); let mut experimental = json!({ "clash_api": clash });
if fake_ip || onion_fake { if fake_ip || onion_fake {
// Fake addresses handed out survive a restart, so apps still holding one keep working. // Fake addresses handed out survive a restart, so apps still holding one keep working.
+1 -1
View File
@@ -34,7 +34,7 @@ fn env(dir: &PathBuf) -> Env {
hop_login: Some(("hop".into(), "hop-pass".into())), hop_login: Some(("hop".into(), "hop-pass".into())),
warp_profiles: warp, warp_profiles: warp,
direct_processes: vec![r"C:\ProgramData\ProxySuite\bin\tor\tor.exe".into()], direct_processes: vec![r"C:\ProgramData\ProxySuite\bin\tor\tor.exe".into()],
groups_watched: true, groups_watched: false,
sidecar_secret: "0b1c2d3e-4f50-6172-8394-a5b6c7d8e9f0".into(), sidecar_secret: "0b1c2d3e-4f50-6172-8394-a5b6c7d8e9f0".into(),
windows: true, windows: true,
..Default::default() ..Default::default()