Skip to content

Commit 7931058

Browse files
committed
Centralize active network info retrieval
1 parent b53507f commit 7931058

8 files changed

Lines changed: 32 additions & 32 deletions

File tree

v2rayN/ServiceLib/Common/Utils.cs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -629,12 +629,7 @@ private static bool PortInUse(int port)
629629
{
630630
try
631631
{
632-
List<IPEndPoint> lstIpEndPoints = new();
633-
List<TcpConnectionInformation> lstTcpConns = new();
634-
635-
lstIpEndPoints.AddRange(IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners());
636-
lstIpEndPoints.AddRange(IPGlobalProperties.GetIPGlobalProperties().GetActiveUdpListeners());
637-
lstTcpConns.AddRange(IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpConnections());
632+
var (lstIpEndPoints, lstTcpConns) = GetActiveNetworkInfo();
638633

639634
if (lstIpEndPoints?.FindIndex(it => it.Port == port) >= 0)
640635
{
@@ -676,6 +671,27 @@ public static int GetFreePort(int defaultPort = 0)
676671
return 59090;
677672
}
678673

674+
public static (List<IPEndPoint> endpoints, List<TcpConnectionInformation> connections) GetActiveNetworkInfo()
675+
{
676+
var endpoints = new List<IPEndPoint>();
677+
var connections = new List<TcpConnectionInformation>();
678+
679+
try
680+
{
681+
var ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();
682+
683+
endpoints.AddRange(ipGlobalProperties.GetActiveTcpListeners());
684+
endpoints.AddRange(ipGlobalProperties.GetActiveUdpListeners());
685+
connections.AddRange(ipGlobalProperties.GetActiveTcpConnections());
686+
}
687+
catch (Exception ex)
688+
{
689+
Logging.SaveLog(_tag, ex);
690+
}
691+
692+
return (endpoints, connections);
693+
}
694+
679695
#endregion Speed Test
680696

681697
#region Miscellaneous

v2rayN/ServiceLib/Manager/CoreManager.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ public async Task LoadCore(ProfileItem? node)
7272
{
7373
context = context with
7474
{
75-
TunProtectSsPort = preContext.TunProtectSsPort, ProxyRelaySsPort = preContext.ProxyRelaySsPort,
75+
TunProtectSsPort = preContext.TunProtectSsPort,
76+
ProxyRelaySsPort = preContext.ProxyRelaySsPort,
7677
};
7778
}
7879
var result = await CoreConfigHandler.GenerateClientConfig(context, fileName);

v2rayN/ServiceLib/Models/CoreConfigContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public record CoreConfigContext
1616
// TUN Compatibility
1717
public bool IsTunEnabled { get; init; } = false;
1818
public HashSet<string> ProtectDomainList { get; init; } = new();
19-
// -> tun inbound --(if routing proxy)--> relay outbound
19+
// -> tun inbound --(if routing proxy)--> relay outbound
2020
// -> proxy core (relay inbound --> proxy outbound --(dialerProxy)--> protect outbound)
2121
// -> protect inbound -> direct proxy outbound data -> internet
2222
public int TunProtectSsPort { get; init; } = 0;

v2rayN/ServiceLib/Models/SingboxConfig.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ public class Server4Sbox : BaseServer4Sbox
257257

258258
// Deprecated in sing-box 1.12.0 , kept for backward compatibility
259259
public string? address { get; set; }
260+
260261
public string? address_resolver { get; set; }
261262
public string? address_strategy { get; set; }
262263
public string? strategy { get; set; }

v2rayN/ServiceLib/Services/CoreConfig/Singbox/CoreConfigSingboxService.cs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -137,18 +137,8 @@ public RetResult GenerateClientSpeedtestConfig(List<ServerTestItem> selecteds)
137137
ret.Msg = ResUI.FailedGenDefaultConfiguration;
138138
return ret;
139139
}
140-
List<IPEndPoint> lstIpEndPoints = new();
141-
List<TcpConnectionInformation> lstTcpConns = new();
142-
try
143-
{
144-
lstIpEndPoints.AddRange(IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners());
145-
lstIpEndPoints.AddRange(IPGlobalProperties.GetIPGlobalProperties().GetActiveUdpListeners());
146-
lstTcpConns.AddRange(IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpConnections());
147-
}
148-
catch (Exception ex)
149-
{
150-
Logging.SaveLog(_tag, ex);
151-
}
140+
141+
var (lstIpEndPoints, lstTcpConns) = Utils.GetActiveNetworkInfo();
152142

153143
GenLog();
154144
GenMinimizedDns();

v2rayN/ServiceLib/Services/CoreConfig/Singbox/SingboxOutboundService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ private List<BaseServer4Sbox> BuildGroupProxyOutbounds(string baseTagName = Glob
4343
case EConfigType.PolicyGroup:
4444
proxyOutboundList = BuildOutboundsList(baseTagName);
4545
break;
46+
4647
case EConfigType.ProxyChain:
4748
proxyOutboundList = BuildChainOutboundsList(baseTagName);
4849
break;

v2rayN/ServiceLib/Services/CoreConfig/V2ray/CoreConfigV2rayService.cs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -94,18 +94,8 @@ public RetResult GenerateClientSpeedtestConfig(List<ServerTestItem> selecteds)
9494
ret.Msg = ResUI.FailedGenDefaultConfiguration;
9595
return ret;
9696
}
97-
List<IPEndPoint> lstIpEndPoints = new();
98-
List<TcpConnectionInformation> lstTcpConns = new();
99-
try
100-
{
101-
lstIpEndPoints.AddRange(IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners());
102-
lstIpEndPoints.AddRange(IPGlobalProperties.GetIPGlobalProperties().GetActiveUdpListeners());
103-
lstTcpConns.AddRange(IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpConnections());
104-
}
105-
catch (Exception ex)
106-
{
107-
Logging.SaveLog(_tag, ex);
108-
}
97+
98+
var (lstIpEndPoints, lstTcpConns) = Utils.GetActiveNetworkInfo();
10999

110100
GenLog();
111101
_coreConfig.inbounds.Clear();

v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayOutboundService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ private List<Outbounds4Ray> BuildGroupProxyOutbounds(string baseTagName = Global
6868
case EConfigType.PolicyGroup:
6969
proxyOutboundList.AddRange(BuildOutboundsList(baseTagName));
7070
break;
71+
7172
case EConfigType.ProxyChain:
7273
proxyOutboundList.AddRange(BuildChainOutboundsList(baseTagName));
7374
break;

0 commit comments

Comments
 (0)