解决获取本机ip慢的问题
This commit is contained in:
parent
566930208b
commit
da2993a6d9
@ -1,7 +1,6 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.NetworkInformation;
|
||||
|
||||
namespace FastGithub.Configuration
|
||||
{
|
||||
@ -33,7 +32,7 @@ namespace FastGithub.Configuration
|
||||
throw new FastGithubException($"无效的ip:{this.IPAddress}");
|
||||
}
|
||||
|
||||
if (this.Port == 53 && IsLocalMachineIPAddress(address))
|
||||
if (this.Port == 53 && IsLocalIPAddress(address))
|
||||
{
|
||||
throw new FastGithubException($"配置的dns值不能指向{nameof(FastGithub)}自身:{this.IPAddress}:{this.Port}");
|
||||
}
|
||||
@ -51,12 +50,18 @@ namespace FastGithub.Configuration
|
||||
/// </summary>
|
||||
/// <param name="address"></param>
|
||||
/// <returns></returns>
|
||||
private static bool IsLocalMachineIPAddress(IPAddress address)
|
||||
private static bool IsLocalIPAddress(IPAddress address)
|
||||
{
|
||||
return IPGlobalProperties
|
||||
.GetIPGlobalProperties()
|
||||
.GetUnicastAddresses()
|
||||
.Any(item => item.Address.Equals(address));
|
||||
if (address.Equals(System.Net.IPAddress.Loopback))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (address.Equals(System.Net.IPAddress.IPv6Loopback))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
var addresses = Dns.GetHostAddresses(Dns.GetHostName());
|
||||
return addresses.Contains(address);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,11 +1,11 @@
|
||||
using FastGithub.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.NetworkInformation;
|
||||
using System.Net.Sockets;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace FastGithub.Dns
|
||||
@ -48,12 +48,7 @@ namespace FastGithub.Dns
|
||||
return;
|
||||
}
|
||||
|
||||
var localAddresses = IPGlobalProperties
|
||||
.GetIPGlobalProperties()
|
||||
.GetUnicastAddresses()
|
||||
.Select(item => item.Address)
|
||||
.ToArray();
|
||||
|
||||
var localAddresses = await GetLocalIPv4AddressesAsync();
|
||||
var lines = await File.ReadAllLinesAsync(hostsPath);
|
||||
foreach (var line in lines)
|
||||
{
|
||||
@ -72,6 +67,29 @@ namespace FastGithub.Dns
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取本机所有IPv4
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private static async Task<HashSet<IPAddress>> GetLocalIPv4AddressesAsync()
|
||||
{
|
||||
var hashSet = new HashSet<IPAddress>
|
||||
{
|
||||
IPAddress.Loopback
|
||||
};
|
||||
|
||||
var hostName = System.Net.Dns.GetHostName();
|
||||
var addresses = await System.Net.Dns.GetHostAddressesAsync(hostName);
|
||||
foreach (var address in addresses)
|
||||
{
|
||||
if (address.AddressFamily == AddressFamily.InterNetwork)
|
||||
{
|
||||
hashSet.Add(address);
|
||||
}
|
||||
}
|
||||
return hashSet;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// hosts文件记录
|
||||
|
||||
@ -187,17 +187,14 @@ namespace FastGithub
|
||||
yield break;
|
||||
}
|
||||
|
||||
var globalPropreties = IPGlobalProperties.GetIPGlobalProperties();
|
||||
if (string.IsNullOrEmpty(globalPropreties.HostName) == false)
|
||||
{
|
||||
yield return globalPropreties.HostName;
|
||||
}
|
||||
var hostName = Dns.GetHostName();
|
||||
yield return hostName;
|
||||
|
||||
foreach (var item in globalPropreties.GetUnicastAddresses())
|
||||
foreach (var address in Dns.GetHostAddresses(hostName))
|
||||
{
|
||||
if (item.Address.AddressFamily == AddressFamily.InterNetwork)
|
||||
if (address.AddressFamily == AddressFamily.InterNetwork)
|
||||
{
|
||||
yield return item.Address.ToString();
|
||||
yield return address.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user