Example #1
0
        public static IOcelotPipelineBuilder BuildCustomOcelotPipeline(this IOcelotPipelineBuilder builder,
                                                                       OcelotPipelineConfiguration pipelineConfiguration)
        {
            builder.UseExceptionHandlerMiddleware();
            builder.MapWhen(context => context.HttpContext.WebSockets.IsWebSocketRequest,
                            app =>
            {
                app.UseDownstreamRouteFinderMiddleware();
                app.UseDownstreamRequestInitialiser();
                app.UseLoadBalancingMiddleware();
                app.UseDownstreamUrlCreatorMiddleware();
                app.UseWebSocketsProxyMiddleware();
            });
            builder.UseIfNotNull(pipelineConfiguration.PreErrorResponderMiddleware);
            builder.UseResponderMiddleware();
            builder.UseDownstreamRouteFinderMiddleware();
            builder.UseSecurityMiddleware();
            if (pipelineConfiguration.MapWhenOcelotPipeline != null)
            {
                foreach (var pipeline in pipelineConfiguration.MapWhenOcelotPipeline)
                {
                    builder.MapWhen(pipeline);
                }
            }
            builder.UseHttpHeadersTransformationMiddleware();
            builder.UseDownstreamRequestInitialiser();
            builder.UseRateLimiting();

            builder.UseRequestIdMiddleware();
            builder.UseIfNotNull(pipelineConfiguration.PreAuthenticationMiddleware);
            if (pipelineConfiguration.AuthenticationMiddleware == null)
            {
                builder.UseAuthenticationMiddleware();
            }
            else
            {
                builder.Use(pipelineConfiguration.AuthenticationMiddleware);
            }
            builder.UseClaimsToClaimsMiddleware();
            builder.UseIfNotNull(pipelineConfiguration.PreAuthorisationMiddleware);
            if (pipelineConfiguration.AuthorisationMiddleware == null)
            {
                builder.UseAuthorisationMiddleware();
            }
            else
            {
                builder.Use(pipelineConfiguration.AuthorisationMiddleware);
            }
            builder.UseClaimsToHeadersMiddleware();
            builder.UseIfNotNull(pipelineConfiguration.PreQueryStringBuilderMiddleware);
            builder.UseClaimsToQueryStringMiddleware();
            builder.UseLoadBalancingMiddleware();
            builder.UseDownstreamUrlCreatorMiddleware();
            builder.UseOutputCacheMiddleware();
            builder.UseHttpRequesterMiddleware();

            return(builder);
        }
