windows: autoProxy routes slow sites, and has a page in the app
As upstream's sampler and slow judge: every minute, eleven Clash API snapshots a second apart; a site whose best direct transfer never beat slowBelowKiBps three times is owed an exit, and gets the first, in probe order, that answers it as well as direct does (probe --via); routed slow, it moves on after crawling twice there, and with no exit left it stays direct for a TTL. Here the Clash API is always on, so it works whatever the selection. The app's autoProxy page: on or off, a site probed or learned, the sites routed (forget one or all), the exits (bad ones marked), the exclusions and the slowness threshold. tools/screenshots.sh takes every page. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
694e176353
commit
95ec15e14a
26 files changed
+727
-20
No files matched your search
@@ -35,6 +35,8 @@ pub struct AutoProxy {
|
||||
pub requests: std::sync::Mutex<Vec<String>>,
|
||||
/// Hosts apps dialled directly since the last run, with how often.
|
||||
pub dialled: std::sync::Mutex<BTreeMap<String, u64>>,
|
||||
/// What the transfer sampler saw since the last run: host, exit, crawling, fastest second.
|
||||
pub samples: std::sync::Mutex<Vec<(String, String, bool, u64)>>,
|
||||
pub wake: tokio::sync::Notify,
|
||||
}
|
||||
|
||||
@@ -390,6 +392,65 @@ pub async fn run(shared: &Shared, mode: Mode) {
|
||||
.collect();
|
||||
state.queue(&counts, now, ttl);
|
||||
save_and(&state, false).await;
|
||||
|
||||
// What the sampler saw crawl: each such site gets the next exit, in probe order, that
|
||||
// answers it as well as direct does; none left, it stays direct for a TTL.
|
||||
let samples = std::mem::take(&mut *auto.samples.lock().unwrap());
|
||||
if opts.slow_below_kibps > 0 && !samples.is_empty() {
|
||||
let obs: Vec<ap::Observation> = samples
|
||||
.into_iter()
|
||||
.filter(|(h, ..)| ap::host_ok(h) && !excluded(&opts, h))
|
||||
.filter_map(|(host, exit, slow, peak)| ap::registrable(&host).map(|domain| ap::Observation { domain, host, exit, slow, peak }))
|
||||
.collect();
|
||||
let before = state.domains.clone();
|
||||
state.judge_slow(&obs, now, 3);
|
||||
let (_, _, ordered) = ap::rounds(&exits.tags(), &state);
|
||||
let wanted: Vec<(String, ap::SlowWant)> = state.slow_want.iter().take(20).map(|(d, w)| (d.clone(), w.clone())).collect();
|
||||
for (dom, want) in wanted {
|
||||
let mut tried = want.tried.clone();
|
||||
let was = tried.last().cloned();
|
||||
// It crawled twice on the exit it was routed through.
|
||||
if let Some(was) = &was {
|
||||
state.strike(std::slice::from_ref(was), &dom, "slow", now, ttl);
|
||||
}
|
||||
let mut via = None;
|
||||
for tag in &ordered {
|
||||
if tried.contains(tag) {
|
||||
continue;
|
||||
}
|
||||
tried.push(tag.clone());
|
||||
let host = want.host.as_str();
|
||||
let fetch = |t: String, path: String| async move { exits.fetch(&t, host, &path).await };
|
||||
let p = ap::stand_in(host, tag, &PATHS, fetch).await;
|
||||
if p.verdict == "ok" {
|
||||
via = Some(tag.clone());
|
||||
break;
|
||||
}
|
||||
// Refused what direct gets: by a block page, or by the origin.
|
||||
let rows: Vec<&ap::Row> = p.rows.iter().filter(|r| &r.tag == tag).collect();
|
||||
let why = rows
|
||||
.iter()
|
||||
.find(|r| r.judgement == ap::Judgement::Wall)
|
||||
.map(|r| format!("wall:{}", r.fetch.block))
|
||||
.or_else(|| rows.iter().any(|r| r.judgement == ap::Judgement::Blocked).then(|| "refused".to_owned()));
|
||||
if let Some(why) = why {
|
||||
state.strike(std::slice::from_ref(tag), &dom, &why, now, ttl);
|
||||
}
|
||||
}
|
||||
let how = was.as_deref().map_or("directly".to_owned(), |w| format!("via {w}"));
|
||||
match via {
|
||||
Some(v) => {
|
||||
note(format!("slow {dom}: crawls {how}, routed via {v}")).await;
|
||||
state.route_slow(&dom, &want.host, &v, tried, now);
|
||||
}
|
||||
None => {
|
||||
note(format!("slow {dom}: crawls {how}, and no other exit reaches it; left direct")).await;
|
||||
state.skip_slow(&dom, now, ttl);
|
||||
}
|
||||
}
|
||||
}
|
||||
save_and(&state, state.domains != before).await;
|
||||
}
|
||||
}
|
||||
|
||||
// Probe: the requests, then broken routes, then the backlog.
|
||||
@@ -539,6 +600,36 @@ pub async fn sample(shared: Shared) {
|
||||
}
|
||||
}
|
||||
|
||||
/// Live transfers every minute while slow sites are routed (`slowBelowKiBps`): eleven Clash
|
||||
/// API snapshots a second apart, the transfers that moved enough kept for the next run, as
|
||||
/// upstream's sampler timer does.
|
||||
pub async fn sample_slow(shared: Shared) {
|
||||
loop {
|
||||
tokio::time::sleep(Duration::from_secs(60)).await;
|
||||
let (clash, auto, below) = {
|
||||
let s = shared.lock().await;
|
||||
let cfg = s.cfg();
|
||||
let below = cfg.proxy.auto_proxy.slow_below_kibps;
|
||||
if below == 0 || exits(&s).is_none() {
|
||||
continue;
|
||||
}
|
||||
(crate::clash::Clash::new(cfg.proxy.sing_box.clash_api_port, &s.store.secrets.clash_secret), s.ap.clone(), below)
|
||||
};
|
||||
let mut snapshots = Vec::with_capacity(11);
|
||||
for i in 0..11 {
|
||||
if i > 0 {
|
||||
tokio::time::sleep(Duration::from_secs(1)).await;
|
||||
}
|
||||
snapshots.push(clash.connections().await.unwrap_or(Value::Null));
|
||||
}
|
||||
let seen = ap::sample_transfers(&snapshots, 300 * 1024, u64::from(below) * 1024);
|
||||
let mut samples = auto.samples.lock().unwrap();
|
||||
samples.extend(seen);
|
||||
let over = samples.len().saturating_sub(10_000);
|
||||
samples.drain(..over);
|
||||
}
|
||||
}
|
||||
|
||||
/// 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.
|
||||
@@ -579,6 +670,7 @@ pub fn listing(paths: &Paths, exits: Option<&Exits>, opts: &Options, detail: boo
|
||||
"hostsJudged": s.hosts.len(),
|
||||
"censored": censored,
|
||||
"backlog": s.backlog.len(),
|
||||
"slowWaiting": s.slow_want.len(),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -624,7 +716,7 @@ mod tests {
|
||||
ProbeExit { tag: "nl".into(), port: 2, rule_set: Some("autoproxy-1".into()), path: Some(path.to_string_lossy().into()) },
|
||||
];
|
||||
let mut s = State::default();
|
||||
s.domains.insert("x.example".into(), ap::Route { verdict: "destination".into(), exit: "nl".into(), host: "x.example".into(), path: "/".into(), at: 1 });
|
||||
s.domains.insert("x.example".into(), ap::Route { verdict: "destination".into(), exit: "nl".into(), host: "x.example".into(), path: "/".into(), at: 1, bad: 0, tried: Vec::new() });
|
||||
publish(&exits, &s).unwrap();
|
||||
let v: Value = serde_json::from_slice(&std::fs::read(&path).unwrap()).unwrap();
|
||||
assert_eq!(v, json!({ "version": 1, "rules": [{ "domain_suffix": ["x.example"] }] }));
|
||||
|
||||
@@ -106,6 +106,7 @@ pub async fn run(
|
||||
tokio::spawn(api::serve(server, shared.clone()));
|
||||
tokio::spawn(autoproxy::runner(shared.clone()));
|
||||
tokio::spawn(autoproxy::sample(shared.clone()));
|
||||
tokio::spawn(autoproxy::sample_slow(shared.clone()));
|
||||
// First fetches: what the configuration needs and has not got yet.
|
||||
{
|
||||
let shared = shared.clone();
|
||||
|
||||
Reference in new issue
Block a user