diff --git a/FastGithub.Configuration/LocalMachine.cs b/FastGithub.Configuration/LocalMachine.cs
deleted file mode 100644
index 94d3ea5..0000000
--- a/FastGithub.Configuration/LocalMachine.cs
+++ /dev/null
@@ -1,94 +0,0 @@
-using System.Collections.Generic;
-using System.Linq;
-using System.Net.NetworkInformation;
-using System.Net.Sockets;
-
-namespace FastGithub.Configuration
-{
- ///
- /// 提供本机设备信息
- ///
- public static class LocalMachine
- {
- ///
- /// 获取可用的随机Tcp端口
- ///
- ///
- /// 最小值
- ///
- public static int GetAvailableTcpPort(AddressFamily addressFamily, int min = 1025)
- {
- var hashSet = new HashSet();
- var tcpListeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners();
-
- foreach (var item in tcpListeners)
- {
- if (item.AddressFamily == addressFamily)
- {
- hashSet.Add(item.Port);
- }
- }
-
- for (var port = min; port < ushort.MaxValue; port++)
- {
- if (hashSet.Contains(port) == false)
- {
- return port;
- }
- }
-
- throw new FastGithubException("当前无可用的端口");
- }
-
-
- ///
- /// 获取可用的随机端口
- ///
- ///
- /// 最小值
- ///
- public static int GetAvailablePort(AddressFamily addressFamily, int min = 1025)
- {
- var hashSet = new HashSet();
- var tcpListeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners();
- var udpListeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveUdpListeners();
-
- foreach (var item in tcpListeners)
- {
- if (item.AddressFamily == addressFamily)
- {
- hashSet.Add(item.Port);
- }
- }
-
- foreach (var item in udpListeners)
- {
- if (item.AddressFamily == addressFamily)
- {
- hashSet.Add(item.Port);
- }
- }
-
- for (var port = min; port < ushort.MaxValue; port++)
- {
- if (hashSet.Contains(port) == false)
- {
- return port;
- }
- }
-
- throw new FastGithubException("当前无可用的端口");
- }
-
- ///
- /// 是否可以监听指定tcp端口
- ///
- ///
- ///
- public static bool CanListenTcp(int port)
- {
- var tcpListeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners();
- return tcpListeners.Any(item => item.Port == port) == false;
- }
- }
-}
diff --git a/FastGithub.Dns/DnsInterceptor.cs b/FastGithub.Dns/DnsInterceptor.cs
index f812ced..fbfa18a 100644
--- a/FastGithub.Dns/DnsInterceptor.cs
+++ b/FastGithub.Dns/DnsInterceptor.cs
@@ -162,7 +162,7 @@ namespace FastGithub.Dns
}
WinDivert.WinDivertHelperCalcChecksums(winDivertBuffer, packetLength, ref winDivertAddress, WinDivertChecksumHelperParam.All);
- this.logger.LogInformation($"已拦截dns查询{domain}并伪造响应内容为{IPAddress.Loopback}");
+ this.logger.LogInformation($"已拦截dns查询{domain}并伪造解析结果为{IPAddress.Loopback}");
}
}
}
diff --git a/FastGithub.DomainResolve/DnscryptProxy.cs b/FastGithub.DomainResolve/DnscryptProxy.cs
index c986f19..8868b74 100644
--- a/FastGithub.DomainResolve/DnscryptProxy.cs
+++ b/FastGithub.DomainResolve/DnscryptProxy.cs
@@ -1,9 +1,12 @@
using FastGithub.Configuration;
using System;
+using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
+using System.Net.NetworkInformation;
+using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
@@ -35,7 +38,7 @@ namespace FastGithub.DomainResolve
public async Task StartAsync(CancellationToken cancellationToken)
{
var tomlPath = Path.Combine(PATH, $"{NAME}.toml");
- var port = LocalMachine.GetAvailablePort(IPAddress.Loopback.AddressFamily, min: 5533);
+ var port = GetAvailablePort(IPAddress.Loopback.AddressFamily);
var localEndPoint = new IPEndPoint(IPAddress.Loopback, port);
await TomlUtil.SetListensAsync(tomlPath, localEndPoint, cancellationToken);
@@ -65,6 +68,47 @@ namespace FastGithub.DomainResolve
}
}
+
+
+ ///
+ /// 获取可用的随机端口
+ ///
+ ///
+ /// 最小值
+ ///
+ private static int GetAvailablePort(AddressFamily addressFamily, int min = 5533)
+ {
+ var hashSet = new HashSet();
+ var tcpListeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners();
+ var udpListeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveUdpListeners();
+
+ foreach (var endPoint in tcpListeners)
+ {
+ if (endPoint.AddressFamily == addressFamily)
+ {
+ hashSet.Add(endPoint.Port);
+ }
+ }
+
+ foreach (var endPoint in udpListeners)
+ {
+ if (endPoint.AddressFamily == addressFamily)
+ {
+ hashSet.Add(endPoint.Port);
+ }
+ }
+
+ for (var port = min; port < IPEndPoint.MaxPort; port++)
+ {
+ if (hashSet.Contains(port) == false)
+ {
+ return port;
+ }
+ }
+
+ throw new FastGithubException("当前无可用的端口");
+ }
+
///
/// 进程退出时
///
diff --git a/FastGithub.HttpServer/HttpsReverseProxyPort.cs b/FastGithub.HttpServer/HttpsReverseProxyPort.cs
index 4427715..f3d33db 100644
--- a/FastGithub.HttpServer/HttpsReverseProxyPort.cs
+++ b/FastGithub.HttpServer/HttpsReverseProxyPort.cs
@@ -1,5 +1,8 @@
using FastGithub.Configuration;
using System;
+using System.Collections.Generic;
+using System.Net;
+using System.Net.NetworkInformation;
using System.Net.Sockets;
namespace FastGithub.HttpServer
@@ -12,6 +15,36 @@ namespace FastGithub.HttpServer
///
/// 获取端口值
///
- public static int Value { get; } = OperatingSystem.IsWindows() ? 443 : LocalMachine.GetAvailableTcpPort(AddressFamily.InterNetwork);
+ public static int Value { get; } = OperatingSystem.IsWindows() ? 443 : GetAvailableTcpPort(AddressFamily.InterNetwork);
+
+ ///
+ /// 获取可用的随机Tcp端口
+ ///
+ ///
+ /// 最小值
+ ///
+ private static int GetAvailableTcpPort(AddressFamily addressFamily, int min = 12345)
+ {
+ var hashSet = new HashSet();
+ var tcpListeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners();
+
+ foreach (var endpoint in tcpListeners)
+ {
+ if (endpoint.AddressFamily == addressFamily)
+ {
+ hashSet.Add(endpoint.Port);
+ }
+ }
+
+ for (var port = min; port < IPEndPoint.MaxPort; port++)
+ {
+ if (hashSet.Contains(port) == false)
+ {
+ return port;
+ }
+ }
+
+ throw new FastGithubException("当前无可用的端口");
+ }
}
}
diff --git a/FastGithub.HttpServer/KestrelServerOptionsExtensions.cs b/FastGithub.HttpServer/KestrelServerOptionsExtensions.cs
index 3493e98..fb9e3d1 100644
--- a/FastGithub.HttpServer/KestrelServerOptionsExtensions.cs
+++ b/FastGithub.HttpServer/KestrelServerOptionsExtensions.cs
@@ -7,7 +7,10 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
+using System.Linq;
using System.Net;
+using System.Net.NetworkInformation;
+using System.Net.Sockets;
namespace FastGithub
{
@@ -36,7 +39,7 @@ namespace FastGithub
var options = kestrel.ApplicationServices.GetRequiredService>().Value;
var httpProxyPort = options.HttpProxyPort;
- if (LocalMachine.CanListenTcp(httpProxyPort) == false)
+ if (CanListenTcp(httpProxyPort) == false)
{
throw new FastGithubException($"tcp端口{httpProxyPort}已经被其它进程占用,请在配置文件更换{nameof(FastGithubOptions.HttpProxyPort)}为其它端口");
}
@@ -53,7 +56,7 @@ namespace FastGithub
public static void ListenSshReverseProxy(this KestrelServerOptions kestrel)
{
const int SSH_PORT = 22;
- if (LocalMachine.CanListenTcp(SSH_PORT) == true)
+ if (CanListenTcp(SSH_PORT) == true)
{
kestrel.Listen(IPAddress.Loopback, SSH_PORT, listen => listen.UseConnectionHandler());
kestrel.GetLogger().LogInformation($"已监听ssh://{IPAddress.Loopback}:{SSH_PORT},github的ssh反向代理服务启动完成");
@@ -67,7 +70,7 @@ namespace FastGithub
public static void ListenHttpReverseProxy(this KestrelServerOptions kestrel)
{
const int HTTP_PORT = 80;
- if (LocalMachine.CanListenTcp(HTTP_PORT) == true)
+ if (CanListenTcp(HTTP_PORT) == true)
{
kestrel.Listen(IPAddress.Loopback, HTTP_PORT);
kestrel.GetLogger().LogInformation($"已监听http://{IPAddress.Loopback}:{HTTP_PORT},http反向代理服务启动完成");
@@ -88,7 +91,7 @@ namespace FastGithub
TcpTable.KillPortOwner(httpsPort);
}
- if (LocalMachine.CanListenTcp(httpsPort) == false)
+ if (CanListenTcp(httpsPort) == false)
{
throw new FastGithubException($"tcp端口{httpsPort}已经被其它进程占用");
}
@@ -121,5 +124,17 @@ namespace FastGithub
var loggerFactory = kestrel.ApplicationServices.GetRequiredService();
return loggerFactory.CreateLogger($"{nameof(FastGithub)}.{nameof(HttpServer)}");
}
+
+ ///
+ /// 是否可以监听指定tcp端口
+ ///
+ ///
+ ///
+ ///
+ private static bool CanListenTcp(int port, AddressFamily addressFamily = AddressFamily.InterNetwork)
+ {
+ var tcpListeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners();
+ return tcpListeners.Any(item => item.AddressFamily == addressFamily && item.Port == port) == false;
+ }
}
}