diff --git a/FastGithub.DomainResolve/DomainResolveHostedService.cs b/FastGithub.DomainResolve/DomainResolveHostedService.cs index e4b2db0..4bc07fa 100644 --- a/FastGithub.DomainResolve/DomainResolveHostedService.cs +++ b/FastGithub.DomainResolve/DomainResolveHostedService.cs @@ -1,4 +1,5 @@ using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; using System; using System.Threading; using System.Threading.Tasks; @@ -12,7 +13,8 @@ namespace FastGithub.DomainResolve { private readonly DnscryptProxy dnscryptProxy; private readonly IDomainResolver domainResolver; - private readonly TimeSpan testPeriodTimeSpan = TimeSpan.FromSeconds (1d); + private readonly ILogger logger; + private readonly TimeSpan testPeriodTimeSpan = TimeSpan.FromSeconds(1d); /// /// 域名解析后台服务 @@ -21,10 +23,12 @@ namespace FastGithub.DomainResolve /// public DomainResolveHostedService( DnscryptProxy dnscryptProxy, - IDomainResolver domainResolver) + IDomainResolver domainResolver, + ILogger logger) { this.dnscryptProxy = dnscryptProxy; this.domainResolver = domainResolver; + this.logger = logger; } /// @@ -34,11 +38,21 @@ namespace FastGithub.DomainResolve /// protected override async Task ExecuteAsync(CancellationToken stoppingToken) { - await this.dnscryptProxy.StartAsync(stoppingToken); - while (stoppingToken.IsCancellationRequested == false) + try { - await this.domainResolver.TestAllEndPointsAsync(stoppingToken); - await Task.Delay(this.testPeriodTimeSpan, stoppingToken); + await this.dnscryptProxy.StartAsync(stoppingToken); + while (stoppingToken.IsCancellationRequested == false) + { + await this.domainResolver.TestAllEndPointsAsync(stoppingToken); + await Task.Delay(this.testPeriodTimeSpan, stoppingToken); + } + } + catch (OperationCanceledException) + { + } + catch (Exception ex) + { + this.logger.LogError(ex, "域名解析异常"); } } diff --git a/FastGithub.PacketIntercept/DnsInterceptHostedService.cs b/FastGithub.PacketIntercept/DnsInterceptHostedService.cs index af11f42..cab5359 100644 --- a/FastGithub.PacketIntercept/DnsInterceptHostedService.cs +++ b/FastGithub.PacketIntercept/DnsInterceptHostedService.cs @@ -2,6 +2,7 @@ using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; +using System.ComponentModel; using System.Runtime.Versioning; using System.Threading; using System.Threading.Tasks; @@ -77,9 +78,14 @@ namespace FastGithub.PacketIntercept { await this.dnsInterceptor.InterceptAsync(stoppingToken); } - catch (Exception ex) + catch (OperationCanceledException) { - stoppingToken.ThrowIfCancellationRequested(); + } + catch (Win32Exception ex) when (ex.NativeErrorCode == 995) + { + } + catch (Exception ex) + { this.logger.LogError(ex, "dns拦截器异常"); await this.host.StopAsync(stoppingToken); } diff --git a/FastGithub.PacketIntercept/TcpInterceptHostedService.cs b/FastGithub.PacketIntercept/TcpInterceptHostedService.cs index 04c1a53..28a4f63 100644 --- a/FastGithub.PacketIntercept/TcpInterceptHostedService.cs +++ b/FastGithub.PacketIntercept/TcpInterceptHostedService.cs @@ -2,6 +2,7 @@ using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; +using System.ComponentModel; using System.Linq; using System.Runtime.Versioning; using System.Threading; @@ -47,9 +48,14 @@ namespace FastGithub.PacketIntercept var tasks = this.tcpInterceptors.Select(item => item.InterceptAsync(stoppingToken)); await Task.WhenAll(tasks); } + catch (OperationCanceledException) + { + } + catch (Win32Exception ex) when (ex.NativeErrorCode == 995) + { + } catch (Exception ex) { - stoppingToken.ThrowIfCancellationRequested(); this.logger.LogError(ex, "tcp拦截器异常"); await this.host.StopAsync(stoppingToken); } diff --git a/FastGithub/AppOptions.cs b/FastGithub/AppOptions.cs index 2a15c98..b9a63a8 100644 --- a/FastGithub/AppOptions.cs +++ b/FastGithub/AppOptions.cs @@ -9,5 +9,10 @@ /// 父进程id /// public int ParentProcessId { get; set; } + + /// + /// udp日志服务器端口 + /// + public int UdpLoggerPort { get; set; } } } diff --git a/FastGithub/FastGithub.csproj b/FastGithub/FastGithub.csproj index 7d346c2..f85a77e 100644 --- a/FastGithub/FastGithub.csproj +++ b/FastGithub/FastGithub.csproj @@ -11,6 +11,7 @@ + @@ -19,7 +20,7 @@ - + @@ -44,4 +45,8 @@ + + + + diff --git a/FastGithub/Program.cs b/FastGithub/Program.cs index f7bfd0c..eb3c037 100644 --- a/FastGithub/Program.cs +++ b/FastGithub/Program.cs @@ -2,8 +2,10 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; using Serilog; +using Serilog.Sinks.Network; using System; using System.IO; +using System.Net; namespace FastGithub { @@ -71,6 +73,12 @@ namespace FastGithub .Enrich.FromLogContext() .WriteTo.Console(outputTemplate: template) .WriteTo.File(Path.Combine("logs", @"log.txt"), rollingInterval: RollingInterval.Day, outputTemplate: template); + + var udpLoggerPort = hosting.Configuration.GetValue(nameof(AppOptions.UdpLoggerPort), 0); + if (udpLoggerPort > 0) + { + logger.WriteTo.UDPSink(IPAddress.Loopback, udpLoggerPort); + } }); }); } diff --git a/FastGithub/Properties/launchSettings.json b/FastGithub/Properties/launchSettings.json index b100c0a..0a87a1b 100644 --- a/FastGithub/Properties/launchSettings.json +++ b/FastGithub/Properties/launchSettings.json @@ -1,14 +1,7 @@ -{ - "$schema": "http://json.schemastore.org/launchsettings.json", +{ "profiles": { "FastGithub": { - "commandName": "Project", - "dotnetRunMessages": "true", - "launchBrowser": true, - "environmentVariables": { - "DOTNET_ENVIRONMENT": "Development", - "Logging__LogLevel__Default": "Trace" - } + "commandName": "Project" } } -} +} \ No newline at end of file diff --git a/FastGithub/Startup.cs b/FastGithub/Startup.cs index ab8919d..5dab33d 100644 --- a/FastGithub/Startup.cs +++ b/FastGithub/Startup.cs @@ -64,8 +64,7 @@ namespace FastGithub { appBuilder.UseRequestLogging(); appBuilder.UseHttpReverseProxy(); - - app.UseStaticFiles(); + appBuilder.UseRouting(); appBuilder.UseEndpoints(endpoint => {