Flood protection.

This commit is contained in:
cnaude 2015-05-02 18:08:30 -07:00
parent 3e00ae9087
commit 3101663625
6 changed files with 218 additions and 17 deletions

View File

@ -0,0 +1,111 @@
/*
* Copyright (C) 2015 cnaude
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.cnaude.purpleirc;
import java.util.HashMap;
import java.util.Map;
import org.bukkit.entity.Player;
import org.pircbotx.User;
/**
*
* @author cnaude
*/
public class FloodChecker {
private final PurpleIRC plugin;
private final PurpleBot ircBot;
private final String TIME_FORMAT = "%2.2f";
private final Map<String, MyUser> myUsers;
private class MyUser {
Long lastCommand = System.currentTimeMillis();
Long coolDownTimer = 0L;
int commandCount = 0;
public MyUser() {
}
}
public FloodChecker(final PurpleBot ircBot, final PurpleIRC plugin) {
this.ircBot = ircBot;
this.plugin = plugin;
this.myUsers = new HashMap<>();
}
public boolean isSpam(Object object) {
if (ircBot.floodControlEnabled) {
String key;
String name;
if (object instanceof User) {
key = ((User) object).getHostmask();
name = ((User) object).getNick();
} else if (object instanceof Player) {
key = ((Player) object).getUniqueId().toString();
name = ((Player) object).getName();
} else {
return false;
}
if (myUsers.containsKey(key)) {
MyUser myUser = myUsers.get(key);
Long timeNow = System.currentTimeMillis();
Long timeDiff = timeNow - myUser.lastCommand;
Long coolDiff = timeNow - myUser.coolDownTimer;
myUser.commandCount = Math.max(myUser.commandCount
- (Math.round(timeDiff / ircBot.floodControlTimeInterval * ircBot.floodControlMaxMessages)), 0) + 1;
myUser.lastCommand = timeNow;
if (coolDiff < ircBot.floodControlCooldown) {
plugin.logDebug("Cooldown: " + name + "(" + coolDiff + " < " + ircBot.floodControlCooldown + ")");
return true;
}
if (myUser.commandCount > ircBot.floodControlMaxMessages) {
myUser.coolDownTimer = timeNow;
plugin.logDebug("Spam ignored from: " + name + "(" + key + ")");
return true;
}
} else {
myUsers.put(key, new MyUser());
}
}
return false;
}
public String getCoolDown(User user) {
return getCoolDown(user.getHostmask());
}
public String getCoolDown(Player player) {
return getCoolDown(player.getUniqueId().toString());
}
private String getCoolDown(String key) {
Long timeNow = System.currentTimeMillis();
if (myUsers.containsKey(key)) {
return String.format(TIME_FORMAT,
((ircBot.floodControlCooldown - (timeNow - myUsers.get(key).coolDownTimer)) / 1000f));
}
return "0";
}
}

View File

