Skip to content

知了 Cicada · 任何会议和直播的实时字幕、同声传译、AI 会议纪要,Mac、Windows、Linux、Android 免费开源。Live subtitles, interpreting and meeting notes for any call or stream, on Mac, Windows, Linux and Android.

fix(windows): turning the system proxy off while Cicada is checking the AI service still makes it check again once that check ends

wquguru331831ac9f5d
diff --git a/windows/src/Cicada.Core/AICheck.cs b/windows/src/Cicada.Core/AICheck.cs
index a7afd47..dd0c371 100644
--- a/windows/src/Cicada.Core/AICheck.cs
+++ b/windows/src/Cicada.Core/AICheck.cs
@@ -286,6 +286,35 @@ public sealed class AICheck
/// Words the "can't reach the AI service" hint. Read-only on purpose: the app never sets or uses a
/// proxy of its own (HttpClient already goes through the system proxy); it only tells the user which
/// of three situations they are in. The system settings come from the app (the registry on Windows).
+/// When to look at the AI service again after the system proxy changed (the app polls the
+/// settings while this says so). It watches from the first unreachable result until a check
+/// finishes reachable, and keeps watching while a check runs: a change made during that check
+/// counts once it is over (the check may have gone out on the old settings).
+public sealed class ProxyWatch
+{
+ string? seen;
+
+ /// After every change to the check. True while the settings should be polled.
+ public bool Update(bool unreachable, bool running, Func<string> settings)
+ {
+ if (!unreachable && !running) { seen = null; return false; }
+ if (seen is null)
+ {
+ if (!unreachable) return false; // a check from a good state: nothing to watch yet
+ seen = settings();
+ }
+ return true;
+ }
+
+ /// A poll: true when the settings differ from the last ones seen and no check is running.
+ public bool Changed(string now, bool running)
+ {
+ if (seen is null || running || now == seen) return false;
+ seen = now;
+ return true;
+ }
+}
+
public static class ProxyProbe
{
/// Ports common proxy apps listen on (Clash 7890, Clash Verge 7897, SOCKS 1080).
diff --git a/windows/src/Cicada/App.cs b/windows/src/Cicada/App.cs
index cd1c6a7..195c76a 100644
--- a/windows/src/Cicada/App.cs
+++ b/windows/src/Cicada/App.cs
@@ -918,33 +918,32 @@ sealed class Controller
}
/// Turning the system proxy on or off (or pointing it elsewhere) changes no address, so the
- /// watch above misses it. While the service is unreachable the proxy settings are read every
- /// 3 s (three registry values; HttpClient follows them by itself) and a change is checked like
- /// a network change. Not polled otherwise.
+ /// watch above misses it. While the service is unreachable (and through the check that follows)
+ /// the proxy settings are read every 3 s (three registry values; HttpClient follows them by
+ /// itself) and a change is checked like a network change. Not polled otherwise. ProxyWatch decides.
void WatchProxy()
{
- var unreachable = Ling.HasKey && Ling.Health.Failure?.Why == AICheck.Reason.Unreachable;
- if (!unreachable) { proxyPoll?.Stop(); return; }
+ var h = Ling.Health;
+ if (!proxyWatch.Update(Ling.HasKey && h.Failure?.Why == AICheck.Reason.Unreachable, Ling.HasKey && h.IsRunning, WinProxy.Snapshot))
+ {
+ proxyPoll?.Stop();
+ return;
+ }
if (proxyPoll is null)
{
proxyPoll = new DispatcherTimer { Interval = TimeSpan.FromSeconds(3) };
proxyPoll.Tick += (_, _) =>
{
- var now = WinProxy.Snapshot();
- // A check under way may have started on the old settings: the change waits for it.
- if (now == proxySeen || Ling.Health.IsRunning) return;
+ if (!proxyWatch.Changed(WinProxy.Snapshot(), Ling.Health.IsRunning)) return;
Log.Debug("proxy: system proxy settings changed");
- proxySeen = now;
Ling.NetworkChanged();
};
}
- if (proxyPoll.IsEnabled) return;
- proxySeen = WinProxy.Snapshot();
- proxyPoll.Start();
+ if (!proxyPoll.IsEnabled) proxyPoll.Start();
}
DispatcherTimer? proxyPoll;
- string? proxySeen;
+ readonly ProxyWatch proxyWatch = new();
/// The service, its address, models or key changed: forget the old check and check again.
public void AIChanged()
diff --git a/windows/tests/Cicada.Tests/Program.cs b/windows/tests/Cicada.Tests/Program.cs
index 2a76d14..78ba454 100644
--- a/windows/tests/Cicada.Tests/Program.cs
+++ b/windows/tests/Cicada.Tests/Program.cs
@@ -1286,6 +1286,26 @@ static class Tests
Check((DateTime.UtcNow - t).TotalSeconds < 5, "fails soon after the stall");
}
+ /// The system proxy watch: polled from the first unreachable result; a change made while a
+ /// check runs is still acted on once that check ends unreachable; a reachable result stops it.
+ public static void ProxyWatchKeepsAChangeMadeDuringACheck()
+ {
+ var w = new ProxyWatch();
+ var proxy = "1|10.0.2.2:7890|";
+ Check(!w.Update(false, false, () => proxy), "reachable: not polled");
+ Check(!w.Update(false, true, () => proxy), "a check from a good state: not polled");
+ Check(w.Update(true, false, () => proxy), "unreachable: polled");
+ Check(!w.Changed(proxy, false), "same settings");
+ Check(w.Update(false, true, () => proxy), "kept through the next check");
+ proxy = "0||";
+ Check(!w.Changed(proxy, true), "not while the check runs");
+ Check(w.Update(true, false, () => proxy), "the check ended unreachable");
+ Check(w.Changed(proxy, false), "the change made during it now counts");
+ Check(!w.Changed(proxy, false), "once");
+ Check(!w.Update(false, false, () => proxy), "reachable: stopped");
+ Check(!w.Changed("1|x|", false), "stopped means no changes reported");
+ }
+
public static void UpdateSkipsReleasesWithoutAWindowsZip()
{
var list = UpdateChecker.Decode($"[{Rel("v1.6.0", "Cicada-1.6.0-macOS-AppleSilicon.zip")},{Rel("v1.5.0", "Cicada-1.5.0-Windows-x64.zip", "Cicada-1.5.0-Windows-ARM64.zip")}]")!;