From 7931058342aa2ef9c713f5309cf75aeb6e21d199 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Thu, 26 Feb 2026 17:34:57 +0800 Subject: [PATCH] Centralize active network info retrieval --- v2rayN/ServiceLib/Common/Utils.cs | 28 +++++++++++++++---- v2rayN/ServiceLib/Manager/CoreManager.cs | 3 +- v2rayN/ServiceLib/Models/CoreConfigContext.cs | 2 +- v2rayN/ServiceLib/Models/SingboxConfig.cs | 1 + .../Singbox/CoreConfigSingboxService.cs | 14 ++-------- .../Singbox/SingboxOutboundService.cs | 1 + .../V2ray/CoreConfigV2rayService.cs | 14 ++-------- .../CoreConfig/V2ray/V2rayOutboundService.cs | 1 + 8 files changed, 32 insertions(+), 32 deletions(-) diff --git a/v2rayN/ServiceLib/Common/Utils.cs b/v2rayN/ServiceLib/Common/Utils.cs index 70365e99..75ef4627 100644 --- a/v2rayN/ServiceLib/Common/Utils.cs +++ b/v2rayN/ServiceLib/Common/Utils.cs @@ -629,12 +629,7 @@ public class Utils { try { - List lstIpEndPoints = new(); - List lstTcpConns = new(); - - lstIpEndPoints.AddRange(IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners()); - lstIpEndPoints.AddRange(IPGlobalProperties.GetIPGlobalProperties().GetActiveUdpListeners()); - lstTcpConns.AddRange(IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpConnections()); + var (lstIpEndPoints, lstTcpConns) = GetActiveNetworkInfo(); if (lstIpEndPoints?.FindIndex(it => it.Port == port) >= 0) { @@ -676,6 +671,27 @@ public class Utils return 59090; } + public static (List endpoints, List connections) GetActiveNetworkInfo() + { + var endpoints = new List(); + var connections = new List(); + + try + { + var ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties(); + + endpoints.AddRange(ipGlobalProperties.GetActiveTcpListeners()); + endpoints.AddRange(ipGlobalProperties.GetActiveUdpListeners()); + connections.AddRange(ipGlobalProperties.GetActiveTcpConnections()); + } + catch (Exception ex) + { + Logging.SaveLog(_tag, ex); + } + + return (endpoints, connections); + } + #endregion Speed Test #region Miscellaneous diff --git a/v2rayN/ServiceLib/Manager/CoreManager.cs b/v2rayN/ServiceLib/Manager/CoreManager.cs index 3efa4592..159af85e 100644 --- a/v2rayN/ServiceLib/Manager/CoreManager.cs +++ b/v2rayN/ServiceLib/Manager/CoreManager.cs @@ -72,7 +72,8 @@ public class CoreManager { context = context with { - TunProtectSsPort = preContext.TunProtectSsPort, ProxyRelaySsPort = preContext.ProxyRelaySsPort, + TunProtectSsPort = preContext.TunProtectSsPort, + ProxyRelaySsPort = preContext.ProxyRelaySsPort, }; } var result = await CoreConfigHandler.GenerateClientConfig(context, fileName); diff --git a/v2rayN/ServiceLib/Models/CoreConfigContext.cs b/v2rayN/ServiceLib/Models/CoreConfigContext.cs index bdf2deac..a0a43a20 100644 --- a/v2rayN/ServiceLib/Models/CoreConfigContext.cs +++ b/v2rayN/ServiceLib/Models/CoreConfigContext.cs @@ -16,7 +16,7 @@ public record CoreConfigContext // TUN Compatibility public bool IsTunEnabled { get; init; } = false; public HashSet ProtectDomainList { get; init; } = new(); - // -> tun inbound --(if routing proxy)--> relay outbound + // -> tun inbound --(if routing proxy)--> relay outbound // -> proxy core (relay inbound --> proxy outbound --(dialerProxy)--> protect outbound) // -> protect inbound -> direct proxy outbound data -> internet public int TunProtectSsPort { get; init; } = 0; diff --git a/v2rayN/ServiceLib/Models/SingboxConfig.cs b/v2rayN/ServiceLib/Models/SingboxConfig.cs index c1eaa3c8..02deaabf 100644 --- a/v2rayN/ServiceLib/Models/SingboxConfig.cs +++ b/v2rayN/ServiceLib/Models/SingboxConfig.cs @@ -257,6 +257,7 @@ public class Server4Sbox : BaseServer4Sbox // Deprecated in sing-box 1.12.0 , kept for backward compatibility public string? address { get; set; } + public string? address_resolver { get; set; } public string? address_strategy { get; set; } public string? strategy { get; set; } diff --git a/v2rayN/ServiceLib/Services/CoreConfig/Singbox/CoreConfigSingboxService.cs b/v2rayN/ServiceLib/Services/CoreConfig/Singbox/CoreConfigSingboxService.cs index f4481b78..87b59e11 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/Singbox/CoreConfigSingboxService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/Singbox/CoreConfigSingboxService.cs @@ -137,18 +137,8 @@ public partial class CoreConfigSingboxService(CoreConfigContext context) ret.Msg = ResUI.FailedGenDefaultConfiguration; return ret; } - List lstIpEndPoints = new(); - List lstTcpConns = new(); - try - { - lstIpEndPoints.AddRange(IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners()); - lstIpEndPoints.AddRange(IPGlobalProperties.GetIPGlobalProperties().GetActiveUdpListeners()); - lstTcpConns.AddRange(IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpConnections()); - } - catch (Exception ex) - { - Logging.SaveLog(_tag, ex); - } + + var (lstIpEndPoints, lstTcpConns) = Utils.GetActiveNetworkInfo(); GenLog(); GenMinimizedDns(); diff --git a/v2rayN/ServiceLib/Services/CoreConfig/Singbox/SingboxOutboundService.cs b/v2rayN/ServiceLib/Services/CoreConfig/Singbox/SingboxOutboundService.cs index 23b331f6..40f8951d 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/Singbox/SingboxOutboundService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/Singbox/SingboxOutboundService.cs @@ -43,6 +43,7 @@ public partial class CoreConfigSingboxService case EConfigType.PolicyGroup: proxyOutboundList = BuildOutboundsList(baseTagName); break; + case EConfigType.ProxyChain: proxyOutboundList = BuildChainOutboundsList(baseTagName); break; diff --git a/v2rayN/ServiceLib/Services/CoreConfig/V2ray/CoreConfigV2rayService.cs b/v2rayN/ServiceLib/Services/CoreConfig/V2ray/CoreConfigV2rayService.cs index a9f6e053..f4c9c852 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/V2ray/CoreConfigV2rayService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/V2ray/CoreConfigV2rayService.cs @@ -94,18 +94,8 @@ public partial class CoreConfigV2rayService(CoreConfigContext context) ret.Msg = ResUI.FailedGenDefaultConfiguration; return ret; } - List lstIpEndPoints = new(); - List lstTcpConns = new(); - try - { - lstIpEndPoints.AddRange(IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners()); - lstIpEndPoints.AddRange(IPGlobalProperties.GetIPGlobalProperties().GetActiveUdpListeners()); - lstTcpConns.AddRange(IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpConnections()); - } - catch (Exception ex) - { - Logging.SaveLog(_tag, ex); - } + + var (lstIpEndPoints, lstTcpConns) = Utils.GetActiveNetworkInfo(); GenLog(); _coreConfig.inbounds.Clear(); diff --git a/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayOutboundService.cs b/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayOutboundService.cs index 05d61d16..c8862648 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayOutboundService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayOutboundService.cs @@ -68,6 +68,7 @@ public partial class CoreConfigV2rayService case EConfigType.PolicyGroup: proxyOutboundList.AddRange(BuildOutboundsList(baseTagName)); break; + case EConfigType.ProxyChain: proxyOutboundList.AddRange(BuildChainOutboundsList(baseTagName)); break;