@ -42,6 +42,15 @@ public class IRCMessageHandler {
this.plugin = plugin;
}
private void sendFloodWarning(User user, PurpleBot ircBot) {
String message = plugin.colorConverter.gameColorsToIrc(plugin.getMsgTemplate(
ircBot.botNick, TemplateName.IRC_FLOOD_WARNING)
.replace("%COOLDOWN%", ircBot.floodChecker.getCoolDown(user)));
if (!message.isEmpty()) {
user.send().notice(message);
}
}
/**
*
* @param ircBot
@ -51,6 +60,10 @@ public class IRCMessageHandler {
* @param privateMessage
*/
public void processMessage(PurpleBot ircBot, User user, Channel channel, String message, boolean privateMessage) {
if (ircBot.floodChecker.isSpam(user)) {
sendFloodWarning(user, ircBot);
return;
}
plugin.logDebug("processMessage: " + message);
String myChannel = channel.getName();
if (ircBot.muteList.get(myChannel).contains(user.getNick())) {

View File

@ -157,6 +157,11 @@ public final class PurpleBot {
public List<String> channelCmdNotifyIgnore;
private final ArrayList<ListenerAdapter> ircListeners;
public IRCMessageQueueWatcher messageQueue;
public FloodChecker floodChecker;
protected boolean floodControlEnabled;
protected int floodControlMaxMessages;
protected long floodControlTimeInterval;
protected long floodControlCooldown;
private final String fileName;
int joinNoticeCoolDown;
boolean joinNoticeEnabled;
@ -241,6 +246,7 @@ public final class PurpleBot {
}
messageQueue = new IRCMessageQueueWatcher(this, plugin);
floodChecker = new FloodChecker(this, plugin);
}
@ -929,6 +935,12 @@ public final class PurpleBot {
plugin.logDebug("join-notice.ctcp: " + joinNoticeCtcp);
plugin.logDebug("join-notice.message: " + joinNoticeMessage);
// flood control setup
floodControlEnabled = config.getBoolean("flood-control.enabled", false);
floodControlMaxMessages = config.getInt("flood-control.max-messages", 2);
floodControlTimeInterval = config.getLong("flood-control.time-interval", 1000L);
floodControlCooldown = config.getLong("flood-control.cooldown", 60000L);
// build command map
CaseInsensitiveMap<CaseInsensitiveMap<String>> map = new CaseInsensitiveMap<>();
CaseInsensitiveMap<List<String>> extraMap = new CaseInsensitiveMap<>();
@ -1010,6 +1022,10 @@ public final class PurpleBot {
if (!this.isConnected()) {
return;
}
if (floodChecker.isSpam(player)) {
sendFloodWarning(player);
return;
}
for (String channelName : botChannels) {
if (!isPlayerInValidWorld(player, channelName)) {
continue;
@ -1058,6 +1074,15 @@ public final class PurpleBot {
}
}
private void sendFloodWarning(Player player) {
String message = plugin.getMsgTemplate(
botNick, TemplateName.GAME_FLOOD_WARNING)
.replace("%COOLDOWN%", floodChecker.getCoolDown(player));
if (!message.isEmpty()) {
player.sendMessage(message);
}
}
// Called from HeroChat listener
/**
*
@ -1070,6 +1095,10 @@ public final class PurpleBot {
return;
}
Player player = chatter.getPlayer();
if (floodChecker.isSpam(player)) {
sendFloodWarning(player);
return;
}
for (String channelName : botChannels) {
if (!isPlayerInValidWorld(player, channelName)) {
continue;
@ -1094,6 +1123,10 @@ public final class PurpleBot {
if (!this.isConnected()) {
return;
}
if (floodChecker.isSpam(player)) {
sendFloodWarning(player);
return;
}
for (String channelName : botChannels) {
if (!isPlayerInValidWorld(player, channelName)) {
continue;
@ -1114,6 +1147,10 @@ public final class PurpleBot {
if (!this.isConnected()) {
return;
}
if (floodChecker.isSpam(player)) {
sendFloodWarning(player);
return;
}
for (String channelName : botChannels) {
if (!isPlayerInValidWorld(player, channelName)) {
continue;
@ -1134,6 +1171,10 @@ public final class PurpleBot {
if (!this.isConnected()) {
return;
}
if (floodChecker.isSpam(player)) {
sendFloodWarning(player);
return;
}
for (String channelName : botChannels) {
if (!isPlayerInValidWorld(player, channelName)) {
continue;
@ -1154,6 +1195,10 @@ public final class PurpleBot {
if (!this.isConnected()) {
return;
}
if (floodChecker.isSpam(player)) {
sendFloodWarning(player);
return;
}
if (plugin.tcHook != null) {
for (String channelName : botChannels) {
if (!isPlayerInValidWorld(player, channelName)) {
@ -1180,6 +1225,10 @@ public final class PurpleBot {
return;
}
Player player = chatter.getPlayer();
if (floodChecker.isSpam(player)) {
sendFloodWarning(player);
return;
}
for (String channelName : botChannels) {
if (!isPlayerInValidWorld(player, channelName)) {
continue;
@ -1213,6 +1262,10 @@ public final class PurpleBot {
return;
}
Player player = plugin.getServer().getPlayer(participant.getName());
if (floodChecker.isSpam(player)) {
sendFloodWarning(player);
return;
}
for (String channelName : botChannels) {
if (!isPlayerInValidWorld(player, channelName)) {
continue;
@ -1362,6 +1415,10 @@ public final class PurpleBot {
if (!this.isConnected()) {
return;
}
if (floodChecker.isSpam(player)) {
sendFloodWarning(player);
return;
}
for (String channelName : botChannels) {
if (isMessageEnabled(channelName, TemplateName.BROADCAST_MESSAGE)) {
asyncIRCMessage(channelName, plugin.tokenizer
@ -1644,6 +1701,10 @@ public final class PurpleBot {
if (!this.isConnected()) {
return;
}
if (floodChecker.isSpam(player)) {
sendFloodWarning(player);
return;
}
for (String channelName : botChannels) {
if (isMessageEnabled(channelName, TemplateName.GAME_ACTION)) {
if (!isPlayerInValidWorld(player, channelName)) {

View File

@ -133,4 +133,7 @@ public class TemplateName {
public final static String FAKE_JOIN = "fake-join";
public final static String FAKE_QUIT = "fake-quit";
public final static String GAME_FLOOD_WARNING = "game-flood-warning";
public final static String IRC_FLOOD_WARNING = "irc-flood-warning";
}

View File

@ -62,6 +62,16 @@ command-notify:
- example
ignore:
- /example
# Messaging flood control (game and IRC)
flood-control:
# Enable or disable flood control
enabled: false
# The maximum number of messages per interval
max-messages: 2
# Time interval in milliseconds
time-interval: 1000
# Cooldown in milliseconds. If user is spamming then this cooldown takes effect.
cooldown: 60000
# Automatically part invalid channels
part-invalid-channels: false
# Message when leaving invalid channel

View File

@ -213,6 +213,9 @@ message-format:
default-group-prefix: ''
default-player-world: 'world'
default-player-group: ''
# Flood control
game-flood-warning: '&3Message not sent to IRC due to spamming. &rCooldown: %COOLDOWN%s'
irc-flood-warning: '&3Message not sent to game due to spamming. &rCooldown: %COOLDOWN%s'
# Format for the @list command in IRC
list-format: '[&9Minecraft&r] &2Online &r(%COUNT%/%MAX%): %PLAYERS%'
list-separator: ', '