public bool CanAccess(string pattern, string clientAddress, int maxRequests, LimitUnit unit)
        {
            CleanUpExpiredData();

            var requestKey = new RequestKey(pattern, clientAddress);

            if (!_requests.ContainsKey(requestKey))
            {
                _requests[requestKey] = new List <DateTime>();
            }

            var fromTime = unit switch
            {
                LimitUnit.Sec => _dateTime.Now.AddSeconds(-1),
                LimitUnit.Min => _dateTime.Now.AddMinutes(-1),
                LimitUnit.Hr => _dateTime.Now.AddHours(-1),
                _ => throw new ArgumentOutOfRangeException(nameof(unit), unit, null)
            };

            var totalRequestFromTime = _requests[requestKey].Count(x => x >= fromTime);

            // if an api must be throttled not need to add request and refresh timer
            if (totalRequestFromTime >= maxRequests)
            {
                return(false);
            }
            _requests[requestKey].Add(_dateTime.Now);
            return(true);
        }
Пример #2
0
        private String GetUnit(LimitUnit limitUnit)
        {
            switch (limitUnit)
            {
            case LimitUnit.Second:
                return("second");

                break;

            case LimitUnit.Minute:
                return("minute");

                break;

            case LimitUnit.Hour:
                return("hour");

            case LimitUnit.Day:
                return("day");
            }

            throw new IpTablesNetException("Invalid limit unit");
        }
Пример #3
0
        int IIpTablesModuleInternal.Feed(RuleParser parser, bool not)
        {
            switch (parser.GetCurrentArg())
            {
            case OptionLimit:
                string[] s = parser.GetNextArg().Split(new[] { '/' });
                LimitRate = int.Parse(s[0]);
                if (s.Length == 2)
                {
                    Unit = GetUnit(s[1]);
                }
                else if (s.Length > 2)
                {
                    throw new IpTablesNetException("Invalid limit format");
                }
                return(1);

            case OptionLimitBurst:
                Burst = int.Parse(parser.GetNextArg());
                return(1);
            }

            return(0);
        }
Пример #4
0
        public int Feed(CommandParser parser, bool not)
        {
            String current = parser.GetCurrentArg();

            switch (current)
            {
            case OptionHashLimitMode:
                Mode = parser.GetNextArg();
                return(1);

            case OptionHashLimitSrcMask:
                SrcMask = int.Parse(parser.GetNextArg());
                return(1);

            case OptionHashLimitDstMask:
                DstMask = int.Parse(parser.GetNextArg());
                return(1);

            case OptionHashLimitHtableSize:
                HtableSize = int.Parse(parser.GetNextArg());
                return(1);

            case OptionHashLimitHtableMax:
                HtableMax = int.Parse(parser.GetNextArg());
                return(1);

            case OptionHashLimitHtableExpire:
                HtableExpire = int.Parse(parser.GetNextArg());
                return(1);

            case OptionHashLimitHtableGcInterval:
                HtableGcInterval = int.Parse(parser.GetNextArg());
                return(1);

            case OptionHashLimitName:
                Name = parser.GetNextArg();
                if (Name.Length > 15)
                {
                    Name = Name.Substring(0, 15);
                }
                return(1);

            case OptionHashLimit:
            case OptionHashLimitAbove:
            case OptionHashLimitUpto:
                string[] s = parser.GetNextArg().Split(new[] { '/' });
                LimitRate = int.Parse(s[0]);
                if (s.Length == 2)
                {
                    Unit = GetUnit(s[1]);
                }
                else if (s.Length > 2)
                {
                    throw new IpTablesNetException("Invalid limit format");
                }

                LimitMode = current == OptionHashLimitAbove ? HashLimitMode.Above : HashLimitMode.Upto;

                return(1);

            case OptionHashLimitBurst:
                Burst = int.Parse(parser.GetNextArg());
                return(1);
            }

            return(0);
        }
Пример #5
0
 public Limit(int value, LimitUnit unit)
 {
     this.Value = value;
     this.Unit  = unit;
 }