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