From 19fdab2bdca746e3195d693610d6de07b80ab880 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E9=99=88=E5=9B=BD=E4=BC=9F?= <366193849@qq.com>
Date: Wed, 4 Aug 2021 09:24:53 +0800
Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84LocalMachine?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
FastGithub.Configuration/LocalMachine.cs | 32 ++++++++++++++++---
FastGithub.Dns/DnsServer.cs | 5 +--
FastGithub.Dns/RemoteEndPointRequest.cs | 4 +--
FastGithub.Dns/RequestResolver.cs | 2 +-
.../KestrelServerOptionsExtensions.cs | 17 ++--------
5 files changed, 33 insertions(+), 27 deletions(-)
diff --git a/FastGithub.Configuration/LocalMachine.cs b/FastGithub.Configuration/LocalMachine.cs
index a3d1c2f..a9ed7e1 100644
--- a/FastGithub.Configuration/LocalMachine.cs
+++ b/FastGithub.Configuration/LocalMachine.cs
@@ -18,7 +18,7 @@ namespace FastGithub.Configuration
public static string Name => Environment.MachineName;
///
- /// 获取设备所有IP
+ /// 获取本机设备所有IP
///
///
public static IEnumerable GetAllIPAddresses()
@@ -36,7 +36,7 @@ namespace FastGithub.Configuration
}
///
- /// 获取设备所有IPv4
+ /// 获取本机设备所有IPv4
///
///
public static IEnumerable GetAllIPv4Addresses()
@@ -51,7 +51,7 @@ namespace FastGithub.Configuration
}
///
- /// 返回设备是否包含指定IP
+ /// 返回本机设备是否包含指定IP
///
///
///
@@ -61,11 +61,11 @@ namespace FastGithub.Configuration
}
///
- /// 获取对应的本机地址
+ /// 获取与远程节点通讯的的本机IP地址
///
/// 远程地址
///
- public static IPAddress? GetLocalAddress(EndPoint remoteEndPoint)
+ public static IPAddress? GetLocalIPAddress(EndPoint remoteEndPoint)
{
try
{
@@ -78,5 +78,27 @@ namespace FastGithub.Configuration
return default;
}
}
+
+ ///
+ /// 是否可以监听指定tcp端口
+ ///
+ ///
+ ///
+ public static bool CanListenTcp(int port)
+ {
+ var tcpListeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners();
+ return tcpListeners.Any(item => item.Port == port) == false;
+ }
+
+ ///
+ /// 是否可以监听指定udp端口
+ ///
+ ///
+ ///
+ public static bool CanListenUdp(int port)
+ {
+ var udpListeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveUdpListeners();
+ return udpListeners.Any(item => item.Port == port) == false;
+ }
}
}
diff --git a/FastGithub.Dns/DnsServer.cs b/FastGithub.Dns/DnsServer.cs
index f2489b0..202270b 100644
--- a/FastGithub.Dns/DnsServer.cs
+++ b/FastGithub.Dns/DnsServer.cs
@@ -2,9 +2,7 @@
using FastGithub.Configuration;
using Microsoft.Extensions.Logging;
using System;
-using System.Linq;
using System.Net;
-using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
@@ -47,8 +45,7 @@ namespace FastGithub.Dns
UdpTable.KillPortOwner(port);
}
- var udpListeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveUdpListeners();
- if (udpListeners.Any(item => item.Port == port))
+ if (LocalMachine.CanListenUdp(port) == false)
{
throw new FastGithubException($"udp端口{port}已经被其它进程占用");
}
diff --git a/FastGithub.Dns/RemoteEndPointRequest.cs b/FastGithub.Dns/RemoteEndPointRequest.cs
index fc1c466..a997d7e 100644
--- a/FastGithub.Dns/RemoteEndPointRequest.cs
+++ b/FastGithub.Dns/RemoteEndPointRequest.cs
@@ -29,9 +29,9 @@ namespace FastGithub.Dns
/// 获取对应的本机地址
///
///
- public IPAddress? GetLocalAddress()
+ public IPAddress? GetLocalIPAddress()
{
- return LocalMachine.GetLocalAddress(this.RemoteEndPoint);
+ return LocalMachine.GetLocalIPAddress(this.RemoteEndPoint);
}
}
}
diff --git a/FastGithub.Dns/RequestResolver.cs b/FastGithub.Dns/RequestResolver.cs
index 84cd48a..73dc259 100644
--- a/FastGithub.Dns/RequestResolver.cs
+++ b/FastGithub.Dns/RequestResolver.cs
@@ -57,7 +57,7 @@ namespace FastGithub.Dns
var domain = question.Name;
if (this.fastGithubConfig.IsMatch(domain.ToString()) == true)
{
- var localAddress = remoteEndPointRequest.GetLocalAddress() ?? IPAddress.Loopback;
+ var localAddress = remoteEndPointRequest.GetLocalIPAddress() ?? IPAddress.Loopback;
var record = new IPAddressResourceRecord(domain, localAddress, this.ttl);
response.AnswerRecords.Add(record);
diff --git a/FastGithub.ReverseProxy/KestrelServerOptionsExtensions.cs b/FastGithub.ReverseProxy/KestrelServerOptionsExtensions.cs
index 8ae55c0..353fc02 100644
--- a/FastGithub.ReverseProxy/KestrelServerOptionsExtensions.cs
+++ b/FastGithub.ReverseProxy/KestrelServerOptionsExtensions.cs
@@ -5,9 +5,7 @@ using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
-using System.Linq;
using System.Net;
-using System.Net.NetworkInformation;
using System.Security.Authentication;
namespace FastGithub
@@ -29,7 +27,7 @@ namespace FastGithub
TcpTable.KillPortOwner(HTTP_PORT);
}
- if (CanTcpListen(HTTP_PORT) == false)
+ if (LocalMachine.CanListenTcp(HTTP_PORT) == false)
{
var loggerFactory = kestrel.ApplicationServices.GetRequiredService();
var logger = loggerFactory.CreateLogger($"{nameof(FastGithub)}.{nameof(ReverseProxy)}");
@@ -53,7 +51,7 @@ namespace FastGithub
TcpTable.KillPortOwner(HTTPS_PORT);
}
- if (CanTcpListen(HTTPS_PORT) == false)
+ if (LocalMachine.CanListenTcp(HTTPS_PORT) == false)
{
throw new FastGithubException($"由于tcp端口{HTTPS_PORT}已经被其它进程占用,{nameof(FastGithub)}无法进行必须的https反向代理");
}
@@ -71,16 +69,5 @@ namespace FastGithub
https.ServerCertificateSelector = (ctx, domain) => certService.GetOrCreateServerCert(domain);
}));
}
-
- ///
- /// 是否可以监听指定端口
- ///
- ///
- ///
- private static bool CanTcpListen(int port)
- {
- var tcpListeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners();
- return tcpListeners.Any(item => item.Port == port) == false;
- }
}
}