v2rayN/v2rayN/ServiceLib/Manager/CoreAdminManager.cs

93 lines
2.9 KiB
C#
Raw Normal View History

2025-04-26 01:50:31 +00:00
using System.Text;
using CliWrap;
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;
private Func<bool, string, Task>? _updateFunc;
2025-04-26 01:50:31 +00:00
private int _linuxSudoPid = -1;
private const string _tag = "CoreAdminHandler";
2025-04-26 01:50:31 +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;
await Task.CompletedTask;
2025-04-26 01:50:31 +00:00
}
private async Task UpdateFunc(bool notify, string msg)
2025-04-26 01:50:31 +00:00
{
await _updateFunc?.Invoke(notify, msg);
2025-04-26 01:50:31 +00:00
}
public async Task<ProcessService?> RunProcessAsLinuxSudo(string fileName, CoreInfo coreInfo, string configPath)
2025-04-26 01:50:31 +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())}";
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
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
await procService.StartAsync(AppManager.Instance.LinuxSudoPwd);
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
}
_linuxSudoPid = procService.Id;
2025-04-28 07:16:58 +00:00
return procService;
2025-04-26 01:50:31 +00:00
}
public async Task KillProcessAsLinuxSudo()
{
if (_linuxSudoPid < 0)
{
return;
}
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();
}
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))
.ExecuteBufferedAsync();
2025-04-26 01:50:31 +00:00
await UpdateFunc(false, result.StandardOutput.ToString());
2025-04-26 01:50:31 +00:00
}
catch (Exception ex)
2025-04-26 01:50:31 +00:00
{
Logging.SaveLog(_tag, ex);
2025-04-26 01:50:31 +00:00
}
_linuxSudoPid = -1;
2025-04-26 01:50:31 +00:00
}
}