Centralize active network info retrieval

This commit is contained in:
2dust 2026-02-26 17:34:57 +08:00
parent b53507f486
commit 7931058342
8 changed files with 32 additions and 32 deletions

View file

@ -629,12 +629,7 @@ public class Utils
{ {
try try
{ {
List<IPEndPoint> lstIpEndPoints = new(); var (lstIpEndPoints, lstTcpConns) = GetActiveNetworkInfo();
List<TcpConnectionInformation> lstTcpConns = new();
lstIpEndPoints.AddRange(IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners());
lstIpEndPoints.AddRange(IPGlobalProperties.GetIPGlobalProperties().GetActiveUdpListeners());
lstTcpConns.AddRange(IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpConnections());
if (lstIpEndPoints?.FindIndex(it => it.Port == port) >= 0) if (lstIpEndPoints?.FindIndex(it => it.Port == port) >= 0)
{ {
@ -676,6 +671,27 @@ public class Utils
return 59090; return 59090;
} }
public static (List<IPEndPoint> endpoints, List<TcpConnectionInformation> connections) GetActiveNetworkInfo()
{
var endpoints = new List<IPEndPoint>();
var connections = new List<TcpConnectionInformation>();
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 #endregion Speed Test
#region Miscellaneous #region Miscellaneous

View file

@ -72,7 +72,8 @@ public class CoreManager
{ {
context = context with context = context with
{ {
TunProtectSsPort = preContext.TunProtectSsPort, ProxyRelaySsPort = preContext.ProxyRelaySsPort, TunProtectSsPort = preContext.TunProtectSsPort,
ProxyRelaySsPort = preContext.ProxyRelaySsPort,
}; };
} }
var result = await CoreConfigHandler.GenerateClientConfig(context, fileName); var result = await CoreConfigHandler.GenerateClientConfig(context, fileName);

View file

@ -257,6 +257,7 @@ public class Server4Sbox : BaseServer4Sbox
// Deprecated in sing-box 1.12.0 , kept for backward compatibility // Deprecated in sing-box 1.12.0 , kept for backward compatibility
public string? address { get; set; } public string? address { get; set; }
public string? address_resolver { get; set; } public string? address_resolver { get; set; }
public string? address_strategy { get; set; } public string? address_strategy { get; set; }
public string? strategy { get; set; } public string? strategy { get; set; }

View file

@ -137,18 +137,8 @@ public partial class CoreConfigSingboxService(CoreConfigContext context)
ret.Msg = ResUI.FailedGenDefaultConfiguration; ret.Msg = ResUI.FailedGenDefaultConfiguration;
return ret; return ret;
} }
List<IPEndPoint> lstIpEndPoints = new();
List<TcpConnectionInformation> lstTcpConns = new(); var (lstIpEndPoints, lstTcpConns) = Utils.GetActiveNetworkInfo();
try
{
lstIpEndPoints.AddRange(IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners());
lstIpEndPoints.AddRange(IPGlobalProperties.GetIPGlobalProperties().GetActiveUdpListeners());
lstTcpConns.AddRange(IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpConnections());
}
catch (Exception ex)
{
Logging.SaveLog(_tag, ex);
}
GenLog(); GenLog();
GenMinimizedDns(); GenMinimizedDns();

View file

@ -43,6 +43,7 @@ public partial class CoreConfigSingboxService
case EConfigType.PolicyGroup: case EConfigType.PolicyGroup:
proxyOutboundList = BuildOutboundsList(baseTagName); proxyOutboundList = BuildOutboundsList(baseTagName);
break; break;
case EConfigType.ProxyChain: case EConfigType.ProxyChain:
proxyOutboundList = BuildChainOutboundsList(baseTagName); proxyOutboundList = BuildChainOutboundsList(baseTagName);
break; break;

View file

@ -94,18 +94,8 @@ public partial class CoreConfigV2rayService(CoreConfigContext context)
ret.Msg = ResUI.FailedGenDefaultConfiguration; ret.Msg = ResUI.FailedGenDefaultConfiguration;
return ret; return ret;
} }
List<IPEndPoint> lstIpEndPoints = new();
List<TcpConnectionInformation> lstTcpConns = new(); var (lstIpEndPoints, lstTcpConns) = Utils.GetActiveNetworkInfo();
try
{
lstIpEndPoints.AddRange(IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners());
lstIpEndPoints.AddRange(IPGlobalProperties.GetIPGlobalProperties().GetActiveUdpListeners());
lstTcpConns.AddRange(IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpConnections());
}
catch (Exception ex)
{
Logging.SaveLog(_tag, ex);
}
GenLog(); GenLog();
_coreConfig.inbounds.Clear(); _coreConfig.inbounds.Clear();

View file

@ -68,6 +68,7 @@ public partial class CoreConfigV2rayService
case EConfigType.PolicyGroup: case EConfigType.PolicyGroup:
proxyOutboundList.AddRange(BuildOutboundsList(baseTagName)); proxyOutboundList.AddRange(BuildOutboundsList(baseTagName));
break; break;
case EConfigType.ProxyChain: case EConfigType.ProxyChain:
proxyOutboundList.AddRange(BuildChainOutboundsList(baseTagName)); proxyOutboundList.AddRange(BuildChainOutboundsList(baseTagName));
break; break;