From 694e176353d699ffa345e5f400811247056e86ca Mon Sep 17 00:00:00 2001 From: faumaray Date: Tue, 27 Oct 2026 16:36:00 +0300 Subject: [PATCH] windows: autoProxy keeps to its exclusions, and its listing to its scope proxy.autoProxy.exclude is never probed or routed: learn and probe requests for an excluded host are refused, and a site learned whole (example.com) routes no excluded name under it (intranet.example.com): the learned rule skips them. Without the secrets scope, proxy-ctl proxy auto lists the sites routed and counts: the hosts probed, those left to zapret and the sites behind each strike are browsing history, as the logs are. Co-Authored-By: Claude Opus 5.5 --- .../crates/ps-core/src/render/autoproxy.rs | 13 +++++- windows/crates/ps-core/tests/render.rs | 22 ++++++++++ windows/crates/ps-ctl/src/main.rs | 7 ++-- windows/crates/ps-service/src/api.rs | 10 ++++- windows/crates/ps-service/src/autoproxy.rs | 42 +++++++++++++++---- 5 files changed, 82 insertions(+), 12 deletions(-) diff --git a/windows/crates/ps-core/src/render/autoproxy.rs b/windows/crates/ps-core/src/render/autoproxy.rs index c14ffa3..85f4b77 100644 --- a/windows/crates/ps-core/src/render/autoproxy.rs +++ b/windows/crates/ps-core/src/render/autoproxy.rs @@ -83,11 +83,22 @@ pub(super) fn apply( let at = pin_at.min(rules.len()); rules.splice(at..at, pins); + // autoProxy.exclude is never routed by it: a site learned whole (example.com) must not take + // an excluded name under it (intranet.example.com) along. + let exclude: Vec = input.cfg.proxy.auto_proxy.exclude.iter().map(|x| x.trim_start_matches('.').to_ascii_lowercase()).filter(|x| !x.is_empty()).collect(); for e in exits { let (Some(tag), Some(path)) = (&e.rule_set, &e.path) else { continue }; rule_sets.push(json!({ "type": "local", "format": "source", "tag": tag, "path": path })); if mode != RouteMode::AllBypass { - rules.push(json!({ "rule_set": [tag], "outbound": e.tag })); + rules.push(if exclude.is_empty() { + json!({ "rule_set": [tag], "outbound": e.tag }) + } else { + json!({ + "type": "logical", "mode": "and", + "rules": [{ "rule_set": [tag] }, { "domain_suffix": exclude, "invert": true }], + "outbound": e.tag, + }) + }); } dns_rules.push(json!({ "rule_set": [tag], "server": "remote" })); } diff --git a/windows/crates/ps-core/tests/render.rs b/windows/crates/ps-core/tests/render.rs index 4bbd2cf..838751a 100644 --- a/windows/crates/ps-core/tests/render.rs +++ b/windows/crates/ps-core/tests/render.rs @@ -267,3 +267,25 @@ fn auto_proxy_adds_probe_listeners_and_learned_rules() { let sb = render_with(&state).sing_box.unwrap(); assert!(!sb["route"]["rules"].as_array().unwrap().iter().any(|r| r["rule_set"] == json!(["autoproxy-1"]) && r.get("outbound").is_some())); } + +#[test] +fn auto_proxy_never_routes_what_it_excludes() { + let dir = scratch("autoproxy-exclude"); + let mut cfg = base(); + cfg.proxy.auto_proxy.enable = true; + cfg.proxy.auto_proxy.max_exits = 2; + cfg.proxy.auto_proxy.exclude = vec!["intranet.example.com".into()]; + let rules_dir = dir.join("rules"); + std::fs::create_dir_all(&rules_dir).unwrap(); + std::fs::write(rules_dir.join("rs-1.json"), r#"{"version":1,"rules":[{"domain_suffix":["example.com"]}]}"#).unwrap(); + let mut e = env(&dir); + e.autoproxy_rules_dir = Some(rules_dir.to_string_lossy().into()); + e.windows = false; + let r = render(&Input { cfg: &cfg, state: &State::default(), subscriptions: &BTreeMap::new(), env: &e, read_file: &no_files, tun: false }).unwrap(); + let sb = r.sing_box.unwrap(); + assert_eq!( + sb["route"]["rules"].as_array().unwrap().last().unwrap(), + &json!({ "type": "logical", "mode": "and", "rules": [{ "rule_set": ["autoproxy-1"] }, { "domain_suffix": ["intranet.example.com"], "invert": true }], "outbound": "nl" }) + ); + check_sing_box(&sb, &dir); +} diff --git a/windows/crates/ps-ctl/src/main.rs b/windows/crates/ps-ctl/src/main.rs index 9a6fc45..51a9c46 100644 --- a/windows/crates/ps-ctl/src/main.rs +++ b/windows/crates/ps-ctl/src/main.rs @@ -584,14 +584,15 @@ async fn auto(ctl: &Ctl, verb: Option) -> Result<()> { } else { println!(" {:<32} {:<20} PROBED AS", "DOMAIN", "EXIT"); for d in &domains { - println!(" {:<32} {:<20} {}", d["domain"].as_str().unwrap_or(""), d["exit"].as_str().unwrap_or(""), d["host"].as_str().unwrap_or("")); + println!(" {:<32} {:<20} {}", d["domain"].as_str().unwrap_or(""), d["exit"].as_str().unwrap_or(""), d["host"].as_str().unwrap_or("-")); } } for e in r["exits"].as_array().into_iter().flatten().filter(|e| e["bad"] == true) { let by: Vec<&str> = e["badBy"].as_array().into_iter().flatten().filter_map(Value::as_str).collect(); - println!(" bad exit {} (probed last): {}", e["tag"].as_str().unwrap_or(""), by.join(", ")); + let why = if by.is_empty() { format!("{} strike(s)", e["strikes"]) } else { by.join(", ") }; + println!(" bad exit {} (probed last): {why}", e["tag"].as_str().unwrap_or("")); } - let censored = r["censored"].as_array().map_or(0, Vec::len); + let censored = r["censored"].as_array().map_or_else(|| r["censored"].as_u64().unwrap_or(0) as usize, Vec::len); println!(" {} host(s) judged, {censored} left to zapret, {} waiting to be probed", r["hostsJudged"], r["backlog"]); } AutoVerb::Probe { target, exits, via, keep_going } => { diff --git a/windows/crates/ps-service/src/api.rs b/windows/crates/ps-service/src/api.rs index 124af1e..bad562b 100644 --- a/windows/crates/ps-service/src/api.rs +++ b/windows/crates/ps-service/src/api.rs @@ -581,7 +581,7 @@ pub async fn handle(req: Request, peer: &Peer, shared: &Shared) -> Response { Request::AutoList => { let s = shared.lock().await; let exits = crate::autoproxy::exits(&s); - Response::ok(crate::autoproxy::listing(s.paths(), exits.as_ref(), &s.cfg().proxy.auto_proxy)) + Response::ok(crate::autoproxy::listing(s.paths(), exits.as_ref(), &s.cfg().proxy.auto_proxy, has_secrets(s.cfg(), peer))) } Request::AutoProbe { target, exits, via, keep_going } => { @@ -604,6 +604,14 @@ pub async fn handle(req: Request, peer: &Peer, shared: &Shared) -> Response { if !bad.is_empty() { return Response::err(format!("not host names: {}", bad.iter().map(|h| h.as_str()).collect::>().join(", "))); } + let opts = &s.cfg().proxy.auto_proxy; + let skipped: Vec<&String> = hosts.iter().filter(|h| crate::autoproxy::excluded(opts, &h.trim().to_ascii_lowercase())).collect(); + if !skipped.is_empty() { + return Response::err(format!( + "in proxy.autoProxy.exclude, never probed: {}", + skipped.iter().map(|h| h.as_str()).collect::>().join(", ") + )); + } let queued = { let mut r = s.ap.requests.lock().unwrap(); // Bounded: every request is a probe. diff --git a/windows/crates/ps-service/src/autoproxy.rs b/windows/crates/ps-service/src/autoproxy.rs index c15781e..2069bad 100644 --- a/windows/crates/ps-service/src/autoproxy.rs +++ b/windows/crates/ps-service/src/autoproxy.rs @@ -96,6 +96,8 @@ pub fn publish(exits: &[ProbeExit], state: &State) -> Result<()> { pub struct Exits { pub list: Vec, pub login: (String, String), + /// autoProxy.exclude: never probed. + pub opts: Options, } impl Exits { @@ -133,6 +135,9 @@ impl Exits { if !ap::host_ok(domain) { bail!("'{domain}' is not a host name"); } + if excluded(&self.opts, &domain.to_ascii_lowercase()) { + bail!("{domain} is in proxy.autoProxy.exclude: never probed"); + } if !self.up().await { bail!("the probe listeners are not up: is the local proxy running?"); } @@ -248,10 +253,14 @@ pub fn exits(s: &crate::service::Service) -> Option { return None; } let list = s.last.as_ref()?.probe_exits.clone(); - (!list.is_empty()).then(|| Exits { list, login: (s.store.secrets.hop_user.clone(), s.store.secrets.hop_password.clone()) }) + (!list.is_empty()).then(|| Exits { + list, + login: (s.store.secrets.hop_user.clone(), s.store.secrets.hop_password.clone()), + opts: cfg.proxy.auto_proxy.clone(), + }) } -fn excluded(opts: &Options, host: &str) -> bool { +pub fn excluded(opts: &Options, host: &str) -> bool { opts.exclude.iter().any(|x| { let x = x.trim_start_matches('.').to_ascii_lowercase(); host == x || host.ends_with(&format!(".{x}")) @@ -391,7 +400,7 @@ pub async fn run(shared: &Shared, mode: Mode) { let taken: Vec = std::mem::take(&mut *requests); for h in taken { let h = h.trim().to_ascii_lowercase(); - if !ap::host_ok(&h) || !seen.insert(h.clone()) { + if !ap::host_ok(&h) || excluded(&opts, &h) || !seen.insert(h.clone()) { continue; } if let Some(d) = ap::registrable(&h) { @@ -530,16 +539,35 @@ pub async fn sample(shared: Shared) { } } -/// What `proxy-ctl proxy auto` lists. -pub fn listing(paths: &Paths, exits: Option<&Exits>, opts: &Options) -> Value { +/// What `proxy-ctl proxy auto` lists. Without `detail` (the secrets scope) the sites routed +/// and the counts only: the hosts probed, those left to zapret and the sites behind each +/// strike are browsing history, as the logs are. +pub fn listing(paths: &Paths, exits: Option<&Exits>, opts: &Options, detail: bool) -> Value { let s = load(paths); - let domains: Vec = s.domains.iter().map(|(d, r)| json!({ "domain": d, "exit": r.exit, "verdict": r.verdict, "host": r.host, "at": r.at })).collect(); + let domains: Vec = s + .domains + .iter() + .map(|(d, r)| { + let mut v = json!({ "domain": d, "exit": r.exit, "verdict": r.verdict, "at": r.at }); + if detail { + v["host"] = r.host.clone().into(); + } + v + }) + .collect(); let exit_list: Vec = s .exits .iter() - .map(|(t, e)| json!({ "tag": t, "ip": e.ip, "asn": e.asn, "bad": e.bad, "badBy": e.bad_by })) + .map(|(t, e)| { + let mut v = json!({ "tag": t, "ip": e.ip, "asn": e.asn, "bad": e.bad, "strikes": e.strikes.len() }); + if detail { + v["badBy"] = json!(e.bad_by); + } + v + }) .collect(); let censored: Vec<&String> = s.hosts.iter().filter(|(_, h)| h.verdict == "censor").map(|(h, _)| h).collect(); + let censored = if detail { json!(censored) } else { json!(censored.len()) }; json!({ "enabled": opts.enable, "running": exits.is_some(),