网站首页 网站源码
website
站点相关全部源代码,隐藏了一些关于服务器的信息
using Microsoft.AspNetCore.DataProtection.KeyManagement;
using Microsoft.AspNetCore.Localization;
using Microsoft.Extensions.Primitives;
using StackExchange.Redis;

const string corsScheme = "Open-Dpz-Core";

Log.Logger = new LoggerConfiguration().Enrich.FromLogContext().CreateBootstrapLogger();

try
{
    var builder = WebApplication.CreateBuilder(args);
    builder.Host.UseAgileConfig(new ConfigClient(builder.Configuration));
    var configuration = builder.Configuration;
    CdnBaseAddress =
        configuration["CDNBaseAddress"]
        ?? throw new Exception("configuration node CDNBaseAddress is null or empty");

    var logSeq = configuration.GetSection("LogSeq").Get<LogSeq>();
    builder.Host.ConfigurationLog(logSeq);
    const int threads = 100;
    if (ThreadPool.SetMinThreads(threads, threads))
    {
        Log.Information(
            "set worker threads:{WorkerThreads},set completion  port threads {CompletionPortThreads}",
            threads,
            threads
        );
    }

    builder
        .WebHost.UseKestrel(
            (_, option) =>
            {
                option.AddServerHeader = false;
                option.ConfigureHttpsDefaults(x =>
                    x.SslProtocols = SslProtocols.Tls12 | SslProtocols.Tls13
                );
            }
        )
        .UseIIS()
        .UseIISIntegration();

    #region services

    var services = builder.Services;
    services.AddMvc(x =>
    {
        x.Filters.Add(typeof(GlobalFilter));
        x.Filters.Add(new TypeFilterAttribute(typeof(ExceptionHandleAttribute)));
    });
    //压缩
    services.AddResponseCompression(options =>
    {
        options.Providers.Add<BrotliCompressionProvider>();
        options.Providers.Add<GzipCompressionProvider>();
        options.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(
            ["application/font-woff2", "image/svg+xml", "text/plain", "application/lrc"]
        );
        options.EnableForHttps = true;
    });

    services.Configure<BrotliCompressionProviderOptions>(options =>
    {
        options.Level = CompressionLevel.Optimal;
    });

    services.Configure<GzipCompressionProviderOptions>(options =>
    {
        options.Level = CompressionLevel.Optimal;
    });

#if DEBUG
    services.Configure<KestrelServerOptions>(options =>
    {
        options.Limits.MaxRequestBodySize = int.MaxValue;
    });
#endif

    //依赖注入
    services.AddDefaultServices(configuration).AddInject();

    //缩小html
    services
        .AddWebMarkupMin(x =>
        {
            x.AllowMinificationInDevelopmentEnvironment = true;
            x.AllowCompressionInDevelopmentEnvironment = true;
        })
        .AddHtmlMinification(x =>
        {
            var settings = x.MinificationSettings;
            settings.RemoveOptionalEndTags = false;
        })
        .AddXmlMinification()
        .AddHttpCompression(option =>
        {
            option.CompressorFactories = new List<ICompressorFactory>
            {
                new BuiltInBrotliCompressorFactory(
                    new BuiltInBrotliCompressionSettings { Level = CompressionLevel.Optimal }
                ),
                new DeflateCompressorFactory(
                    new DeflateCompressionSettings { Level = CompressionLevel.Optimal }
                ),
                new GZipCompressorFactory(
                    new GZipCompressionSettings { Level = CompressionLevel.Optimal }
                ),
            };
        });

    // 响应式服务
    services.AddResponsive();
    // 设备识别
    services.AddDetection();
    // .AddDevice().AddBrowser()
    // .AddPlatform()
    // .AddEngine()
    // .AddCrawler();

    //注册 响应服务
    services.AddResponsive();

    //services.AddHostedService<TimedHostedService>();
    services.AddControllersWithViews();

    //注册 缓存中间件
    services.AddResponseCaching();

    var redisConnectionString =
        configuration.GetConnectionString("redis")
        ?? throw new Exception("configuration node redis is null or empty");
    //缓存
    services.AddRedisCacheOutput(redisConnectionString);
    services.AddBusinessServices(configuration);

    // Cors
    services.AddCors(options =>
    {
        options.AddPolicy(
            corsScheme,
            cfg =>
            {
                cfg.WithOrigins(configuration.GetSection("Origins").Get<string[]>() ?? [])
                    .WithMethods("GET", "PUT", "POST", "DELETE", "PATCH", "OPTION")
                    .AllowAnyHeader();
            }
        );
    });

    //注册 身份认证服务
    services.Configure<TokenManagement>(configuration.GetSection("TokenManagement"));
    services
        .AddAuthentication(x =>
        {
            x.DefaultAuthenticateScheme = AuthorizeCookieName;
            x.DefaultChallengeScheme = AuthorizeCookieName;
            x.DefaultSignInScheme = AuthorizeCookieName;
        })
        .AddCookie(
            AuthorizeCookieName,
            options =>
            {
                options.LoginPath = "/login.html";
                options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
            }
        )
        .AddJwtBearer(
            JwtBearerDefaults.AuthenticationScheme,
            x =>
            {
                var token = configuration.GetSection("TokenManagement").Get<TokenManagement>();
                if (token == null)
                {
                    return;
                }

                x.RequireHttpsMetadata = true;
                x.SaveToken = true;
                x.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey = new SymmetricSecurityKey(
                        Encoding.UTF8.GetBytes(token.Secret)
                    ),
                    ValidIssuer = token.Issuer,
                    ValidAudience = token.Audience,
                    ValidateIssuer = true,
                    ValidateAudience = true,
                };
            }
        );

    //services.AddAuthorization(x => )

    services
        .AddIdentityCore<VmUserInfo>(x =>
            x.ClaimsIdentity.SecurityStampClaimType = AuthorizeCookieName
        )
        .AddUserStore<UserStore>()
        .AddSignInManager<ApplicationSignInManager>();
    services.AddDataProtection().SetApplicationName(AuthorizeCookieName);
    services
        .AddOptions<KeyManagementOptions>()
        .Configure<IServiceScopeFactory>(
            (options, factory) =>
            {
                options.XmlRepository = new XmlRepositoryService(
                    factory,
                    AuthorizeCookieName + ".Key"
                );
            }
        );

    //register SignalR
    services
        .AddSignalR(x =>
        {
            x.HandshakeTimeout = TimeSpan.FromMinutes(2);
            x.MaximumReceiveMessageSize = 32768;
        })
        .AddStackExchangeRedis(
            redisConnectionString,
            options =>
            {
                options.Configuration.ChannelPrefix = RedisChannel.Literal("Dpz.Core.signalR");
            }
        );

    // 添加Hangfire 服务
    services.HangfireService(configuration);

    #endregion


    var app = builder.Build();

    #region configuration

    if (app.Environment.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
        app.UseRouteDebugger();
    }

    var supportedCultures = new[] { new CultureInfo("zh-CN") };
    app.UseRequestLocalization(
        new RequestLocalizationOptions
        {
            DefaultRequestCulture = new RequestCulture("zh-CN"),
            // Formatting numbers, dates, etc.
            SupportedCultures = supportedCultures,
            // UI strings that we have localized.
            SupportedUICultures = supportedCultures,
        }
    );

    app.UseSerilogRequestLogging(options =>
    {
        options.EnrichDiagnosticContext = (diagnosticContext, httpContext) =>
        {
            diagnosticContext.Set("RequestHost", httpContext.Request.Host.Value);
            diagnosticContext.Set("RequestScheme", httpContext.Request.Scheme);
            //diagnosticContext.Set("UserAgent", httpContext.Request.Headers.UserAgent.ToString());
            diagnosticContext.Set("IpAddress", httpContext.Request.GetIpAddress());

            var referer = httpContext.Request.Headers.Referer;
            if (!StringValues.IsNullOrEmpty(referer))
            {
                diagnosticContext.Set("Referer", referer);
            }
        };
    });

    //启用压缩
    app.UseResponseCompression();

    // app.UseHsts();
    // app.UseHttpsRedirection();

    //启用静态文件
    app.UseStaticFiles(
        new StaticFileOptions()
        {
            OnPrepareResponse = ctx =>
            {
                // 如果访问静态资源的图片,那么设置缓存
                if (
                    ctx.Context.Response.ContentType?.StartsWith(
                        "image/",
                        StringComparison.CurrentCultureIgnoreCase
                    ) == true
                )
                {
                    ctx.Context.Response.Headers.Append("Cache-Control", "public,max-age=2592000");
                    ctx.Context.Response.Headers.Append(
                        "Expires",
                        DateTime.UtcNow.AddDays(30).ToString("R", CultureInfo.InvariantCulture)
                    );
                }
            },
        }
    );

    app.UseWebMarkupMin();

    //启用缓存中间件
    app.UseResponseCaching();

    //启用响应服务
    app.UseResponsive();

    //拒绝爬虫
    app.UseRejectBots();

    app.UseCors(corsScheme);

    app.UseResponsive();
    app.UseDetection();

    app.UseRouting().UseMiddleware<HttpResponseHeaderHandel>().UseRecordRequest();

    //启用身份认证
    app.UseAuthentication();
    //启用身份授权
    app.UseAuthorization();

    // 路由
    app.ConfigurationRoute();

    //系统初始化
    app.Init();

    // 使用 hangfire UI,及周期性任务调度
    app.HangfireSetting();

    #endregion

    Log.Information("Starting web core host");

    app.Run();
}
catch (Exception ex)
{
    Console.Error.WriteLine(ex);
    Log.Fatal(ex, "Host terminated unexpectedly");
}

