v2rayN/v2rayN/ServiceLib/Manager/CoreManager.cs

293 lines
9.6 KiB
C#
Raw Normal View History

2025-08-17 08:52:51 +00:00
namespace ServiceLib.Manager;
/// <summary>
/// Core process processing class
/// </summary>
2025-08-17 09:31:55 +00:00
public class CoreManager
2019-10-11 06:15:20 +00:00
{
2025-08-17 09:31:55 +00:00
private static readonly Lazy<CoreManager> _instance = new(() => new());
public static CoreManager Instance => _instance.Value;
private Config _config;
private WindowsJob? _processJob;
private ProcessService? _processService;
private ProcessService? _processPreService;
2025-04-26 01:50:31 +00:00
private bool _linuxSudo = false;
private Func<bool, string, Task>? _updateFunc;
private const string _tag = "CoreHandler";
public async Task Init(Config config, Func<bool, string, Task> updateFunc)
2019-10-11 06:15:20 +00:00
{
_config = config;
_updateFunc = updateFunc;
2023-02-10 03:22:03 +00:00
//Copy the bin folder to the storage location (for init)
if (Environment.GetEnvironmentVariable(Global.LocalAppData) == "1")
{
var fromPath = Utils.GetBaseDirectory("bin");
var toPath = Utils.GetBinPath("");
if (fromPath != toPath)
{
FileManager.CopyDirectory(fromPath, toPath, true, false);
}
}
if (Utils.IsNonWindows())
{
2025-08-17 09:31:55 +00:00
var coreInfo = CoreInfoManager.Instance.GetCoreInfo();
foreach (var it in coreInfo)
{
if (it.CoreType == ECoreType.v2rayN)
{
if (Utils.UpgradeAppExists(out var upgradeFileName))
{
await Utils.SetLinuxChmod(upgradeFileName);
}
continue;
}
foreach (var name in it.CoreExes)
{
var exe = Utils.GetBinPath(Utils.GetExeName(name), it.CoreType.ToString());
if (File.Exists(exe))
{
await Utils.SetLinuxChmod(exe);
}
}
}
2019-10-11 06:15:20 +00:00
}
}
public async Task LoadCore(ProfileItem? node)
{
if (node == null)
{
await UpdateFunc(false, ResUI.CheckServerSettings);
return;
}
2019-10-11 06:15:20 +00:00
var fileName = Utils.GetBinConfigPath(Global.CoreConfigFileName);
var result = await CoreConfigHandler.GenerateClientConfig(node, fileName);
if (result.Success != true)
2024-07-21 03:37:11 +00:00
{
await UpdateFunc(true, result.Msg);
return;
}
2022-03-19 11:26:51 +00:00
await UpdateFunc(false, $"{node.GetSummary()}");
await UpdateFunc(false, $"{Utils.GetRuntimeInfo()}");
await UpdateFunc(false, string.Format(ResUI.StartService, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")));
await CoreStop();
await Task.Delay(100);
2024-11-27 03:05:41 +00:00
if (Utils.IsWindows() && _config.TunModeItem.EnableTun)
{
2024-11-27 03:05:41 +00:00
await Task.Delay(100);
await WindowsUtils.RemoveTunDevice();
}
await CoreStart(node);
await CoreStartPreService(node);
if (_processService != null)
{
await UpdateFunc(true, $"{node.GetSummary()}");
}
}
public async Task<ProcessService?> LoadCoreConfigSpeedtest(List<ServerTestItem> selecteds)
{
var coreType = selecteds.Exists(t => t.ConfigType is EConfigType.Hysteria2 or EConfigType.TUIC or EConfigType.Anytls) ? ECoreType.sing_box : ECoreType.Xray;
var fileName = string.Format(Global.CoreSpeedtestConfigFileName, Utils.GetGuid(false));
var configPath = Utils.GetBinConfigPath(fileName);
var result = await CoreConfigHandler.GenerateClientSpeedtestConfig(_config, configPath, selecteds, coreType);
await UpdateFunc(false, result.Msg);
if (result.Success != true)
{
return null;
2019-10-11 06:15:20 +00:00
}
await UpdateFunc(false, string.Format(ResUI.StartService, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")));
await UpdateFunc(false, configPath);
2025-08-17 09:31:55 +00:00
var coreInfo = CoreInfoManager.Instance.GetCoreInfo(coreType);
return await RunProcess(coreInfo, fileName, true, false);
}
2025-01-04 07:06:30 +00:00
public async Task<ProcessService?> LoadCoreConfigSpeedtest(ServerTestItem testItem)
{
2025-08-17 09:31:55 +00:00
var node = await AppManager.Instance.GetProfileItem(testItem.IndexId);
if (node is null)
{
return null;
}
var fileName = string.Format(Global.CoreSpeedtestConfigFileName, Utils.GetGuid(false));
var configPath = Utils.GetBinConfigPath(fileName);
var result = await CoreConfigHandler.GenerateClientSpeedtestConfig(_config, node, testItem, configPath);
if (result.Success != true)
{
return null;
}
2025-08-17 09:31:55 +00:00
var coreType = AppManager.Instance.GetCoreType(node, node.ConfigType);
var coreInfo = CoreInfoManager.Instance.GetCoreInfo(coreType);
return await RunProcess(coreInfo, fileName, true, false);
}
public async Task CoreStop()
{
try
{
2025-04-26 01:50:31 +00:00
if (_linuxSudo)
2025-04-25 08:36:28 +00:00
{
2025-08-17 09:31:55 +00:00
await CoreAdminManager.Instance.KillProcessAsLinuxSudo();
2025-04-26 01:50:31 +00:00
_linuxSudo = false;
2025-04-25 08:36:28 +00:00
}
if (_processService != null)
{
await _processService.StopAsync();
_processService.Dispose();
_processService = null;
}
if (_processPreService != null)
{
await _processPreService.StopAsync();
_processPreService.Dispose();
_processPreService = null;
}
2019-10-21 02:35:54 +00:00
}
catch (Exception ex)
2019-10-11 06:15:20 +00:00
{
Logging.SaveLog(_tag, ex);
2019-10-11 06:15:20 +00:00
}
}
2023-01-01 11:42:01 +00:00
#region Private
2024-02-12 13:12:57 +00:00
private async Task CoreStart(ProfileItem node)
{
2025-08-17 09:31:55 +00:00
var coreType = _config.RunningCoreType = AppManager.Instance.GetCoreType(node, node.ConfigType);
var coreInfo = CoreInfoManager.Instance.GetCoreInfo(coreType);
2023-05-04 03:44:51 +00:00
var displayLog = node.ConfigType != EConfigType.Custom || node.DisplayLog;
var proc = await RunProcess(coreInfo, Global.CoreConfigFileName, displayLog, true);
if (proc is null)
{
return;
2024-11-27 03:05:41 +00:00
}
_processService = proc;
}
private async Task CoreStartPreService(ProfileItem node)
{
if (_processService != null && !_processService.HasExited)
2024-11-27 03:05:41 +00:00
{
2025-08-17 09:31:55 +00:00
var coreType = AppManager.Instance.GetCoreType(node, node.ConfigType);
var itemSocks = await ConfigHandler.GetPreSocksItem(_config, node, coreType);
if (itemSocks != null)
{
var preCoreType = itemSocks.CoreType ?? ECoreType.sing_box;
var fileName = Utils.GetBinConfigPath(Global.CorePreConfigFileName);
var result = await CoreConfigHandler.GenerateClientConfig(itemSocks, fileName);
if (result.Success)
{
2025-08-17 09:31:55 +00:00
var coreInfo = CoreInfoManager.Instance.GetCoreInfo(preCoreType);
var proc = await RunProcess(coreInfo, Global.CorePreConfigFileName, true, true);
if (proc is null)
2023-04-21 06:49:33 +00:00
{
return;
}
_processPreService = proc;
}
2019-10-11 06:15:20 +00:00
}
}
}
2023-01-01 11:42:01 +00:00
private async Task UpdateFunc(bool notify, string msg)
{
await _updateFunc?.Invoke(notify, msg);
}
2019-12-24 01:01:13 +00:00
#endregion Private
#region Process
private async Task<ProcessService?> RunProcess(CoreInfo? coreInfo, string configPath, bool displayLog, bool mayNeedSudo)
{
2025-08-17 09:31:55 +00:00
var fileName = CoreInfoManager.Instance.GetCoreExecFile(coreInfo, out var msg);
if (fileName.IsNullOrEmpty())
2019-12-24 01:01:13 +00:00
{
await UpdateFunc(false, msg);
return null;
}
2025-04-28 07:16:58 +00:00
try
{
if (mayNeedSudo
&& _config.TunModeItem.EnableTun
&& coreInfo.CoreType == ECoreType.sing_box
&& Utils.IsNonWindows())
{
_linuxSudo = true;
2025-08-17 09:31:55 +00:00
await CoreAdminManager.Instance.Init(_config, _updateFunc);
return await CoreAdminManager.Instance.RunProcessAsLinuxSudo(fileName, coreInfo, configPath);
2025-04-28 07:16:58 +00:00
}
return await RunProcessNormal(fileName, coreInfo, configPath, displayLog);
}
catch (Exception ex)
2025-04-26 01:50:31 +00:00
{
2025-04-28 07:16:58 +00:00
Logging.SaveLog(_tag, ex);
await UpdateFunc(mayNeedSudo, ex.Message);
2025-04-28 07:16:58 +00:00
return null;
2025-04-26 01:50:31 +00:00
}
2025-04-28 07:16:58 +00:00
}
2025-04-26 01:50:31 +00:00
private async Task<ProcessService?> RunProcessNormal(string fileName, CoreInfo? coreInfo, string configPath, bool displayLog)
2025-04-28 07:16:58 +00:00
{
var environmentVars = new Dictionary<string, string>();
2025-08-25 09:43:53 +00:00
foreach (var kv in coreInfo.Environment)
{
environmentVars[kv.Key] = string.Format(kv.Value, coreInfo.AbsolutePath ? Utils.GetBinConfigPath(configPath).AppendQuotes() : configPath);
2025-08-25 09:43:53 +00:00
}
2025-04-28 07:16:58 +00:00
var procService = new ProcessService(
fileName: fileName,
arguments: string.Format(coreInfo.Arguments, coreInfo.AbsolutePath ? Utils.GetBinConfigPath(configPath).AppendQuotes() : configPath),
workingDirectory: Utils.GetBinConfigPath(),
displayLog: displayLog,
redirectInput: false,
environmentVars: environmentVars,
updateFunc: _updateFunc
);
2024-11-27 08:52:25 +00:00
await procService.StartAsync();
2025-04-28 07:16:58 +00:00
await Task.Delay(100);
if (procService is null or { HasExited: true })
2024-11-27 08:52:25 +00:00
{
2025-04-28 07:16:58 +00:00
throw new Exception(ResUI.FailedToRunCore);
}
AddProcessJob(procService.Handle);
return procService;
}
2024-11-27 08:52:25 +00:00
private void AddProcessJob(nint processHandle)
{
if (Utils.IsWindows())
{
_processJob ??= new();
try
{
_processJob?.AddProcess(processHandle);
}
catch { }
}
}
#endregion Process
}