mirror of
https://github.com/2dust/v2rayN.git
synced 2026-11-18 10:25:00 +03:00
Extend CustomOutbound to support multiple outbounds (#10578)
* Extend CustomOutbound to support multiple outbounds * Optimize behavior
This commit is contained in:
@@ -177,9 +177,9 @@ public class InnerFmt : BaseFmt
|
|||||||
}
|
}
|
||||||
var customOutboundFilePath = string.Empty;
|
var customOutboundFilePath = string.Empty;
|
||||||
if (jsonObj.TryGetPropertyValue("CustomOutboundObj", out var customOutboundNode)
|
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);
|
customOutboundFilePath = WriteAllText(customOutboundContent);
|
||||||
jsonObj.Remove("CustomOutboundObj");
|
jsonObj.Remove("CustomOutboundObj");
|
||||||
}
|
}
|
||||||
@@ -239,9 +239,9 @@ public class InnerFmt : BaseFmt
|
|||||||
if (!customOutboundFilePath.IsNullOrEmpty()
|
if (!customOutboundFilePath.IsNullOrEmpty()
|
||||||
&& File.Exists(customOutboundFilePath)
|
&& File.Exists(customOutboundFilePath)
|
||||||
&& File.ReadAllText(customOutboundFilePath) is { Length: > 0 } customOutboundContent
|
&& 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");
|
jsonObj.Remove("Address");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -53,29 +53,64 @@ public partial class CoreConfigSingboxService
|
|||||||
customOutboundContent = customOutboundContent.Replace("{{tag}}", outboundTag);
|
customOutboundContent = customOutboundContent.Replace("{{tag}}", outboundTag);
|
||||||
customOutboundContent = customOutboundContent.Replace("{{detour}}", outboundDetour);
|
customOutboundContent = customOutboundContent.Replace("{{detour}}", outboundDetour);
|
||||||
customOutboundContent = customOutboundContent.Replace("{{interface}}", outboundBindInterface);
|
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)
|
if (!containTagPlaceholder)
|
||||||
{
|
{
|
||||||
customOutboundObj?["tag"] = outboundTag;
|
customOutboundObj["tag"] = outboundTag;
|
||||||
}
|
}
|
||||||
if (!containDetourPlaceholder && !outboundDetour.IsNullOrEmpty())
|
if (!containDetourPlaceholder && !outboundDetour.IsNullOrEmpty())
|
||||||
{
|
{
|
||||||
customOutboundObj?["detour"] = outboundDetour;
|
customOutboundDialingObj["detour"] = outboundDetour;
|
||||||
}
|
|
||||||
else if (outboundDetour.IsNullOrEmpty())
|
|
||||||
{
|
|
||||||
customOutboundObj?.Remove("detour");
|
|
||||||
}
|
}
|
||||||
if (!containBindInterfacePlaceholder && !outboundBindInterface.IsNullOrEmpty())
|
if (!containBindInterfacePlaceholder && !outboundBindInterface.IsNullOrEmpty())
|
||||||
{
|
{
|
||||||
customOutboundObj?["bind_interface"] = outboundBindInterface;
|
customOutboundDialingObj["bind_interface"] = outboundBindInterface;
|
||||||
}
|
}
|
||||||
|
|
||||||
var index = jsonArrayOutbounds
|
var index = jsonArrayOutbounds
|
||||||
.Select((node, idx) => new { node, idx })
|
.Select((node, idx) => new { node, idx })
|
||||||
.FirstOrDefault(x => x.node?["tag"]?.ToString() == outboundTag)?.idx ?? -1;
|
.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;
|
jsonArrayOutbounds[index] = customOutboundObj;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,39 +39,61 @@ public partial class CoreConfigV2rayService
|
|||||||
customOutboundContent = customOutboundContent.Replace("{{tag}}", outboundTag);
|
customOutboundContent = customOutboundContent.Replace("{{tag}}", outboundTag);
|
||||||
customOutboundContent = customOutboundContent.Replace("{{detour}}", outboundDetour);
|
customOutboundContent = customOutboundContent.Replace("{{detour}}", outboundDetour);
|
||||||
customOutboundContent = customOutboundContent.Replace("{{interface}}", outboundBindInterface);
|
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)
|
if (!containTagPlaceholder)
|
||||||
{
|
{
|
||||||
customOutboundObj?["tag"] = outboundTag;
|
customOutboundObj["tag"] = outboundTag;
|
||||||
}
|
}
|
||||||
if (customOutboundObj is not null
|
if (customOutboundObj["targetStrategy"] is null
|
||||||
&& customOutboundObj["targetStrategy"] is null
|
|
||||||
&& !outboundTargetStrategy.IsNullOrEmpty())
|
&& !outboundTargetStrategy.IsNullOrEmpty())
|
||||||
{
|
{
|
||||||
customOutboundObj["targetStrategy"] = outboundTargetStrategy;
|
customOutboundObj["targetStrategy"] = outboundTargetStrategy;
|
||||||
}
|
}
|
||||||
if (!containDetourPlaceholder && !outboundDetour.IsNullOrEmpty())
|
if (!containDetourPlaceholder && !outboundDetour.IsNullOrEmpty())
|
||||||
{
|
{
|
||||||
customOutboundObj!["streamSettings"] ??= new JsonObject();
|
customOutboundDialingObj["streamSettings"] ??= new JsonObject();
|
||||||
customOutboundObj["streamSettings"]["sockopt"] ??= new JsonObject();
|
customOutboundDialingObj["streamSettings"]["sockopt"] ??= new JsonObject();
|
||||||
customOutboundObj["streamSettings"]["sockopt"]["dialerProxy"] = outboundDetour;
|
customOutboundDialingObj["streamSettings"]["sockopt"]["dialerProxy"] = outboundDetour;
|
||||||
if (customOutboundObj["streamSettings"]?["xhttpSettings"]?["extra"]?["downloadSettings"] is JsonObject downloadSettings)
|
if (customOutboundDialingObj["streamSettings"]?["xhttpSettings"]?["extra"]?["downloadSettings"] is JsonObject downloadSettings)
|
||||||
{
|
{
|
||||||
downloadSettings["sockopt"] ??= new JsonObject();
|
downloadSettings["sockopt"] ??= new JsonObject();
|
||||||
downloadSettings["sockopt"]["dialerProxy"] = outboundDetour;
|
downloadSettings["sockopt"]["dialerProxy"] = outboundDetour;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (outboundDetour.IsNullOrEmpty())
|
|
||||||
{
|
|
||||||
(customOutboundObj?["streamSettings"]?["sockopt"] as JsonObject)?.Remove("dialerProxy");
|
|
||||||
}
|
|
||||||
if (!containBindInterfacePlaceholder && !outboundBindInterface.IsNullOrEmpty())
|
if (!containBindInterfacePlaceholder && !outboundBindInterface.IsNullOrEmpty())
|
||||||
{
|
{
|
||||||
customOutboundObj!["streamSettings"] ??= new JsonObject();
|
customOutboundDialingObj["streamSettings"] ??= new JsonObject();
|
||||||
customOutboundObj["streamSettings"]["sockopt"] ??= new JsonObject();
|
customOutboundDialingObj["streamSettings"]["sockopt"] ??= new JsonObject();
|
||||||
customOutboundObj["streamSettings"]["sockopt"]["interface"] = outboundBindInterface;
|
customOutboundDialingObj["streamSettings"]["sockopt"]["interface"] = outboundBindInterface;
|
||||||
if (customOutboundObj["streamSettings"]?["xhttpSettings"]?["extra"]?["downloadSettings"] is JsonObject downloadSettings)
|
if (customOutboundDialingObj["streamSettings"]?["xhttpSettings"]?["extra"]?["downloadSettings"] is JsonObject downloadSettings)
|
||||||
{
|
{
|
||||||
downloadSettings["sockopt"] ??= new JsonObject();
|
downloadSettings["sockopt"] ??= new JsonObject();
|
||||||
downloadSettings["sockopt"]["interface"] = outboundBindInterface;
|
downloadSettings["sockopt"]["interface"] = outboundBindInterface;
|
||||||
@@ -81,7 +103,19 @@ public partial class CoreConfigV2rayService
|
|||||||
var index = coreConfigOutboundsNode
|
var index = coreConfigOutboundsNode
|
||||||
.Select((node, idx) => new { node, idx })
|
.Select((node, idx) => new { node, idx })
|
||||||
.FirstOrDefault(x => x.node?["tag"]?.ToString() == outboundTag)?.idx ?? -1;
|
.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;
|
coreConfigOutboundsNode[index] = customOutboundObj;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user