public async Task ExecuteAsync(HttpContext context, HttpConnectionOptions options, ConnectionDelegate connectionDelegate)
        {
            // Create the log scope and attempt to pass the Connection ID to it so as many logs as possible contain
            // the Connection ID metadata. If this is the negotiate request then the Connection ID for the scope will
            // be set a little later.
            var logScope = new ConnectionLogScope(GetConnectionId(context));

            using (_logger.BeginScope(logScope))
            {
                if (!await AuthorizeHelper.AuthorizeAsync(context, options.AuthorizationData))
                {
                    return;
                }

                if (HttpMethods.IsPost(context.Request.Method))
                {
                    // POST /{path}
                    await ProcessSend(context, options);
                }
                else if (HttpMethods.IsGet(context.Request.Method))
                {
                    // GET /{path}
                    await ExecuteAsync(context, connectionDelegate, options, logScope);
                }
                else
                {
                    context.Response.ContentType = "text/plain";
                    context.Response.StatusCode  = StatusCodes.Status405MethodNotAllowed;
                }
            }
        }
        public async Task ExecuteNegotiateAsync(HttpContext context, HttpSocketOptions options)
        {
            // Create the log scope and the scope connectionId param will be set when the connection is created.
            var logScope = new ConnectionLogScope(connectionId: string.Empty);

            using (_logger.BeginScope(logScope))
            {
                if (!await AuthorizeHelper.AuthorizeAsync(context, options.AuthorizationData))
                {
                    return;
                }

                if (HttpMethods.IsPost(context.Request.Method))
                {
                    // POST /{path}/negotiate
                    await ProcessNegotiate(context, options, logScope);
                }
                else
                {
                    context.Response.StatusCode = StatusCodes.Status405MethodNotAllowed;
                }
            }
        }
