协议分类

This commit is contained in:
陈国伟 2021-09-22 14:29:27 +08:00
parent e5dcab793f
commit b441405cb7
13 changed files with 55 additions and 39 deletions

View File

@ -11,15 +11,16 @@ using System.Net;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Threading;
using System.Threading.Tasks;
using WinDivertSharp;
namespace FastGithub.PacketIntercept
namespace FastGithub.PacketIntercept.Dns
{
/// <summary>
/// dns拦截器
/// </summary>
[SupportedOSPlatform("windows")]
sealed class DnsInterceptor
sealed class DnsInterceptor : IDnsInterceptor
{
private const string DNS_FILTER = "udp.DstPort == 53";
private readonly FastGithubConfig fastGithubConfig;
@ -52,8 +53,11 @@ namespace FastGithub.PacketIntercept
/// DNS拦截
/// </summary>
/// <param name="cancellationToken"></param>
public void Intercept(CancellationToken cancellationToken)
/// <returns></returns>
public async Task InterceptAsync(CancellationToken cancellationToken)
{
await Task.Yield();
var handle = WinDivert.WinDivertOpen(DNS_FILTER, WinDivertLayer.Network, 0, WinDivertOpenFlags.None);
if (handle == IntPtr.Zero)
{
@ -163,7 +167,7 @@ namespace FastGithub.PacketIntercept
{
winDivertAddress.Direction = WinDivertDirection.Inbound;
}
WinDivert.WinDivertHelperCalcChecksums(winDivertBuffer, packetLength, ref winDivertAddress, WinDivertChecksumHelperParam.All);
this.logger.LogInformation($"{domain} => {IPAddress.Loopback}");
}

View File

@ -6,13 +6,13 @@ using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace FastGithub.PacketIntercept
namespace FastGithub.PacketIntercept.Dns
{
/// <summary>
/// host文件冲解决者
/// </summary>
[SupportedOSPlatform("windows")]
sealed class HostsConflictSolver : IConflictSolver
sealed class HostsConflictSolver : IDnsConflictSolver
{
private readonly FastGithubConfig fastGithubConfig;

View File

@ -11,13 +11,13 @@ using System.Runtime.Versioning;
using System.Threading;
using System.Threading.Tasks;
namespace FastGithub.PacketIntercept
namespace FastGithub.PacketIntercept.Dns
{
/// <summary>
/// 代理冲突解决者
/// </summary>
[SupportedOSPlatform("windows")]
sealed class ProxyConflictSolver : IConflictSolver
sealed class ProxyConflictSolver : IDnsConflictSolver
{
private const int INTERNET_OPTION_REFRESH = 37;
private const int INTERNET_OPTION_PROXY_SETTINGS_CHANGED = 95;

View File

@ -12,8 +12,8 @@ namespace FastGithub.PacketIntercept
[SupportedOSPlatform("windows")]
sealed class DnsInterceptHostedService : BackgroundService
{
private readonly DnsInterceptor dnsInterceptor;
private readonly IEnumerable<IConflictSolver> conflictSolvers;
private readonly IDnsInterceptor dnsInterceptor;
private readonly IEnumerable<IDnsConflictSolver> conflictSolvers;
/// <summary>
/// dns拦截后台服务
@ -21,8 +21,8 @@ namespace FastGithub.PacketIntercept
/// <param name="dnsInterceptor"></param>
/// <param name="conflictSolvers"></param>
public DnsInterceptHostedService(
DnsInterceptor dnsInterceptor,
IEnumerable<IConflictSolver> conflictSolvers)
IDnsInterceptor dnsInterceptor,
IEnumerable<IDnsConflictSolver> conflictSolvers)
{
this.dnsInterceptor = dnsInterceptor;
this.conflictSolvers = conflictSolvers;
@ -61,10 +61,9 @@ namespace FastGithub.PacketIntercept
/// </summary>
/// <param name="stoppingToken"></param>
/// <returns></returns>
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
protected override Task ExecuteAsync(CancellationToken stoppingToken)
{
await Task.Yield();
this.dnsInterceptor.Intercept(stoppingToken);
return this.dnsInterceptor.InterceptAsync(stoppingToken);
}
}
}

View File

@ -6,7 +6,7 @@ namespace FastGithub.PacketIntercept
/// <summary>
/// Dns冲突解决者
/// </summary>
interface IConflictSolver
interface IDnsConflictSolver
{
/// <summary>
/// 解决冲突

View File

@ -0,0 +1,18 @@
using System.Threading;
using System.Threading.Tasks;
namespace FastGithub.PacketIntercept
{
/// <summary>
/// dns拦截器接口
/// </summary>
interface IDnsInterceptor
{
/// <summary>
/// 拦截数据包
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task InterceptAsync(CancellationToken cancellationToken);
}
}

View File

@ -1,4 +1,5 @@
using System.Threading;
using System.Threading.Tasks;
namespace FastGithub.PacketIntercept
{
@ -8,9 +9,10 @@ namespace FastGithub.PacketIntercept
interface ITcpInterceptor
{
/// <summary>
/// 拦截tcp
/// 拦截数据包
/// </summary>
/// <param name="cancellationToken"></param>
void Intercept(CancellationToken cancellationToken);
/// <returns></returns>
Task InterceptAsync(CancellationToken cancellationToken);
}
}

View File

@ -1,4 +1,6 @@
using FastGithub.PacketIntercept;
using FastGithub.PacketIntercept.Dns;
using FastGithub.PacketIntercept.Tcp;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using System.Runtime.Versioning;
@ -18,9 +20,9 @@ namespace FastGithub
[SupportedOSPlatform("windows")]
public static IServiceCollection AddPacketIntercept(this IServiceCollection services)
{
services.AddSingleton<IConflictSolver, HostsConflictSolver>();
services.AddSingleton<IConflictSolver, ProxyConflictSolver>();
services.TryAddSingleton<DnsInterceptor>();
services.AddSingleton<IDnsConflictSolver, HostsConflictSolver>();
services.AddSingleton<IDnsConflictSolver, ProxyConflictSolver>();
services.TryAddSingleton<IDnsInterceptor, DnsInterceptor>();
services.AddHostedService<DnsInterceptHostedService>();
services.AddSingleton<ITcpInterceptor, SshInterceptor>();

View File

@ -2,7 +2,7 @@
using Microsoft.Extensions.Logging;
using System.Runtime.Versioning;
namespace FastGithub.PacketIntercept
namespace FastGithub.PacketIntercept.Tcp
{
/// <summary>
/// http拦截器

View File

@ -2,7 +2,7 @@
using Microsoft.Extensions.Logging;
using System.Runtime.Versioning;
namespace FastGithub.PacketIntercept
namespace FastGithub.PacketIntercept.Tcp
{
/// <summary>
/// https拦截器

View File

@ -2,7 +2,7 @@
using Microsoft.Extensions.Logging;
using System.Runtime.Versioning;
namespace FastGithub.PacketIntercept
namespace FastGithub.PacketIntercept.Tcp
{
/// <summary>
/// ssh拦截器

View File

@ -4,9 +4,10 @@ using System.Buffers.Binary;
using System.Net;
using System.Runtime.Versioning;
using System.Threading;
using System.Threading.Tasks;
using WinDivertSharp;
namespace FastGithub.PacketIntercept
namespace FastGithub.PacketIntercept.Tcp
{
/// <summary>
/// tcp拦截器
@ -37,13 +38,15 @@ namespace FastGithub.PacketIntercept
/// 拦截指定端口的数据包
/// </summary>
/// <param name="cancellationToken"></param>
public void Intercept(CancellationToken cancellationToken)
public async Task InterceptAsync(CancellationToken cancellationToken)
{
if (this.oldServerPort == this.newServerPort)
{
return;
}
await Task.Yield();
var handle = WinDivert.WinDivertOpen(this.filter, WinDivertLayer.Network, 0, WinDivertOpenFlags.None);
if (handle == IntPtr.Zero)
{

View File

@ -31,20 +31,8 @@ namespace FastGithub.PacketIntercept
/// <returns></returns>
protected override Task ExecuteAsync(CancellationToken stoppingToken)
{
var tasks = this.tcpInterceptors.Select(item => this.InterceptAsync(item, stoppingToken));
var tasks = this.tcpInterceptors.Select(item => item.InterceptAsync(stoppingToken));
return Task.WhenAll(tasks);
}
/// <summary>
/// 拦截
/// </summary>
/// <param name="interceptor"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
private async Task InterceptAsync(ITcpInterceptor interceptor, CancellationToken cancellationToken)
{
await Task.Yield();
interceptor.Intercept(cancellationToken);
}
}
}