增加LocalMachine类型
This commit is contained in:
parent
e43778c505
commit
359ac01033
@ -1,5 +1,4 @@
|
|||||||
using System.Diagnostics.CodeAnalysis;
|
using System.Diagnostics.CodeAnalysis;
|
||||||
using System.Linq;
|
|
||||||
using System.Net;
|
using System.Net;
|
||||||
|
|
||||||
namespace FastGithub.Configuration
|
namespace FastGithub.Configuration
|
||||||
@ -32,7 +31,7 @@ namespace FastGithub.Configuration
|
|||||||
throw new FastGithubException($"无效的ip:{this.IPAddress}");
|
throw new FastGithubException($"无效的ip:{this.IPAddress}");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.Port == 53 && IsLocalIPAddress(address))
|
if (this.Port == 53 && LocalMachine.ContainsIPAddress(address))
|
||||||
{
|
{
|
||||||
throw new FastGithubException($"配置的dns值不能指向{nameof(FastGithub)}自身:{this.IPAddress}:{this.Port}");
|
throw new FastGithubException($"配置的dns值不能指向{nameof(FastGithub)}自身:{this.IPAddress}:{this.Port}");
|
||||||
}
|
}
|
||||||
@ -44,24 +43,5 @@ namespace FastGithub.Configuration
|
|||||||
{
|
{
|
||||||
return $"{this.IPAddress}:{this.Port}";
|
return $"{this.IPAddress}:{this.Port}";
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 是否为本机ip
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="address"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
private static bool IsLocalIPAddress(IPAddress 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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
82
FastGithub.Configuration/LocalMachine.cs
Normal file
82
FastGithub.Configuration/LocalMachine.cs
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
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>
|
||||||
|
/// 获取对应的本机地址
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="remoteEndPoint">远程地址</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static IPAddress? GetLocalAddress(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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,11 +1,10 @@
|
|||||||
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.Sockets;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace FastGithub.Dns
|
namespace FastGithub.Dns
|
||||||
@ -48,7 +47,7 @@ namespace FastGithub.Dns
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var localAddresses = await GetLocalIPv4AddressesAsync();
|
var localAddresses = LocalMachine.GetAllIPv4Addresses().ToArray();
|
||||||
var lines = await File.ReadAllLinesAsync(hostsPath);
|
var lines = await File.ReadAllLinesAsync(hostsPath);
|
||||||
foreach (var line in lines)
|
foreach (var line in lines)
|
||||||
{
|
{
|
||||||
@ -67,30 +66,6 @@ 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文件记录
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@ -1,7 +1,6 @@
|
|||||||
using DNS.Protocol;
|
using DNS.Protocol;
|
||||||
using System;
|
using FastGithub.Configuration;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Sockets;
|
|
||||||
|
|
||||||
namespace FastGithub.Dns
|
namespace FastGithub.Dns
|
||||||
{
|
{
|
||||||
@ -32,16 +31,7 @@ namespace FastGithub.Dns
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public IPAddress? GetLocalAddress()
|
public IPAddress? GetLocalAddress()
|
||||||
{
|
{
|
||||||
try
|
return LocalMachine.GetLocalAddress(this.RemoteEndPoint);
|
||||||
{
|
|
||||||
using var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
|
|
||||||
socket.Connect(this.RemoteEndPoint);
|
|
||||||
return socket.LocalEndPoint is IPEndPoint localEndPoint ? localEndPoint.Address : default;
|
|
||||||
}
|
|
||||||
catch (Exception)
|
|
||||||
{
|
|
||||||
return default;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
using FastGithub.ReverseProxy;
|
using FastGithub.Configuration;
|
||||||
|
using FastGithub.ReverseProxy;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.AspNetCore.Server.Kestrel.Core;
|
using Microsoft.AspNetCore.Server.Kestrel.Core;
|
||||||
using Microsoft.Extensions.Caching.Memory;
|
using Microsoft.Extensions.Caching.Memory;
|
||||||
@ -11,7 +12,6 @@ using System.IO;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.NetworkInformation;
|
using System.Net.NetworkInformation;
|
||||||
using System.Net.Sockets;
|
|
||||||
using System.Security.Cryptography.X509Certificates;
|
using System.Security.Cryptography.X509Certificates;
|
||||||
|
|
||||||
namespace FastGithub
|
namespace FastGithub
|
||||||
@ -196,16 +196,10 @@ namespace FastGithub
|
|||||||
yield break;
|
yield break;
|
||||||
}
|
}
|
||||||
|
|
||||||
var hostName = Dns.GetHostName();
|
yield return LocalMachine.Name;
|
||||||
yield return hostName;
|
foreach (var address in LocalMachine.GetAllIPv4Addresses())
|
||||||
yield return IPAddress.Loopback.ToString();
|
|
||||||
|
|
||||||
foreach (var address in Dns.GetHostAddresses(hostName))
|
|
||||||
{
|
{
|
||||||
if (address.AddressFamily == AddressFamily.InterNetwork)
|
yield return address.ToString();
|
||||||
{
|
|
||||||
yield return address.ToString();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user