-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEAT] Implement basic message spam detector
- Loading branch information
1 parent
3ae6cc7
commit b96963a
Showing
3 changed files
with
86 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
Common/src/main/java/com/hypherionmc/sdlink/core/managers/SpamManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.hypherionmc.sdlink.core.managers; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* @author HypherionSA | ||
* Basic Message Spam Detector | ||
*/ | ||
public class SpamManager { | ||
|
||
private final ConcurrentHashMap<String, List<Long>> messageTimestamps = new ConcurrentHashMap<>(); | ||
private final Set<String> blockedMessages = new HashSet<>(); | ||
|
||
private final int threshold; | ||
private final int timeWindowMillis; | ||
private final int blockMillis; | ||
private final ScheduledExecutorService executor; | ||
|
||
public SpamManager(int threshold, int timeWindowMillis, int blockMillis, ScheduledExecutorService executor) { | ||
this.threshold = threshold; | ||
this.timeWindowMillis = timeWindowMillis; | ||
this.blockMillis = blockMillis; | ||
this.executor = executor; | ||
startSpamChecker(); | ||
} | ||
|
||
public void receiveMessage(String message) { | ||
long currentTime = System.currentTimeMillis(); | ||
|
||
messageTimestamps.compute(message, (msg, timestamps) -> { | ||
if (timestamps == null) | ||
timestamps = new ArrayList<>(); | ||
|
||
timestamps.add(currentTime); | ||
return timestamps.stream() | ||
.filter(timestamp -> currentTime - timestamp <= timeWindowMillis) | ||
.toList(); | ||
}); | ||
|
||
if (messageTimestamps.get(message).size() >= threshold) | ||
blockedMessages.add(message); | ||
} | ||
|
||
public boolean isBlocked(String message) { | ||
return blockedMessages.contains(message); | ||
} | ||
|
||
private void startSpamChecker() { | ||
executor.scheduleAtFixedRate(() -> { | ||
long currentTime = System.currentTimeMillis(); | ||
blockedMessages.removeIf(message -> { | ||
List<Long> timestamps = messageTimestamps.getOrDefault(message, new ArrayList<>()); | ||
timestamps = timestamps.stream() | ||
.filter(timestamp -> currentTime - timestamp <= timeWindowMillis) | ||
.toList(); | ||
messageTimestamps.put(message, timestamps); | ||
return timestamps.size() < threshold; | ||
}); | ||
}, blockMillis, blockMillis, TimeUnit.MILLISECONDS); | ||
} | ||
|
||
} |