From 23c2e4f40f10dac8b396cbf342ab705940355881 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E9=99=88=E5=9B=BD=E4=BC=9F?= <366193849@qq.com>
Date: Fri, 24 Sep 2021 12:49:13 +0800
Subject: [PATCH] =?UTF-8?q?=E5=BC=82=E5=B8=B8=E7=9F=AD=E8=B7=AF?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../Dns/DnsInterceptor.cs | 29 +++++++---------
.../Tcp/TcpInterceptor.cs | 34 ++++++++-----------
FastGithub/AppHostedService.cs | 29 ++++++----------
3 files changed, 38 insertions(+), 54 deletions(-)
diff --git a/FastGithub.PacketIntercept/Dns/DnsInterceptor.cs b/FastGithub.PacketIntercept/Dns/DnsInterceptor.cs
index ad8ad1e..70a7d38 100644
--- a/FastGithub.PacketIntercept/Dns/DnsInterceptor.cs
+++ b/FastGithub.PacketIntercept/Dns/DnsInterceptor.cs
@@ -81,25 +81,22 @@ namespace FastGithub.PacketIntercept.Dns
DnsFlushResolverCache();
while (cancellationToken.IsCancellationRequested == false)
{
- if (WinDivert.WinDivertRecv(handle, winDivertBuffer, ref winDivertAddress, ref packetLength))
+ if (WinDivert.WinDivertRecv(handle, winDivertBuffer, ref winDivertAddress, ref packetLength) == false)
{
- try
- {
- this.ModifyDnsPacket(winDivertBuffer, ref winDivertAddress, ref packetLength);
- }
- catch (Exception ex)
- {
- this.logger.LogWarning(ex.Message);
- }
- finally
- {
- WinDivert.WinDivertSend(handle, winDivertBuffer, packetLength, ref winDivertAddress);
- }
+ throw new Win32Exception();
}
- else
+
+ try
{
- var errorCode = Marshal.GetLastWin32Error();
- throw new Win32Exception(errorCode);
+ this.ModifyDnsPacket(winDivertBuffer, ref winDivertAddress, ref packetLength);
+ }
+ catch (Exception ex)
+ {
+ this.logger.LogWarning(ex.Message);
+ }
+ finally
+ {
+ WinDivert.WinDivertSend(handle, winDivertBuffer, packetLength, ref winDivertAddress);
}
}
}
diff --git a/FastGithub.PacketIntercept/Tcp/TcpInterceptor.cs b/FastGithub.PacketIntercept/Tcp/TcpInterceptor.cs
index 4b49299..2dfd4c7 100644
--- a/FastGithub.PacketIntercept/Tcp/TcpInterceptor.cs
+++ b/FastGithub.PacketIntercept/Tcp/TcpInterceptor.cs
@@ -3,7 +3,6 @@ using System;
using System.Buffers.Binary;
using System.ComponentModel;
using System.Net;
-using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Threading;
using System.Threading.Tasks;
@@ -16,7 +15,7 @@ namespace FastGithub.PacketIntercept.Tcp
///
[SupportedOSPlatform("windows")]
abstract class TcpInterceptor : ITcpInterceptor
- {
+ {
private readonly string filter;
private readonly ushort oldServerPort;
private readonly ushort newServerPort;
@@ -52,7 +51,7 @@ namespace FastGithub.PacketIntercept.Tcp
var handle = WinDivert.WinDivertOpen(this.filter, WinDivertLayer.Network, 0, WinDivertOpenFlags.None);
if (handle == IntPtr.MaxValue || handle == IntPtr.Zero)
- {
+ {
const int ERROR_INVALID_HANDLE = 0x6;
throw new Win32Exception(ERROR_INVALID_HANDLE, "打开驱动失败");
}
@@ -66,25 +65,22 @@ namespace FastGithub.PacketIntercept.Tcp
while (cancellationToken.IsCancellationRequested == false)
{
- if (WinDivert.WinDivertRecv(handle, winDivertBuffer, ref winDivertAddress, ref packetLength))
+ if (WinDivert.WinDivertRecv(handle, winDivertBuffer, ref winDivertAddress, ref packetLength) == false)
{
- try
- {
- this.ModifyTcpPacket(winDivertBuffer, ref winDivertAddress, ref packetLength);
- }
- catch (Exception ex)
- {
- this.logger.LogWarning(ex.Message);
- }
- finally
- {
- WinDivert.WinDivertSend(handle, winDivertBuffer, packetLength, ref winDivertAddress);
- }
+ throw new Win32Exception();
}
- else
+
+ try
{
- var errorCode = Marshal.GetLastWin32Error();
- throw new Win32Exception(errorCode);
+ this.ModifyTcpPacket(winDivertBuffer, ref winDivertAddress, ref packetLength);
+ }
+ catch (Exception ex)
+ {
+ this.logger.LogWarning(ex.Message);
+ }
+ finally
+ {
+ WinDivert.WinDivertSend(handle, winDivertBuffer, packetLength, ref winDivertAddress);
}
}
}
diff --git a/FastGithub/AppHostedService.cs b/FastGithub/AppHostedService.cs
index 38f00d9..7baf2fc 100644
--- a/FastGithub/AppHostedService.cs
+++ b/FastGithub/AppHostedService.cs
@@ -39,17 +39,6 @@ namespace FastGithub
return base.StartAsync(cancellationToken);
}
- ///
- /// 停止完成
- ///
- ///
- ///
- public override Task StopAsync(CancellationToken cancellationToken)
- {
- this.logger.LogInformation($"{nameof(FastGithub)}已停止运行");
- return base.StopAsync(cancellationToken);
- }
-
///
/// 后台任务
///
@@ -57,14 +46,16 @@ namespace FastGithub
///
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
- if (OperatingSystem.IsWindows() == false)
+ if (OperatingSystem.IsWindows())
{
- await Task.Delay(TimeSpan.FromSeconds(1d), stoppingToken);
- if (await this.UseFastGithubProxyAsync() == false)
- {
- var httpProxyPort = this.options.Value.HttpProxyPort;
- this.logger.LogWarning($"请设置系统自动代理为http://{IPAddress.Loopback}:{httpProxyPort},或手动代理http/https为{IPAddress.Loopback}:{httpProxyPort}");
- }
+ return;
+ }
+
+ await Task.Delay(TimeSpan.FromSeconds(1d), stoppingToken);
+ if (await this.UseFastGithubProxyAsync() == false)
+ {
+ var httpProxyPort = this.options.Value.HttpProxyPort;
+ this.logger.LogWarning($"请设置系统自动代理为http://{IPAddress.Loopback}:{httpProxyPort},或手动代理http/https为{IPAddress.Loopback}:{httpProxyPort}");
}
}
@@ -108,7 +99,7 @@ namespace FastGithub
try
{
- var addresses = await System.Net.Dns.GetHostAddressesAsync(proxyServer.Host);
+ var addresses = await Dns.GetHostAddressesAsync(proxyServer.Host);
return addresses.Contains(IPAddress.Loopback);
}
catch (Exception)