mirror of
https://github.com/chylex/Discord-History-Tracker.git
synced 2025-01-06 22:42:49 +01:00
24 lines
661 B
C#
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;
|
|
}
|
|
|
|
}
|