示例#1
0
        static IpRateLimit()
        {
            if (Setting.Configuration?.IpRateLimiting == null)
            {
                return;
            }

            Logger = Factory.Logger.Value;

            Options = Setting.Configuration.IpRateLimiting;

            Policies = Setting.Configuration.IpRateLimitPolicies;

            IpParser = new ReversProxyIpParser(Options.RealIpHeader);

            MemoryCache = Factory.MemoryCache.Value;

            var rateLimitCounterStore = Factory.RateLimitCounterStore.Value;

            var ipPolicyStore = new MemoryCacheIpPolicyStore(MemoryCache, Options, Policies);

            Processor = new IpRateLimitProcessor(Options, rateLimitCounterStore, ipPolicyStore, IpParser);

            Configurationed = true;
        }
示例#2
0
 public IpRateLimitProcessor(
     IpRateLimitOptions options,
     ICacheManager cacheManager,
     IRateLimitConfiguration config)
     : base(cacheManager, options, new IpCounterKeyBuilder(), config)
 {
     _options = options;
 }
示例#3
0
 public MemoryCacheIpPolicyStore(
     IMemoryCache cache,
     IpRateLimitOptions options   = null,
     IpRateLimitPolicies policies = null) : base(cache)
 {
     _options  = options;
     _policies = policies;
 }
        public RateLimitConfiguration(
            IpRateLimitOptions ipOptions,
            ClientRateLimitOptions clientOptions)
        {
            IpRateLimitOptions     = ipOptions;
            ClientRateLimitOptions = clientOptions;

            RegisterResolvers();
        }
示例#5
0
 public RedisIpPolicyStore(
     ICache cache,
     IOptions <MineIpRateLimitOptions> options = null,
     IOptions <IpRateLimitPolicies> policies   = null) : base(cache)
 {
     _options  = options?.Value;
     _policies = policies?.Value;
     _cache    = cache;
 }
示例#6
0
 public IpPolicyStore(
     ICacheManager cacheManager,
     IOptions <IpRateLimitOptions> options   = null,
     IOptions <IpRateLimitPolicies> policies = null)
 {
     _cacheManager = cacheManager;
     _options      = options?.Value;
     _policies     = policies?.Value;
 }
 public IpRateLimitProcessor(
     IpRateLimitOptions options,
     IRateLimitCounterStore counterStore,
     IIpPolicyStore policyStore,
     IRateLimitConfiguration config)
     : base(options, counterStore, new IpCounterKeyBuilder(options), config)
 {
     _options     = options;
     _policyStore = policyStore;
 }
示例#8
0
 public CustomRateLimitConfiguration(IHttpContextAccessor httpContextAccessor, IClientPolicyStore clientPolicyStore, IOptions <IpRateLimitOptions> ipOptions, IOptions <ClientRateLimitOptions> clientOptions, IOptions <ClientRateLimitPolicies> clientPolicies)
 {
     IpRateLimitOptions      = ipOptions?.Value;
     ClientRateLimitOptions  = clientOptions?.Value;
     ClientRateLimitPolicies = clientPolicies?.Value;
     HttpContextAccessor     = httpContextAccessor;
     ClientPolicyStore       = clientPolicyStore;
     ClientResolvers         = new List <IClientResolveContributor>();
     IpResolvers             = new List <IIpResolveContributor>();
     RegisterResolvers();
 }
 public CustomRateLimitingMiddleware(RequestDelegate next,
                                     IProcessingStrategy processingStrategy,
                                     IOptions <IpRateLimitOptions> options,
                                     IRateLimitCounterStore counterStore,
                                     IIpPolicyStore policyStore,
                                     IRateLimitConfiguration config,
                                     ILogger <IpRateLimitMiddleware> logger)
     : base(next, processingStrategy, options, counterStore, policyStore, config, logger)
 {
     _rateLimitOptions = options.Value;
 }