public partial class Program
{
    /// <summary>
    /// 身份认证Cookie名称
    /// </summary>
    public const string AuthorizeCookieName = "Dpz.Web.Core.Authoriza";

    /// <summary>
    /// 获取CDN基础地址
    /// </summary>
    public static string CdnBaseAddress { get; private set; } = "";

    /// <summary>
    /// 版本号
    /// </summary>
    public static string Version =>
        Assembly.GetEntryAssembly()?.GetName().Version?.ToString() ?? "_version";
}
⚠⚠    以下内容为AI分析的结果,请根据实际情况进行判断。

这段代码是一个 ASP.NET Core 应用程序的启动代码,主要用于配置和初始化 Web 应用程序的各个方面。以下是对代码功能的详细解释:

1. 日志记录

  • 使用 Serilog 进行日志记录,创建一个引导日志记录器。

2. 应用程序构建

  • 使用 WebApplication.CreateBuilder(args) 创建一个应用程序构建器。
  • 从配置中读取 CDN 基础地址和日志配置。

3. 线程池设置

  • 设置最小工作线程和完成端口线程数为 100。

4. Kestrel 服务器配置

  • 配置 Kestrel 服务器,禁用服务器头,并设置 HTTPS 默认协议为 TLS 1.2 和 TLS 1.3。

