移除LocalMachine类
This commit is contained in:
parent
19db6388db
commit
351be8c207
@ -1,94 +0,0 @@
|
|||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Net.NetworkInformation;
|
|
||||||
using System.Net.Sockets;
|
|
||||||
|
|
||||||
namespace FastGithub.Configuration
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 提供本机设备信息
|
|
||||||
/// </summary>
|
|
||||||
public static class LocalMachine
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 获取可用的随机Tcp端口
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="addressFamily"></param>
|
|
||||||
/// <param name="min">最小值</param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public static int GetAvailableTcpPort(AddressFamily addressFamily, int min = 1025)
|
|
||||||
{
|
|
||||||
var hashSet = new HashSet<int>();
|
|
||||||
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("当前无可用的端口");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 获取可用的随机端口
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="addressFamily"></param>
|
|
||||||
/// <param name="min">最小值</param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public static int GetAvailablePort(AddressFamily addressFamily, int min = 1025)
|
|
||||||
{
|
|
||||||
var hashSet = new HashSet<int>();
|
|
||||||
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("当前无可用的端口");
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 是否可以监听指定tcp端口
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="port"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public static bool CanListenTcp(int port)
|
|
||||||
{
|
|
||||||
var tcpListeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners();
|
|
||||||
return tcpListeners.Any(item => item.Port == port) == false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -162,7 +162,7 @@ namespace FastGithub.Dns
|
|||||||
}
|
}
|
||||||
|
|
||||||
WinDivert.WinDivertHelperCalcChecksums(winDivertBuffer, packetLength, ref winDivertAddress, WinDivertChecksumHelperParam.All);
|
WinDivert.WinDivertHelperCalcChecksums(winDivertBuffer, packetLength, ref winDivertAddress, WinDivertChecksumHelperParam.All);
|
||||||
this.logger.LogInformation($"已拦截dns查询{domain}并伪造响应内容为{IPAddress.Loopback}");
|
this.logger.LogInformation($"已拦截dns查询{domain}并伪造解析结果为{IPAddress.Loopback}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,9 +1,12 @@
|
|||||||
using FastGithub.Configuration;
|
using FastGithub.Configuration;
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
|
using System.Net.NetworkInformation;
|
||||||
|
using System.Net.Sockets;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
@ -35,7 +38,7 @@ namespace FastGithub.DomainResolve
|
|||||||
public async Task StartAsync(CancellationToken cancellationToken)
|
public async Task StartAsync(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var tomlPath = Path.Combine(PATH, $"{NAME}.toml");
|
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);
|
var localEndPoint = new IPEndPoint(IPAddress.Loopback, port);
|
||||||
await TomlUtil.SetListensAsync(tomlPath, localEndPoint, cancellationToken);
|
await TomlUtil.SetListensAsync(tomlPath, localEndPoint, cancellationToken);
|
||||||
|
|
||||||
@ -65,6 +68,47 @@ namespace FastGithub.DomainResolve
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取可用的随机端口
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="addressFamily"></param>
|
||||||
|
/// <param name="min">最小值</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
private static int GetAvailablePort(AddressFamily addressFamily, int min = 5533)
|
||||||
|
{
|
||||||
|
var hashSet = new HashSet<int>();
|
||||||
|
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("当前无可用的端口");
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 进程退出时
|
/// 进程退出时
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@ -1,5 +1,8 @@
|
|||||||
using FastGithub.Configuration;
|
using FastGithub.Configuration;
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Net;
|
||||||
|
using System.Net.NetworkInformation;
|
||||||
using System.Net.Sockets;
|
using System.Net.Sockets;
|
||||||
|
|
||||||
namespace FastGithub.HttpServer
|
namespace FastGithub.HttpServer
|
||||||
@ -12,6 +15,36 @@ namespace FastGithub.HttpServer
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取端口值
|
/// 获取端口值
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static int Value { get; } = OperatingSystem.IsWindows() ? 443 : LocalMachine.GetAvailableTcpPort(AddressFamily.InterNetwork);
|
public static int Value { get; } = OperatingSystem.IsWindows() ? 443 : GetAvailableTcpPort(AddressFamily.InterNetwork);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取可用的随机Tcp端口
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="addressFamily"></param>
|
||||||
|
/// <param name="min">最小值</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
private static int GetAvailableTcpPort(AddressFamily addressFamily, int min = 12345)
|
||||||
|
{
|
||||||
|
var hashSet = new HashSet<int>();
|
||||||
|
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("当前无可用的端口");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,7 +7,10 @@ using Microsoft.Extensions.DependencyInjection;
|
|||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
using System;
|
using System;
|
||||||
|
using System.Linq;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
|
using System.Net.NetworkInformation;
|
||||||
|
using System.Net.Sockets;
|
||||||
|
|
||||||
namespace FastGithub
|
namespace FastGithub
|
||||||
{
|
{
|
||||||
@ -36,7 +39,7 @@ namespace FastGithub
|
|||||||
var options = kestrel.ApplicationServices.GetRequiredService<IOptions<FastGithubOptions>>().Value;
|
var options = kestrel.ApplicationServices.GetRequiredService<IOptions<FastGithubOptions>>().Value;
|
||||||
var httpProxyPort = options.HttpProxyPort;
|
var httpProxyPort = options.HttpProxyPort;
|
||||||
|
|
||||||
if (LocalMachine.CanListenTcp(httpProxyPort) == false)
|
if (CanListenTcp(httpProxyPort) == false)
|
||||||
{
|
{
|
||||||
throw new FastGithubException($"tcp端口{httpProxyPort}已经被其它进程占用,请在配置文件更换{nameof(FastGithubOptions.HttpProxyPort)}为其它端口");
|
throw new FastGithubException($"tcp端口{httpProxyPort}已经被其它进程占用,请在配置文件更换{nameof(FastGithubOptions.HttpProxyPort)}为其它端口");
|
||||||
}
|
}
|
||||||
@ -53,7 +56,7 @@ namespace FastGithub
|
|||||||
public static void ListenSshReverseProxy(this KestrelServerOptions kestrel)
|
public static void ListenSshReverseProxy(this KestrelServerOptions kestrel)
|
||||||
{
|
{
|
||||||
const int SSH_PORT = 22;
|
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<SshReverseProxyHandler>());
|
kestrel.Listen(IPAddress.Loopback, SSH_PORT, listen => listen.UseConnectionHandler<SshReverseProxyHandler>());
|
||||||
kestrel.GetLogger().LogInformation($"已监听ssh://{IPAddress.Loopback}:{SSH_PORT},github的ssh反向代理服务启动完成");
|
kestrel.GetLogger().LogInformation($"已监听ssh://{IPAddress.Loopback}:{SSH_PORT},github的ssh反向代理服务启动完成");
|
||||||
@ -67,7 +70,7 @@ namespace FastGithub
|
|||||||
public static void ListenHttpReverseProxy(this KestrelServerOptions kestrel)
|
public static void ListenHttpReverseProxy(this KestrelServerOptions kestrel)
|
||||||
{
|
{
|
||||||
const int HTTP_PORT = 80;
|
const int HTTP_PORT = 80;
|
||||||
if (LocalMachine.CanListenTcp(HTTP_PORT) == true)
|
if (CanListenTcp(HTTP_PORT) == true)
|
||||||
{
|
{
|
||||||
kestrel.Listen(IPAddress.Loopback, HTTP_PORT);
|
kestrel.Listen(IPAddress.Loopback, HTTP_PORT);
|
||||||
kestrel.GetLogger().LogInformation($"已监听http://{IPAddress.Loopback}:{HTTP_PORT},http反向代理服务启动完成");
|
kestrel.GetLogger().LogInformation($"已监听http://{IPAddress.Loopback}:{HTTP_PORT},http反向代理服务启动完成");
|
||||||
@ -88,7 +91,7 @@ namespace FastGithub
|
|||||||
TcpTable.KillPortOwner(httpsPort);
|
TcpTable.KillPortOwner(httpsPort);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (LocalMachine.CanListenTcp(httpsPort) == false)
|
if (CanListenTcp(httpsPort) == false)
|
||||||
{
|
{
|
||||||
throw new FastGithubException($"tcp端口{httpsPort}已经被其它进程占用");
|
throw new FastGithubException($"tcp端口{httpsPort}已经被其它进程占用");
|
||||||
}
|
}
|
||||||
@ -121,5 +124,17 @@ namespace FastGithub
|
|||||||
var loggerFactory = kestrel.ApplicationServices.GetRequiredService<ILoggerFactory>();
|
var loggerFactory = kestrel.ApplicationServices.GetRequiredService<ILoggerFactory>();
|
||||||
return loggerFactory.CreateLogger($"{nameof(FastGithub)}.{nameof(HttpServer)}");
|
return loggerFactory.CreateLogger($"{nameof(FastGithub)}.{nameof(HttpServer)}");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 是否可以监听指定tcp端口
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="port"></param>
|
||||||
|
/// <param name="addressFamily"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user