Bläddra i källkod

增加udp日志、background异常处理

陈国伟 3 år sedan
förälder
incheckning
1ef5d2465b

+ 20 - 6
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<DomainResolveHostedService> logger;
+        private readonly TimeSpan testPeriodTimeSpan = TimeSpan.FromSeconds(1d);
 
         /// <summary>
         /// 域名解析后台服务
@@ -21,10 +23,12 @@ namespace FastGithub.DomainResolve
         /// <param name="domainResolver"></param>
         public DomainResolveHostedService(
             DnscryptProxy dnscryptProxy,
-            IDomainResolver domainResolver)
+            IDomainResolver domainResolver,
+            ILogger<DomainResolveHostedService> logger)
         {
             this.dnscryptProxy = dnscryptProxy;
             this.domainResolver = domainResolver;
+            this.logger = logger;
         }
 
         /// <summary>
@@ -34,11 +38,21 @@ namespace FastGithub.DomainResolve
         /// <returns></returns>
         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, "域名解析异常");
             }
         }
 

+ 8 - 2
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)
+            {
+            }
+            catch (Win32Exception ex) when (ex.NativeErrorCode == 995)
             {
-                stoppingToken.ThrowIfCancellationRequested();
+            }
+            catch (Exception ex)
+            { 
                 this.logger.LogError(ex, "dns拦截器异常");
                 await this.host.StopAsync(stoppingToken);
             }

+ 7 - 1
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);
             }

+ 5 - 0
FastGithub/AppOptions.cs

@@ -9,5 +9,10 @@
         /// 父进程id
         /// </summary>
         public int ParentProcessId { get; set; }
+
+        /// <summary>
+        /// udp日志服务器端口
+        /// </summary>
+        public int UdpLoggerPort { get; set; }
     }
 }

+ 6 - 1
FastGithub/FastGithub.csproj

@@ -11,6 +11,7 @@
   <ItemGroup>
     <PackageReference Include="PInvoke.AdvApi32" Version="0.7.104" />
     <PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="5.0.1" />
+    <PackageReference Include="Serilog.Sinks.Network" Version="2.0.2.68" />
     <ProjectReference Include="..\FastGithub.DomainResolve\FastGithub.DomainResolve.csproj" />
     <ProjectReference Include="..\FastGithub.HttpServer\FastGithub.HttpServer.csproj" />
     <ProjectReference Include="..\FastGithub.PacketIntercept\FastGithub.PacketIntercept.csproj" />
@@ -19,7 +20,7 @@
   <ItemGroup>
     <PackageReference Include="Serilog.AspNetCore" Version="4.1.0" />
     <PackageReference Include="Serilog.Sinks.Console" Version="4.0.0" />
-    <PackageReference Include="Serilog.Sinks.File" Version="5.0.0" /> 
+    <PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
     <PackageReference Include="Serilog.Settings.Configuration" Version="3.2.0" />
   </ItemGroup>
 
@@ -44,4 +45,8 @@
     </None>
   </ItemGroup>
 
+  <ItemGroup>
+    <Folder Include="Properties\" />
+  </ItemGroup>
+
 </Project>

+ 8 - 0
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);
+                        }
                     });
                 });
         }

+ 3 - 10
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"
     }
   }
-}
+}

+ 1 - 2
FastGithub/Startup.cs

@@ -64,8 +64,7 @@ namespace FastGithub
             {
                 appBuilder.UseRequestLogging();
                 appBuilder.UseHttpReverseProxy();
-
-                app.UseStaticFiles();
+                 
                 appBuilder.UseRouting();
                 appBuilder.UseEndpoints(endpoint =>
                 {