1
0
mirror of https://github.com/chylex/Discord-History-Tracker.git synced 2024-10-17 09:42:44 +02:00
Discord-History-Tracker/app/Server/Service/ServerUtils.cs
2023-07-21 18:47:21 +02:00

24 lines
661 B
C#

using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
namespace DHT.Server.Service;
public static partial class ServerUtils {
public static ushort FindAvailablePort(ushort min, ushort max) {
var properties = IPGlobalProperties.GetIPGlobalProperties();
var occupied = new HashSet<int>();
occupied.UnionWith(properties.GetActiveTcpListeners().Select(static tcp => tcp.Port));
occupied.UnionWith(properties.GetActiveTcpConnections().Select(static tcp => tcp.LocalEndPoint.Port));
for (int port = min; port < max; port++) {
if (!occupied.Contains(port)) {
return (ushort) port;
}
}
return min;
}
}