示例#10
0
 public CustomIpRateLimitMiddleware(
     RequestDelegate next,
     IOptions <IpRateLimitOptions> options,
     IRateLimitCounterStore counterStore,
     IIpPolicyStore policyStore,
     ILogger <IpRateLimitMiddleware> logger,
     IIpAddressParser ipParser = null
     ) : base(next, options, counterStore, policyStore, logger, ipParser)
 {
     _options = options.Value;
 }
        public IpRateLimitProcessor(IpRateLimitOptions options,
                                    IRateLimitCounterStore counterStore,
                                    IIpPolicyStore policyStore,
                                    IIpAddressParser ipParser)
        {
            _options      = options;
            _counterStore = counterStore;
            _policyStore  = policyStore;
            _ipParser     = ipParser;

            _core = new RateLimitCore(true, options, _counterStore);
        }
示例#12
0
        public MemoryCacheIpPolicyStore(IMemoryCache memoryCache,
                                        IpRateLimitOptions options   = null,
                                        IpRateLimitPolicies policies = null)
        {
            _memoryCache = memoryCache;

            //save ip rules defined in appsettings in cache on startup
            if (options != null && policies != null && policies.IpRules != null)
            {
                Set($"{options.IpPolicyPrefix}", policies);
            }
        }
 public MyIPRateMiddleware(
     RequestDelegate next,
     IOptions <IpRateLimitOptions> options,
     IRateLimitCounterStore counterStore,
     IIpPolicyStore policyStore,
     ILogger <IpRateLimitMiddleware> logger,
     IIpAddressParser ipParser = null)
 {
     this._next      = next;
     this._options   = options.Value;
     this._logger    = logger;
     this._ipParser  = ipParser != null ? ipParser : (IIpAddressParser) new ReversProxyIpParser(this._options.RealIpHeader);
     this._processor = new IpRateLimitProcessor(this._options, counterStore, policyStore, this._ipParser);
 }
        /// <summary>
        /// Get Client Ip using HttpContextAccessor
        /// </summary>
        /// <param name="settings"></param>
        /// <param name="httpContext"></param>
        /// <returns></returns>
        public static string GetClientIP(IpRateLimitOptions settings, IHttpContextAccessor httpContext)
        {
            string ip = string.Empty;

            if (string.IsNullOrWhiteSpace(settings.RealIpHeader))
            {
                ip = httpContext.HttpContext.Request.HttpContext.Connection.RemoteIpAddress.ToString();
            }
            else
            {
                ip = httpContext.HttpContext.Request.Headers.FirstOrDefault(x => x.Key == settings.RealIpHeader).Value;
            }
            return(ip);
        }
 public CustomIpRateLimitMiddleware(
     IMemoryCache memoryCache,
     IBlockIpService blockIpService,
     RequestDelegate next,
     IOptions<IpRateLimitOptions> options,
     IRateLimitCounterStore counterStore,
     IIpPolicyStore policyStore,
     ILogger<IpRateLimitMiddleware> logger,
     IIpAddressParser ipParser = null)
     : base(next, options, counterStore, policyStore, logger, ipParser)
 {
     _memoryCache = memoryCache;
     _blockIpService = blockIpService;
     _options = options.Value;
     _logger = logger;
 }
示例#16
0
 public CustomIpRateLimitMiddleware(
     IDistributedCache distributedCache,
     IBlockIpService blockIpService,
     RequestDelegate next,
     IProcessingStrategy processingStrategy,
     IRateLimitConfiguration rateLimitConfiguration,
     IOptions <IpRateLimitOptions> options,
     IIpPolicyStore policyStore,
     ILogger <CustomIpRateLimitMiddleware> logger)
     : base(next, processingStrategy, options, policyStore, rateLimitConfiguration, logger)
 {
     _distributedCache = distributedCache;
     _blockIpService   = blockIpService;
     _options          = options.Value;
     _logger           = logger;
 }
 public CustomIpRateLimitMiddleware(
     IMemoryCache memoryCache,
     IBlockIpService blockIpService,
     RequestDelegate next,
     IOptions <IpRateLimitOptions> options,
     IRateLimitCounterStore counterStore,
     IIpPolicyStore policyStore,
     ILogger <IpRateLimitMiddleware> logger,
     IIpAddressParser ipParser = null)
     : base(next, options, counterStore, policyStore, logger, ipParser)
 {
     _memoryCache    = memoryCache;
     _blockIpService = blockIpService;
     _options        = options.Value;
     _logger         = logger;
 }
