[BLEEDING] Peek into ProtocolLib to fight certain packet spam (3).

This lessens or removes most effects of spamming the PacketPlayInFlying
(3, legacy 10), such as "magnet" or "repell" effects.
This commit is contained in:
asofold 2014-07-12 22:30:51 +02:00
parent 56611cd44b
commit 04b82ad0a6
8 changed files with 205 additions and 2 deletions

View File

@ -0,0 +1,39 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>fr.neatmonster</groupId>
<artifactId>ncpcompatprotocollib</artifactId>
<packaging>jar</packaging>
<name>NCPCompatProtocolLib</name>
<version>static</version>
<parent>
<groupId>fr.neatmonster</groupId>
<artifactId>nocheatplus-parent</artifactId>
<version>static</version>
</parent>
<repositories>
<repository>
<id>comphenix-rep</id>
<name>Comphenix Repository</name>
<url>http://repo.comphenix.net/content/groups/public</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>fr.neatmonster</groupId>
<artifactId>ncpcore</artifactId>
<version>static</version>
</dependency>
<dependency>
<groupId>com.comphenix.protocol</groupId>
<artifactId>ProtocolLib</artifactId>
<version>LATEST</version>
</dependency>
</dependencies>
<description>Features using the plugin ProtocolLib:
http://dev.bukkit.org/bukkit-plugins/protocollib/</description>
</project>

View File

@ -0,0 +1,91 @@
package fr.neatmonster.nocheatplus.net.protocollib;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.plugin.Plugin;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.PacketType.Protocol;
import com.comphenix.protocol.PacketType.Sender;
import com.comphenix.protocol.events.PacketAdapter;
import com.comphenix.protocol.events.PacketEvent;
import fr.neatmonster.nocheatplus.components.JoinLeaveListener;
import fr.neatmonster.nocheatplus.utilities.ActionFrequency;
/**
* Prevent extremely fast ticking by just sending packets that don't do anything
* new and also don't trigger moving events in CraftBukkit.
*
* @author dev1mc
*
*/
public class MoveFrequency extends PacketAdapter implements Listener, JoinLeaveListener {
// TODO: Optimized options (receive only, other?).
// TODO: Async version ?
// private static Collection<PacketType> getPacketTypes() {
// final Collection<PacketType> packetTypes = PacketType.fromName("C03PacketPlayer");
// if (packetTypes.isEmpty()) {
// throw new RuntimeException("Packet types not available.");
// }
// return packetTypes;
// }
private Map<String, ActionFrequency> freqMap = new LinkedHashMap<String, ActionFrequency>();
public MoveFrequency(Plugin plugin) {
// PacketPlayInFlying[3, legacy: 10]
super(plugin, PacketType.findCurrent(Protocol.PLAY, Sender.CLIENT, 3)); //getPacketTypes());
// TODO: Try to get packet by name first + legacy first.
}
private ActionFrequency addName(String name) {
Map<String, ActionFrequency> freqMap = new HashMap<String, ActionFrequency>(this.freqMap);
ActionFrequency freq = new ActionFrequency(5, 1000);
freqMap.put(name, freq);
this.freqMap = freqMap;
return freq;
}
private void removeName(String name) {
Map<String, ActionFrequency> freq = new HashMap<String, ActionFrequency>(this.freqMap);
freq.remove(name);
this.freqMap = freq;
}
@Override
public void playerJoins(Player player) {
addName(player.getName()); // Could spare that one.
}
@Override
public void playerLeaves(Player player) {
removeName(player.getName());
}
private ActionFrequency getFreq(String name) {
ActionFrequency freq = this.freqMap.get(name);
if (freq == null) {
return addName(name);
} else {
return freq;
}
}
@Override
public void onPacketReceiving(PacketEvent event) {
// TODO: Add several (at least has look + has pos individually, maybe none/onground)
ActionFrequency freq = getFreq(event.getPlayer().getName());
freq.add(System.currentTimeMillis(), 1f);
if (freq.score(1f) > 300) {
event.setCancelled(true);
}
}
}

View File

