src/Library/NetPro.Checker/CheckMiddlewareHandler.cs

(开头部分) 8KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using NetTools;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;

namespace NetPro.Checker
{
    public static class CheckMiddlewareHandler
    {
        private static readonly string DEFAULT_CONTENT_TYPE = "application/json";

        /// <summary>
        /// inclued: EnvCheck ;InfoCheck
        /// </summary>
        /// <param name="app"></param>
        /// <param name="envPath"></param>
        /// <param name="infoPath"></param>
        /// <param name="health"></param>
        public static void UseCheck(this IApplicationBuilder app, string envPath = "/env", string infoPath = "/info", string health = "/check")
        {
            app.UseEnvCheck(envPath);
            app.UseInfoCheck(infoPath);
            app.UseHealthCheck(health);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="app"></param>
        /// <param name="path"></param>
        public static void UseEnvCheck(this IApplicationBuilder app, string path = "/env")
        {
            var config = app.ApplicationServices.GetService<IConfiguration>();
            app.Map(path, s =>
            {
                s.Run(async context =>
                {
                    //ip可篡改,只作为基础限制,实际建议nginx处配置路由映射的方式限制
                    var remoteIp = context.Connection.RemoteIpAddress;
                    var rangeA = IPAddressRange.Parse("192.168.0.0 - 192.168.255.255");
                    var rangeB = IPAddressRange.Parse("172.16.0.0 - 172.31.255.255");
                    var rangeC = IPAddressRange.Parse("10.0.0.0 - 10.255.255.255");
                    if (IPAddress.IsLoopback(remoteIp) || rangeA.Contains(remoteIp) || rangeB.Contains(remoteIp) || rangeC.Contains(remoteIp))
                    {
                        var env = AppEnvironment.GetAppEnvironment();
                        context.Response.ContentType = DEFAULT_CONTENT_TYPE;
                        await context.Response.WriteAsync(Serialize(env));
                    }
                    else
                    {
                        context.Response.StatusCode = 403;
                        context.Response.ContentType = "application/html";
后面还有 155 行代码,解锁后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明

AI 解读 AI

登录后可用,每次 10 算力,解读结果公开显示在下面。

还没有人解读过这个文件。