异常短路

This commit is contained in:
陈国伟 2021-09-24 12:49:13 +08:00
parent b9d8fccd77
commit 23c2e4f40f
3 changed files with 38 additions and 54 deletions

View File

@ -81,8 +81,11 @@ namespace FastGithub.PacketIntercept.Dns
DnsFlushResolverCache(); DnsFlushResolverCache();
while (cancellationToken.IsCancellationRequested == false) while (cancellationToken.IsCancellationRequested == false)
{ {
if (WinDivert.WinDivertRecv(handle, winDivertBuffer, ref winDivertAddress, ref packetLength)) if (WinDivert.WinDivertRecv(handle, winDivertBuffer, ref winDivertAddress, ref packetLength) == false)
{ {
throw new Win32Exception();
}
try try
{ {
this.ModifyDnsPacket(winDivertBuffer, ref winDivertAddress, ref packetLength); this.ModifyDnsPacket(winDivertBuffer, ref winDivertAddress, ref packetLength);
@ -96,12 +99,6 @@ namespace FastGithub.PacketIntercept.Dns
WinDivert.WinDivertSend(handle, winDivertBuffer, packetLength, ref winDivertAddress); WinDivert.WinDivertSend(handle, winDivertBuffer, packetLength, ref winDivertAddress);
} }
} }
else
{
var errorCode = Marshal.GetLastWin32Error();
throw new Win32Exception(errorCode);
}
}
} }
/// <summary> /// <summary>

View File

@ -3,7 +3,6 @@ using System;
using System.Buffers.Binary; using System.Buffers.Binary;
using System.ComponentModel; using System.ComponentModel;
using System.Net; using System.Net;
using System.Runtime.InteropServices;
using System.Runtime.Versioning; using System.Runtime.Versioning;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -66,8 +65,11 @@ namespace FastGithub.PacketIntercept.Tcp
while (cancellationToken.IsCancellationRequested == false) while (cancellationToken.IsCancellationRequested == false)
{ {
if (WinDivert.WinDivertRecv(handle, winDivertBuffer, ref winDivertAddress, ref packetLength)) if (WinDivert.WinDivertRecv(handle, winDivertBuffer, ref winDivertAddress, ref packetLength) == false)
{ {
throw new Win32Exception();
}
try try
{ {
this.ModifyTcpPacket(winDivertBuffer, ref winDivertAddress, ref packetLength); this.ModifyTcpPacket(winDivertBuffer, ref winDivertAddress, ref packetLength);
@ -81,12 +83,6 @@ namespace FastGithub.PacketIntercept.Tcp
WinDivert.WinDivertSend(handle, winDivertBuffer, packetLength, ref winDivertAddress); WinDivert.WinDivertSend(handle, winDivertBuffer, packetLength, ref winDivertAddress);
} }
} }
else
{
var errorCode = Marshal.GetLastWin32Error();
throw new Win32Exception(errorCode);
}
}
} }
/// <summary> /// <summary>

View File

@ -39,17 +39,6 @@ namespace FastGithub
return base.StartAsync(cancellationToken); return base.StartAsync(cancellationToken);
} }
/// <summary>
/// 停止完成
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public override Task StopAsync(CancellationToken cancellationToken)
{
this.logger.LogInformation($"{nameof(FastGithub)}已停止运行");
return base.StopAsync(cancellationToken);
}
/// <summary> /// <summary>
/// 后台任务 /// 后台任务
/// </summary> /// </summary>
@ -57,8 +46,11 @@ namespace FastGithub
/// <returns></returns> /// <returns></returns>
protected override async Task ExecuteAsync(CancellationToken stoppingToken) protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{ {
if (OperatingSystem.IsWindows() == false) if (OperatingSystem.IsWindows())
{ {
return;
}
await Task.Delay(TimeSpan.FromSeconds(1d), stoppingToken); await Task.Delay(TimeSpan.FromSeconds(1d), stoppingToken);
if (await this.UseFastGithubProxyAsync() == false) if (await this.UseFastGithubProxyAsync() == false)
{ {
@ -66,7 +58,6 @@ namespace FastGithub
this.logger.LogWarning($"请设置系统自动代理为http://{IPAddress.Loopback}:{httpProxyPort}或手动代理http/https为{IPAddress.Loopback}:{httpProxyPort}"); this.logger.LogWarning($"请设置系统自动代理为http://{IPAddress.Loopback}:{httpProxyPort}或手动代理http/https为{IPAddress.Loopback}:{httpProxyPort}");
} }
} }
}
/// <summary> /// <summary>
/// 应用fastgithub代理 /// 应用fastgithub代理
@ -108,7 +99,7 @@ namespace FastGithub
try try
{ {
var addresses = await System.Net.Dns.GetHostAddressesAsync(proxyServer.Host); var addresses = await Dns.GetHostAddressesAsync(proxyServer.Host);
return addresses.Contains(IPAddress.Loopback); return addresses.Contains(IPAddress.Loopback);
} }
catch (Exception) catch (Exception)