2025-04-26 01:50:31 +00:00
|
|
|
using System.Text;
|
|
|
|
using CliWrap;
|
2025-07-30 11:52:45 +00:00
|
|
|
using CliWrap.Buffered;
|
2025-04-26 01:50:31 +00:00
|
|
|
|
2025-08-17 08:52:51 +00:00
|
|
|
namespace ServiceLib.Manager;
|
2025-04-26 01:50:31 +00:00
|
|
|
|
2025-08-17 09:31:55 +00:00
|
|
|
public class CoreAdminManager
|
2025-04-26 01:50:31 +00:00
|
|
|
{
|
2025-08-17 09:31:55 +00:00
|
|
|
private static readonly Lazy<CoreAdminManager> _instance = new(() => new());
|
|
|
|
public static CoreAdminManager Instance => _instance.Value;
|
2025-04-26 01:50:31 +00:00
|
|
|
private Config _config;
|
2025-08-29 01:49:30 +00:00
|
|
|
private Func<bool, string, Task>? _updateFunc;
|
2025-04-26 01:50:31 +00:00
|
|
|
private int _linuxSudoPid = -1;
|
2025-07-30 11:52:45 +00:00
|
|
|
private const string _tag = "CoreAdminHandler";
|
2025-04-26 01:50:31 +00:00
|
|
|
|
2025-08-29 01:49:30 +00:00
|
|
|
public async Task Init(Config config, Func<bool, string, Task> updateFunc)
|
2025-04-26 01:50:31 +00:00
|
|
|
{
|
|
|
|
if (_config != null)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_config = config;
|
|
|
|
_updateFunc = updateFunc;
|
2025-07-29 11:42:59 +00:00
|
|
|
|
|
|
|
await Task.CompletedTask;
|
2025-04-26 01:50:31 +00:00
|
|
|
}
|
|
|
|
|
2025-08-29 01:49:30 +00:00
|
|
|
private async Task UpdateFunc(bool notify, string msg)
|
2025-04-26 01:50:31 +00:00
|
|
|
{
|
2025-08-29 01:49:30 +00:00
|
|
|
await _updateFunc?.Invoke(notify, msg);
|
2025-04-26 01:50:31 +00:00
|
|
|
}
|
|
|
|
|
2025-10-01 11:49:28 +00:00
|
|
|
public async Task<ProcessService?> RunProcessAsLinuxSudo(string fileName, CoreInfo coreInfo, string configPath)
|
2025-04-26 01:50:31 +00:00
|
|
|
{
|
2025-07-30 11:52:45 +00:00
|
|
|
StringBuilder sb = new();
|
|
|
|
sb.AppendLine("#!/bin/bash");
|
2025-04-28 07:16:58 +00:00
|
|
|
var cmdLine = $"{fileName.AppendQuotes()} {string.Format(coreInfo.Arguments, Utils.GetBinConfigPath(configPath).AppendQuotes())}";
|
2025-07-30 11:52:45 +00:00
|
|
|
sb.AppendLine($"sudo -S {cmdLine}");
|
|
|
|
var shFilePath = await FileManager.CreateLinuxShellFile("run_as_sudo.sh", sb.ToString(), true);
|
2025-04-26 01:50:31 +00:00
|
|
|
|
2025-10-01 11:49:28 +00:00
|
|
|
var procService = new ProcessService(
|
|
|
|
fileName: shFilePath,
|
|
|
|
arguments: "",
|
|
|
|
workingDirectory: Utils.GetBinConfigPath(),
|
|
|
|
displayLog: true,
|
|
|
|
redirectInput: true,
|
|
|
|
environmentVars: null,
|
|
|
|
updateFunc: _updateFunc
|
|
|
|
);
|
2025-04-26 01:50:31 +00:00
|
|
|
|
2025-10-01 11:49:28 +00:00
|
|
|
await procService.StartAsync(AppManager.Instance.LinuxSudoPwd);
|
2025-07-26 12:55:55 +00:00
|
|
|
|
2025-10-01 11:49:28 +00:00
|
|
|
if (procService is null or { HasExited: true })
|
2025-04-26 01:50:31 +00:00
|
|
|
{
|
2025-04-28 07:16:58 +00:00
|
|
|
throw new Exception(ResUI.FailedToRunCore);
|
2025-04-26 01:50:31 +00:00
|
|
|
}
|
2025-10-01 11:49:28 +00:00
|
|
|
_linuxSudoPid = procService.Id;
|
2025-04-28 07:16:58 +00:00
|
|
|
|
2025-10-01 11:49:28 +00:00
|
|
|
return procService;
|
2025-04-26 01:50:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public async Task KillProcessAsLinuxSudo()
|
|
|
|
{
|
|
|
|
if (_linuxSudoPid < 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2025-07-30 11:52:45 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
var shellFileName = Utils.IsOSX() ? Global.KillAsSudoOSXShellFileName : Global.KillAsSudoLinuxShellFileName;
|
|
|
|
var shFilePath = await FileManager.CreateLinuxShellFile("kill_as_sudo.sh", EmbedUtils.GetEmbedText(shellFileName), true);
|
2025-07-30 12:33:51 +00:00
|
|
|
if (shFilePath.Contains(' '))
|
|
|
|
{
|
|
|
|
shFilePath = shFilePath.AppendQuotes();
|
|
|
|
}
|
2025-07-30 11:52:45 +00:00
|
|
|
var arg = new List<string>() { "-c", $"sudo -S {shFilePath} {_linuxSudoPid}" };
|
|
|
|
var result = await Cli.Wrap(Global.LinuxBash)
|
|
|
|
.WithArguments(arg)
|
2025-08-17 09:31:55 +00:00
|
|
|
.WithStandardInputPipe(PipeSource.FromString(AppManager.Instance.LinuxSudoPwd))
|
2025-07-30 11:52:45 +00:00
|
|
|
.ExecuteBufferedAsync();
|
2025-04-26 01:50:31 +00:00
|
|
|
|
2025-08-29 01:49:30 +00:00
|
|
|
await UpdateFunc(false, result.StandardOutput.ToString());
|
2025-04-26 01:50:31 +00:00
|
|
|
}
|
2025-07-30 11:52:45 +00:00
|
|
|
catch (Exception ex)
|
2025-04-26 01:50:31 +00:00
|
|
|
{
|
2025-07-30 11:52:45 +00:00
|
|
|
Logging.SaveLog(_tag, ex);
|
2025-04-26 01:50:31 +00:00
|
|
|
}
|
|
|
|
|
2025-07-30 11:52:45 +00:00
|
|
|
_linuxSudoPid = -1;
|
2025-04-26 01:50:31 +00:00
|
|
|
}
|
|
|
|
}
|