Example #1
0
        public async Task <(bool, Rule)> IsBlockedAsync(int siteId, string ipAddress, string sessionId)
        {
            var rules = await _ruleRepository.GetAllAsync(siteId);

            if (rules == null || rules.Count == 0)
            {
                return(false, null);
            }

            var geoNameId = GetGeoNameId(ipAddress);
            var area      = GetArea(geoNameId);

            foreach (var rule in rules)
            {
                if (rule.BlockMethod == BlockMethod.Password && !string.IsNullOrEmpty(sessionId))
                {
                    if (rule.Password == _settingsManager.Decrypt(sessionId))
                    {
                        continue;
                    }
                }

                var isMatch = false;
                if (area != null)
                {
                    if (rule.BlockAreas != null && rule.BlockAreas.Contains(area.GeoNameId))
                    {
                        isMatch = true;
                    }
                }

                var isBlocked = false;
                if (rule.AreaType == AreaType.Includes)
                {
                    isBlocked = !isMatch;
                }
                else if (rule.AreaType == AreaType.Excludes)
                {
                    isBlocked = isMatch;
                }

                if (!isBlocked)
                {
                    isBlocked = !PageUtils.IsAllowed(ipAddress, rule.BlockList, rule.AllowList);
                }

                if (isBlocked)
                {
                    await _analysisRepository.AddBlockAsync(siteId);

                    return(true, rule);
                }
            }

            return(false, null);
        }