Refactor the Linux version to not store sudo password

This commit is contained in:
2dust
2025-04-25 15:19:49 +08:00
parent ea42246d1b
commit c6d347d49a
18 changed files with 161 additions and 77 deletions
+2
View File
@@ -40,6 +40,8 @@ public sealed class AppHandler
}
}
public string LinuxSudoPwd { get; set; }
#endregion Property
#region Init
+10 -20
View File
@@ -229,9 +229,7 @@ public class CoreHandler
{
return _config.TunModeItem.EnableTun
&& eCoreType == ECoreType.sing_box
&& (Utils.IsNonWindows())
//&& _config.TunModeItem.LinuxSudoPwd.IsNotEmpty()
;
&& Utils.IsNonWindows();
}
#endregion Private
@@ -288,13 +286,11 @@ public class CoreHandler
}
proc.Start();
if (isNeedSudo && _config.TunModeItem.LinuxSudoPwd.IsNotEmpty())
if (isNeedSudo && AppHandler.Instance.LinuxSudoPwd.IsNotEmpty())
{
var pwd = DesUtils.Decrypt(_config.TunModeItem.LinuxSudoPwd);
await proc.StandardInput.WriteLineAsync();
await Task.Delay(10);
await proc.StandardInput.WriteLineAsync(pwd);
await Task.Delay(10);
await proc.StandardInput.WriteLineAsync(pwd);
await proc.StandardInput.WriteLineAsync(AppHandler.Instance.LinuxSudoPwd);
}
if (isNeedSudo)
_linuxSudoPid = proc.Id;
@@ -333,7 +329,7 @@ public class CoreHandler
proc.StartInfo.FileName = shFilePath;
proc.StartInfo.Arguments = "";
proc.StartInfo.WorkingDirectory = "";
if (_config.TunModeItem.LinuxSudoPwd.IsNotEmpty())
if (AppHandler.Instance.LinuxSudoPwd.IsNotEmpty())
{
proc.StartInfo.StandardInputEncoding = Encoding.UTF8;
proc.StartInfo.RedirectStandardInput = true;
@@ -342,7 +338,7 @@ public class CoreHandler
private async Task KillProcessAsLinuxSudo()
{
var cmdLine = $"kill {_linuxSudoPid}";
var cmdLine = $"pkill -P {_linuxSudoPid} ; kill {_linuxSudoPid}";
var shFilePath = await CreateLinuxShellFile(cmdLine, "kill_as_sudo.sh");
Process proc = new()
{
@@ -357,15 +353,13 @@ public class CoreHandler
};
proc.Start();
if (_config.TunModeItem.LinuxSudoPwd.IsNotEmpty())
if (AppHandler.Instance.LinuxSudoPwd.IsNotEmpty())
{
try
{
var pwd = DesUtils.Decrypt(_config.TunModeItem.LinuxSudoPwd);
await proc.StandardInput.WriteLineAsync();
await Task.Delay(10);
await proc.StandardInput.WriteLineAsync(pwd);
await Task.Delay(10);
await proc.StandardInput.WriteLineAsync(pwd);
await proc.StandardInput.WriteLineAsync(AppHandler.Instance.LinuxSudoPwd);
}
catch (Exception)
{
@@ -375,7 +369,7 @@ public class CoreHandler
var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(10));
await proc.WaitForExitAsync(timeout.Token);
await Task.Delay(3000);
await Task.Delay(1000);
}
private async Task<string> CreateLinuxShellFile(string cmdLine, string fileName)
@@ -389,10 +383,6 @@ public class CoreHandler
{
sb.AppendLine($"{cmdLine}");
}
else if (_config.TunModeItem.LinuxSudoPwd.IsNullOrEmpty())
{
sb.AppendLine($"pkexec {cmdLine}");
}
else
{
sb.AppendLine($"sudo -S {cmdLine}");