diff --git a/FastGithub.Configuration/DnsConfig.cs b/FastGithub.Configuration/DnsConfig.cs
index c8febce..21dd80e 100644
--- a/FastGithub.Configuration/DnsConfig.cs
+++ b/FastGithub.Configuration/DnsConfig.cs
@@ -1,5 +1,4 @@
using System.Diagnostics.CodeAnalysis;
-using System.Linq;
using System.Net;
namespace FastGithub.Configuration
@@ -32,7 +31,7 @@ namespace FastGithub.Configuration
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}");
}
@@ -44,24 +43,5 @@ namespace FastGithub.Configuration
{
return $"{this.IPAddress}:{this.Port}";
}
-
- ///
- /// 是否为本机ip
- ///
- ///
- ///
- 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);
- }
}
}
diff --git a/FastGithub.Configuration/LocalMachine.cs b/FastGithub.Configuration/LocalMachine.cs
new file mode 100644
index 0000000..a3d1c2f
--- /dev/null
+++ b/FastGithub.Configuration/LocalMachine.cs
@@ -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
+{
+ ///
+ /// 提供本机设备信息
+ ///
+ public static class LocalMachine
+ {
+ ///
+ /// 获取设备名
+ ///
+ public static string Name => Environment.MachineName;
+
+ ///
+ /// 获取设备所有IP
+ ///
+ ///
+ public static IEnumerable 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;
+ }
+ }
+ }
+
+ ///
+ /// 获取设备所有IPv4
+ ///
+ ///
+ public static IEnumerable GetAllIPv4Addresses()
+ {
+ foreach (var address in GetAllIPAddresses())
+ {
+ if (address.AddressFamily == AddressFamily.InterNetwork)
+ {
+ yield return address;
+ }
+ }
+ }
+
+ ///
+ /// 返回设备是否包含指定IP
+ ///
+ ///
+ ///
+ public static bool ContainsIPAddress(IPAddress address)
+ {
+ return GetAllIPAddresses().Contains(address);
+ }
+
+ ///
+ /// 获取对应的本机地址
+ ///
+ /// 远程地址
+ ///
+ 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;
+ }
+ }
+ }
+}
diff --git a/FastGithub.Dns/HostsValidator.cs b/FastGithub.Dns/HostsValidator.cs
index 739ce46..992ea54 100644
--- a/FastGithub.Dns/HostsValidator.cs
+++ b/FastGithub.Dns/HostsValidator.cs
@@ -1,11 +1,10 @@
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.Sockets;
using System.Threading.Tasks;
namespace FastGithub.Dns
@@ -48,7 +47,7 @@ namespace FastGithub.Dns
return;
}
- var localAddresses = await GetLocalIPv4AddressesAsync();
+ var localAddresses = LocalMachine.GetAllIPv4Addresses().ToArray();
var lines = await File.ReadAllLinesAsync(hostsPath);
foreach (var line in lines)
{
@@ -67,30 +66,6 @@ namespace FastGithub.Dns
}
}
- ///
- /// 获取本机所有IPv4
- ///
- ///
- private static async Task> GetLocalIPv4AddressesAsync()
- {
- var hashSet = new HashSet
- {
- 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;
- }
-
-
///
/// hosts文件记录
///
diff --git a/FastGithub.Dns/RemoteEndPointRequest.cs b/FastGithub.Dns/RemoteEndPointRequest.cs
index 4ec6116..fc1c466 100644
--- a/FastGithub.Dns/RemoteEndPointRequest.cs
+++ b/FastGithub.Dns/RemoteEndPointRequest.cs
@@ -1,7 +1,6 @@
using DNS.Protocol;
-using System;
+using FastGithub.Configuration;
using System.Net;
-using System.Net.Sockets;
namespace FastGithub.Dns
{
@@ -32,16 +31,7 @@ namespace FastGithub.Dns
///
public IPAddress? GetLocalAddress()
{
- try
- {
- 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;
- }
+ return LocalMachine.GetLocalAddress(this.RemoteEndPoint);
}
}
}
diff --git a/FastGithub.ReverseProxy/KestrelServerOptionsExtensions.cs b/FastGithub.ReverseProxy/KestrelServerOptionsExtensions.cs
index ab77cc0..5ca6761 100644
--- a/FastGithub.ReverseProxy/KestrelServerOptionsExtensions.cs
+++ b/FastGithub.ReverseProxy/KestrelServerOptionsExtensions.cs
@@ -1,4 +1,5 @@
-using FastGithub.ReverseProxy;
+using FastGithub.Configuration;
+using FastGithub.ReverseProxy;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.Caching.Memory;
@@ -11,7 +12,6 @@ using System.IO;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
-using System.Net.Sockets;
using System.Security.Cryptography.X509Certificates;
namespace FastGithub
@@ -196,16 +196,10 @@ namespace FastGithub
yield break;
}
- var hostName = Dns.GetHostName();
- yield return hostName;
- yield return IPAddress.Loopback.ToString();
-
- foreach (var address in Dns.GetHostAddresses(hostName))
+ yield return LocalMachine.Name;
+ foreach (var address in LocalMachine.GetAllIPv4Addresses())
{
- if (address.AddressFamily == AddressFamily.InterNetwork)
- {
- yield return address.ToString();
- }
+ yield return address.ToString();
}
}
}