Extend CustomOutbound to support multiple outbounds (#10578)

* Extend CustomOutbound to support multiple outbounds

* Optimize behavior
This commit is contained in:
DHR60
2026-11-18 06:22:21 +00:00
committed by GitHub
parent 2db57e5a8a
commit 562d289495
3 changed files with 99 additions and 30 deletions
+4 -4
View File
@@ -177,9 +177,9 @@ public class InnerFmt : BaseFmt
}
var customOutboundFilePath = string.Empty;
if (jsonObj.TryGetPropertyValue("CustomOutboundObj", out var customOutboundNode)
&& customOutboundNode is JsonObject customOutboundObj)
&& customOutboundNode is JsonObject or JsonArray)
{
var customOutboundContent = JsonUtils.Serialize(customOutboundObj, indented: true, nullValue: true);
var customOutboundContent = JsonUtils.Serialize(customOutboundNode, indented: true, nullValue: true);
customOutboundFilePath = WriteAllText(customOutboundContent);
jsonObj.Remove("CustomOutboundObj");
}
@@ -239,9 +239,9 @@ public class InnerFmt : BaseFmt
if (!customOutboundFilePath.IsNullOrEmpty()
&& File.Exists(customOutboundFilePath)
&& File.ReadAllText(customOutboundFilePath) is { Length: > 0 } customOutboundContent
&& JsonUtils.ParseJson(customOutboundContent) is JsonObject customOutboundObj)
&& JsonUtils.ParseJson(customOutboundContent) is (JsonObject or JsonArray) and { } customOutboundNode)
{
jsonObj["CustomOutboundObj"] = customOutboundObj;
jsonObj["CustomOutboundObj"] = customOutboundNode;
jsonObj.Remove("Address");
}
}
@@ -53,29 +53,64 @@ public partial class CoreConfigSingboxService
customOutboundContent = customOutboundContent.Replace("{{tag}}", outboundTag);
customOutboundContent = customOutboundContent.Replace("{{detour}}", outboundDetour);
customOutboundContent = customOutboundContent.Replace("{{interface}}", outboundBindInterface);
var customOutboundObj = JsonUtils.ParseJson(customOutboundContent) as JsonObject;
var customOutboundNode = JsonUtils.ParseJson(customOutboundContent);
JsonObject customOutboundObj;
JsonObject customOutboundDialingObj;
if (customOutboundNode is JsonArray jsonArray)
{
// NOTE: When the custom outbound definition is an array,
// treat it as an outbound proxy chain:
// apply outbound-level settings (such as the tag) to the first element,
// and apply dialing settings (such as the detour) to the last element.
if (jsonArray.Count == 0
|| jsonArray[0] is not JsonObject firstObj
|| jsonArray[^1] is not JsonObject lastObj)
{
continue;
}
customOutboundObj = firstObj;
customOutboundDialingObj = lastObj;
}
else if (customOutboundNode is JsonObject jsonObject)
{
customOutboundObj = jsonObject;
customOutboundDialingObj = jsonObject;
}
else
{
continue;
}
if (!containTagPlaceholder)
{
customOutboundObj?["tag"] = outboundTag;
customOutboundObj["tag"] = outboundTag;
}
if (!containDetourPlaceholder && !outboundDetour.IsNullOrEmpty())
{
customOutboundObj?["detour"] = outboundDetour;
}
else if (outboundDetour.IsNullOrEmpty())
{
customOutboundObj?.Remove("detour");
customOutboundDialingObj["detour"] = outboundDetour;
}
if (!containBindInterfacePlaceholder && !outboundBindInterface.IsNullOrEmpty())
{
customOutboundObj?["bind_interface"] = outboundBindInterface;
customOutboundDialingObj["bind_interface"] = outboundBindInterface;
}
var index = jsonArrayOutbounds
.Select((node, idx) => new { node, idx })
.FirstOrDefault(x => x.node?["tag"]?.ToString() == outboundTag)?.idx ?? -1;
if (index != -1)
if (index == -1)
{
continue;
}
if (customOutboundNode is JsonArray customOutboundArray)
{
jsonArrayOutbounds.RemoveAt(index);
foreach (var item in customOutboundArray)
{
jsonArrayOutbounds.Insert(index++, item?.DeepClone());
}
}
else
{
jsonArrayOutbounds[index] = customOutboundObj;
}
@@ -39,39 +39,61 @@ public partial class CoreConfigV2rayService
customOutboundContent = customOutboundContent.Replace("{{tag}}", outboundTag);
customOutboundContent = customOutboundContent.Replace("{{detour}}", outboundDetour);
customOutboundContent = customOutboundContent.Replace("{{interface}}", outboundBindInterface);
var customOutboundObj = JsonUtils.ParseJson(customOutboundContent) as JsonObject;
var customOutboundNode = JsonUtils.ParseJson(customOutboundContent);
JsonObject customOutboundObj;
JsonObject customOutboundDialingObj;
if (customOutboundNode is JsonArray jsonArray)
{
// NOTE: When the custom outbound definition is an array,
// treat it as an outbound proxy chain:
// apply outbound-level settings (such as the tag) to the first element,
// and apply dialing settings (such as the detour) to the last element.
if (jsonArray.Count == 0
|| jsonArray[0] is not JsonObject firstObj
|| jsonArray[^1] is not JsonObject lastObj)
{
continue;
}
customOutboundObj = firstObj;
customOutboundDialingObj = lastObj;
}
else if (customOutboundNode is JsonObject jsonObject)
{
customOutboundObj = jsonObject;
customOutboundDialingObj = jsonObject;
}
else
{
continue;
}
if (!containTagPlaceholder)
{
customOutboundObj?["tag"] = outboundTag;
customOutboundObj["tag"] = outboundTag;
}
if (customOutboundObj is not null
&& customOutboundObj["targetStrategy"] is null
if (customOutboundObj["targetStrategy"] is null
&& !outboundTargetStrategy.IsNullOrEmpty())
{
customOutboundObj["targetStrategy"] = outboundTargetStrategy;
}
if (!containDetourPlaceholder && !outboundDetour.IsNullOrEmpty())
{
customOutboundObj!["streamSettings"] ??= new JsonObject();
customOutboundObj["streamSettings"]["sockopt"] ??= new JsonObject();
customOutboundObj["streamSettings"]["sockopt"]["dialerProxy"] = outboundDetour;
if (customOutboundObj["streamSettings"]?["xhttpSettings"]?["extra"]?["downloadSettings"] is JsonObject downloadSettings)
customOutboundDialingObj["streamSettings"] ??= new JsonObject();
customOutboundDialingObj["streamSettings"]["sockopt"] ??= new JsonObject();
customOutboundDialingObj["streamSettings"]["sockopt"]["dialerProxy"] = outboundDetour;
if (customOutboundDialingObj["streamSettings"]?["xhttpSettings"]?["extra"]?["downloadSettings"] is JsonObject downloadSettings)
{
downloadSettings["sockopt"] ??= new JsonObject();
downloadSettings["sockopt"]["dialerProxy"] = outboundDetour;
}
}
else if (outboundDetour.IsNullOrEmpty())
{
(customOutboundObj?["streamSettings"]?["sockopt"] as JsonObject)?.Remove("dialerProxy");
}
if (!containBindInterfacePlaceholder && !outboundBindInterface.IsNullOrEmpty())
{
customOutboundObj!["streamSettings"] ??= new JsonObject();
customOutboundObj["streamSettings"]["sockopt"] ??= new JsonObject();
customOutboundObj["streamSettings"]["sockopt"]["interface"] = outboundBindInterface;
if (customOutboundObj["streamSettings"]?["xhttpSettings"]?["extra"]?["downloadSettings"] is JsonObject downloadSettings)
customOutboundDialingObj["streamSettings"] ??= new JsonObject();
customOutboundDialingObj["streamSettings"]["sockopt"] ??= new JsonObject();
customOutboundDialingObj["streamSettings"]["sockopt"]["interface"] = outboundBindInterface;
if (customOutboundDialingObj["streamSettings"]?["xhttpSettings"]?["extra"]?["downloadSettings"] is JsonObject downloadSettings)
{
downloadSettings["sockopt"] ??= new JsonObject();
downloadSettings["sockopt"]["interface"] = outboundBindInterface;
@@ -81,7 +103,19 @@ public partial class CoreConfigV2rayService
var index = coreConfigOutboundsNode
.Select((node, idx) => new { node, idx })
.FirstOrDefault(x => x.node?["tag"]?.ToString() == outboundTag)?.idx ?? -1;
if (index != -1)
if (index == -1)
{
continue;
}
if (customOutboundNode is JsonArray customOutboundArray)
{
coreConfigOutboundsNode.RemoveAt(index);
foreach (var item in customOutboundArray)
{
coreConfigOutboundsNode.Insert(index++, item?.DeepClone());
}
}
else
{
coreConfigOutboundsNode[index] = customOutboundObj;
}