Beispiel #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ThrottlingMiddleware"/> class.
        /// Persists the policy object in cache using <see cref="IPolicyRepository"/> implementation.
        /// The policy object can be updated by <see cref="ThrottleManager"/> at runtime.
        /// </summary>
        /// <param name="policy">
        /// The policy.
        /// </param>
        /// <param name="policyRepository">
        /// The policy repository.
        /// </param>
        /// <param name="repository">
        /// The repository.
        /// </param>
        /// <param name="logger">
        /// The logger.
        /// </param>
        /// <param name="ipAddressParser">
        /// The IpAddressParser
        /// </param>
        public ThrottlingMiddleware(OwinMiddleware next,
                                    ThrottlePolicy policy,
                                    IPolicyRepository policyRepository,
                                    IThrottleRepository repository,
                                    IThrottleLogger logger,
                                    IIpAddressParser ipAddressParser)
            : base(next)
        {
            core            = new ThrottlingCore();
            core.Repository = repository;
            Repository      = repository;
            Logger          = logger;

            if (ipAddressParser != null)
            {
                core.IpAddressParser = ipAddressParser;
            }

            QuotaExceededResponseCode = (HttpStatusCode)429;

            this.policy           = policy;
            this.policyRepository = policyRepository;

            if (policyRepository != null)
            {
                policyRepository.Save(ThrottleManager.GetPolicyKey(), policy);
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="ThrottlingHandler"/> class.
        /// Persists the policy object in cache using <see cref="IPolicyRepository"/> implementation.
        /// The policy object can be updated by <see cref="ThrottleManager"/> at runtime.
        /// </summary>
        /// <param name="policy">
        /// The policy.
        /// </param>
        /// <param name="policyRepository">
        /// The policy repository.
        /// </param>
        /// <param name="repository">
        /// The repository.
        /// </param>
        /// <param name="logger">
        /// The logger.
        /// </param>
        /// <param name="ipAddressParser">
        /// The IpAddressParser
        /// </param>
        public ThrottlingHandler(ThrottlePolicy policy,
                                 IPolicyRepository policyRepository,
                                 IThrottleRepository repository,
                                 IThrottleLogger logger,
                                 IIpAddressParser ipAddressParser = null)
        {
            core = new ThrottlingCore
            {
                Repository = repository
            };
            Repository = repository;
            Logger     = logger;

            if (ipAddressParser != null)
            {
                core.IpAddressParser = ipAddressParser;
            }

            QuotaExceededResponseCode = (HttpStatusCode)429;

            this.Policy           = policy;
            this.PolicyRepository = policyRepository;

            policyRepository?.Save(ThrottleManager.GetPolicyKey(), policy);
        }
Beispiel #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ThrottleProcesser"/> class.
        /// By default, the <see cref="QuotaExceededResponseCode"/> property
        /// is set to 429 (Too Many Requests).
        /// </summary>
        public ThrottleProcesser(
            ThrottlePolicy policy,
            IIpAddressParser ipAddressParser,
            IPolicyRepository policyRepo     = null,
            IThrottleRepository throttleRepo = null)
        {
            Logger = new DefaultThrottleLogger();
            QuotaExceededResponseCode = (HttpStatusCode)429;
            processResult             = new ThrottleProcessResult {
                IsPass = true
            };

            ThrottleRepo = throttleRepo;
            if (ThrottleRepo == null)
            {
                ThrottleRepo = new WebCacheThrottleRepository();
            }
            ThrottlingCore = new ThrottlingCore(ipAddressParser);
            ThrottlingCore.ThrottleRepo = ThrottleRepo;

            PolicyRepo = policyRepo;
            if (PolicyRepo == null)
            {
                PolicyRepo = new WebCachePolicyRepository();
            }
            Policy = policy;
            PolicyRepo.Save(ThrottleManager.GetPolicyKey(), policy);
        }
Beispiel #4
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;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="ThrottlingMiddleware"/> class.
        /// Persists the policy object in cache using <see cref="IPolicyRepository"/> implementation.
        /// The policy object can be updated by <see cref="ThrottleManager"/> at runtime. 
        /// </summary>
        /// <param name="policy">
        /// The policy.
        /// </param>
        /// <param name="policyRepository">
        /// The policy repository.
        /// </param>
        /// <param name="repository">
        /// The repository.
        /// </param>
        /// <param name="logger">
        /// The logger.
        /// </param>
        /// <param name="ipAddressParser">
        /// The IpAddressParser
        /// </param>
        public ThrottlingMiddleware(OwinMiddleware next, 
            ThrottlePolicy policy, 
            IPolicyRepository policyRepository, 
            IThrottleRepository repository, 
            IThrottleLogger logger,
            IIpAddressParser ipAddressParser)
            : base(next)
        {
            core = new ThrottlingCore();
            core.Repository = repository;
            Repository = repository;
            Logger = logger;

            if (ipAddressParser != null)
            {
                core.IpAddressParser = ipAddressParser;
            }

            QuotaExceededResponseCode = (HttpStatusCode)429;

            this.policy = policy;
            this.policyRepository = policyRepository;

            if (policyRepository != null)
            {
                policyRepository.Save(ThrottleManager.GetPolicyKey(), policy);
            }
        }
Beispiel #6
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;
 }
Beispiel #7
0
        public IpRateLimitProcessor(IpRateLimitOptions options,
                                    IRateLimitCounterStore counterStore,
                                    IIpPolicyStore policyStore,
                                    IIpAddressParser ipParser)
        {
            _options      = options;
            _counterStore = counterStore;
            _policyStore  = policyStore;
            _ipParser     = ipParser;

            _core = new RateLimitCore(true, options, _counterStore);
        }
        public IpRateLimitMiddleware(RequestDelegate next,
                                     IOptions <IpRateLimitOptions> options,
                                     IRateLimitCounterStore counterStore,
                                     IIpPolicyStore policyStore,
                                     ILogger <IpRateLimitMiddleware> logger,
                                     IIpAddressParser ipParser = null)
        {
            _next     = next;
            _options  = options.Value;
            _logger   = logger;
            _ipParser = ipParser ?? new ReversProxyIpParser(_options.RealIpHeader);

            _processor = new IpRateLimitProcessor(_options, counterStore, policyStore, _ipParser);
        }
 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);
 }
