Example #1
0
 private void RefreshTrie(RaidProtectionSettings settings)
 {
     if (settings.Enabled && settings.PhraseBlacklistRule.Enabled && settings.PhraseBlacklistRule.Blacklist.Any())
     {
         _blacklistTries[settings.ServerId] = CreateTrie(settings.PhraseBlacklistRule.Blacklist);
     }
     else
     {
         _blacklistTries.TryRemove(settings.ServerId, out _);
     }
 }
Example #2
0
        private async Task CleanupContext(RaidProtectionSettings settings)
        {
            try
            {
                // Guild contexts
                await _context.Mutex.WaitAsync();

                DateTime now = DateTime.UtcNow;
                if (_context.LastCleanup > now - CleanupTimer)
                {
                    return;
                }

                _context.LastCleanup = now;

                var toRemove = new List <ulong>();
                foreach (var guildContext in _context.GuildContexts)
                {
                    try
                    {
                        // User contexts
                        await guildContext.Value.Mutex.WaitAsync();

                        foreach (var userContext in guildContext.Value.UserContexts.ToList())
                        {
                            try
                            {
                                await userContext.Value.Mutex.WaitAsync();

                                foreach (var offenses in userContext.Value.Offenses.ToList())
                                {
                                    offenses.Value.SlideWindow(settings.GetRule <RaidProtectionRule>(offenses.Key).OffenseWindow, now - MaxMessageProcessingDelay);
                                    if (offenses.Value.Count <= 0)
                                    {
                                        userContext.Value.Offenses.Remove(offenses.Key);
                                    }
                                }

                                userContext.Value.TextPosts.SlideWindow(settings.TextSpamRule.Window, now - MaxMessageProcessingDelay);
                                userContext.Value.ImagePosts.SlideWindow(settings.ImageSpamRule.Window, now - MaxMessageProcessingDelay);

                                if (userContext.Value.Empty)
                                {
                                    guildContext.Value.UserContexts.Remove(userContext.Key);
                                }
                            }
                            finally
                            {
                                userContext.Value.Mutex.Release();
                            }
                        }

                        if (guildContext.Value.UserContexts.Count <= 0)
                        {
                            toRemove.Add(guildContext.Key);
                        }
                    }
                    finally
                    {
                        guildContext.Value.Mutex.Release();
                    }
                }

                foreach (var removable in toRemove)
                {
                    _context.GuildContexts.Remove(removable);
                }

                if (_context.LastReport < now - ReportTimer)
                {
                    _context.LastReport = now;
                    _logger.LogInformation("Raid protection status: {RaidProtectionGuildContextCount} guild contexts, {RaidProtectionUserContextCount} user contexts",
                                           _context.GuildContexts.Count,
                                           _context.GuildContexts.Aggregate(0, (x, y) => x + y.Value.UserContexts.Count));
                }
            }
            finally
            {
                _context.Mutex.Release();
            }
        }