mirror of
https://github.com/dotnetcore/DotnetSpider.git
synced 2026-10-28 13:50:21 +08:00
@@ -68,13 +68,7 @@ namespace DotnetSpider.Common
|
||||
TotalMemory = (int) (infoDic["MemTotal:"] / 1024);
|
||||
}
|
||||
|
||||
var networkInterface = NetworkInterface.GetAllNetworkInterfaces()
|
||||
.First(i => i.NetworkInterfaceType == NetworkInterfaceType.Ethernet ||
|
||||
i.NetworkInterfaceType == NetworkInterfaceType.Wireless80211);
|
||||
var unicastAddresses = networkInterface.GetIPProperties().UnicastAddresses;
|
||||
IpAddress = unicastAddresses.First(a =>
|
||||
a.IPv4Mask.ToString() != "255.255.255.255" && a.Address.AddressFamily == AddressFamily.InterNetwork)
|
||||
.Address.ToString();
|
||||
IpAddress = NetworkHelper.CurrentIpAddress();
|
||||
|
||||
OsDescription = $"{Environment.OSVersion.Platform} {Environment.OSVersion.Version}";
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.NetworkInformation;
|
||||
using System.Net.Sockets;
|
||||
|
||||
namespace DotnetSpider.Common
|
||||
{
|
||||
public static class NetworkHelper
|
||||
{
|
||||
public static string CurrentIpAddress()
|
||||
{
|
||||
var networkInterfaces = NetworkInterface.GetAllNetworkInterfaces()
|
||||
.Where(i => i.NetworkInterfaceType == NetworkInterfaceType.Ethernet ||
|
||||
i.NetworkInterfaceType == NetworkInterfaceType.Wireless80211);
|
||||
var unicastAddresses = networkInterfaces
|
||||
.Select(i => i.GetIPProperties())
|
||||
.SelectMany(p => p.UnicastAddresses);
|
||||
var addressInfo = unicastAddresses
|
||||
.Where(a => a.IPv4Mask.ToString() != "255.255.255.255" &&
|
||||
a.Address.AddressFamily == AddressFamily.InterNetwork &&
|
||||
!IPAddress.IsLoopback(a.Address))
|
||||
.FirstOrDefault();
|
||||
var ipAddress = addressInfo?.Address.ToString() ?? "127.0.0.1";
|
||||
return ipAddress;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user