public static IServiceCollection AddCoreOcelot(this IServiceCollection services, Action <CoreOcelotConfiguration> coreOcelotConfig = null)
        {
            //System.Diagnostics.Debugger.Break();

            services.AddScoped <ILoadBalancerFactory, LoadBalancerFactory>();
            services.AddScoped <IIpHasher, IpHasher>();
            services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>();
            services.AddScoped <IIPRateLimiter, IPRateLimiter>();
            services.AddScoped <IIpAddressParser, RemoteIpParser>();



            var coreOcelotConfiguration = new CoreOcelotConfiguration();

            coreOcelotConfig.Invoke(coreOcelotConfiguration);

            services.AddSingleton <CoreOcelotConfiguration>(provider =>
            {
                return(coreOcelotConfiguration);
            });

            var _configuration  = services.BuildServiceProvider().GetService <IConfiguration>();
            var ybmLoadBalancer = _configuration.GetSection("LoadBalancer").Get <LoadBalance>();

            foreach (var lb in ybmLoadBalancer?.Servers)
            {
                var serverIdentity = lb.IP + lb.Port;
                services.AddHttpClient(serverIdentity, c =>
                {
                    c.BaseAddress = new Uri(serverIdentity);
                });
            }

            return(services);
        }
Exemplo n.º 2
0
        public async Task Invoke(HttpContext context)
        {
            //System.Diagnostics.Debugger.Break();

            if (context == null)
            {
                return;
            }

            CoreOcelotConfiguration coreOcelotConfiguration = (CoreOcelotConfiguration)context.RequestServices.GetService(typeof(CoreOcelotConfiguration));

            if (coreOcelotConfiguration != null && coreOcelotConfiguration.EnableAutorization)
            {
                coreOcelotConfiguration.CoreOcelotAuthorizer.Authorize(context);
            }

            await this.nextMiddleware.Invoke(context);
        }
Exemplo n.º 3
0
        public async Task Invoke(HttpContext context)
        {
            //System.Diagnostics.Debugger.Break();
            if (context == null)
            {
                return;
            }

            CoreOcelotConfiguration coreOcelotConfiguration = (CoreOcelotConfiguration)context.RequestServices.GetService(typeof(CoreOcelotConfiguration));
            IIPRateLimiter          ipRateLimiter           = (IIPRateLimiter)context.RequestServices.GetService(typeof(IIPRateLimiter));

            if (coreOcelotConfiguration != null && coreOcelotConfiguration.IPRateLimitingSetting.EnableEndpointRateLimiting == false)
            {
                await this.nextMiddleware.Invoke(context);

                return;
            }

            if (coreOcelotConfiguration.IPRateLimitingSetting == null)
            {
                throw new Exception("EnableEndpointRateLimiting is true but no configuration is provided");
            }

            var _options   = coreOcelotConfiguration.IPRateLimitingSetting;
            var isExceeded = ipRateLimiter.RateExceeded(context, _options, out int retryAfter);

            if (isExceeded == false)
            {
                await this.nextMiddleware.Invoke(context);

                return;
            }

            if (!_options.DisableRateLimitHeaders)
            {
                context.Response.Headers["Retry-After"] = $@"Retry after {retryAfter} second(s)";;
            }

            context.Response.StatusCode = coreOcelotConfiguration.IPRateLimitingSetting.StatusCode;
            await context.Response.WriteAsync(coreOcelotConfiguration.IPRateLimitingSetting.Message);

            return;
        }