windows: autoProxy

As upstream's prober: the sites apps reach directly (the Clash API's
direct connections here; upstream's inbound access log) are probed
through each exit, direct first, every interval and at once for
proxy-ctl proxy auto learn. One blocked directly but reached through an
exit is routed through it: its exit's local rule-set, which sing-box
reloads. Upstream's judgement, ported and tested: block pages, the walk
over paths and exits, one exit per network first, strikes against exits
that refuse, a new egress discarding what was learned, rechecks once per
TTL. Blocks zapret can fix stay direct.

The config gains a loopback probe listener per exit (with the hop login,
pinned behind the loopback guard) and the learned rules after the user's.
proxy-ctl proxy auto list|probe|learn|forget|clear over new API requests
(autoProxy scope). Slow sites (slowBelowKiBps) are not routed yet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
faumarayandClaude Opus 5.5 committed 2026-10-27 16:31:29 +03:00
1 parent 69e70d6f74
commit 5a89b8d11d
19 files changed
+1640 -10

No files matched your search

+37
View File
@@ -230,3 +230,40 @@ fn no_outbounds_is_an_error() {
let err = render(&Input { cfg: &cfg, state: &State::default(), subscriptions: &BTreeMap::new(), env: &env(&dir), read_file: &no_files, tun: false }).unwrap_err();
assert!(err.0.contains("no proxy outbounds"));
}
#[test]
fn auto_proxy_adds_probe_listeners_and_learned_rules() {
let dir = scratch("autoproxy");
let mut cfg = base();
cfg.proxy.auto_proxy.enable = true;
cfg.proxy.auto_proxy.max_exits = 2;
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":["blocked.example"]}]}"#).unwrap();
let mut e = env(&dir);
e.autoproxy_rules_dir = Some(rules_dir.to_string_lossy().into());
e.windows = false;
let mut state = State::default();
let render_with = |state: &State| {
render(&Input { cfg: &cfg, state, subscriptions: &BTreeMap::new(), env: &e, read_file: &no_files, tun: false }).unwrap()
};
let r = render_with(&state);
// direct, then the first exit: maxExits 2.
assert_eq!(r.probe_exits.iter().map(|x| (x.tag.as_str(), x.port)).collect::<Vec<_>>(), [("direct", 18540), ("nl", 18541)]);
let sb = r.sing_box.unwrap();
let probe_in: Vec<&Value> = sb["inbounds"].as_array().unwrap().iter().filter(|i| i["tag"].as_str().unwrap().starts_with("probe-in-")).collect();
assert_eq!(probe_in.len(), 2);
assert_eq!(probe_in[1]["users"], json!([{ "username": "hop", "password": "hop-pass" }]));
let rules = sb["route"]["rules"].as_array().unwrap();
let guard = rules.iter().position(|r| r["domain"] == json!(["localhost"])).unwrap();
let pin = rules.iter().position(|r| r["inbound"] == json!(["probe-in-1"])).unwrap();
assert!(pin > guard, "the probes stay behind the loopback guard");
assert_eq!(rules.last().unwrap(), &json!({ "rule_set": ["autoproxy-1"], "outbound": "nl" }));
assert!(sb["dns"]["rules"].as_array().unwrap().contains(&json!({ "rule_set": ["autoproxy-1"], "server": "remote" })));
check_sing_box(&sb, &dir);
// all-bypass: everything direct, learned routes too.
state.route_mode = Some(RouteMode::AllBypass);
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()));
}