Exemple #3
0
        public async Task InvokeAsync(HttpContext httpContext)
        {
            var opts = httpContext.RequestServices.GetService <LogDashboardOptions>();

            var requestUrl = httpContext.Request.Path.Value;

            //EmbeddedFile
            if (requestUrl.Contains("css") || requestUrl.Contains("js"))
            {
                await httpContext.Response.WriteAsync(LogDashboardEmbeddedFiles.IncludeEmbeddedFile(httpContext, requestUrl));

                return;
            }

            // Find Router
            var router = LogDashboardRoutes.Routes.FindRoute(requestUrl);

            if (router == null)
            {
                httpContext.Response.StatusCode = 404;
                return;
            }


            //Authorization
            if (!await AuthorizeHelper.AuthorizeAsync(httpContext, opts.AuthorizeData))
            {
                return;
            }

            var logDashboardContext = new LogDashboardContext(httpContext, router,
                                                              httpContext.RequestServices.GetService <IRazorLightEngine>(),
                                                              opts);

            if (!AuthorizationFilterHelper.Authorization(opts.AuthorizationFiles, logDashboardContext))
            {
                httpContext.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
                return;
            }


            //Activate Handle
            var handleType = Assembly.GetAssembly(typeof(LogDashboardRoute))
                             .GetTypes().FirstOrDefault(x => x.Name.Contains(router.Handle + "Handle"));

            var handle = httpContext.RequestServices.GetRequiredService(handleType.MakeGenericType(opts.LogModelType)) as ILogDashboardHandle;

            if (handle == null)
            {
                httpContext.Response.StatusCode = 404;
                return;
            }

            using (var uow = httpContext.RequestServices.GetService <IUnitOfWork>())
            {
                handle.Context = logDashboardContext;

                string html;

                var method = handle.GetType().GetMethod(router.Action);
                // ReSharper disable once PossibleNullReferenceException
                var parametersLength = method.GetParameters().Length;

                if (parametersLength == 0)
                {
                    html = await(Task <string>) method.Invoke(handle, null);
                }
                else
                {
                    if (httpContext.Request.ContentLength == null && httpContext.Request.Query.Count <= 0)
                    {
                        html = await(Task <string>) method.Invoke(handle, new Object[] { null });
                    }
                    else
                    {
                        object args;
                        if (httpContext.Request.Query.Count > 0)
                        {
                            var dict = new Dictionary <string, string>();
                            httpContext.Request.Query.ToList().ForEach(x => dict.Add(x.Key, x.Value));
                            args = JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dict), method.GetParameters().First().ParameterType);
                        }
                        else
                        {
                            // ReSharper disable once PossibleInvalidOperationException
                            var bytes = new byte[(int)httpContext.Request.ContentLength];
                            await httpContext.Request.Body.ReadAsync(bytes, 0, (int)httpContext.Request.ContentLength);

                            string requestJson = Encoding.Default.GetString(bytes);

                            args = JsonConvert.DeserializeObject(requestJson, method.GetParameters().First().ParameterType);
                        }

                        html = await(Task <string>) method.Invoke(handle, new[] { args });
                    }
                }

                await httpContext.Response.WriteAsync(html);
            }
        }
        public async Task InvokeAsync(HttpContext httpContext)
        {
            using var scope = httpContext.RequestServices.CreateScope();
            var options = scope.ServiceProvider.GetService <DashboardOptions>();

            var requestUrl = httpContext.Request.Path.Value;

            //EmbeddedFile
            if (requestUrl.Contains("."))
            {
                await DashboardEmbeddedFiles.IncludeEmbeddedFile(httpContext, requestUrl);

                return;
            }


            // Find Router
            var router = DashboardRoutes.Routes.FindRoute(requestUrl);

            if (router == null)
            {
                httpContext.Response.StatusCode = 404;
                return;
            }

            var DashboardContext = new DashboardContext(httpContext, router, options);

            //Activate Handle

            var handles = Assembly.GetAssembly(typeof(DashboardRoute)).GetTypes();

            var handleType = handles.FirstOrDefault(x => x.Name.Contains(router.Handle.Replace("HttpReports", "Dashboard") + "Handle"));

            var handle = scope.ServiceProvider.GetRequiredService(handleType) as IDashboardHandle;

            if (handle == null)
            {
                httpContext.Response.StatusCode = 404;
                return;
            }


            //Authorization
            await AuthorizeHelper.AuthorizeAsync(httpContext, handle, router);

            handle.Context = DashboardContext;

            string html;

            var method           = handle.GetType().GetMethod(router.Action);
            var parametersLength = method.GetParameters().Length;

            if (parametersLength == 0)
            {
                html = await(Task <string>) method.Invoke(handle, null);
            }
            else
            {
                if (httpContext.Request.ContentLength == null && httpContext.Request.Query.Count <= 0)
                {
                    html = await(Task <string>) method.Invoke(handle, new Object[] { null });
                }
                else
                {
                    object args;
                    if (httpContext.Request.Query.Count == 1)
                    {
                        var paraType = method.GetParameters().First().ParameterType;

                        if (paraType.Name.ToLowerInvariant().Contains("string"))
                        {
                            args = httpContext.Request.Query.FirstOrDefault().Value.ToString();
                        }
                        else
                        {
                            var dict = new Dictionary <string, string>();
                            httpContext.Request.Query.ToList().ForEach(x => dict.Add(x.Key, x.Value));
                            args = JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dict));
                        }
                    }
                    else if (httpContext.Request.Query.Count > 1)
                    {
                        var dict = new Dictionary <string, string>();
                        httpContext.Request.Query.ToList().ForEach(x => dict.Add(x.Key, x.Value));
                        args = JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dict), method.GetParameters().First().ParameterType);
                    }
                    else
                    {
                        string requestJson = await GetRequestBodyAsync(httpContext);

                        var paraType = method.GetParameters().First().ParameterType;

                        args = JsonConvert.DeserializeObject(requestJson, paraType);
                    }

                    html = await(Task <string>) method.Invoke(handle, new[] { args });
                }
            }

            await httpContext.Response.WriteAsync(html);
        }
        public async Task InvokeAsync(HttpContext httpContext)
        {
            var opts = httpContext.RequestServices.GetService <LogDashboardOptions>();

            var requestUrl = httpContext.Request.Path.Value;
            var configs    = httpContext.RequestServices.GetService <IConfiguration>();
            var log        = configs["LogDashboard"];

            if (log != "open")
            {
                var Engine = httpContext.RequestServices.GetService <IRazorLightEngine>();
                var Route  = LogDashboardRoutes.Routes.FindRoute("/Dashboard/Tip");
                var vhtml  = await Engine.CompileRenderAsync(Route.View, null, null);

                await httpContext.Response.WriteAsync(vhtml);

                return;
                //httpContext.Response.ContentType = "text/html; charset=utf-8";
                //await httpContext.Response.WriteAsync("<div style='margin:10px auto;padding:20px; width:600px; height:300px; border:1px solid #ccc;background:yellow;line-height:50px;size:font-size:40px;'>");
                //await httpContext.Response.WriteAsync("日志面板被锁定<br>请联系华瑞技术中心: 0571-83786666 <br>提示:在配置中心设置 LogDashboard=open </div>");
            }

            //EmbeddedFile
            if (requestUrl.Contains("css") || requestUrl.Contains("js"))
            {
                await httpContext.Response.WriteAsync(LogDashboardEmbeddedFiles.IncludeEmbeddedFile(httpContext, requestUrl));

                return;
            }

            // Find Router
            var router = LogDashboardRoutes.Routes.FindRoute(requestUrl);

            if (router == null)
            {
                httpContext.Response.StatusCode = 404;
                return;
            }


            //Authorization
            if (!await AuthorizeHelper.AuthorizeAsync(httpContext, opts.AuthorizeData))
            {
                return;
            }

            var logDashboardContext = new LogDashboardContext(httpContext, router,
                                                              httpContext.RequestServices.GetService <IRazorLightEngine>(),
                                                              opts);

            if (!AuthorizationFilterHelper.Authorization(opts.AuthorizationFiles, logDashboardContext))
            {
                httpContext.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
                return;
            }


            //Activate Handle
            var handleType = Assembly.GetAssembly(typeof(LogDashboardRoute))
                             .GetTypes().FirstOrDefault(x => x.Name.Contains(router.Handle + "Handle"));

            var handle = httpContext.RequestServices.GetRequiredService(handleType.MakeGenericType(opts.LogModelType)) as ILogDashboardHandle;

            if (handle == null)
            {
                httpContext.Response.StatusCode = 404;
                return;
            }

            using (var uow = httpContext.RequestServices.GetService <IUnitOfWork>())
            {
                handle.Context = logDashboardContext;

                string html;

                var method = handle.GetType().GetMethod(router.Action);
                // ReSharper disable once PossibleNullReferenceException
                var parametersLength = method.GetParameters().Length;

                if (parametersLength == 0)
                {
                    html = await(Task <string>) method.Invoke(handle, null);
                }
                else
                {
                    if (httpContext.Request.ContentLength == null && httpContext.Request.Query.Count <= 0)
                    {
                        html = await(Task <string>) method.Invoke(handle, new Object[] { null });
                    }
                    else
                    {
                        object args;
                        if (httpContext.Request.Query.Count > 0)
                        {
                            var dict = new Dictionary <string, string>();
                            httpContext.Request.Query.ToList().ForEach(x => dict.Add(x.Key, x.Value));
                            args = JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dict), method.GetParameters().First().ParameterType);
                        }
                        else
                        {
                            // ReSharper disable once PossibleInvalidOperationException
                            var bytes = new byte[(int)httpContext.Request.ContentLength];
                            await httpContext.Request.Body.ReadAsync(bytes, 0, (int)httpContext.Request.ContentLength);

                            string requestJson = Encoding.Default.GetString(bytes);

                            args = JsonConvert.DeserializeObject(requestJson, method.GetParameters().First().ParameterType);
                        }

                        html = await(Task <string>) method.Invoke(handle, new[] { args });
                    }
                }

                await httpContext.Response.WriteAsync(html);
            }
        }