From b74188d90485cfee5a3a3124a9978dc3b70df923 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Mon, 25 Dec 2023 16:42:23 +0800 Subject: [PATCH] Previous and next proxy 4 sing-box --- v2rayN/v2rayN/Handler/CoreConfigSingbox.cs | 69 +++++++++++++++---- v2rayN/v2rayN/Handler/CoreConfigV2ray.cs | 13 ++-- v2rayN/v2rayN/Mode/SingboxConfig.cs | 2 +- .../v2rayN/Sample/SingboxSampleClientConfig | 11 ++- v2rayN/v2rayN/Sample/custom_routing_white | 4 ++ v2rayN/v2rayN/Sample/tun_singbox_rules | 4 -- 6 files changed, 81 insertions(+), 22 deletions(-) diff --git a/v2rayN/v2rayN/Handler/CoreConfigSingbox.cs b/v2rayN/v2rayN/Handler/CoreConfigSingbox.cs index c3607e87..4c396d11 100644 --- a/v2rayN/v2rayN/Handler/CoreConfigSingbox.cs +++ b/v2rayN/v2rayN/Handler/CoreConfigSingbox.cs @@ -45,7 +45,9 @@ namespace v2rayN.Handler GenInbounds(singboxConfig); - GenOutbound(node, singboxConfig); + GenOutbound(node, singboxConfig.outbounds[0]); + + GenMoreOutbounds(node, singboxConfig); GenRouting(singboxConfig); @@ -202,20 +204,10 @@ namespace v2rayN.Handler #region outbound private - private int GenOutbound(ProfileItem node, SingboxConfig singboxConfig) + private int GenOutbound(ProfileItem node, Outbound4Sbox outbound) { try { - if (_config.tunModeItem.enableTun) - { - singboxConfig.outbounds.Add(new() - { - type = "dns", - tag = "dns_out" - }); - } - - var outbound = singboxConfig.outbounds[0]; outbound.server = node.address; outbound.server_port = node.port; @@ -442,6 +434,59 @@ namespace v2rayN.Handler return 0; } + private int GenMoreOutbounds(ProfileItem node, SingboxConfig singboxConfig) + { + if (node.subid.IsNullOrEmpty()) + { + return 0; + } + try + { + var subItem = LazyConfig.Instance.GetSubItem(node.subid); + if (subItem is null) + { + return 0; + } + + //current proxy + var outbound = singboxConfig.outbounds[0]; + var txtOutbound = Utils.GetEmbedText(Global.V2raySampleOutbound); + + //Previous proxy + var prevNode = LazyConfig.Instance.GetProfileItemViaRemarks(subItem.prevProfile!); + if (prevNode is not null + && prevNode.configType != EConfigType.Custom) + { + var prevOutbound = Utils.FromJson(txtOutbound); + GenOutbound(prevNode, prevOutbound); + prevOutbound.tag = $"{Global.ProxyTag}2"; + singboxConfig.outbounds.Add(prevOutbound); + + outbound.detour = prevOutbound.tag; + } + + //Next proxy + var nextNode = LazyConfig.Instance.GetProfileItemViaRemarks(subItem.nextProfile!); + if (nextNode is not null + && nextNode.configType != EConfigType.Custom) + { + var nextOutbound = Utils.FromJson(txtOutbound); + GenOutbound(nextNode, nextOutbound); + nextOutbound.tag = Global.ProxyTag; + singboxConfig.outbounds.Insert(0, nextOutbound); + + outbound.tag = $"{Global.ProxyTag}1"; + nextOutbound.detour = outbound.tag; + } + } + catch (Exception ex) + { + Utils.SaveLog(ex.Message, ex); + } + + return 0; + } + #endregion outbound private #region routing rule private diff --git a/v2rayN/v2rayN/Handler/CoreConfigV2ray.cs b/v2rayN/v2rayN/Handler/CoreConfigV2ray.cs index 1123cdcc..8121f7a8 100644 --- a/v2rayN/v2rayN/Handler/CoreConfigV2ray.cs +++ b/v2rayN/v2rayN/Handler/CoreConfigV2ray.cs @@ -838,12 +838,15 @@ namespace v2rayN.Handler //current proxy var outbound = v2rayConfig.outbounds[0]; + var txtOutbound = Utils.GetEmbedText(Global.V2raySampleOutbound); //Previous proxy var prevNode = LazyConfig.Instance.GetProfileItemViaRemarks(subItem.prevProfile!); - if (prevNode is not null) + if (prevNode is not null + && prevNode.configType != EConfigType.Custom + && prevNode.configType != EConfigType.Hysteria2 + && prevNode.configType != EConfigType.Tuic) { - var txtOutbound = Utils.GetEmbedText(Global.V2raySampleOutbound); var prevOutbound = Utils.FromJson(txtOutbound); GenOutbound(prevNode, prevOutbound); prevOutbound.tag = $"{Global.ProxyTag}2"; @@ -857,9 +860,11 @@ namespace v2rayN.Handler //Next proxy var nextNode = LazyConfig.Instance.GetProfileItemViaRemarks(subItem.nextProfile!); - if (nextNode is not null) + if (nextNode is not null + && nextNode.configType != EConfigType.Custom + && nextNode.configType != EConfigType.Hysteria2 + && nextNode.configType != EConfigType.Tuic) { - var txtOutbound = Utils.GetEmbedText(Global.V2raySampleOutbound); var nextOutbound = Utils.FromJson(txtOutbound); GenOutbound(nextNode, nextOutbound); nextOutbound.tag = Global.ProxyTag; diff --git a/v2rayN/v2rayN/Mode/SingboxConfig.cs b/v2rayN/v2rayN/Mode/SingboxConfig.cs index 99a914a1..8dfd8efd 100644 --- a/v2rayN/v2rayN/Mode/SingboxConfig.cs +++ b/v2rayN/v2rayN/Mode/SingboxConfig.cs @@ -105,7 +105,7 @@ public int? recv_window_conn { get; set; } public int? recv_window { get; set; } public bool? disable_mtu_discovery { get; set; } - public string detour { get; set; } + public string? detour { get; set; } public string method { get; set; } public string username { get; set; } public string password { get; set; } diff --git a/v2rayN/v2rayN/Sample/SingboxSampleClientConfig b/v2rayN/v2rayN/Sample/SingboxSampleClientConfig index ce3d9305..8e34738f 100644 --- a/v2rayN/v2rayN/Sample/SingboxSampleClientConfig +++ b/v2rayN/v2rayN/Sample/SingboxSampleClientConfig @@ -25,9 +25,18 @@ { "type": "block", "tag": "block" + }, + { + "tag": "dns_out", + "type": "dns" } ], "route": { - "rules": [] + "rules": [ + { + "protocol": [ "dns" ], + "outbound": "dns_out" + } + ] } } \ No newline at end of file diff --git a/v2rayN/v2rayN/Sample/custom_routing_white b/v2rayN/v2rayN/Sample/custom_routing_white index 7a139e31..a708ae0d 100644 --- a/v2rayN/v2rayN/Sample/custom_routing_white +++ b/v2rayN/v2rayN/Sample/custom_routing_white @@ -24,5 +24,9 @@ "geoip:private", "geoip:cn" ] + }, + { + "port": "0-65535", + "outboundTag": "proxy" } ] \ No newline at end of file diff --git a/v2rayN/v2rayN/Sample/tun_singbox_rules b/v2rayN/v2rayN/Sample/tun_singbox_rules index 93762e27..528adb76 100644 --- a/v2rayN/v2rayN/Sample/tun_singbox_rules +++ b/v2rayN/v2rayN/Sample/tun_singbox_rules @@ -1,8 +1,4 @@ [ - { - "inbound": [ "dns_in" ], - "outbound": "dns_out" - }, { "protocol": [ "dns" ], "outbound": "dns_out"