mirror of
https://github.com/chylex/Discord-History-Tracker.git
synced 2024-12-22 23:42:50 +01:00
41 lines
1.2 KiB
C#
41 lines
1.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net.NetworkInformation;
|
|
using System.Security.Cryptography;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace DHT.Server.Service {
|
|
public static class ServerUtils {
|
|
public static int FindAvailablePort(int min, int 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 port;
|
|
}
|
|
}
|
|
|
|
return min;
|
|
}
|
|
|
|
private static Regex TokenFilter { get; } = new("[^25679bcdfghjkmnpqrstwxyz]", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
|
|
|
|
public static string GenerateRandomToken(int length) {
|
|
byte[] bytes = new byte[length * 3 / 2]; // Extra bytes compensate for filtered out characters.
|
|
var rng = new RNGCryptoServiceProvider();
|
|
|
|
string token = "";
|
|
while (token.Length < length) {
|
|
rng.GetBytes(bytes);
|
|
token = TokenFilter.Replace(Convert.ToBase64String(bytes), "");
|
|
}
|
|
|
|
return token[..length];
|
|
}
|
|
}
|
|
}
|