5. 服务注册

  • MVC: 添加 MVC 支持,并注册全局过滤器。
  • 响应压缩: 配置 Brotli 和 Gzip 压缩。
  • 依赖注入: 注册默认服务和自定义服务。
  • HTML 和 XML 最小化: 使用 WebMarkupMin 进行 HTML 和 XML 的最小化。
  • 响应式服务: 注册响应式服务和设备识别服务。
  • 缓存: 使用 Redis 进行缓存,并注册响应缓存中间件。
  • CORS: 配置跨源资源共享(CORS)策略。
  • 身份认证: 配置 Cookie 和 JWT 身份认证。
  • SignalR: 注册 SignalR 服务,并使用 Redis 作为后端。
  • Hangfire: 注册 Hangfire 服务用于后台任务调度。

6. 应用程序中间件配置

  • 开发环境: 在开发环境中使用开发者异常页面和路由调试。
  • 请求文化: 设置支持的文化(如中文)。
  • 请求日志记录: 使用 Serilog 记录请求信息。
  • 静态文件: 配置静态文件服务,并为图像设置缓存策略。
  • 响应压缩: 启用响应压缩中间件。
  • 响应服务: 启用响应式服务和设备检测。
  • 身份认证和授权: 启用身份认证和授权中间件。
  • 路由: 配置路由。
  • 系统初始化: 调用应用程序的初始化方法。
  • Hangfire 设置: 配置 Hangfire 的 UI 和任务调度。

7. 异常处理

  • 捕获启动过程中可能发生的异常,并记录致命错误。

8. 公共常量和属性

  • 定义了身份认证 Cookie 的名称、CDN 基础地址和应用程序版本号。

总结

这段代码是一个复杂的 ASP.NET Core 应用程序的启动配置,涵盖了从日志记录、服务注册、身份认证、CORS、SignalR、Hangfire 到中间件配置等多个方面,旨在构建一个高效、可扩展的 Web 应用程序。

loading