18 lines
777 B
C#
18 lines
777 B
C#
|
|
using System.Net;
|
||
|
|
using System.Net.NetworkInformation;
|
||
|
|
|
||
|
|
namespace MusicBridge.Agent.Network;
|
||
|
|
|
||
|
|
internal static class LocalAddresses
|
||
|
|
{
|
||
|
|
public static IPAddress[] Read() => NetworkInterface.GetAllNetworkInterfaces()
|
||
|
|
.Where(nic => nic.OperationalStatus == OperationalStatus.Up && nic.SupportsMulticast
|
||
|
|
&& nic.NetworkInterfaceType != NetworkInterfaceType.Loopback)
|
||
|
|
.SelectMany(nic => nic.GetIPProperties().UnicastAddresses)
|
||
|
|
.Select(a => a.Address)
|
||
|
|
.Where(IsEligible).Distinct().OrderBy(a => a.ToString(), StringComparer.Ordinal).ToArray();
|
||
|
|
|
||
|
|
public static bool IsEligible(IPAddress ip) => ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork
|
||
|
|
&& ConnectionInvitation.IsLocalEndpoint(new Uri($"https://{ip}"));
|
||
|
|
}
|