Example #2
0
        public static OcelotRequestDelegate BuildOcelotPipeline(this IOcelotPipelineBuilder builder,
                                                                OcelotPipelineConfiguration pipelineConfiguration)
        {
            // This is registered to catch any global exceptions that are not handled
            // It also sets the Request Id if anything is set globally
            builder.UseExceptionHandlerMiddleware();

            // If the request is for websockets upgrade we fork into a different pipeline
            builder.MapWhen(context => context.HttpContext.WebSockets.IsWebSocketRequest,
                            app =>
            {
                app.UseDownstreamRouteFinderMiddleware();
                app.UseDownstreamRequestInitialiser();
                app.UseLoadBalancingMiddleware();
                app.UseDownstreamUrlCreatorMiddleware();
                app.UseWebSocketsProxyMiddleware();
            });

            // Allow the user to respond with absolutely anything they want.
            builder.UseIfNotNull(pipelineConfiguration.PreErrorResponderMiddleware);

            // This is registered first so it can catch any errors and issue an appropriate response
            builder.UseResponderMiddleware();

            // Then we get the downstream route information
            builder.UseDownstreamRouteFinderMiddleware();

            // This security module, IP whitelist blacklist, extended security mechanism
            builder.UseSecurityMiddleware();

            //Expand other branch pipes
            if (pipelineConfiguration.MapWhenOcelotPipeline != null)
            {
                foreach (var pipeline in pipelineConfiguration.MapWhenOcelotPipeline)
                {
                    builder.MapWhen(pipeline);
                }
            }

            // Now we have the ds route we can transform headers and stuff?
            builder.UseHttpHeadersTransformationMiddleware();

            // Initialises downstream request
            builder.UseDownstreamRequestInitialiser();

            // We check whether the request is ratelimit, and if there is no continue processing
            builder.UseRateLimiting();

            // This adds or updates the request id (initally we try and set this based on global config in the error handling middleware)
            // If anything was set at global level and we have a different setting at re route level the global stuff will be overwritten
            // This means you can get a scenario where you have a different request id from the first piece of middleware to the request id middleware.
            builder.UseRequestIdMiddleware();

            // Allow pre authentication logic. The idea being people might want to run something custom before what is built in.
            builder.UseIfNotNull(pipelineConfiguration.PreAuthenticationMiddleware);

            // Now we know where the client is going to go we can authenticate them.
            // We allow the ocelot middleware to be overriden by whatever the
            // user wants
            if (pipelineConfiguration.AuthenticationMiddleware == null)
            {
                builder.UseAuthenticationMiddleware();
            }
            else
            {
                builder.Use(pipelineConfiguration.AuthenticationMiddleware);
            }

            // The next thing we do is look at any claims transforms in case this is important for authorisation
            builder.UseClaimsToClaimsMiddleware();

            // Allow pre authorisation logic. The idea being people might want to run something custom before what is built in.
            builder.UseIfNotNull(pipelineConfiguration.PreAuthorisationMiddleware);

            // Now we have authenticated and done any claims transformation we
            // can authorise the request
            // We allow the ocelot middleware to be overriden by whatever the
            // user wants
            if (pipelineConfiguration.AuthorisationMiddleware == null)
            {
                builder.UseAuthorisationMiddleware();
            }
            else
            {
                builder.Use(pipelineConfiguration.AuthorisationMiddleware);
            }

            // Now we can run the claims to headers transformation middleware
            builder.UseClaimsToHeadersMiddleware();

            // Allow the user to implement their own query string manipulation logic
            builder.UseIfNotNull(pipelineConfiguration.PreQueryStringBuilderMiddleware);

            // Now we can run any claims to query string transformation middleware
            builder.UseClaimsToQueryStringMiddleware();

            // Get the load balancer for this request
            builder.UseLoadBalancingMiddleware();

            // This takes the downstream route we retrieved earlier and replaces any placeholders with the variables that should be used
            builder.UseDownstreamUrlCreatorMiddleware();

            // Not sure if this is the best place for this but we use the downstream url
            // as the basis for our cache key.
            builder.UseOutputCacheMiddleware();

            //We fire off the request and set the response on the scoped data repo
            builder.UseHttpRequesterMiddleware();

            return(builder.Build());
        }
    private static Predicate AddOrleansHttpGateway(this IOcelotPipelineBuilder builder, OcelotPipelineConfiguration pipelineConfiguration)
    {
        // Allow the user to respond with absolutely anything they want.
        builder.UseIfNotNull(pipelineConfiguration.PreErrorResponderMiddleware);

        // This is registered first so it can catch any errors and issue an appropriate response
        builder.UseResponderMiddleware();

        // Then we get the downstream route information
        builder.UseDownstreamRouteFinderMiddleware();

        // Now we have the ds route we can transform headers and stuff?
        builder.UseHttpHeadersTransformationMiddleware();

        // Initialises downstream request
        builder.UseDownstreamRequestInitialiser();

        // We check whether the request is ratelimit, and if there is no continue processing
        builder.UseRateLimiting();

        // This adds or updates the request id (initally we try and set this based on global config in the error handling middleware)
        // If anything was set at global level and we have a different setting at re route level the global stuff will be overwritten
        // This means you can get a scenario where you have a different request id from the first piece of middleware to the request id middleware.
        builder.UseRequestIdMiddleware();

        // Allow pre authentication logic. The idea being people might want to run something custom before what is built in.
        builder.UseIfNotNull(pipelineConfiguration.PreAuthenticationMiddleware);

        // Now we know where the client is going to go we can authenticate them.
        // We allow the ocelot middleware to be overriden by whatever the
        // user wants
        if (pipelineConfiguration.AuthenticationMiddleware == null)
        {
            builder.UseAuthenticationMiddleware();
        }
        else
        {
            builder.Use(pipelineConfiguration.AuthenticationMiddleware);
        }

        // The next thing we do is look at any claims transforms in case this is important for authorisation
        builder.UseClaimsBuilderMiddleware();

        // Allow pre authorisation logic. The idea being people might want to run something custom before what is built in.
        builder.UseIfNotNull(pipelineConfiguration.PreAuthorisationMiddleware);

        // Now we have authenticated and done any claims transformation we
        // can authorise the request
        // We allow the ocelot middleware to be overriden by whatever the
        // user wants
        if (pipelineConfiguration.AuthorisationMiddleware == null)
        {
            builder.UseAuthorisationMiddleware();
        }
        else
        {
            builder.Use(pipelineConfiguration.AuthorisationMiddleware);
        }

        // Now we can run any header transformation logic
        builder.UseHttpRequestHeadersBuilderMiddleware();

        // Allow the user to implement their own query string manipulation logic
        builder.UseIfNotNull(pipelineConfiguration.PreQueryStringBuilderMiddleware);

        // Now we can run any query string transformation logic
        builder.UseQueryStringBuilderMiddleware();

        // This takes the downstream route we retrieved earlier and replaces any placeholders with the variables that should be used
        builder.UseDownstreamUrlCreatorMiddleware();

        // Not sure if this is the best place for this but we use the downstream url
        // as the basis for our cache key.
        builder.UseOutputCacheMiddleware();

        //We fire off the request and set the response on the scoped data repo
        builder.UseOrleansRequesterMiddleware();


        return((context) =>
        {
            return context.DownstreamReRoute.DownstreamScheme.Equals("orleans", StringComparison.OrdinalIgnoreCase);
        });
    }
        public static OcelotRequestDelegate BuildCzarOcelotPipeline(this IOcelotPipelineBuilder builder,
                                                                    OcelotPipelineConfiguration pipelineConfiguration)
        {
            // 注册一个全局异常
            builder.UseExceptionHandlerMiddleware();

            // 如果请求是websocket使用单独的管道
            builder.MapWhen(context => context.HttpContext.WebSockets.IsWebSocketRequest,
                            app =>
            {
                app.UseDownstreamRouteFinderMiddleware();
                app.UseDownstreamRequestInitialiser();
                app.UseLoadBalancingMiddleware();
                app.UseDownstreamUrlCreatorMiddleware();
                app.UseWebSocketsProxyMiddleware();
            });

            // 添加自定义的错误管道
            builder.UseIfNotNull(pipelineConfiguration.PreErrorResponderMiddleware);

            //使用自定义的输出管道
            builder.UseCzarResponderMiddleware();

            // 下游路由匹配管道
            builder.UseDownstreamRouteFinderMiddleware();

            //增加自定义扩展管道
            if (pipelineConfiguration.MapWhenOcelotPipeline != null)
            {
                foreach (var pipeline in pipelineConfiguration.MapWhenOcelotPipeline)
                {
                    builder.MapWhen(pipeline);
                }
            }

            // 使用Http头部转换管道
            builder.UseHttpHeadersTransformationMiddleware();

            // 初始化下游请求管道
            builder.UseDownstreamRequestInitialiser();

            // 使用自定义限流管道
            builder.UseRateLimiting();

            //使用请求ID生成管道
            builder.UseRequestIdMiddleware();

            //使用自定义授权前管道
            builder.UseIfNotNull(pipelineConfiguration.PreAuthenticationMiddleware);

            //根据请求判断是否启用授权来使用管道
            if (pipelineConfiguration.AuthenticationMiddleware == null)
            {
                builder.UseAuthenticationMiddleware();
            }
            else
            {
                builder.Use(pipelineConfiguration.AuthenticationMiddleware);
            }

            //添加自定义限流中间件 2018-11-18 金焰的世界
            builder.UseCzarClientRateLimitMiddleware();

            //添加自定义授权中间件  2018-11-15 金焰的世界
            builder.UseAhphAuthenticationMiddleware();

            //启用自定义的认证之前中间件
            builder.UseIfNotNull(pipelineConfiguration.PreAuthorisationMiddleware);

            //是否使用自定义的认证中间件
            if (pipelineConfiguration.AuthorisationMiddleware == null)
            {
                builder.UseAuthorisationMiddleware();
            }
            else
            {
                builder.Use(pipelineConfiguration.AuthorisationMiddleware);
            }

            // 使用自定义的参数构建中间件
            builder.UseIfNotNull(pipelineConfiguration.PreQueryStringBuilderMiddleware);

            // 使用负载均衡中间件
            builder.UseLoadBalancingMiddleware();

            // 使用下游地址创建中间件
            builder.UseDownstreamUrlCreatorMiddleware();

            // 使用缓存中间件
            builder.UseOutputCacheMiddleware();

            //判断下游的是否启用rpc通信,切换到RPC处理
            builder.MapWhen(context => context.DownstreamReRoute.DownstreamScheme.Equals("rpc", StringComparison.OrdinalIgnoreCase), app =>
            {
                app.UseCzarRpcMiddleware();
            });

            //使用下游请求中间件
            builder.UseCzaHttpRequesterMiddleware();

            return(builder.Build());
        }