diff --git a/FastGithub.PacketIntercept/Dns/HostsConflictSolver.cs b/FastGithub.PacketIntercept/Dns/HostsConflictSolver.cs index 2427de8..0282290 100644 --- a/FastGithub.PacketIntercept/Dns/HostsConflictSolver.cs +++ b/FastGithub.PacketIntercept/Dns/HostsConflictSolver.cs @@ -52,7 +52,7 @@ namespace FastGithub.PacketIntercept.Dns using var streamReader = new StreamReader(fileStream); while (streamReader.EndOfStream == false) { - var line = await streamReader.ReadLineAsync(); + var line = await streamReader.ReadLineAsync(cancellationToken); if (this.IsConflictingLine(line)) { hasConflicting = true; diff --git a/FastGithub/Program.cs b/FastGithub/Program.cs index 2c2129b..6c2e9c3 100644 --- a/FastGithub/Program.cs +++ b/FastGithub/Program.cs @@ -1,14 +1,11 @@ -锘縰sing Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.Hosting; -using Serilog; -using Serilog.Sinks.Network; +锘縰sing Microsoft.AspNetCore.Builder; using System; using System.IO; -using System.Net; +using System.Linq; namespace FastGithub { + class Program { /// @@ -18,68 +15,37 @@ namespace FastGithub public static void Main(string[] args) { ConsoleUtil.DisableQuickEdit(); - CreateHostBuilder(args).Build().Run(); + + var contentRoot = Path.GetDirectoryName(Environment.GetCommandLineArgs().First()); + if (string.IsNullOrEmpty(contentRoot) == false) + { + Environment.CurrentDirectory = contentRoot; + } + var options = new WebApplicationOptions + { + Args = args, + ContentRootPath = contentRoot + }; + CreateWebApplication(options).Run(); } /// /// 鍒涘缓host /// - /// + /// /// - public static IHostBuilder CreateHostBuilder(string[] args) + private static WebApplication CreateWebApplication(WebApplicationOptions options) { - return Host - .CreateDefaultBuilder(args) - .UseSystemd() - .UseWindowsService() - .UseDefaultServiceProvider(c => - { - c.ValidateOnBuild = false; - }) - .ConfigureAppConfiguration(c => - { - const string APPSETTINGS = "appsettings"; - if (Directory.Exists(APPSETTINGS) == true) - { - foreach (var file in Directory.GetFiles(APPSETTINGS, "appsettings.*.json")) - { - var jsonFile = Path.Combine(APPSETTINGS, Path.GetFileName(file)); - c.AddJsonFile(jsonFile, true, true); - } - } - }) - .UseSerilog((hosting, logger) => - { - var template = "{Timestamp:O} [{Level:u3}]{NewLine}{SourceContext}{NewLine}{Message:lj}{NewLine}{Exception}{NewLine}"; - logger - .ReadFrom.Configuration(hosting.Configuration) - .Enrich.FromLogContext() - .WriteTo.Console(outputTemplate: template) - .WriteTo.File(Path.Combine("logs", @"log.txt"), rollingInterval: RollingInterval.Day, outputTemplate: template); + var builder = WebApplication.CreateBuilder(options); + builder.ConfigureHost(); + builder.ConfigureWebHost(); + builder.ConfigureConfiguration(); + builder.ConfigureServices(); - var udpLoggerPort = hosting.Configuration.GetValue(nameof(AppOptions.UdpLoggerPort), 38457); - logger.WriteTo.UDPSink(IPAddress.Loopback, udpLoggerPort); - }) - .ConfigureWebHostDefaults(webBuilder => - { - webBuilder.UseStartup(); - webBuilder.UseShutdownTimeout(TimeSpan.FromSeconds(1d)); - webBuilder.UseKestrel(kestrel => - { - kestrel.NoLimit(); - if (OperatingSystem.IsWindows()) - { - kestrel.ListenHttpsReverseProxy(); - kestrel.ListenHttpReverseProxy(); - kestrel.ListenSshReverseProxy(); - kestrel.ListenGitReverseProxy(); - } - else - { - kestrel.ListenHttpProxy(); - } - }); - }); + var app = builder.Build(); + app.ConfigureApp(); + return app; } + } } diff --git a/FastGithub/ServiceExtensions.cs b/FastGithub/ServiceExtensions.cs index 917bd80..088d053 100644 --- a/FastGithub/ServiceExtensions.cs +++ b/FastGithub/ServiceExtensions.cs @@ -38,13 +38,7 @@ namespace FastGithub /// /// public static IHostBuilder UseWindowsService(this IHostBuilder hostBuilder) - { - var contentRoot = Path.GetDirectoryName(Environment.GetCommandLineArgs().First()); - if (contentRoot != null) - { - Environment.CurrentDirectory = contentRoot; - hostBuilder.UseContentRoot(contentRoot); - } + { return WindowsServiceLifetimeHostBuilderExtensions.UseWindowsService(hostBuilder); } diff --git a/FastGithub/Startup.cs b/FastGithub/Startup.cs index 1e6b29c..a7ef3cb 100644 --- a/FastGithub/Startup.cs +++ b/FastGithub/Startup.cs @@ -1,37 +1,99 @@ using FastGithub.Configuration; using FastGithub.FlowAnalyze; using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Serilog; +using Serilog.Sinks.Network; using System; +using System.IO; +using System.Net; namespace FastGithub { /// /// 启动项 /// - public class Startup + static class Startup { - public IConfiguration Configuration { get; } + /// + /// 配置通用主机 + /// + /// + public static void ConfigureHost(this WebApplicationBuilder builder) + { + builder.Host.UseSystemd().UseWindowsService(); + builder.Host.UseSerilog((hosting, logger) => + { + var template = "{Timestamp:O} [{Level:u3}]{NewLine}{SourceContext}{NewLine}{Message:lj}{NewLine}{Exception}{NewLine}"; + logger + .ReadFrom.Configuration(hosting.Configuration) + .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), 38457); + logger.WriteTo.UDPSink(IPAddress.Loopback, udpLoggerPort); + }); + } /// - /// 启动项 + /// 配置web主机 /// - /// - public Startup(IConfiguration configuration) + /// + public static void ConfigureWebHost(this WebApplicationBuilder builder) { - this.Configuration = configuration; + builder.WebHost.UseShutdownTimeout(TimeSpan.FromSeconds(1d)); + builder.WebHost.UseKestrel(kestrel => + { + kestrel.NoLimit(); + if (OperatingSystem.IsWindows()) + { + kestrel.ListenHttpsReverseProxy(); + kestrel.ListenHttpReverseProxy(); + kestrel.ListenSshReverseProxy(); + kestrel.ListenGitReverseProxy(); + } + else + { + kestrel.ListenHttpProxy(); + } + }); } + + /// + /// 配置配置 + /// + /// + public static void ConfigureConfiguration(this WebApplicationBuilder builder) + { + const string APPSETTINGS = "appsettings"; + if (Directory.Exists(APPSETTINGS) == true) + { + foreach (var file in Directory.GetFiles(APPSETTINGS, "appsettings.*.json")) + { + var jsonFile = Path.Combine(APPSETTINGS, Path.GetFileName(file)); + builder.Configuration.AddJsonFile(jsonFile, true, true); + } + } + } + + /// /// 配置服务 /// - /// - public void ConfigureServices(IServiceCollection services) + /// + public static void ConfigureServices(this WebApplicationBuilder builder) { - services.Configure(this.Configuration); - services.Configure(this.Configuration.GetSection(nameof(FastGithub))); + var services = builder.Services; + var configuration = builder.Configuration; + + services.Configure(configuration); + services.Configure(configuration.GetSection(nameof(FastGithub))); services.AddConfiguration(); services.AddDomainResolve(); @@ -47,10 +109,10 @@ namespace FastGithub } /// - /// 配置中间件 + /// 配置应用 /// /// - public void Configure(IApplicationBuilder app) + public static void ConfigureApp(this WebApplication app) { app.UseHttpProxyPac(); app.UseRequestLogging(); @@ -58,13 +120,11 @@ namespace FastGithub app.UseRouting(); app.DisableRequestLogging(); - app.UseEndpoints(endpoint => + + app.MapGet("/flowStatistics", context => { - endpoint.MapGet("/flowStatistics", context => - { - var flowStatistics = context.RequestServices.GetRequiredService().GetFlowStatistics(); - return context.Response.WriteAsJsonAsync(flowStatistics); - }); + var flowStatistics = context.RequestServices.GetRequiredService().GetFlowStatistics(); + return context.Response.WriteAsJsonAsync(flowStatistics); }); } }