public static void AddHackerSpray(this IServiceCollection services, IConfigurationSection section)
        {
            var prefix = section["Prefix"];

            if (string.IsNullOrWhiteSpace(prefix))
            {
                throw new Exception("Invalid prefix configured in section: " + section.Key);
            }

            var redis = section["Redis"];

            if (string.IsNullOrWhiteSpace(redis))
            {
                throw new Exception("Invalid redis connection string configured in section: " + section.Key);
            }

            IEnumerable <string> keys = section.GetSection("Keys").GetChildren().Select(x => x.Value);
            var option = new HackerSprayOption
            {
                Keys   = keys.ToList(),
                Prefix = prefix,
                Redis  = redis
            };

            services.AddSingleton <HackerSprayOption>(option);
        }
Пример #2
0
        public HackerSprayerMiddleware(RequestDelegate next, ILoggerFactory loggerFactory, HackerSprayOption option)
        {
            _next   = next;
            _logger = loggerFactory.CreateLogger("HackerSpray");

            if (_option == null)
            {
                lock (_lockObject)
                {
                    if (_option == null)
                    {
                        Debug("Initializing HackerSpray options.");

                        _option = option;

                        Hacker.Logger = _logger;
                        Hacker.Store  = new RedisDefenceStore(_option.Redis,
                                                              _option.Prefix, Hacker.Config);

                        _keys = new HackerSprayOptionKey[_option.Keys.Count];

                        for (var i = 0; i < _option.Keys.Count; i++)
                        {
                            var parts = _option.Keys[i].Split(' ');
                            if (parts.Length != 5)
                            {
                                throw new Exception("Each key must have exactly 4 parts - METHOD PATH MAXATTEMPTS INTERVAL MODE. But you have put: " + _option.Keys[i]);
                            }

                            long maxAttempts;
                            if (!long.TryParse(parts[2], out maxAttempts))
                            {
                                throw new Exception($"Invalid max attempts configured for key{parts[0]}. Must be a number.");
                            }

                            TimeSpan interval;
                            if (!TimeSpan.TryParse(parts[3], out interval))
                            {
                                throw new Exception($"Invalid interval configured for key{parts[0]}. Must be a TimeSpan eg 00:01:00.");
                            }

                            var mode = default(HackerSprayOptionKey.HitCountMode);
                            if (parts[4] == "key")
                            {
                                mode = HackerSprayOptionKey.HitCountMode.PerKey;
                            }
                            else if (parts[4] == "origin")
                            {
                                mode = HackerSprayOptionKey.HitCountMode.PerOrigin;
                            }
                            else
                            {
                                mode = HackerSprayOptionKey.HitCountMode.PerKeyOrigin;
                            }

                            _keys[i] = new HackerSprayOptionKey()
                            {
                                Method      = parts[0],
                                Key         = parts[1],
                                MaxAttempts = maxAttempts,
                                Interval    = interval,
                                Mode        = mode
                            };
                        }

                        Debug("HackerSpray options processed");
                    }
                }
            }
        }