From 93cc0382b2942730fa38078e7f33fc2769ddcb05 Mon Sep 17 00:00:00 2001 From: Vita Chumakova Date: Thu, 24 Oct 2024 03:29:26 +0400 Subject: [PATCH] Use native Process.Kill() because powershell might not always be available --- .../OutlineService/OutlineService.cs | 27 ++++++++++++------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/client/electron/windows/OutlineService/OutlineService/OutlineService.cs b/client/electron/windows/OutlineService/OutlineService/OutlineService.cs index 654dfeec0f..6100fc793e 100755 --- a/client/electron/windows/OutlineService/OutlineService/OutlineService.cs +++ b/client/electron/windows/OutlineService/OutlineService/OutlineService.cs @@ -487,7 +487,7 @@ public void ResetRouting(string proxyIp, int gatewayInterfaceIndex) try { - // This is only necessary when disconecting without network connectivity. + // This is only necessary when disconecting without network connectivity. StartRoutingIpv4(); } catch (Exception) {} @@ -531,7 +531,7 @@ public void ResetRouting(string proxyIp, int gatewayInterfaceIndex) { StopSmartDnsBlock(); eventLog.WriteEntry($"stopped smartdnsblock"); - } + } catch (Exception e) { eventLog.WriteEntry($"failed to stop smartdnsblock: {e.Message}", @@ -606,15 +606,24 @@ private void StartSmartDnsBlock() } } - private void StopSmartDnsBlock() + private static void StopSmartDnsBlock() { - try + var errors = new List(0); + foreach (var process in Process.GetProcessesByName("smartdnsblock")) { - RunCommand("powershell", "stop-process -name smartdnsblock"); + try + { + process.Kill(); + } + catch (Exception e) + { + errors.Add(e.Message); + } } - catch (Exception e) + + if (errors.Count > 0) { - throw new Exception($"could not stop smartdnsblock: {e.Message}"); + throw new Exception($"could not stop smartdnsblock: {string.Join("; ", errors)}"); } } @@ -693,7 +702,7 @@ private void StopRoutingIpv4() { RunCommand(CMD_NETSH, $"interface ipv4 set route {subnet} interface={NetworkInterface.LoopbackInterfaceIndex} metric=0 store=active"); } - } + } } private void StartRoutingIpv6() @@ -938,7 +947,7 @@ private void NetworkAddressChanged(object sender, EventArgs evt) // Only send on actual change, to prevent duplicate notifications (mostly // harmless but can make debugging harder). eventLog.WriteEntry($"network changed but gateway and interface stayed the same"); - return; + return; } else if (gatewayIp == null) {