拦截器异常后能出应用程序
This commit is contained in:
parent
cd98c4511b
commit
b9d8fccd77
@ -14,17 +14,17 @@ namespace FastGithub.Configuration
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// ssh端口
|
/// ssh端口
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static int Ssh { get; } = GetAvailableTcpPort(22);
|
public static int Ssh { get; } = OperatingSystem.IsWindows() ? GetAvailableTcpPort(22) : GetAvailableTcpPort(3822);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// http端口
|
/// http端口
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static int Http { get; } = GetAvailableTcpPort(80);
|
public static int Http { get; } = OperatingSystem.IsWindows() ? GetAvailableTcpPort(80) : GetAvailableTcpPort(3880);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// https端口
|
/// https端口
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static int Https { get; } = OperatingSystem.IsWindows() ? GetAvailableTcpPort(443) : GetAvailableTcpPort(48457);
|
public static int Https { get; } = OperatingSystem.IsWindows() ? GetAvailableTcpPort(443) : GetAvailableTcpPort(38443);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取可用的随机Tcp端口
|
/// 获取可用的随机Tcp端口
|
||||||
|
|||||||
@ -23,7 +23,6 @@ namespace FastGithub.PacketIntercept.Dns
|
|||||||
[SupportedOSPlatform("windows")]
|
[SupportedOSPlatform("windows")]
|
||||||
sealed class DnsInterceptor : IDnsInterceptor
|
sealed class DnsInterceptor : IDnsInterceptor
|
||||||
{
|
{
|
||||||
private const int ERROR_INVALID_HANDLE = 0x6;
|
|
||||||
private const string DNS_FILTER = "udp.DstPort == 53";
|
private const string DNS_FILTER = "udp.DstPort == 53";
|
||||||
private readonly FastGithubConfig fastGithubConfig;
|
private readonly FastGithubConfig fastGithubConfig;
|
||||||
private readonly ILogger<DnsInterceptor> logger;
|
private readonly ILogger<DnsInterceptor> logger;
|
||||||
@ -56,6 +55,7 @@ namespace FastGithub.PacketIntercept.Dns
|
|||||||
/// DNS拦截
|
/// DNS拦截
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="cancellationToken"></param>
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <exception cref="Win32Exception"></exception>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task InterceptAsync(CancellationToken cancellationToken)
|
public async Task InterceptAsync(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
@ -64,8 +64,8 @@ namespace FastGithub.PacketIntercept.Dns
|
|||||||
var handle = WinDivert.WinDivertOpen(DNS_FILTER, WinDivertLayer.Network, 0, WinDivertOpenFlags.None);
|
var handle = WinDivert.WinDivertOpen(DNS_FILTER, WinDivertLayer.Network, 0, WinDivertOpenFlags.None);
|
||||||
if (handle == IntPtr.MaxValue || handle == IntPtr.Zero)
|
if (handle == IntPtr.MaxValue || handle == IntPtr.Zero)
|
||||||
{
|
{
|
||||||
this.logger.LogError($"打开驱动失败");
|
const int ERROR_INVALID_HANDLE = 0x6;
|
||||||
return;
|
throw new Win32Exception(ERROR_INVALID_HANDLE, "打开驱动失败");
|
||||||
}
|
}
|
||||||
|
|
||||||
cancellationToken.Register(hwnd =>
|
cancellationToken.Register(hwnd =>
|
||||||
@ -99,11 +99,7 @@ namespace FastGithub.PacketIntercept.Dns
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
var errorCode = Marshal.GetLastWin32Error();
|
var errorCode = Marshal.GetLastWin32Error();
|
||||||
this.logger.LogError(new Win32Exception(errorCode).Message);
|
throw new Win32Exception(errorCode);
|
||||||
if (errorCode == ERROR_INVALID_HANDLE)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,4 +1,6 @@
|
|||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Runtime.Versioning;
|
using System.Runtime.Versioning;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
@ -14,18 +16,26 @@ namespace FastGithub.PacketIntercept
|
|||||||
{
|
{
|
||||||
private readonly IDnsInterceptor dnsInterceptor;
|
private readonly IDnsInterceptor dnsInterceptor;
|
||||||
private readonly IEnumerable<IDnsConflictSolver> conflictSolvers;
|
private readonly IEnumerable<IDnsConflictSolver> conflictSolvers;
|
||||||
|
private readonly ILogger<DnsInterceptHostedService> logger;
|
||||||
|
private readonly IHost host;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// dns拦截后台服务
|
/// dns拦截后台服务
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="dnsInterceptor"></param>
|
/// <param name="dnsInterceptor"></param>
|
||||||
/// <param name="conflictSolvers"></param>
|
/// <param name="conflictSolvers"></param>
|
||||||
|
/// <param name="logger"></param>
|
||||||
|
/// <param name="host"></param>
|
||||||
public DnsInterceptHostedService(
|
public DnsInterceptHostedService(
|
||||||
IDnsInterceptor dnsInterceptor,
|
IDnsInterceptor dnsInterceptor,
|
||||||
IEnumerable<IDnsConflictSolver> conflictSolvers)
|
IEnumerable<IDnsConflictSolver> conflictSolvers,
|
||||||
|
ILogger<DnsInterceptHostedService> logger,
|
||||||
|
IHost host)
|
||||||
{
|
{
|
||||||
this.dnsInterceptor = dnsInterceptor;
|
this.dnsInterceptor = dnsInterceptor;
|
||||||
this.conflictSolvers = conflictSolvers;
|
this.conflictSolvers = conflictSolvers;
|
||||||
|
this.logger = logger;
|
||||||
|
this.host = host;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -61,9 +71,18 @@ namespace FastGithub.PacketIntercept
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="stoppingToken"></param>
|
/// <param name="stoppingToken"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
protected override Task ExecuteAsync(CancellationToken stoppingToken)
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||||
{
|
{
|
||||||
return this.dnsInterceptor.InterceptAsync(stoppingToken);
|
try
|
||||||
|
{
|
||||||
|
await this.dnsInterceptor.InterceptAsync(stoppingToken);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
stoppingToken.ThrowIfCancellationRequested();
|
||||||
|
this.logger.LogError(ex, "dns拦截器异常");
|
||||||
|
await this.host.StopAsync(stoppingToken);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -17,7 +17,6 @@ namespace FastGithub.PacketIntercept.Tcp
|
|||||||
[SupportedOSPlatform("windows")]
|
[SupportedOSPlatform("windows")]
|
||||||
abstract class TcpInterceptor : ITcpInterceptor
|
abstract class TcpInterceptor : ITcpInterceptor
|
||||||
{
|
{
|
||||||
private const int ERROR_INVALID_HANDLE = 0x6;
|
|
||||||
private readonly string filter;
|
private readonly string filter;
|
||||||
private readonly ushort oldServerPort;
|
private readonly ushort oldServerPort;
|
||||||
private readonly ushort newServerPort;
|
private readonly ushort newServerPort;
|
||||||
@ -41,6 +40,7 @@ namespace FastGithub.PacketIntercept.Tcp
|
|||||||
/// 拦截指定端口的数据包
|
/// 拦截指定端口的数据包
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="cancellationToken"></param>
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <exception cref="Win32Exception"></exception>
|
||||||
public async Task InterceptAsync(CancellationToken cancellationToken)
|
public async Task InterceptAsync(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
if (this.oldServerPort == this.newServerPort)
|
if (this.oldServerPort == this.newServerPort)
|
||||||
@ -53,8 +53,8 @@ namespace FastGithub.PacketIntercept.Tcp
|
|||||||
var handle = WinDivert.WinDivertOpen(this.filter, WinDivertLayer.Network, 0, WinDivertOpenFlags.None);
|
var handle = WinDivert.WinDivertOpen(this.filter, WinDivertLayer.Network, 0, WinDivertOpenFlags.None);
|
||||||
if (handle == IntPtr.MaxValue || handle == IntPtr.Zero)
|
if (handle == IntPtr.MaxValue || handle == IntPtr.Zero)
|
||||||
{
|
{
|
||||||
this.logger.LogError($"打开驱动失败");
|
const int ERROR_INVALID_HANDLE = 0x6;
|
||||||
return;
|
throw new Win32Exception(ERROR_INVALID_HANDLE, "打开驱动失败");
|
||||||
}
|
}
|
||||||
|
|
||||||
this.logger.LogInformation($"tcp://{IPAddress.Loopback}:{BinaryPrimitives.ReverseEndianness(this.oldServerPort)} => tcp://{IPAddress.Loopback}:{BinaryPrimitives.ReverseEndianness(this.newServerPort)}");
|
this.logger.LogInformation($"tcp://{IPAddress.Loopback}:{BinaryPrimitives.ReverseEndianness(this.oldServerPort)} => tcp://{IPAddress.Loopback}:{BinaryPrimitives.ReverseEndianness(this.newServerPort)}");
|
||||||
@ -84,11 +84,7 @@ namespace FastGithub.PacketIntercept.Tcp
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
var errorCode = Marshal.GetLastWin32Error();
|
var errorCode = Marshal.GetLastWin32Error();
|
||||||
this.logger.LogError(new Win32Exception(errorCode).Message);
|
throw new Win32Exception(errorCode);
|
||||||
if (errorCode == ERROR_INVALID_HANDLE)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,4 +1,6 @@
|
|||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Runtime.Versioning;
|
using System.Runtime.Versioning;
|
||||||
@ -14,14 +16,23 @@ namespace FastGithub.PacketIntercept
|
|||||||
sealed class TcpInterceptHostedService : BackgroundService
|
sealed class TcpInterceptHostedService : BackgroundService
|
||||||
{
|
{
|
||||||
private readonly IEnumerable<ITcpInterceptor> tcpInterceptors;
|
private readonly IEnumerable<ITcpInterceptor> tcpInterceptors;
|
||||||
|
private readonly ILogger<TcpInterceptHostedService> logger;
|
||||||
|
private readonly IHost host;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// tcp拦截后台服务
|
/// tcp拦截后台服务
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="tcpInterceptors"></param>
|
/// <param name="tcpInterceptors"></param>
|
||||||
public TcpInterceptHostedService(IEnumerable<ITcpInterceptor> tcpInterceptors)
|
/// <param name="logger"></param>
|
||||||
|
/// <param name="host"></param>
|
||||||
|
public TcpInterceptHostedService(
|
||||||
|
IEnumerable<ITcpInterceptor> tcpInterceptors,
|
||||||
|
ILogger<TcpInterceptHostedService> logger,
|
||||||
|
IHost host)
|
||||||
{
|
{
|
||||||
this.tcpInterceptors = tcpInterceptors;
|
this.tcpInterceptors = tcpInterceptors;
|
||||||
|
this.logger = logger;
|
||||||
|
this.host = host;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -29,10 +40,19 @@ namespace FastGithub.PacketIntercept
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="stoppingToken"></param>
|
/// <param name="stoppingToken"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
protected override Task ExecuteAsync(CancellationToken stoppingToken)
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
var tasks = this.tcpInterceptors.Select(item => item.InterceptAsync(stoppingToken));
|
var tasks = this.tcpInterceptors.Select(item => item.InterceptAsync(stoppingToken));
|
||||||
return Task.WhenAll(tasks);
|
await Task.WhenAll(tasks);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
stoppingToken.ThrowIfCancellationRequested();
|
||||||
|
this.logger.LogError(ex, "tcp拦截器异常");
|
||||||
|
await this.host.StopAsync(stoppingToken);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user