Net: 8 s health-check timeout, show a dead proxy only after two misses
AmneziaWG 3 tunnels idle for a while need 5+ s for the first handshake, so the 4-5 s checks marked working connections as dead. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -57,6 +57,16 @@ final class NetCore: ObservableObject {
|
||||
|
||||
/// Delays measured by the group test (covers the subscription nodes too).
|
||||
private var measured: [String: Int] = [:]
|
||||
/// Failed checks in a row per proxy. An AmneziaWG 3 tunnel idle for a while can miss one
|
||||
/// check while its handshake warms up, so «нет ответа» is shown only after two misses.
|
||||
private var misses: [String: Int] = [:]
|
||||
|
||||
private func record(_ name: String, _ delay: Int) {
|
||||
if delay > 0 { misses[name] = 0; measured[name] = delay; return }
|
||||
let n = (misses[name] ?? 0) + 1
|
||||
misses[name] = n
|
||||
if n >= 2 { measured[name] = 0 } // first miss: keep the previous result
|
||||
}
|
||||
|
||||
static let clientInfo: [String: (title: String, subnet: String)] = [
|
||||
"saga": ("Сага", "192.168.8.0/24"),
|
||||
@@ -143,6 +153,8 @@ final class NetCore: ObservableObject {
|
||||
awgNames.sort()
|
||||
func lastDelay(_ name: String) -> Int? {
|
||||
if let d = measured[name] { return d }
|
||||
// One miss so far: the core's history already says 0, don't show it yet.
|
||||
if misses[name] == 1 { return nil }
|
||||
if let h = proxies[name]?["history"] as? [[String: Any]], let d = h.last?["delay"] as? Int { return d }
|
||||
return providerDelay[name]
|
||||
}
|
||||
@@ -186,17 +198,17 @@ final class NetCore: ObservableObject {
|
||||
func measure() async {
|
||||
busy = true
|
||||
if let m = (await call(["cmd": "delay", "group": "ai-out"], timeout: 15))?["delays"] as? [String: Int] {
|
||||
measured = m
|
||||
// Members that didn't answer are missing from the map: mark them dead.
|
||||
for e in exits where e.id != "auto" && m[e.id] == nil { measured[e.id] = 0 }
|
||||
for (k, v) in m { record(k, v) }
|
||||
// Members that didn't answer are missing from the map: count a miss.
|
||||
for e in exits where e.id != "auto" && m[e.id] == nil { record(e.id, 0) }
|
||||
}
|
||||
if let m = (await call(["cmd": "delay", "group": "amnezia"], timeout: 15))?["delays"] as? [String: Int] {
|
||||
for (k, v) in m { measured[k] = v }
|
||||
for c in amneziaConns where c.id != "amnezia-auto" && m[c.id] == nil { measured[c.id] = 0 }
|
||||
for (k, v) in m { record(k, v) }
|
||||
for c in amneziaConns where c.id != "amnezia-auto" && m[c.id] == nil { record(c.id, 0) }
|
||||
}
|
||||
for c in clients where c.on {
|
||||
if let m = (await call(["cmd": "delay", "proxy": c.id], timeout: 15))?["delays"] as? [String: Int] {
|
||||
for (k, v) in m { measured[k] = v }
|
||||
for (k, v) in m { record(k, v) }
|
||||
}
|
||||
}
|
||||
await refresh()
|
||||
@@ -216,7 +228,7 @@ final class NetCore: ObservableObject {
|
||||
_ = await call(["cmd": "select", "group": "\(id)-sw", "name": on ? id : off])
|
||||
SoundEngine.shared.play(on ? "pop" : "close")
|
||||
if on, let m = (await call(["cmd": "delay", "proxy": id], timeout: 15))?["delays"] as? [String: Int] {
|
||||
for (k, v) in m { measured[k] = v }
|
||||
for (k, v) in m { record(k, v) }
|
||||
}
|
||||
await refresh()
|
||||
}
|
||||
@@ -267,7 +279,7 @@ final class NetCore: ObservableObject {
|
||||
busy = false
|
||||
if r?["ok"] as? Bool == true {
|
||||
lastError = nil
|
||||
measured.removeValue(forKey: name)
|
||||
measured.removeValue(forKey: name); misses.removeValue(forKey: name)
|
||||
SoundEngine.shared.play(cmd == "hide" ? "close" : "pop")
|
||||
} else {
|
||||
lastError = "Не получилось: \(r?["error"] as? String ?? "помощник не ответил")."
|
||||
|
||||
@@ -229,7 +229,7 @@ def main():
|
||||
# Fetch through the exit: the subscription host is unreachable directly from home
|
||||
# (the pull failed with EOF and the node list went stale).
|
||||
"type": "http", "url": sub, "interval": 3600, "path": "./providers/vless.yaml", "proxy": "exit",
|
||||
"health-check": {"enable": True, "url": TEST_URL, "interval": 300}}
|
||||
"health-check": {"enable": True, "url": TEST_URL, "interval": 300, "timeout": 8000}}
|
||||
if hid["vless"]:
|
||||
# Hidden nodes stay in the subscription, the core just skips them.
|
||||
vless["exclude-filter"] = "^(?:" + "|".join(rx(n) for n in hid["vless"]) + ")$"
|
||||
@@ -241,11 +241,11 @@ def main():
|
||||
vless_file = os.path.join(KEYS, "vless.txt")
|
||||
if os.path.exists(vless_file) and any(l.strip().startswith("vless://") for l in open(vless_file)):
|
||||
extra_prov["vless-keys"] = {"type": "file", "path": "./keys/vless.txt",
|
||||
"health-check": {"enable": True, "url": TEST_URL, "interval": 300}}
|
||||
"health-check": {"enable": True, "url": TEST_URL, "interval": 300, "timeout": 8000}}
|
||||
if hid["vless"]:
|
||||
extra_prov["vless-keys"]["exclude-filter"] = vless["exclude-filter"]
|
||||
awg_prov = {"type": "file", "path": "./keys/amnezia.yaml",
|
||||
"health-check": {"enable": True, "url": TEST_URL, "interval": 300}}
|
||||
"health-check": {"enable": True, "url": TEST_URL, "interval": 300, "timeout": 8000}}
|
||||
|
||||
cfg = {
|
||||
"mixed-port": 7890, "allow-lan": False, "mode": "rule", "log-level": "error", "ipv6": False,
|
||||
@@ -268,18 +268,18 @@ def main():
|
||||
# Back to the pick as soon as it answers again. Checked every 30 s and right after
|
||||
# 3 failed connections.
|
||||
{"name": "exit", "type": "fallback", "proxies": ["ai-out", "auto"],
|
||||
"url": TEST_URL, "interval": 30, "timeout": 4000, "max-failed-times": 3, "lazy": False},
|
||||
"url": TEST_URL, "interval": 30, "timeout": 8000, "max-failed-times": 3, "lazy": False},
|
||||
# What BroV's globe panel switches: "auto", the Amnezia group, or one VLESS node.
|
||||
{"name": "ai-out", "type": "select", "proxies": ["auto", "amnezia"], "use": ["vless-cluster", *extra_prov]},
|
||||
# Fastest alive exit among every Amnezia connection and every VLESS node;
|
||||
# switches only when another one is 100+ ms faster.
|
||||
{"name": "auto", "type": "url-test", "use": ["amnezia-keys", "vless-cluster", *extra_prov],
|
||||
"url": TEST_URL, "interval": 60, "timeout": 4000, "max-failed-times": 3,
|
||||
"url": TEST_URL, "interval": 60, "timeout": 8000, "max-failed-times": 3,
|
||||
"tolerance": 100, "lazy": False},
|
||||
# Amnezia: "amnezia-auto" (fastest connection) or one fixed connection.
|
||||
{"name": "amnezia", "type": "select", "proxies": ["amnezia-auto"], "use": ["amnezia-keys"]},
|
||||
{"name": "amnezia-auto", "type": "url-test", "use": ["amnezia-keys"],
|
||||
"url": TEST_URL, "interval": 60, "timeout": 4000, "max-failed-times": 3,
|
||||
"url": TEST_URL, "interval": 60, "timeout": 8000, "max-failed-times": 3,
|
||||
"tolerance": 100, "lazy": False},
|
||||
] + [
|
||||
# Client networks: off (REJECT) until switched on in BroV.
|
||||
|
||||
@@ -108,13 +108,13 @@ def cmd_select(r):
|
||||
|
||||
|
||||
def cmd_delay(r):
|
||||
test = "url=" + q(TEST_URL) + "&timeout=5000"
|
||||
test = "url=" + q(TEST_URL) + "&timeout=8000" # AmneziaWG 3 handshakes after idle can take 5+ s
|
||||
if r.get("group") in DELAY_GROUPS:
|
||||
return {"ok": True, "delays": api("GET", "/group/%s/delay?%s" % (q(r["group"]), test), timeout=10)}
|
||||
return {"ok": True, "delays": api("GET", "/group/%s/delay?%s" % (q(r["group"]), test), timeout=15)}
|
||||
if r.get("proxy") in CLIENT_TUNNELS:
|
||||
try:
|
||||
d = api("GET", "/providers/proxies/clients/%s/healthcheck?%s" % (q(r["proxy"]), test),
|
||||
timeout=10).get("delay", 0)
|
||||
timeout=15).get("delay", 0)
|
||||
except Exception:
|
||||
d = 0
|
||||
return {"ok": True, "delays": {r["proxy"]: d}}
|
||||
|
||||
Reference in New Issue
Block a user