anti spam filter packetlogger

This commit is contained in:
sirjonasxx 2021-04-25 23:41:40 +02:00
parent 71496b8a30
commit da35c41524
2 changed files with 55 additions and 1 deletions

View File

@ -10,6 +10,7 @@ import javafx.event.ActionEvent;
import javafx.fxml.Initializable;
import javafx.scene.control.CheckMenuItem;
import javafx.scene.control.Label;
import javafx.scene.control.RadioMenuItem;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
@ -42,6 +43,18 @@ public class UiLoggerController implements Initializable {
public CheckMenuItem chkHideOnDisconnect;
public CheckMenuItem chkResetOnDisconnect;
private final static int FILTER_AMOUNT_THRESHOLD_S = 15;
private final static int FILTER_AMOUNT_THRESHOLD_M = 9;
private final static int FILTER_AMOUNT_THRESHOLD_H = 4;
private final static int FILTER_TIME_THRESHOLD = 5000;
public RadioMenuItem chkAntiSpam_none;
public RadioMenuItem chkAntiSpam_low;
public RadioMenuItem chkAntiSpam_medium;
public RadioMenuItem chkAntiSpam_high;
private Map<Integer, LinkedList<Long>> filterTimestamps = new HashMap<>();
private StyleClassedTextArea area;
private Stage stage;
@ -90,11 +103,40 @@ public class UiLoggerController implements Initializable {
return text.trim();
}
private boolean checkFilter(HPacket packet) {
int headerId = packet.headerId();
int threshold = chkAntiSpam_none.isSelected() ? 100000000 : (
chkAntiSpam_low.isSelected() ? FILTER_AMOUNT_THRESHOLD_S : (
chkAntiSpam_medium.isSelected() ? FILTER_AMOUNT_THRESHOLD_M : FILTER_AMOUNT_THRESHOLD_H
)
);
if (!filterTimestamps.containsKey(headerId)) {
filterTimestamps.put(headerId, new LinkedList<>());
}
long queueRemoveThreshold = System.currentTimeMillis() - FILTER_TIME_THRESHOLD;
LinkedList<Long> list = filterTimestamps.get(headerId);
while (!list.isEmpty() && list.get(0) < queueRemoveThreshold) list.removeFirst();
if (list.size() == threshold) list.removeFirst();
list.add(System.currentTimeMillis());
return list.size() >= threshold;
}
public void appendMessage(HPacket packet, int types) {
boolean isBlocked = (types & PacketLogger.MESSAGE_TYPE.BLOCKED.getValue()) != 0;
boolean isReplaced = (types & PacketLogger.MESSAGE_TYPE.REPLACED.getValue()) != 0;
boolean isIncoming = (types & PacketLogger.MESSAGE_TYPE.INCOMING.getValue()) != 0;
if (isIncoming && !isBlocked && !isReplaced) {
boolean filter = checkFilter(packet);
if (filter) return;
}
if (isIncoming && !viewIncoming) return;
if (!isIncoming && !viewOutgoing) return;

View File

@ -50,6 +50,18 @@
<CheckMenuItem fx:id="chkUseNewStructures" mnemonicParsing="false" onAction="#toggleMessageHash" selected="true" text="New structures" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Anti-spam filter">
<items>
<RadioMenuItem fx:id="chkAntiSpam_none" mnemonicParsing="false" selected="true" text="None">
<toggleGroup>
<ToggleGroup fx:id="antispam" />
</toggleGroup>
</RadioMenuItem>
<RadioMenuItem fx:id="chkAntiSpam_low" mnemonicParsing="false" text="Low" toggleGroup="$antispam" />
<RadioMenuItem fx:id="chkAntiSpam_medium" mnemonicParsing="false" text="Medium" toggleGroup="$antispam" />
<RadioMenuItem fx:id="chkAntiSpam_high" mnemonicParsing="false" text="High" toggleGroup="$antispam" />
</items>
</Menu>
<CheckMenuItem fx:id="chkSkipBigPackets" mnemonicParsing="false" onAction="#toggleSkipPackets" selected="true" text="Skip big packets" />
</items>
</Menu>