protect against msg attack
All checks were successful
/ Auto-Build-App (push) Successful in 1m50s

This commit is contained in:
pietru 2024-11-02 23:58:39 +01:00
parent 713749dc78
commit 6de72981e6
2 changed files with 32 additions and 1 deletions

View file

@ -9,21 +9,43 @@ import finalforeach.cosmicreach.networking.server.ServerSingletons;
import io.netty.channel.ChannelHandlerContext;
import net.pietru.cookie_utils.api.TextCommands;
import net.pietru.cookie_utils.setups.Setup;
import net.pietru.cookie_utils.utils.MessageInfo;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import java.util.HashMap;
import java.util.Map;
@Mixin(MessagePacket.class)
public class MessagePacketMixin {
@Shadow
public String playerUniqueId;
@Shadow public String message;
private static Map<String, MessageInfo> ply_msgs = new HashMap<>();
@Inject(method = "handle", at = @At("HEAD"), cancellable = true)
private void event_on_message(CallbackInfo ci, @Local NetworkIdentity identity, @Local ChannelHandlerContext ctx){
if (identity.getSide() != NetworkSide.CLIENT) {
if (ply_msgs.containsKey(playerUniqueId)){
MessageInfo msg = ply_msgs.get(playerUniqueId);
long time = System.currentTimeMillis();
if (msg.last_msg+msg.msg_delay>time || msg.last_msg_txt.equals(message)) {
msg.msg_delay += 1000;
ci.cancel();
} else
msg.msg_delay=1000;
msg.last_msg = System.currentTimeMillis();
msg.last_msg_txt=message;
} else {
ply_msgs.put(playerUniqueId,new MessageInfo());
}
if (identity.getSide() != NetworkSide.CLIENT && ci.isCancelled()) {
Account account = ServerSingletons.getAccount(identity);
if (message.startsWith(":")) {
String cmd = message.replaceFirst(":","");

View file

@ -0,0 +1,9 @@
package net.pietru.cookie_utils.utils;
import java.util.ArrayList;
public class MessageInfo {
public long last_msg = 0;
public long msg_delay = 0;
public String last_msg_txt = "";
}