@ -0,0 +1,55 @@
package fr.neatmonster.nocheatplus.net.protocollib;
import java.util.LinkedList;
import java.util.List;
import org.bukkit.plugin.Plugin;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.ProtocolManager;
import com.comphenix.protocol.events.PacketAdapter;
import fr.neatmonster.nocheatplus.components.DisableListener;
import fr.neatmonster.nocheatplus.logging.LogUtil;
/**
* Quick and dirty ProtocolLib setup.
* @author dev1mc
*
*/
public class ProtocolLibComponent implements DisableListener{
private final List<PacketAdapter> registeredPacketAdapters = new LinkedList<PacketAdapter>();
public ProtocolLibComponent(Plugin plugin) {
// Register with ProtocolLib
final ProtocolManager protocolManager = ProtocolLibrary.getProtocolManager();
LogUtil.logInfo("[NoCheatPlus] ProtocolLib seems to be available.");
try {
PacketAdapter adapter = new MoveFrequency(plugin);
protocolManager.addPacketListener(adapter);
registeredPacketAdapters.add(adapter);
LogUtil.logWarning("[NoCheatPlus] Registered some packet-level hook.");
} catch (Throwable t) {
LogUtil.logWarning("[NoCheatPlus] Could not register some packet-level hook.");
LogUtil.logWarning(t); // TODO: Maybe temporary.
}
}
@Override
public void onDisable() {
final ProtocolManager protocolManager = ProtocolLibrary.getProtocolManager();
for (PacketAdapter adapter : registeredPacketAdapters) {
try {
protocolManager.removePacketListener(adapter);
} catch (Throwable t) {
LogUtil.logWarning("[NoCheatPlus] Failed to unregister protocol listener: " + adapter.getClass().getName());
}
}
}
}

View File

@ -98,6 +98,11 @@
<artifactId>ncpcompatcbdev</artifactId>
<version>static</version>
</dependency>
<dependency>
<groupId>fr.neatmonster</groupId>
<artifactId>ncpcompatprotocollib</artifactId>
<version>static</version>
</dependency>
</dependencies>
<!-- Properties -->

View File

@ -766,7 +766,7 @@ public class NoCheatPlus extends JavaPlugin implements NoCheatPlusAPI {
// Register optional default components.
final DefaultComponentFactory dcf = new DefaultComponentFactory();
for (final Object obj : dcf.getAvailableComponentsOnEnable()){
for (final Object obj : dcf.getAvailableComponentsOnEnable(this)){
addComponent(obj);
// Register sub-components to enable registries for optional components.
processQueuedSubComponentHolders();

View File

@ -4,8 +4,10 @@ import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import fr.neatmonster.nocheatplus.NoCheatPlus;
import fr.neatmonster.nocheatplus.checks.inventory.FastConsume;
import fr.neatmonster.nocheatplus.logging.LogUtil;
import fr.neatmonster.nocheatplus.net.protocollib.ProtocolLibComponent;
/**
* Default factory for add-in components which might only be available under certain circumstances.
@ -16,14 +18,16 @@ public class DefaultComponentFactory {
/**
* This will be called from within the plugin in onEnable, after registration of all core listeners and components. After each components addition processQueuedSubComponentHolders() will be called to allow registries for further optional components.
* @param plugin
* @return
*/
public Collection<Object> getAvailableComponentsOnEnable(){
public Collection<Object> getAvailableComponentsOnEnable(NoCheatPlus plugin){
final List<Object> available = new LinkedList<Object>();
// Add components (try-catch).
// Check: inventory.fastconsume.
try{
// TODO: Static test methods !?
FastConsume.testAvailability();
available.add(new FastConsume());
}
@ -31,6 +35,13 @@ public class DefaultComponentFactory {
LogUtil.logInfo("[NoCheatPlus] Inventory checks: FastConsume is not available.");
}
// ProtocolLib dependencies.
try {
available.add(new ProtocolLibComponent(plugin));
} catch (Throwable t){
LogUtil.logInfo("[NoCheatPlus] Packet level access: ProtocolLib is not available.");
}
return available;
}
}

View File

@ -103,6 +103,7 @@
<include>fr.neatmonster:ncpcompatcb3026</include>
<include>fr.neatmonster:ncpcompatcb3043</include>
<include>fr.neatmonster:ncpcompatcbdev</include>
<include>fr.neatmonster:ncpcompatprotocollib</include>
<include>fr.neatmonster:ncpplugin</include>
<!-- <include>fr.neatmonster:nocheatplus-parent</include> -->
</includes>

View File

@ -31,6 +31,7 @@
<module>NCPCompatCB3026</module>
<module>NCPCompatCB3043</module>
<module>NCPCompatCBDev</module>
<module>NCPCompatProtocolLib</module>
<module>NCPPlugin</module>
<module>NoCheatPlus</module>
</modules>