mirror of
https://gitea.tendokyu.moe/Kayori/Medusa.net.git
synced 2026-10-04 12:47:53 +03:00
32 lines
922 B
C#
32 lines
922 B
C#
using System.Linq;
|
|
using System.Net;
|
|
using System.Net.NetworkInformation;
|
|
using System.Net.Sockets;
|
|
|
|
namespace Abstractions.Utils;
|
|
|
|
public static class IpUtils
|
|
{
|
|
public static string? GetLocalIPv4()
|
|
{
|
|
var networkInterfaces = NetworkInterface.GetAllNetworkInterfaces()
|
|
.Where(ni =>
|
|
ni.OperationalStatus == OperationalStatus.Up &&
|
|
ni.NetworkInterfaceType != NetworkInterfaceType.Loopback)
|
|
.ToList();
|
|
|
|
foreach (var ni in networkInterfaces)
|
|
{
|
|
var ipProps = ni.GetIPProperties();
|
|
foreach (var addr in ipProps.UnicastAddresses)
|
|
{
|
|
if (addr.Address.AddressFamily != AddressFamily.InterNetwork ||
|
|
IPAddress.IsLoopback(addr.Address)) continue;
|
|
var s = addr.Address.ToString();
|
|
return s;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
} |