Improve tun for linux

pull/6190/head^2
2dust 2024-11-29 10:31:57 +08:00
parent 48a9d208e6
commit 558e5bb340
1 changed files with 19 additions and 8 deletions

View File

@ -334,7 +334,7 @@ namespace ServiceLib.Handler
} }
try try
{ {
proc?.Kill(); proc?.Kill(true);
} }
catch (Exception) catch (Exception)
{ {
@ -375,7 +375,7 @@ namespace ServiceLib.Handler
private async Task KillProcessAsLinuxSudo() private async Task KillProcessAsLinuxSudo()
{ {
var cmdLine = $"kill -9 {_linuxSudoPid}"; var cmdLine = $"kill {_linuxSudoPid}";
var shFilePath = await CreateLinuxShellFile(cmdLine, "kill_as_sudo.sh"); var shFilePath = await CreateLinuxShellFile(cmdLine, "kill_as_sudo.sh");
Process proc = new() Process proc = new()
{ {
@ -391,6 +391,8 @@ namespace ServiceLib.Handler
proc.Start(); proc.Start();
if (_config.TunModeItem.LinuxSudoPwd.IsNotEmpty()) if (_config.TunModeItem.LinuxSudoPwd.IsNotEmpty())
{
try
{ {
var pwd = DesUtils.Decrypt(_config.TunModeItem.LinuxSudoPwd); var pwd = DesUtils.Decrypt(_config.TunModeItem.LinuxSudoPwd);
await Task.Delay(10); await Task.Delay(10);
@ -398,6 +400,11 @@ namespace ServiceLib.Handler
await Task.Delay(10); await Task.Delay(10);
await proc.StandardInput.WriteLineAsync(pwd); await proc.StandardInput.WriteLineAsync(pwd);
} }
catch (Exception)
{
// ignored
}
}
var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(10)); var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(10));
await proc.WaitForExitAsync(timeout.Token); await proc.WaitForExitAsync(timeout.Token);
@ -411,7 +418,11 @@ namespace ServiceLib.Handler
File.Delete(shFilePath); File.Delete(shFilePath);
var sb = new StringBuilder(); var sb = new StringBuilder();
sb.AppendLine("#!/bin/sh"); 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}"); sb.AppendLine($"pkexec {cmdLine}");
} }