From 558e5bb340f9fc9129cdac69f427e465e21e2e0d Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Fri, 29 Nov 2024 10:31:57 +0800 Subject: [PATCH] Improve tun for linux --- v2rayN/ServiceLib/Handler/CoreHandler.cs | 27 +++++++++++++++++------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/v2rayN/ServiceLib/Handler/CoreHandler.cs b/v2rayN/ServiceLib/Handler/CoreHandler.cs index ec4aa60c..4710dc4c 100644 --- a/v2rayN/ServiceLib/Handler/CoreHandler.cs +++ b/v2rayN/ServiceLib/Handler/CoreHandler.cs @@ -334,7 +334,7 @@ namespace ServiceLib.Handler } try { - proc?.Kill(); + proc?.Kill(true); } catch (Exception) { @@ -375,7 +375,7 @@ namespace ServiceLib.Handler private async Task KillProcessAsLinuxSudo() { - var cmdLine = $"kill -9 {_linuxSudoPid}"; + var cmdLine = $"kill {_linuxSudoPid}"; var shFilePath = await CreateLinuxShellFile(cmdLine, "kill_as_sudo.sh"); Process proc = new() { @@ -392,11 +392,18 @@ namespace ServiceLib.Handler if (_config.TunModeItem.LinuxSudoPwd.IsNotEmpty()) { - var pwd = DesUtils.Decrypt(_config.TunModeItem.LinuxSudoPwd); - await Task.Delay(10); - await proc.StandardInput.WriteLineAsync(pwd); - await Task.Delay(10); - await proc.StandardInput.WriteLineAsync(pwd); + try + { + var pwd = DesUtils.Decrypt(_config.TunModeItem.LinuxSudoPwd); + await Task.Delay(10); + await proc.StandardInput.WriteLineAsync(pwd); + await Task.Delay(10); + await proc.StandardInput.WriteLineAsync(pwd); + } + catch (Exception) + { + // ignored + } } var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(10)); @@ -411,7 +418,11 @@ namespace ServiceLib.Handler File.Delete(shFilePath); var sb = new StringBuilder(); sb.AppendLine("#!/bin/sh"); - if (_config.TunModeItem.LinuxSudoPwd.IsNullOrEmpty()) + if (AppHandler.Instance.IsAdministrator) + { + sb.AppendLine($"{cmdLine}"); + } + else if (_config.TunModeItem.LinuxSudoPwd.IsNullOrEmpty()) { sb.AppendLine($"pkexec {cmdLine}"); }