增加https端口占用检测功能;
修复dns端口占用可能检测不到的问题;
This commit is contained in:
parent
f95b19d024
commit
ec4d8d5607
@ -43,14 +43,19 @@ namespace FastGithub.Dns
|
||||
return false;
|
||||
}
|
||||
|
||||
var table = Marshal.PtrToStructure<MIB_UDPTABLE_OWNER_PID>(new IntPtr(pUdpTable));
|
||||
foreach (var row in table.rows)
|
||||
var prt = new IntPtr(pUdpTable);
|
||||
var table = Marshal.PtrToStructure<MIB_UDPTABLE_OWNER_PID>(prt);
|
||||
prt += sizeof(int);
|
||||
for (var i = 0; i < table.dwNumEntries; i++)
|
||||
{
|
||||
var row = Marshal.PtrToStructure<MIB_UDPROW_OWNER_PID>(prt);
|
||||
if (row.LocalPort == port)
|
||||
{
|
||||
processId = row.ProcessId;
|
||||
return true;
|
||||
}
|
||||
|
||||
prt += Marshal.SizeOf<MIB_UDPROW_OWNER_PID>();
|
||||
}
|
||||
}
|
||||
|
||||
@ -69,9 +74,6 @@ namespace FastGithub.Dns
|
||||
private struct MIB_UDPTABLE_OWNER_PID
|
||||
{
|
||||
public uint dwNumEntries;
|
||||
|
||||
[MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.Struct, SizeConst = 1)]
|
||||
public MIB_UDPROW_OWNER_PID[] rows;
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
|
||||
@ -2,6 +2,7 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
45
FastGithub.ReverseProxy/ReverseProxyHostedService.cs
Normal file
45
FastGithub.ReverseProxy/ReverseProxyHostedService.cs
Normal file
@ -0,0 +1,45 @@
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace FastGithub.ReverseProxy
|
||||
{
|
||||
/// <summary>
|
||||
/// 反向代理端口检测后台服务
|
||||
/// </summary>
|
||||
sealed class ReverseProxyHostedService : IHostedService
|
||||
{
|
||||
private readonly ILogger<ReverseProxyHostedService> logger;
|
||||
|
||||
/// <summary>
|
||||
/// 反向代理端口检测后台服务
|
||||
/// </summary>
|
||||
/// <param name="logger"></param>
|
||||
public ReverseProxyHostedService(ILogger<ReverseProxyHostedService> logger)
|
||||
{
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
public Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (OperatingSystem.IsWindows())
|
||||
{
|
||||
const int HTTPSPORT = 443;
|
||||
if (TcpTable.TryGetOwnerProcessId(HTTPSPORT, out var pid))
|
||||
{
|
||||
var process = Process.GetProcessById(pid);
|
||||
this.logger.LogError($"由于进程{process.ProcessName}({pid})占用了{HTTPSPORT}端口,{nameof(FastGithub)}的反向代理无法工作");
|
||||
}
|
||||
}
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -20,7 +20,8 @@ namespace FastGithub
|
||||
.AddHttpForwarder()
|
||||
.AddHttpClient()
|
||||
.AddSingleton<RequestLoggingMilldeware>()
|
||||
.AddSingleton<ReverseProxyMiddleware>();
|
||||
.AddSingleton<ReverseProxyMiddleware>()
|
||||
.AddHostedService<ReverseProxyHostedService>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
109
FastGithub.ReverseProxy/TcpTable.cs
Normal file
109
FastGithub.ReverseProxy/TcpTable.cs
Normal file
@ -0,0 +1,109 @@
|
||||
using System;
|
||||
using System.Buffers.Binary;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.Versioning;
|
||||
|
||||
namespace FastGithub.ReverseProxy
|
||||
{
|
||||
/// <summary>
|
||||
/// windows iphlpapi
|
||||
/// </summary>
|
||||
[SupportedOSPlatform("windows")]
|
||||
unsafe static class TcpTable
|
||||
{
|
||||
private const int ERROR_INSUFFICIENT_BUFFER = 122;
|
||||
|
||||
[DllImport("iphlpapi.dll", SetLastError = true)]
|
||||
private static extern uint GetExtendedTcpTable(void* pTcpTable, ref int pdwSize, bool bOrder, AddressFamily ulAf, TCP_TABLE_CLASS tableClass, uint reserved = 0);
|
||||
|
||||
/// <summary>
|
||||
/// 获取tcp端口的占用进程id
|
||||
/// </summary>
|
||||
/// <param name="port"></param>
|
||||
/// <param name="processId"></param>
|
||||
/// <returns></returns>
|
||||
public static bool TryGetOwnerProcessId(int port, out int processId)
|
||||
{
|
||||
processId = 0;
|
||||
var pdwSize = 0;
|
||||
var result = GetExtendedTcpTable(null, ref pdwSize, false, AddressFamily.InterNetwork, TCP_TABLE_CLASS.TCP_TABLE_OWNER_PID_LISTENER);
|
||||
if (result != ERROR_INSUFFICIENT_BUFFER)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var buffer = new byte[pdwSize];
|
||||
fixed (byte* pTcpTable = &buffer[0])
|
||||
{
|
||||
result = GetExtendedTcpTable(pTcpTable, ref pdwSize, false, AddressFamily.InterNetwork, TCP_TABLE_CLASS.TCP_TABLE_OWNER_PID_LISTENER);
|
||||
if (result != 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var prt = new IntPtr(pTcpTable);
|
||||
var table = Marshal.PtrToStructure<MIB_TCPTABLE_OWNER_PID>(prt);
|
||||
prt += sizeof(int);
|
||||
for (var i = 0; i < table.dwNumEntries; i++)
|
||||
{
|
||||
var row = Marshal.PtrToStructure<MIB_TCPROW_OWNER_PID>(prt);
|
||||
if (row.LocalPort == port)
|
||||
{
|
||||
processId = row.ProcessId;
|
||||
return true;
|
||||
}
|
||||
|
||||
prt += Marshal.SizeOf<MIB_TCPROW_OWNER_PID>();
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
private enum TCP_TABLE_CLASS
|
||||
{
|
||||
TCP_TABLE_BASIC_LISTENER,
|
||||
TCP_TABLE_BASIC_CONNECTIONS,
|
||||
TCP_TABLE_BASIC_ALL,
|
||||
TCP_TABLE_OWNER_PID_LISTENER,
|
||||
TCP_TABLE_OWNER_PID_CONNECTIONS,
|
||||
TCP_TABLE_OWNER_PID_ALL,
|
||||
TCP_TABLE_OWNER_MODULE_LISTENER,
|
||||
TCP_TABLE_OWNER_MODULE_CONNECTIONS,
|
||||
TCP_TABLE_OWNER_MODULE_ALL
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
private struct MIB_TCPTABLE_OWNER_PID
|
||||
{
|
||||
public uint dwNumEntries;
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
private struct MIB_TCPROW_OWNER_PID
|
||||
{
|
||||
public uint state;
|
||||
|
||||
public uint localAddr;
|
||||
|
||||
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
|
||||
public byte[] localPort;
|
||||
|
||||
public uint remoteAddr;
|
||||
|
||||
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
|
||||
public byte[] remotePort;
|
||||
|
||||
public int owningPid;
|
||||
|
||||
public int ProcessId => owningPid;
|
||||
|
||||
public IPAddress LocalAddress => new(localAddr);
|
||||
|
||||
public ushort LocalPort => BinaryPrimitives.ReadUInt16BigEndian(this.localPort);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -42,9 +42,9 @@ namespace FastGithub
|
||||
.ConfigureServices((ctx, services) =>
|
||||
{
|
||||
services.AddDnsServer();
|
||||
services.AddReverseProxy();
|
||||
services.AddDnscryptProxy();
|
||||
services.AddAppUpgrade();
|
||||
services.AddReverseProxy();
|
||||
services.AddSingleton<FastGithubConfig>();
|
||||
services.Configure<FastGithubOptions>(ctx.Configuration.GetSection(nameof(FastGithub)));
|
||||
})
|
||||
|
||||
Loading…
Reference in New Issue
Block a user