v2rayN/v2rayN/ServiceLib/Services/CoreConfig/Singbox/SingboxConfigTemplateService.cs

62 lines
2.3 KiB
C#
Raw Normal View History

namespace ServiceLib.Services.CoreConfig;
public partial class CoreConfigSingboxService
{
2026-02-06 04:55:55 +00:00
private string ApplyFullConfigTemplate()
{
2026-02-06 04:55:55 +00:00
var fullConfigTemplate = context.FullConfigTemplate;
if (fullConfigTemplate is not { Enabled: true })
{
2026-02-06 04:55:55 +00:00
return JsonUtils.Serialize(_coreConfig);
}
2026-02-06 04:55:55 +00:00
var fullConfigTemplateItem = context.IsTunEnabled ? fullConfigTemplate.TunConfig : fullConfigTemplate.Config;
if (fullConfigTemplateItem.IsNullOrEmpty())
{
2026-02-06 04:55:55 +00:00
return JsonUtils.Serialize(_coreConfig);
}
var fullConfigTemplateNode = JsonNode.Parse(fullConfigTemplateItem);
if (fullConfigTemplateNode == null)
{
2026-02-06 04:55:55 +00:00
return JsonUtils.Serialize(_coreConfig);
}
// Process outbounds
var customOutboundsNode = fullConfigTemplateNode["outbounds"] is JsonArray outbounds ? outbounds : new JsonArray();
2026-02-06 04:55:55 +00:00
foreach (var outbound in _coreConfig.outbounds)
{
if (outbound.type.ToLower() is "direct" or "block")
{
if (fullConfigTemplate.AddProxyOnly == true)
{
continue;
}
}
else if (outbound.detour.IsNullOrEmpty() && !fullConfigTemplate.ProxyDetour.IsNullOrEmpty() && !Utils.IsPrivateNetwork(outbound.server ?? string.Empty))
{
outbound.detour = fullConfigTemplate.ProxyDetour;
}
customOutboundsNode.Add(JsonUtils.DeepCopy(outbound));
}
fullConfigTemplateNode["outbounds"] = customOutboundsNode;
// Process endpoints
2026-02-06 04:55:55 +00:00
if (_coreConfig.endpoints != null && _coreConfig.endpoints.Count > 0)
{
var customEndpointsNode = fullConfigTemplateNode["endpoints"] is JsonArray endpoints ? endpoints : new JsonArray();
2026-02-06 04:55:55 +00:00
foreach (var endpoint in _coreConfig.endpoints)
{
if (endpoint.detour.IsNullOrEmpty() && !fullConfigTemplate.ProxyDetour.IsNullOrEmpty())
{
endpoint.detour = fullConfigTemplate.ProxyDetour;
}
customEndpointsNode.Add(JsonUtils.DeepCopy(endpoint));
}
fullConfigTemplateNode["endpoints"] = customEndpointsNode;
}
2026-02-06 04:55:55 +00:00
return JsonUtils.Serialize(fullConfigTemplateNode);
}
}