协议分类
This commit is contained in:
parent
e5dcab793f
commit
b441405cb7
@ -11,15 +11,16 @@ using System.Net;
|
|||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
using System.Runtime.Versioning;
|
using System.Runtime.Versioning;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
using WinDivertSharp;
|
using WinDivertSharp;
|
||||||
|
|
||||||
namespace FastGithub.PacketIntercept
|
namespace FastGithub.PacketIntercept.Dns
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// dns拦截器
|
/// dns拦截器
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[SupportedOSPlatform("windows")]
|
[SupportedOSPlatform("windows")]
|
||||||
sealed class DnsInterceptor
|
sealed class DnsInterceptor : IDnsInterceptor
|
||||||
{
|
{
|
||||||
private const string DNS_FILTER = "udp.DstPort == 53";
|
private const string DNS_FILTER = "udp.DstPort == 53";
|
||||||
private readonly FastGithubConfig fastGithubConfig;
|
private readonly FastGithubConfig fastGithubConfig;
|
||||||
@ -52,8 +53,11 @@ namespace FastGithub.PacketIntercept
|
|||||||
/// DNS拦截
|
/// DNS拦截
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="cancellationToken"></param>
|
/// <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);
|
var handle = WinDivert.WinDivertOpen(DNS_FILTER, WinDivertLayer.Network, 0, WinDivertOpenFlags.None);
|
||||||
if (handle == IntPtr.Zero)
|
if (handle == IntPtr.Zero)
|
||||||
{
|
{
|
||||||
@ -163,7 +167,7 @@ namespace FastGithub.PacketIntercept
|
|||||||
{
|
{
|
||||||
winDivertAddress.Direction = WinDivertDirection.Inbound;
|
winDivertAddress.Direction = WinDivertDirection.Inbound;
|
||||||
}
|
}
|
||||||
|
|
||||||
WinDivert.WinDivertHelperCalcChecksums(winDivertBuffer, packetLength, ref winDivertAddress, WinDivertChecksumHelperParam.All);
|
WinDivert.WinDivertHelperCalcChecksums(winDivertBuffer, packetLength, ref winDivertAddress, WinDivertChecksumHelperParam.All);
|
||||||
this.logger.LogInformation($"{domain} => {IPAddress.Loopback}");
|
this.logger.LogInformation($"{domain} => {IPAddress.Loopback}");
|
||||||
}
|
}
|
||||||
@ -6,13 +6,13 @@ using System.Text;
|
|||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace FastGithub.PacketIntercept
|
namespace FastGithub.PacketIntercept.Dns
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// host文件冲解决者
|
/// host文件冲解决者
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[SupportedOSPlatform("windows")]
|
[SupportedOSPlatform("windows")]
|
||||||
sealed class HostsConflictSolver : IConflictSolver
|
sealed class HostsConflictSolver : IDnsConflictSolver
|
||||||
{
|
{
|
||||||
private readonly FastGithubConfig fastGithubConfig;
|
private readonly FastGithubConfig fastGithubConfig;
|
||||||
|
|
||||||
@ -11,13 +11,13 @@ using System.Runtime.Versioning;
|
|||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace FastGithub.PacketIntercept
|
namespace FastGithub.PacketIntercept.Dns
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 代理冲突解决者
|
/// 代理冲突解决者
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[SupportedOSPlatform("windows")]
|
[SupportedOSPlatform("windows")]
|
||||||
sealed class ProxyConflictSolver : IConflictSolver
|
sealed class ProxyConflictSolver : IDnsConflictSolver
|
||||||
{
|
{
|
||||||
private const int INTERNET_OPTION_REFRESH = 37;
|
private const int INTERNET_OPTION_REFRESH = 37;
|
||||||
private const int INTERNET_OPTION_PROXY_SETTINGS_CHANGED = 95;
|
private const int INTERNET_OPTION_PROXY_SETTINGS_CHANGED = 95;
|
||||||
@ -12,8 +12,8 @@ namespace FastGithub.PacketIntercept
|
|||||||
[SupportedOSPlatform("windows")]
|
[SupportedOSPlatform("windows")]
|
||||||
sealed class DnsInterceptHostedService : BackgroundService
|
sealed class DnsInterceptHostedService : BackgroundService
|
||||||
{
|
{
|
||||||
private readonly DnsInterceptor dnsInterceptor;
|
private readonly IDnsInterceptor dnsInterceptor;
|
||||||
private readonly IEnumerable<IConflictSolver> conflictSolvers;
|
private readonly IEnumerable<IDnsConflictSolver> conflictSolvers;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// dns拦截后台服务
|
/// dns拦截后台服务
|
||||||
@ -21,8 +21,8 @@ namespace FastGithub.PacketIntercept
|
|||||||
/// <param name="dnsInterceptor"></param>
|
/// <param name="dnsInterceptor"></param>
|
||||||
/// <param name="conflictSolvers"></param>
|
/// <param name="conflictSolvers"></param>
|
||||||
public DnsInterceptHostedService(
|
public DnsInterceptHostedService(
|
||||||
DnsInterceptor dnsInterceptor,
|
IDnsInterceptor dnsInterceptor,
|
||||||
IEnumerable<IConflictSolver> conflictSolvers)
|
IEnumerable<IDnsConflictSolver> conflictSolvers)
|
||||||
{
|
{
|
||||||
this.dnsInterceptor = dnsInterceptor;
|
this.dnsInterceptor = dnsInterceptor;
|
||||||
this.conflictSolvers = conflictSolvers;
|
this.conflictSolvers = conflictSolvers;
|
||||||
@ -61,10 +61,9 @@ namespace FastGithub.PacketIntercept
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="stoppingToken"></param>
|
/// <param name="stoppingToken"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
protected override Task ExecuteAsync(CancellationToken stoppingToken)
|
||||||
{
|
{
|
||||||
await Task.Yield();
|
return this.dnsInterceptor.InterceptAsync(stoppingToken);
|
||||||
this.dnsInterceptor.Intercept(stoppingToken);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,7 +6,7 @@ namespace FastGithub.PacketIntercept
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Dns冲突解决者
|
/// Dns冲突解决者
|
||||||
/// </summary>
|
/// </summary>
|
||||||
interface IConflictSolver
|
interface IDnsConflictSolver
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 解决冲突
|
/// 解决冲突
|
||||||
18
FastGithub.PacketIntercept/IDnsInterceptor.cs
Normal file
18
FastGithub.PacketIntercept/IDnsInterceptor.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,4 +1,5 @@
|
|||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace FastGithub.PacketIntercept
|
namespace FastGithub.PacketIntercept
|
||||||
{
|
{
|
||||||
@ -8,9 +9,10 @@ namespace FastGithub.PacketIntercept
|
|||||||
interface ITcpInterceptor
|
interface ITcpInterceptor
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 拦截tcp
|
/// 拦截数据包
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="cancellationToken"></param>
|
/// <param name="cancellationToken"></param>
|
||||||
void Intercept(CancellationToken cancellationToken);
|
/// <returns></returns>
|
||||||
|
Task InterceptAsync(CancellationToken cancellationToken);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1,4 +1,6 @@
|
|||||||
using FastGithub.PacketIntercept;
|
using FastGithub.PacketIntercept;
|
||||||
|
using FastGithub.PacketIntercept.Dns;
|
||||||
|
using FastGithub.PacketIntercept.Tcp;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||||
using System.Runtime.Versioning;
|
using System.Runtime.Versioning;
|
||||||
@ -18,9 +20,9 @@ namespace FastGithub
|
|||||||
[SupportedOSPlatform("windows")]
|
[SupportedOSPlatform("windows")]
|
||||||
public static IServiceCollection AddPacketIntercept(this IServiceCollection services)
|
public static IServiceCollection AddPacketIntercept(this IServiceCollection services)
|
||||||
{
|
{
|
||||||
services.AddSingleton<IConflictSolver, HostsConflictSolver>();
|
services.AddSingleton<IDnsConflictSolver, HostsConflictSolver>();
|
||||||
services.AddSingleton<IConflictSolver, ProxyConflictSolver>();
|
services.AddSingleton<IDnsConflictSolver, ProxyConflictSolver>();
|
||||||
services.TryAddSingleton<DnsInterceptor>();
|
services.TryAddSingleton<IDnsInterceptor, DnsInterceptor>();
|
||||||
services.AddHostedService<DnsInterceptHostedService>();
|
services.AddHostedService<DnsInterceptHostedService>();
|
||||||
|
|
||||||
services.AddSingleton<ITcpInterceptor, SshInterceptor>();
|
services.AddSingleton<ITcpInterceptor, SshInterceptor>();
|
||||||
|
|||||||
@ -2,7 +2,7 @@
|
|||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using System.Runtime.Versioning;
|
using System.Runtime.Versioning;
|
||||||
|
|
||||||
namespace FastGithub.PacketIntercept
|
namespace FastGithub.PacketIntercept.Tcp
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// http拦截器
|
/// http拦截器
|
||||||
@ -2,7 +2,7 @@
|
|||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using System.Runtime.Versioning;
|
using System.Runtime.Versioning;
|
||||||
|
|
||||||
namespace FastGithub.PacketIntercept
|
namespace FastGithub.PacketIntercept.Tcp
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// https拦截器
|
/// https拦截器
|
||||||
@ -2,7 +2,7 @@
|
|||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using System.Runtime.Versioning;
|
using System.Runtime.Versioning;
|
||||||
|
|
||||||
namespace FastGithub.PacketIntercept
|
namespace FastGithub.PacketIntercept.Tcp
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// ssh拦截器
|
/// ssh拦截器
|
||||||
@ -4,9 +4,10 @@ using System.Buffers.Binary;
|
|||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Runtime.Versioning;
|
using System.Runtime.Versioning;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
using WinDivertSharp;
|
using WinDivertSharp;
|
||||||
|
|
||||||
namespace FastGithub.PacketIntercept
|
namespace FastGithub.PacketIntercept.Tcp
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// tcp拦截器
|
/// tcp拦截器
|
||||||
@ -37,13 +38,15 @@ namespace FastGithub.PacketIntercept
|
|||||||
/// 拦截指定端口的数据包
|
/// 拦截指定端口的数据包
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="cancellationToken"></param>
|
/// <param name="cancellationToken"></param>
|
||||||
public void Intercept(CancellationToken cancellationToken)
|
public async Task InterceptAsync(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
if (this.oldServerPort == this.newServerPort)
|
if (this.oldServerPort == this.newServerPort)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await Task.Yield();
|
||||||
|
|
||||||
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.Zero)
|
if (handle == IntPtr.Zero)
|
||||||
{
|
{
|
||||||
@ -31,20 +31,8 @@ namespace FastGithub.PacketIntercept
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
protected override Task ExecuteAsync(CancellationToken stoppingToken)
|
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);
|
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user