From be17f91990628120ef6a7271df6c047a392c4a78 Mon Sep 17 00:00:00 2001 From: Muihd <1083092844@qq.com> Date: Thu, 1 Jan 2026 12:35:20 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20Watchdog=20=E7=A1=AE?= =?UTF-8?q?=E4=BF=9D=E8=BF=9B=E7=A8=8B=E9=80=80=E5=87=BA=E6=97=B6=E5=85=B3?= =?UTF-8?q?=E9=97=AD=E7=B3=BB=E7=BB=9F=E4=BB=A3=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BarrageGrab/Program.cs | 98 +++++++++++++++++++++++++++++++++++ BarrageGrab/Utility/WinApi.cs | 14 ++++- 2 files changed, 110 insertions(+), 2 deletions(-) diff --git a/BarrageGrab/Program.cs b/BarrageGrab/Program.cs index 38f8646..a9196cc 100644 --- a/BarrageGrab/Program.cs +++ b/BarrageGrab/Program.cs @@ -4,6 +4,7 @@ using System.IO; using System.Linq; using System.Net; using System.Net.Sockets; +using System.Diagnostics; using System.Runtime.InteropServices; using System.Text; using System.Threading; @@ -20,8 +21,17 @@ namespace BarrageGrab static WinApi.ControlCtrlDelegate controlCtr = ControlCtrlHandle; static Mutex mutex = new Mutex(false, "DyBarrageGrab"); + private const string WatchdogArg = "--watchdog"; + private const string WatchdogPidArg = "--pid"; + static void Main(string[] args) { + if (IsWatchdogMode(args)) + { + RunWatchdog(args); + return; + } + if (!mutex.WaitOne(TimeSpan.Zero, true)) { Console.WriteLine("另一个实例已在运行。"); @@ -31,6 +41,8 @@ namespace BarrageGrab SetTitle("抖音弹幕监听推送"); + StartWatchdog(); + try { Init(); @@ -56,6 +68,92 @@ namespace BarrageGrab WinApi.SetConsoleCtrlHandler(controlCtr, false);//反注册捕获控制台关闭 } + private static void StartWatchdog() + { + try + { + var exePath = Application.ExecutablePath; + if (string.IsNullOrWhiteSpace(exePath) || !File.Exists(exePath)) return; + + var pid = Process.GetCurrentProcess().Id; + var args = string.Format("{0} {1} {2}", WatchdogArg, WatchdogPidArg, pid); + var psi = new ProcessStartInfo(exePath, args) + { + UseShellExecute = false, + CreateNoWindow = true, + WindowStyle = ProcessWindowStyle.Hidden + }; + Process.Start(psi); + } + catch + { + // best-effort only + } + } + + private static bool IsWatchdogMode(string[] args) + { + return args != null && args.Any(a => string.Equals(a, WatchdogArg, StringComparison.OrdinalIgnoreCase)); + } + + private static int? TryGetWatchdogPid(string[] args) + { + if (args == null) return null; + for (int i = 0; i < args.Length; i++) + { + if (!string.Equals(args[i], WatchdogPidArg, StringComparison.OrdinalIgnoreCase)) continue; + if (i + 1 >= args.Length) return null; + int pid; + if (int.TryParse(args[i + 1], out pid)) return pid; + return null; + } + return null; + } + + private static void RunWatchdog(string[] args) + { + try + { + var pid = TryGetWatchdogPid(args); + if (pid == null || pid <= 0) return; + + Process target; + try + { + target = Process.GetProcessById(pid.Value); + } + catch + { + SafeCloseSystemProxy(); + return; + } + + for (; ; ) + { + if (target.HasExited) break; + Thread.Sleep(1000); + } + + SafeCloseSystemProxy(); + } + catch + { + try { SafeCloseSystemProxy(); } catch { } + } + } + + private static void SafeCloseSystemProxy() + { + try + { + if (!AppSetting.Current.UsedProxy) return; + WinApi.CloseSystemProxy(); + } + catch + { + } + } + private static void Init() { AppRuntime.Init(); diff --git a/BarrageGrab/Utility/WinApi.cs b/BarrageGrab/Utility/WinApi.cs index 0cb42f0..ccc4b1d 100644 --- a/BarrageGrab/Utility/WinApi.cs +++ b/BarrageGrab/Utility/WinApi.cs @@ -1,4 +1,6 @@ -using System; +using BarrageGrab.Proxy.ProxyEventArgs; +using Microsoft.Win32; +using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; @@ -64,7 +66,15 @@ namespace BarrageGrab SetConsoleMode(hStdin, mode); } - + /// + /// 关闭系统代理 + /// + public static void CloseSystemProxy() + { + RegistryKey registry = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true); + registry.SetValue("ProxyEnable", 0); + registry.SetValue("ProxyServer", $""); + } public enum CmdShow : int {