FastGithub/FastGithub.ReverseProxy/ReverseProxyHostedService.cs
xljiulang ec4d8d5607 增加https端口占用检测功能;
修复dns端口占用可能检测不到的问题;
2021-07-24 16:27:40 +08:00

46 lines
1.4 KiB
C#

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;
}
}
}