mirror of
https://github.com/2dust/v2rayN.git
synced 2026-09-28 01:22:07 +03:00
111 lines
3.6 KiB
C#
111 lines
3.6 KiB
C#
namespace ServiceLib.Services.CoreConfig;
|
|
|
|
public partial class CoreConfigSingboxService
|
|
{
|
|
private string ApplyFullConfigTemplate()
|
|
{
|
|
var fullConfigTemplate = context.FullConfigTemplate;
|
|
if (fullConfigTemplate is not { Enabled: true })
|
|
{
|
|
return JsonUtils.Serialize(_coreConfig);
|
|
}
|
|
|
|
var fullConfigTemplateItem = context.IsTunEnabled ? fullConfigTemplate.TunConfig : fullConfigTemplate.Config;
|
|
if (fullConfigTemplateItem.IsNullOrEmpty())
|
|
{
|
|
return JsonUtils.Serialize(_coreConfig);
|
|
}
|
|
|
|
var fullConfigTemplateNode = JsonNode.Parse(fullConfigTemplateItem);
|
|
if (fullConfigTemplateNode == null)
|
|
{
|
|
return JsonUtils.Serialize(_coreConfig);
|
|
}
|
|
|
|
// Process outbounds
|
|
var customOutboundsNode = fullConfigTemplateNode["outbounds"] as JsonArray ?? [];
|
|
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
|
|
if (_coreConfig.endpoints is { Count: > 0 })
|
|
{
|
|
var customEndpointsNode = fullConfigTemplateNode["endpoints"] as JsonArray ?? [];
|
|
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;
|
|
}
|
|
|
|
return JsonUtils.Serialize(fullConfigTemplateNode);
|
|
}
|
|
|
|
private void ApplyOutboundBindInterface()
|
|
{
|
|
var bindInterface = _config.CoreBasicItem.BindInterface?.TrimEx();
|
|
if (bindInterface.IsNullOrEmpty())
|
|
{
|
|
return;
|
|
}
|
|
foreach (var outbound in _coreConfig.outbounds ?? [])
|
|
{
|
|
outbound.bind_interface = ShouldBindNet(outbound) ? bindInterface : null;
|
|
}
|
|
}
|
|
|
|
private void ApplyOutboundSendThrough()
|
|
{
|
|
var sendThrough = _config.CoreBasicItem.SendThrough?.TrimEx();
|
|
if (sendThrough.IsNullOrEmpty())
|
|
{
|
|
return;
|
|
}
|
|
|
|
foreach (var outbound in _coreConfig.outbounds ?? [])
|
|
{
|
|
outbound.inet4_bind_address = ShouldBindNet(outbound) ? sendThrough : null;
|
|
}
|
|
}
|
|
|
|
private static bool ShouldBindNet(Outbound4Sbox outbound)
|
|
{
|
|
if (outbound.type is "direct" or "block" or "dns" or "selector" or "urltest")
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (!outbound.detour.IsNullOrEmpty())
|
|
{
|
|
return false;
|
|
}
|
|
|
|
var outboundAddress = outbound.server ?? string.Empty;
|
|
|
|
if (outboundAddress.Equals("localhost", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return !IPAddress.TryParse(outboundAddress, out var address) || !IPAddress.IsLoopback(address);
|
|
}
|
|
}
|