示例#18
0
        private static void ConfigureIpRateLimitOptions(IpRateLimitOptions options)
        {
            options.EnableEndpointRateLimiting = false;

            // exclude admin api and metrics from throtteling
            options.EndpointWhitelist = new List <string>
            {
                "*:/api/admin",
                "get:/metrics",
                "*:/notifications",
            };

            options.IpWhitelist = clusterConfig.Api?.RateLimiting?.IpWhitelist?.ToList();

            // default to whitelist localhost if whitelist absent
            if (options.IpWhitelist == null || options.IpWhitelist.Count == 0)
            {
                options.IpWhitelist = new List <string>
                {
                    IPAddress.Loopback.ToString(),
                        IPAddress.IPv6Loopback.ToString(),
                        IPUtils.IPv4LoopBackOnIPv6.ToString()
                };
            }

            // limits
            var rules = clusterConfig.Api?.RateLimiting?.Rules?.ToList();

            if (rules == null || rules.Count == 0)
            {
                rules = new List <RateLimitRule>
                {
                    new RateLimitRule
                    {
                        Endpoint = "*",
                        Period   = "1s",
                        Limit    = 5,
                    }
                };
            }

            options.GeneralRules = rules;

            logger.Info(() => $"API access limited to {(string.Join(", ", rules.Select(x => $"{x.Limit} requests per {x.Period}")))}, except from {string.Join(", ", options.IpWhitelist)}");
        }
示例#19
0
 private static void SetupIpRateLimitOptions(IpRateLimitOptions options)
 {
     options.GeneralRules = new List <RateLimitRule>
     {
         new RateLimitRule
         {
             Endpoint = "*",
             Limit    = 1000,
             Period   = "5m"
         },
         new RateLimitRule
         {
             Endpoint = "*",
             Limit    = 200,
             Period   = "10s"
         }
     };
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="ThrottleMiddleware"/> class.
        /// </summary>
        /// <param name="next">The next.</param>
        /// <param name="options">The options.</param>
        /// <param name="counterStore">The counter store.</param>
        /// <param name="policyStore">The policy store.</param>
        /// <param name="config">The rate limit configuration.</param>
        /// <param name="logger">The logger.</param>
        /// <param name="configuration">The API configuration.</param>
        /// <param name="serializer">The JSON serializer.</param>
        /// <exception cref="ArgumentNullException">configuration</exception>
        public ThrottleMiddleware(
            RequestDelegate next,
            IOptions <IpRateLimitOptions> options,
            IRateLimitCounterStore counterStore,
            IIpPolicyStore policyStore,
            IRateLimitConfiguration config,
            ILogger <IpRateLimitMiddleware> logger,
            IApiConfiguration configuration,
            IDefaultJsonSerializer serializer)
            : base(next, options, counterStore, policyStore, config, logger)
        {
            Ensure.ArgumentNotNull(options, nameof(options));

            if (options.Value == null)
            {
                throw new UnexpectedNullException("The options value is null.");
            }

            this.options       = options.Value;
            this.configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
            this.serializer    = serializer ?? throw new ArgumentNullException(nameof(serializer));
        }
 public IpRateLimitController(IOptions <IpRateLimitOptions> optionsAccessor, IIpPolicyStore ipPolicyStore)
 {
     _options       = optionsAccessor.Value;
     _ipPolicyStore = ipPolicyStore;
 }
 public IpCounterKeyBuilder(IpRateLimitOptions options)
 {
     _options = options;
 }
示例#23
0
 public ParametersManagerController(ParametersManager parametersManager, IOptions <IpRateLimitOptions> rateLimitOptions, IIpPolicyStore ipPolicyStore)
 {
     _parametersManager = parametersManager;
     _rateLimitOptions  = rateLimitOptions.Value;
     _ipPolicyStore     = ipPolicyStore;
 }
 public IpRateLimitHttpService(IIpRateLimitStorageProvider provider, IHttpContextAccessor httpContext, IOptions <IpRateLimitOptions> settings)
 {
     _provider    = provider;
     _httpContext = httpContext;
     _settings    = settings.Value;
 }
示例#25
0
 public MemoryCacheProvider(IMemoryCache cache, IOptions <IpRateLimitOptions> settings)
 {
     _cache        = cache;
     this.settings = settings.Value;
 }