2025-08-17 08:52:51 +00:00
|
|
|
namespace ServiceLib.Manager;
|
2025-04-02 03:44:23 +00:00
|
|
|
|
|
|
|
/// <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;
|
2025-04-02 03:44:23 +00:00
|
|
|
private Config _config;
|
2025-10-03 06:13:03 +00:00
|
|
|
private WindowsJob? _processJob;
|
2025-10-01 11:49:28 +00:00
|
|
|
private ProcessService? _processService;
|
|
|
|
private ProcessService? _processPreService;
|
2025-04-26 01:50:31 +00:00
|
|
|
private bool _linuxSudo = false;
|
2025-08-29 01:49:30 +00:00
|
|
|
private Func<bool, string, Task>? _updateFunc;
|
2025-04-02 03:44:23 +00:00
|
|
|
private const string _tag = "CoreHandler";
|
|
|
|
|
2025-08-29 01:49:30 +00:00
|
|
|
public async Task Init(Config config, Func<bool, string, Task> updateFunc)
|
2019-10-11 06:15:20 +00:00
|
|
|
{
|
2025-04-02 03:44:23 +00:00
|
|
|
_config = config;
|
|
|
|
_updateFunc = updateFunc;
|
2023-02-10 03:22:03 +00:00
|
|
|
|
2025-04-02 03:44:23 +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)
|
2025-01-18 08:58:44 +00:00
|
|
|
{
|
2025-04-02 03:44:23 +00:00
|
|
|
FileManager.CopyDirectory(fromPath, toPath, true, false);
|
2025-01-18 08:58:44 +00:00
|
|
|
}
|
2025-04-02 03:44:23 +00:00
|
|
|
}
|
2025-01-18 08:58:44 +00:00
|
|
|
|
2025-04-02 03:44:23 +00:00
|
|
|
if (Utils.IsNonWindows())
|
|
|
|
{
|
2025-08-17 09:31:55 +00:00
|
|
|
var coreInfo = CoreInfoManager.Instance.GetCoreInfo();
|
2025-04-02 03:44:23 +00:00
|
|
|
foreach (var it in coreInfo)
|
2024-10-25 02:10:55 +00:00
|
|
|
{
|
2025-04-02 03:44:23 +00:00
|
|
|
if (it.CoreType == ECoreType.v2rayN)
|
2024-10-25 02:10:55 +00:00
|
|
|
{
|
2025-04-02 03:44:23 +00:00
|
|
|
if (Utils.UpgradeAppExists(out var upgradeFileName))
|
2024-10-25 02:10:55 +00:00
|
|
|
{
|
2025-04-02 03:44:23 +00:00
|
|
|
await Utils.SetLinuxChmod(upgradeFileName);
|
2024-10-25 02:10:55 +00:00
|
|
|
}
|
2025-04-02 03:44:23 +00:00
|
|
|
continue;
|
|
|
|
}
|
2024-10-25 02:10:55 +00:00
|
|
|
|
2025-04-02 03:44:23 +00:00
|
|
|
foreach (var name in it.CoreExes)
|
|
|
|
{
|
|
|
|
var exe = Utils.GetBinPath(Utils.GetExeName(name), it.CoreType.ToString());
|
|
|
|
if (File.Exists(exe))
|
2024-10-25 02:10:55 +00:00
|
|
|
{
|
2025-04-02 03:44:23 +00:00
|
|
|
await Utils.SetLinuxChmod(exe);
|
2024-10-25 02:10:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-11 06:15:20 +00:00
|
|
|
}
|
2025-04-02 03:44:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public async Task LoadCore(ProfileItem? node)
|
|
|
|
{
|
|
|
|
if (node == null)
|
|
|
|
{
|
2025-08-29 01:49:30 +00:00
|
|
|
await UpdateFunc(false, ResUI.CheckServerSettings);
|
2025-04-02 03:44:23 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-10-11 06:15:20 +00:00
|
|
|
|
2025-04-02 03:44:23 +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
|
|
|
{
|
2025-08-29 01:49:30 +00:00
|
|
|
await UpdateFunc(true, result.Msg);
|
2025-04-02 03:44:23 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-03-19 11:26:51 +00:00
|
|
|
|
2025-08-29 01:49:30 +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")));
|
2025-04-02 03:44:23 +00:00
|
|
|
await CoreStop();
|
|
|
|
await Task.Delay(100);
|
2024-11-27 03:05:41 +00:00
|
|
|
|
2025-04-02 03:44:23 +00:00
|
|
|
if (Utils.IsWindows() && _config.TunModeItem.EnableTun)
|
|
|
|
{
|
2024-11-27 03:05:41 +00:00
|
|
|
await Task.Delay(100);
|
2025-04-02 03:44:23 +00:00
|
|
|
await WindowsUtils.RemoveTunDevice();
|
|
|
|
}
|
2025-01-30 02:46:04 +00:00
|
|
|
|
2025-04-02 03:44:23 +00:00
|
|
|
await CoreStart(node);
|
|
|
|
await CoreStartPreService(node);
|
2025-10-01 11:49:28 +00:00
|
|
|
if (_processService != null)
|
2025-04-02 03:44:23 +00:00
|
|
|
{
|
2025-08-29 01:49:30 +00:00
|
|
|
await UpdateFunc(true, $"{node.GetSummary()}");
|
2025-04-02 03:44:23 +00:00
|
|
|
}
|
|
|
|
}
|
2025-01-30 02:46:04 +00:00
|
|
|
|
2025-10-01 11:49:28 +00:00
|
|
|
public async Task<ProcessService?> LoadCoreConfigSpeedtest(List<ServerTestItem> selecteds)
|
2025-04-02 03:44:23 +00:00
|
|
|
{
|
2025-08-10 02:15:32 +00:00
|
|
|
var coreType = selecteds.Exists(t => t.ConfigType is EConfigType.Hysteria2 or EConfigType.TUIC or EConfigType.Anytls) ? ECoreType.sing_box : ECoreType.Xray;
|
2025-04-02 03:44:23 +00:00
|
|
|
var fileName = string.Format(Global.CoreSpeedtestConfigFileName, Utils.GetGuid(false));
|
|
|
|
var configPath = Utils.GetBinConfigPath(fileName);
|
|
|
|
var result = await CoreConfigHandler.GenerateClientSpeedtestConfig(_config, configPath, selecteds, coreType);
|
2025-08-29 01:49:30 +00:00
|
|
|
await UpdateFunc(false, result.Msg);
|
2025-04-02 03:44:23 +00:00
|
|
|
if (result.Success != true)
|
|
|
|
{
|
2025-10-01 11:49:28 +00:00
|
|
|
return null;
|
2019-10-11 06:15:20 +00:00
|
|
|
}
|
|
|
|
|
2025-08-29 01:49:30 +00:00
|
|
|
await UpdateFunc(false, string.Format(ResUI.StartService, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")));
|
|
|
|
await UpdateFunc(false, configPath);
|
2025-04-02 03:44:23 +00:00
|
|
|
|
2025-08-17 09:31:55 +00:00
|
|
|
var coreInfo = CoreInfoManager.Instance.GetCoreInfo(coreType);
|
2025-10-01 11:49:28 +00:00
|
|
|
return await RunProcess(coreInfo, fileName, true, false);
|
2025-04-02 03:44:23 +00:00
|
|
|
}
|
2025-01-04 07:06:30 +00:00
|
|
|
|
2025-10-01 11:49:28 +00:00
|
|
|
public async Task<ProcessService?> LoadCoreConfigSpeedtest(ServerTestItem testItem)
|
2025-04-02 03:44:23 +00:00
|
|
|
{
|
2025-08-17 09:31:55 +00:00
|
|
|
var node = await AppManager.Instance.GetProfileItem(testItem.IndexId);
|
2025-04-02 03:44:23 +00:00
|
|
|
if (node is null)
|
|
|
|
{
|
2025-10-01 11:49:28 +00:00
|
|
|
return null;
|
2025-04-02 03:44:23 +00:00
|
|
|
}
|
2025-02-13 11:46:51 +00:00
|
|
|
|
2025-04-02 03:44:23 +00:00
|
|
|
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)
|
|
|
|
{
|
2025-10-01 11:49:28 +00:00
|
|
|
return null;
|
2025-04-02 03:44:23 +00:00
|
|
|
}
|
|
|
|
|
2025-08-17 09:31:55 +00:00
|
|
|
var coreType = AppManager.Instance.GetCoreType(node, node.ConfigType);
|
|
|
|
var coreInfo = CoreInfoManager.Instance.GetCoreInfo(coreType);
|
2025-10-01 11:49:28 +00:00
|
|
|
return await RunProcess(coreInfo, fileName, true, false);
|
2025-04-02 03:44:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public async Task CoreStop()
|
|
|
|
{
|
|
|
|
try
|
2025-02-13 11:46:51 +00:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2025-10-01 11:49:28 +00:00
|
|
|
if (_processService != null)
|
2025-02-13 11:46:51 +00:00
|
|
|
{
|
2025-10-01 11:49:28 +00:00
|
|
|
await _processService.StopAsync();
|
|
|
|
_processService.Dispose();
|
|
|
|
_processService = null;
|
2025-02-13 11:46:51 +00:00
|
|
|
}
|
|
|
|
|
2025-10-01 11:49:28 +00:00
|
|
|
if (_processPreService != null)
|
2025-02-13 11:46:51 +00:00
|
|
|
{
|
2025-10-01 11:49:28 +00:00
|
|
|
await _processPreService.StopAsync();
|
|
|
|
_processPreService.Dispose();
|
|
|
|
_processPreService = null;
|
2025-02-13 11:46:51 +00:00
|
|
|
}
|
2019-10-21 02:35:54 +00:00
|
|
|
}
|
2025-04-02 03:44:23 +00:00
|
|
|
catch (Exception ex)
|
2019-10-11 06:15:20 +00:00
|
|
|
{
|
2025-04-02 03:44:23 +00:00
|
|
|
Logging.SaveLog(_tag, ex);
|
2019-10-11 06:15:20 +00:00
|
|
|
}
|
2025-04-02 03:44:23 +00:00
|
|
|
}
|
2023-01-01 11:42:01 +00:00
|
|
|
|
2025-04-02 03:44:23 +00:00
|
|
|
#region Private
|
2024-02-12 13:12:57 +00:00
|
|
|
|
2025-04-02 03:44:23 +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
|
|
|
|
2025-04-02 03:44:23 +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
|
|
|
}
|
2025-10-01 11:49:28 +00:00
|
|
|
_processService = proc;
|
2025-04-02 03:44:23 +00:00
|
|
|
}
|
2020-03-15 10:22:11 +00:00
|
|
|
|
2025-04-02 03:44:23 +00:00
|
|
|
private async Task CoreStartPreService(ProfileItem node)
|
|
|
|
{
|
2025-10-01 11:49:28 +00:00
|
|
|
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);
|
2025-04-02 03:44:23 +00:00
|
|
|
var itemSocks = await ConfigHandler.GetPreSocksItem(_config, node, coreType);
|
|
|
|
if (itemSocks != null)
|
2023-04-28 04:09:02 +00:00
|
|
|
{
|
2025-04-02 03:44:23 +00:00
|
|
|
var preCoreType = itemSocks.CoreType ?? ECoreType.sing_box;
|
|
|
|
var fileName = Utils.GetBinConfigPath(Global.CorePreConfigFileName);
|
|
|
|
var result = await CoreConfigHandler.GenerateClientConfig(itemSocks, fileName);
|
|
|
|
if (result.Success)
|
2024-06-24 07:19:42 +00:00
|
|
|
{
|
2025-08-17 09:31:55 +00:00
|
|
|
var coreInfo = CoreInfoManager.Instance.GetCoreInfo(preCoreType);
|
2025-04-02 03:44:23 +00:00
|
|
|
var proc = await RunProcess(coreInfo, Global.CorePreConfigFileName, true, true);
|
|
|
|
if (proc is null)
|
2023-04-21 06:49:33 +00:00
|
|
|
{
|
2025-04-02 03:44:23 +00:00
|
|
|
return;
|
2023-04-28 04:09:02 +00:00
|
|
|
}
|
2025-10-01 11:49:28 +00:00
|
|
|
_processPreService = proc;
|
2020-03-20 04:41:59 +00:00
|
|
|
}
|
2019-10-11 06:15:20 +00:00
|
|
|
}
|
|
|
|
}
|
2025-04-02 03:44:23 +00:00
|
|
|
}
|
2023-01-01 11:42:01 +00:00
|
|
|
|
2025-08-29 01:49:30 +00:00
|
|
|
private async Task UpdateFunc(bool notify, string msg)
|
2025-04-02 03:44:23 +00:00
|
|
|
{
|
2025-08-29 01:49:30 +00:00
|
|
|
await _updateFunc?.Invoke(notify, msg);
|
2025-04-02 03:44:23 +00:00
|
|
|
}
|
2019-12-24 01:01:13 +00:00
|
|
|
|
2025-04-02 03:44:23 +00:00
|
|
|
#endregion Private
|
2024-01-31 01:56:47 +00:00
|
|
|
|
2025-04-02 03:44:23 +00:00
|
|
|
#region Process
|
2023-04-28 04:09:02 +00:00
|
|
|
|
2025-10-01 11:49:28 +00:00
|
|
|
private async Task<ProcessService?> RunProcess(CoreInfo? coreInfo, string configPath, bool displayLog, bool mayNeedSudo)
|
2025-04-02 03:44:23 +00:00
|
|
|
{
|
2025-08-17 09:31:55 +00:00
|
|
|
var fileName = CoreInfoManager.Instance.GetCoreExecFile(coreInfo, out var msg);
|
2025-04-02 03:44:23 +00:00
|
|
|
if (fileName.IsNullOrEmpty())
|
2019-12-24 01:01:13 +00:00
|
|
|
{
|
2025-08-29 01:49:30 +00:00
|
|
|
await UpdateFunc(false, msg);
|
2025-04-02 03:44:23 +00:00
|
|
|
return null;
|
|
|
|
}
|
2024-11-13 08:22:56 +00:00
|
|
|
|
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);
|
2025-08-29 01:49:30 +00:00
|
|
|
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
|
|
|
|
2025-10-01 11:49:28 +00:00
|
|
|
private async Task<ProcessService?> RunProcessNormal(string fileName, CoreInfo? coreInfo, string configPath, bool displayLog)
|
2025-04-28 07:16:58 +00:00
|
|
|
{
|
2025-10-01 11:49:28 +00:00
|
|
|
var environmentVars = new Dictionary<string, string>();
|
2025-08-25 09:43:53 +00:00
|
|
|
foreach (var kv in coreInfo.Environment)
|
|
|
|
{
|
2025-10-01 11:49:28 +00:00
|
|
|
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
|
|
|
|
2025-10-01 11:49:28 +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
|
|
|
|
2025-10-01 11:49:28 +00:00
|
|
|
await procService.StartAsync();
|
2025-04-28 07:16:58 +00:00
|
|
|
|
|
|
|
await Task.Delay(100);
|
2025-10-03 06:13:03 +00:00
|
|
|
|
2025-10-01 11:49:28 +00:00
|
|
|
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);
|
2025-04-02 03:44:23 +00:00
|
|
|
}
|
2025-10-03 06:13:03 +00:00
|
|
|
AddProcessJob(procService.Handle);
|
2025-10-01 11:49:28 +00:00
|
|
|
|
|
|
|
return procService;
|
2025-04-02 03:44:23 +00:00
|
|
|
}
|
2024-11-27 08:52:25 +00:00
|
|
|
|
2025-10-03 06:13:03 +00:00
|
|
|
private void AddProcessJob(nint processHandle)
|
|
|
|
{
|
|
|
|
if (Utils.IsWindows())
|
|
|
|
{
|
|
|
|
_processJob ??= new();
|
|
|
|
try
|
|
|
|
{
|
|
|
|
_processJob?.AddProcess(processHandle);
|
|
|
|
}
|
|
|
|
catch { }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-04-02 03:44:23 +00:00
|
|
|
#endregion Process
|
2025-01-30 02:46:04 +00:00
|
|
|
}
|