Beispiel #10
0
        public IpRateLimitMiddleware(RequestDelegate next,
                                     IOptions <IpRateLimitOptions> options,
                                     IRateLimitCounterStore counterStore,
                                     IIpPolicyStore policyStore,
                                     IRateLimitConfiguration config,
                                     ILogger <IpRateLimitMiddleware> logger)
            : base(next, options?.Value, new IpRateLimitProcessor(options?.Value, counterStore, policyStore, config), config)

        {
            _logger        = logger;
            _ipParser      = ipParser != null ? ipParser : new ReversProxyIpParser(_options.RealIpHeader);
            _ipPolicyStore = policyStore;

            _processor = new IpRateLimitProcessor(_options, counterStore, policyStore, _ipParser);
        }
 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;
 }
 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;
 }
        public CustomIpRateLimitMiddleware(RequestDelegate next, IOptions <IpRateLimitOptions> options, IRateLimitCounterStore counterStore, IIpPolicyStore policyStore, ILogger <IpRateLimitMiddleware> logger, IIpAddressParser ipParser = null)
            : base(next, options, counterStore, policyStore, logger, ipParser)
        {
            if (String.IsNullOrWhiteSpace(options.Value.QuotaExceededMessage))
            {
                throw new InvalidOperationException("API quota exceeded message not set in the config.");
            }

            if (options.Value.HttpStatusCode == default(int))
            {
                throw new InvalidOperationException("API quota exceeded status code message not set in the config.");
            }

            if (String.IsNullOrWhiteSpace(options.Value.RealIpHeader))
            {
                throw new InvalidOperationException("API real IP header value not set in the config.");
            }

            quotaExceededMessage = options.Value.QuotaExceededMessage;
            httpStatusCode       = options.Value.HttpStatusCode;
            ipAddressParser      = ipParser != null ? ipParser : new ReversProxyIpParser(options.Value.RealIpHeader);
        }
        public HttpModuleThrottlingHandler(ThrottlePolicy policy,
                                           IPolicyRepository policyRepository,
                                           IThrottleRepository repository,
                                           IIpAddressParser ipAddressParser = null)
        {
            core            = new ThrottlingCore();
            core.Repository = repository;
            Repository      = repository;

            if (ipAddressParser != null)
            {
                core.IpAddressParser = ipAddressParser;
            }

            QuotaExceededResponseCode = (HttpStatusCode)429;

            this.policy           = policy;
            this.policyRepository = policyRepository;

            if (policyRepository != null)
            {
                policyRepository.Save(ThrottleManager.GetPolicyKey(), policy);
            }
        }
Beispiel #15
0
 public IPRateLimiter(IIpAddressParser ipAddressParser)
 {
     _ipParser = ipAddressParser;
 }
Beispiel #16
0
 public ThrottlingCore(IIpAddressParser ipAddressParser)
 {
     IpAddressParser = ipAddressParser;
 }
 public CustomThrottlingHandler(ThrottlePolicy policy, IPolicyRepository policyRepository, IThrottleRepository repository, IThrottleLogger logger, IIpAddressParser ipAddressParser = null)
     : base(policy, policyRepository, repository, logger, ipAddressParser)
 {
 }
Beispiel #18
0
 public AccountController(AccountServiceClient accountServiceClient, IIpAddressParser ipAddressParser, IMapper mapper)
 {
     _accountServiceClient = accountServiceClient;
     _ipAddressParser      = ipAddressParser;
     _mapper = mapper;
 }