v2rayN/v2rayN/ServiceLib/Handler/CoreAdminHandler.cs

116 lines
3.4 KiB
C#
Raw Normal View History

2025-04-26 01:50:31 +00:00
using System.Diagnostics;
using System.Text;
using CliWrap;
using CliWrap.Buffered;
2025-04-26 01:50:31 +00:00
namespace ServiceLib.Handler;
public class CoreAdminHandler
{
private static readonly Lazy<CoreAdminHandler> _instance = new(() => new());
public static CoreAdminHandler Instance => _instance.Value;
private Config _config;
private Action<bool, string>? _updateFunc;
private int _linuxSudoPid = -1;
private const string _tag = "CoreAdminHandler";
2025-04-26 01:50:31 +00:00
public async Task Init(Config config, Action<bool, string> updateFunc)
{
if (_config != null)
{
return;
}
_config = config;
_updateFunc = updateFunc;
await Task.CompletedTask;
2025-04-26 01:50:31 +00:00
}
private void UpdateFunc(bool notify, string msg)
{
_updateFunc?.Invoke(notify, msg);
}
public async Task<Process?> RunProcessAsLinuxSudo(string fileName, CoreInfo coreInfo, string configPath)
{
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
2025-04-28 07:16:58 +00:00
Process proc = new()
{
StartInfo = new()
2025-04-26 01:50:31 +00:00
{
2025-04-28 07:16:58 +00:00
FileName = shFilePath,
Arguments = "",
WorkingDirectory = Utils.GetBinConfigPath(),
UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true,
StandardOutputEncoding = Encoding.UTF8,
StandardErrorEncoding = Encoding.UTF8,
}
};
void dataHandler(object sender, DataReceivedEventArgs e)
2025-04-28 07:16:58 +00:00
{
if (e.Data.IsNotEmpty())
2025-04-26 01:50:31 +00:00
{
2025-04-28 07:16:58 +00:00
UpdateFunc(false, e.Data + Environment.NewLine);
2025-04-26 01:50:31 +00:00
}
}
2025-04-26 01:50:31 +00:00
proc.OutputDataReceived += dataHandler;
proc.ErrorDataReceived += dataHandler;
2025-04-28 07:16:58 +00:00
proc.Start();
proc.BeginOutputReadLine();
proc.BeginErrorReadLine();
2025-04-26 01:50:31 +00:00
2025-04-28 07:16:58 +00:00
await Task.Delay(10);
await proc.StandardInput.WriteLineAsync(AppHandler.Instance.LinuxSudoPwd);
await Task.Delay(100);
if (proc 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-04-28 07:16:58 +00:00
_linuxSudoPid = proc.Id;
return proc;
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-04-26 01:50:31 +00:00
var arg = new List<string>() { "-c", $"sudo -S {shFilePath} {_linuxSudoPid}" };
var result = await Cli.Wrap(Global.LinuxBash)
.WithArguments(arg)
.WithStandardInputPipe(PipeSource.FromString(AppHandler.Instance.LinuxSudoPwd))
.ExecuteBufferedAsync();
2025-04-26 01:50:31 +00:00
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
}
}