Improved Tun mode function in Linux version

pull/6168/head
2dust 2024-11-26 15:11:02 +08:00
parent 800d193acb
commit a5d99b1eb5
1 changed files with 14 additions and 17 deletions

View File

@ -361,31 +361,28 @@ namespace ServiceLib.Handler
{ {
var cmdLine = $"{fileName.AppendQuotes()} {string.Format(coreInfo.Arguments, Utils.GetConfigPath(configPath).AppendQuotes())}"; 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"); var shFilePath = Utils.GetBinPath("run_as_root.sh");
File.Delete(shFilePath); File.Delete(shFilePath);
var sb = new StringBuilder(); var sb = new StringBuilder();
sb.AppendLine("#!/bin/sh"); sb.AppendLine("#!/bin/sh");
sb.AppendLine(cmdLine); if (_config.TunModeItem.LinuxSudoPwd.IsNullOrEmpty())
await File.WriteAllTextAsync(shFilePath, sb.ToString());
await Utils.SetLinuxChmod(shFilePath);
//Replace command
var args = File.Exists(shFilePath) ? shFilePath : cmdLine;
if (_config.TunModeItem.LinuxSudoPwd.IsNotEmpty())
{ {
proc.StartInfo.FileName = $"/bin/sudo"; sb.AppendLine($"pkexec {cmdLine}");
proc.StartInfo.Arguments = $"-S {args}";
} }
else else
{ {
proc.StartInfo.FileName = $"/bin/pkexec"; sb.AppendLine($"sudo -S {cmdLine}");
proc.StartInfo.Arguments = $"{args}"; proc.StartInfo.StandardInputEncoding = Encoding.UTF8;
proc.StartInfo.RedirectStandardInput = true;
} }
proc.StartInfo.WorkingDirectory = null; await File.WriteAllTextAsync(shFilePath, sb.ToString());
proc.StartInfo.StandardInputEncoding = Encoding.UTF8; await Utils.SetLinuxChmod(shFilePath);
proc.StartInfo.RedirectStandardInput = true;
Logging.SaveLog(proc.StartInfo.Arguments); proc.StartInfo.FileName = shFilePath;
proc.StartInfo.Arguments = "";
proc.StartInfo.WorkingDirectory = "";
Logging.SaveLog(shFilePath);
} }
private async Task KillProcess(Process? proc) private async Task KillProcess(Process? proc)
@ -418,4 +415,4 @@ namespace ServiceLib.Handler
#endregion Process #endregion Process
} }
} }