mirror of
https://github.com/2dust/v2rayN.git
synced 2026-02-28 13:13:04 +00:00
Some checks are pending
release Linux / build (Release) (push) Waiting to run
release Linux / rpm (push) Blocked by required conditions
release macOS / build (Release) (push) Waiting to run
release Windows desktop (Avalonia UI) / build (Release) (push) Waiting to run
release Windows / build (Release) (push) Waiting to run
* Refactor node pre check * Rename method * I18n * Add remark not found warnings * Fix * Fix * Fix * Update v2rayN/ServiceLib/Handler/CoreConfigHandler.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
154 lines
5.4 KiB
C#
154 lines
5.4 KiB
C#
namespace ServiceLib.Handler;
|
|
|
|
/// <summary>
|
|
/// Core configuration file processing class
|
|
/// </summary>
|
|
public static class CoreConfigHandler
|
|
{
|
|
private static readonly string _tag = "CoreConfigHandler";
|
|
|
|
public static async Task<RetResult> GenerateClientConfig(CoreConfigContext context, string? fileName)
|
|
{
|
|
var config = AppManager.Instance.Config;
|
|
var result = new RetResult();
|
|
var node = context.Node;
|
|
|
|
if (node.ConfigType == EConfigType.Custom)
|
|
{
|
|
result = node.CoreType switch
|
|
{
|
|
ECoreType.mihomo => await new CoreConfigClashService(config).GenerateClientCustomConfig(node, fileName),
|
|
_ => await GenerateClientCustomConfig(node, fileName)
|
|
};
|
|
}
|
|
else if (AppManager.Instance.GetCoreType(node, node.ConfigType) == ECoreType.sing_box)
|
|
{
|
|
result = new CoreConfigSingboxService(context).GenerateClientConfigContent();
|
|
}
|
|
else
|
|
{
|
|
result = new CoreConfigV2rayService(context).GenerateClientConfigContent();
|
|
}
|
|
if (result.Success != true)
|
|
{
|
|
return result;
|
|
}
|
|
if (fileName.IsNotEmpty() && result.Data != null)
|
|
{
|
|
await File.WriteAllTextAsync(fileName, result.Data.ToString());
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
private static async Task<RetResult> GenerateClientCustomConfig(ProfileItem node, string? fileName)
|
|
{
|
|
var ret = new RetResult();
|
|
try
|
|
{
|
|
if (node == null || fileName is null)
|
|
{
|
|
ret.Msg = ResUI.CheckServerSettings;
|
|
return ret;
|
|
}
|
|
|
|
if (File.Exists(fileName))
|
|
{
|
|
File.SetAttributes(fileName, FileAttributes.Normal); //If the file has a read-only attribute, direct deletion will fail
|
|
File.Delete(fileName);
|
|
}
|
|
|
|
var addressFileName = node.Address;
|
|
if (!File.Exists(addressFileName))
|
|
{
|
|
addressFileName = Utils.GetConfigPath(addressFileName);
|
|
}
|
|
if (!File.Exists(addressFileName))
|
|
{
|
|
ret.Msg = ResUI.FailedGenDefaultConfiguration;
|
|
return ret;
|
|
}
|
|
File.Copy(addressFileName, fileName);
|
|
File.SetAttributes(fileName, FileAttributes.Normal); //Copy will keep the attributes of addressFileName, so we need to add write permissions to fileName just in case of addressFileName is a read-only file.
|
|
|
|
//check again
|
|
if (!File.Exists(fileName))
|
|
{
|
|
ret.Msg = ResUI.FailedGenDefaultConfiguration;
|
|
return ret;
|
|
}
|
|
|
|
ret.Msg = string.Format(ResUI.SuccessfulConfiguration, "");
|
|
ret.Success = true;
|
|
return await Task.FromResult(ret);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logging.SaveLog(_tag, ex);
|
|
ret.Msg = ResUI.FailedGenDefaultConfiguration;
|
|
return ret;
|
|
}
|
|
}
|
|
|
|
public static async Task<RetResult> GenerateClientSpeedtestConfig(Config config, string fileName, List<ServerTestItem> selecteds, ECoreType coreType)
|
|
{
|
|
var result = new RetResult();
|
|
var dummyNode = new ProfileItem
|
|
{
|
|
CoreType = coreType
|
|
};
|
|
var builderResult = await CoreConfigContextBuilder.Build(config, dummyNode);
|
|
var context = builderResult.Context;
|
|
var ids = selecteds.Where(serverTestItem => !serverTestItem.IndexId.IsNullOrEmpty())
|
|
.Select(serverTestItem => serverTestItem.IndexId);
|
|
var nodes = await AppManager.Instance.GetProfileItemsByIndexIds(ids);
|
|
foreach (var node in nodes)
|
|
{
|
|
var (actNode, _) = await CoreConfigContextBuilder.ResolveNodeAsync(context, node, true);
|
|
if (node.IndexId == actNode.IndexId)
|
|
{
|
|
continue;
|
|
}
|
|
context.ServerTestItemMap[node.IndexId] = actNode.IndexId;
|
|
}
|
|
if (coreType == ECoreType.sing_box)
|
|
{
|
|
result = new CoreConfigSingboxService(context).GenerateClientSpeedtestConfig(selecteds);
|
|
}
|
|
else if (coreType == ECoreType.Xray)
|
|
{
|
|
result = new CoreConfigV2rayService(context).GenerateClientSpeedtestConfig(selecteds);
|
|
}
|
|
if (result.Success != true)
|
|
{
|
|
return result;
|
|
}
|
|
await File.WriteAllTextAsync(fileName, result.Data.ToString());
|
|
return result;
|
|
}
|
|
|
|
public static async Task<RetResult> GenerateClientSpeedtestConfig(Config config, CoreConfigContext context, ServerTestItem testItem, string fileName)
|
|
{
|
|
var result = new RetResult();
|
|
var node = context.Node;
|
|
var initPort = AppManager.Instance.GetLocalPort(EInboundProtocol.speedtest);
|
|
var port = Utils.GetFreePort(initPort + testItem.QueueNum);
|
|
testItem.Port = port;
|
|
|
|
if (AppManager.Instance.GetCoreType(node, node.ConfigType) == ECoreType.sing_box)
|
|
{
|
|
result = new CoreConfigSingboxService(context).GenerateClientSpeedtestConfig(port);
|
|
}
|
|
else
|
|
{
|
|
result = new CoreConfigV2rayService(context).GenerateClientSpeedtestConfig(port);
|
|
}
|
|
if (result.Success != true)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
await File.WriteAllTextAsync(fileName, result.Data.ToString());
|
|
return result;
|
|
}
|
|
}
|