123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Net.NetworkInformation;
- using System.Net.Sockets;
- namespace FastGithub.Configuration
- {
- /// <summary>
- /// 提供本机设备信息
- /// </summary>
- public static class LocalMachine
- {
- /// <summary>
- /// 获取设备名
- /// </summary>
- public static string Name => Environment.MachineName;
- /// <summary>
- /// 获取本机设备所有IP
- /// </summary>
- /// <returns></returns>
- public static IEnumerable<IPAddress> GetAllIPAddresses()
- {
- yield return IPAddress.Loopback;
- yield return IPAddress.IPv6Loopback;
- foreach (var @interface in NetworkInterface.GetAllNetworkInterfaces())
- {
- foreach (var addressInfo in @interface.GetIPProperties().UnicastAddresses)
- {
- yield return addressInfo.Address;
- }
- }
- }
- /// <summary>
- /// 获取本机设备所有IPv4
- /// </summary>
- /// <returns></returns>
- public static IEnumerable<IPAddress> GetAllIPv4Addresses()
- {
- foreach (var address in GetAllIPAddresses())
- {
- if (address.AddressFamily == AddressFamily.InterNetwork)
- {
- yield return address;
- }
- }
- }
- /// <summary>
- /// 返回本机设备是否包含指定IP
- /// </summary>
- /// <param name="address"></param>
- /// <returns></returns>
- public static bool ContainsIPAddress(IPAddress address)
- {
- return GetAllIPAddresses().Contains(address);
- }
- /// <summary>
- /// 获取所有域名和ip
- /// </summary>
- /// <returns></returns>
- public static HashSet<string> GetAllHostNames()
- {
- var hashSet = new HashSet<string>
- {
- Name,
- "localhost",
- };
- foreach (var address in GetAllIPAddresses())
- {
- hashSet.Add(address.ToString());
- }
- return hashSet;
- }
- /// <summary>
- /// 获取与远程节点通讯的的本机IP地址
- /// </summary>
- /// <param name="remoteEndPoint">远程地址</param>
- /// <returns></returns>
- public static IPAddress? GetLocalIPAddress(EndPoint remoteEndPoint)
- {
- try
- {
- using var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- socket.Connect(remoteEndPoint);
- return socket.LocalEndPoint is IPEndPoint localEndPoint ? localEndPoint.Address : default;
- }
- catch (Exception)
- {
- return default;
- }
- }
- /// <summary>
- /// 获取可用的随机端口
- /// </summary>
- /// <param name="addressFamily"></param>
- /// <param name="min">最小值</param>
- /// <returns></returns>
- public static int GetAvailablePort(AddressFamily addressFamily, int min = 1024)
- {
- var hashSet = new HashSet<int>();
- var tcpListeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners();
- var udpListeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveUdpListeners();
- foreach (var item in tcpListeners)
- {
- if (item.AddressFamily == addressFamily)
- {
- hashSet.Add(item.Port);
- }
- }
- foreach (var item in udpListeners)
- {
- if (item.AddressFamily == addressFamily)
- {
- hashSet.Add(item.Port);
- }
- }
- for (var port = min; port < ushort.MaxValue; port++)
- {
- if (hashSet.Contains(port) == false)
- {
- return port;
- }
- }
- throw new FastGithubException("当前无可用的端口");
- }
- /// <summary>
- /// 是否可以监听指定tcp端口
- /// </summary>
- /// <param name="port"></param>
- /// <returns></returns>
- public static bool CanListenTcp(int port)
- {
- var tcpListeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners();
- return tcpListeners.Any(item => item.Port == port) == false;
- }
- /// <summary>
- /// 是否可以监听指定udp端口
- /// </summary>
- /// <param name="port"></param>
- /// <returns></returns>
- public static bool CanListenUdp(int port)
- {
- var udpListeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveUdpListeners();
- return udpListeners.Any(item => item.Port == port) == false;
- }
- }
- }
|