From a5d99b1eb51d202c146c403751a9c36cdb3282db Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Tue, 26 Nov 2024 15:11:02 +0800 Subject: [PATCH] Improved Tun mode function in Linux version --- v2rayN/ServiceLib/Handler/CoreHandler.cs | 31 +++++++++++------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/v2rayN/ServiceLib/Handler/CoreHandler.cs b/v2rayN/ServiceLib/Handler/CoreHandler.cs index ef0a8260..e2b7b3b8 100644 --- a/v2rayN/ServiceLib/Handler/CoreHandler.cs +++ b/v2rayN/ServiceLib/Handler/CoreHandler.cs @@ -361,31 +361,28 @@ namespace ServiceLib.Handler { var cmdLine = $"{fileName.AppendQuotes()} {string.Format(coreInfo.Arguments, Utils.GetConfigPath(configPath).AppendQuotes())}"; - //Prefer shell scripts + //Shell scripts var shFilePath = Utils.GetBinPath("run_as_root.sh"); File.Delete(shFilePath); var sb = new StringBuilder(); sb.AppendLine("#!/bin/sh"); - sb.AppendLine(cmdLine); - await File.WriteAllTextAsync(shFilePath, sb.ToString()); - await Utils.SetLinuxChmod(shFilePath); - - //Replace command - var args = File.Exists(shFilePath) ? shFilePath : cmdLine; - if (_config.TunModeItem.LinuxSudoPwd.IsNotEmpty()) + if (_config.TunModeItem.LinuxSudoPwd.IsNullOrEmpty()) { - proc.StartInfo.FileName = $"/bin/sudo"; - proc.StartInfo.Arguments = $"-S {args}"; + sb.AppendLine($"pkexec {cmdLine}"); } else { - proc.StartInfo.FileName = $"/bin/pkexec"; - proc.StartInfo.Arguments = $"{args}"; + sb.AppendLine($"sudo -S {cmdLine}"); + proc.StartInfo.StandardInputEncoding = Encoding.UTF8; + proc.StartInfo.RedirectStandardInput = true; } - proc.StartInfo.WorkingDirectory = null; - proc.StartInfo.StandardInputEncoding = Encoding.UTF8; - proc.StartInfo.RedirectStandardInput = true; - Logging.SaveLog(proc.StartInfo.Arguments); + await File.WriteAllTextAsync(shFilePath, sb.ToString()); + await Utils.SetLinuxChmod(shFilePath); + + proc.StartInfo.FileName = shFilePath; + proc.StartInfo.Arguments = ""; + proc.StartInfo.WorkingDirectory = ""; + Logging.SaveLog(shFilePath); } private async Task KillProcess(Process? proc) @@ -418,4 +415,4 @@ namespace ServiceLib.Handler #endregion Process } -} +} \ No newline at end of file