From 8b1eb86543fb706fadcafef84813317275704a65 Mon Sep 17 00:00:00 2001 From: asofold Date: Tue, 24 Nov 2015 06:35:58 +0100 Subject: [PATCH] Move AttackFrequency and KeepAliveFrequency as checks to NCPCore. + Add adapters to "packet-listeners" in feature tags. --- .../{protocollib => }/AttackFrequency.java | 2 +- ...veFrequency.java => KeepAliveAdapter.java} | 37 ++++++++++--------- .../checks/net/protocollib/MovingFlying.java | 9 +++++ .../net/protocollib/ProtocolLibComponent.java | 4 +- .../net/protocollib/UseEntityAdapter.java | 1 + .../checks/net/KeepAliveFrequency.java | 36 ++++++++++++++++++ 6 files changed, 69 insertions(+), 20 deletions(-) rename NCPCompatProtocolLib/src/main/java/fr/neatmonster/nocheatplus/checks/net/{protocollib => }/AttackFrequency.java (98%) rename NCPCompatProtocolLib/src/main/java/fr/neatmonster/nocheatplus/checks/net/protocollib/{KeepAliveFrequency.java => KeepAliveAdapter.java} (62%) create mode 100644 NCPCore/src/main/java/fr/neatmonster/nocheatplus/checks/net/KeepAliveFrequency.java diff --git a/NCPCompatProtocolLib/src/main/java/fr/neatmonster/nocheatplus/checks/net/protocollib/AttackFrequency.java b/NCPCompatProtocolLib/src/main/java/fr/neatmonster/nocheatplus/checks/net/AttackFrequency.java similarity index 98% rename from NCPCompatProtocolLib/src/main/java/fr/neatmonster/nocheatplus/checks/net/protocollib/AttackFrequency.java rename to NCPCompatProtocolLib/src/main/java/fr/neatmonster/nocheatplus/checks/net/AttackFrequency.java index 228d0329..a3189809 100644 --- a/NCPCompatProtocolLib/src/main/java/fr/neatmonster/nocheatplus/checks/net/protocollib/AttackFrequency.java +++ b/NCPCompatProtocolLib/src/main/java/fr/neatmonster/nocheatplus/checks/net/AttackFrequency.java @@ -1,4 +1,4 @@ -package fr.neatmonster.nocheatplus.checks.net.protocollib; +package fr.neatmonster.nocheatplus.checks.net; import org.bukkit.entity.Player; diff --git a/NCPCompatProtocolLib/src/main/java/fr/neatmonster/nocheatplus/checks/net/protocollib/KeepAliveFrequency.java b/NCPCompatProtocolLib/src/main/java/fr/neatmonster/nocheatplus/checks/net/protocollib/KeepAliveAdapter.java similarity index 62% rename from NCPCompatProtocolLib/src/main/java/fr/neatmonster/nocheatplus/checks/net/protocollib/KeepAliveFrequency.java rename to NCPCompatProtocolLib/src/main/java/fr/neatmonster/nocheatplus/checks/net/protocollib/KeepAliveAdapter.java index dbae3004..0ed29a23 100644 --- a/NCPCompatProtocolLib/src/main/java/fr/neatmonster/nocheatplus/checks/net/protocollib/KeepAliveFrequency.java +++ b/NCPCompatProtocolLib/src/main/java/fr/neatmonster/nocheatplus/checks/net/protocollib/KeepAliveAdapter.java @@ -1,5 +1,7 @@ package fr.neatmonster.nocheatplus.checks.net.protocollib; +import java.util.Arrays; + import org.bukkit.entity.Player; import org.bukkit.plugin.Plugin; @@ -7,10 +9,12 @@ import com.comphenix.protocol.PacketType; import com.comphenix.protocol.events.ListenerPriority; import com.comphenix.protocol.events.PacketEvent; -import fr.neatmonster.nocheatplus.checks.Check; -import fr.neatmonster.nocheatplus.checks.CheckType; +import fr.neatmonster.nocheatplus.NCPAPIProvider; +import fr.neatmonster.nocheatplus.checks.net.KeepAliveFrequency; import fr.neatmonster.nocheatplus.checks.net.NetConfig; import fr.neatmonster.nocheatplus.checks.net.NetData; +import fr.neatmonster.nocheatplus.config.ConfPaths; +import fr.neatmonster.nocheatplus.config.ConfigManager; /** * Limit keep alive packet frequency, set lastKeepAliveTime (even if disabled, @@ -19,13 +23,19 @@ import fr.neatmonster.nocheatplus.checks.net.NetData; * @author asofold * */ -public class KeepAliveFrequency extends BaseAdapter { +public class KeepAliveAdapter extends BaseAdapter { /** Dummy check for bypass checking and actions execution. */ - private final Check check = new Check(CheckType.NET_KEEPALIVEFREQUENCY) {}; + private final KeepAliveFrequency frequencyCheck = new KeepAliveFrequency(); - public KeepAliveFrequency(Plugin plugin) { + public KeepAliveAdapter(Plugin plugin) { super(plugin, ListenerPriority.LOW, PacketType.Play.Client.KEEP_ALIVE); + + // Add feature tags for checks. + if (ConfigManager.isTrueForAnyConfig(ConfPaths.NET_KEEPALIVEFREQUENCY_ACTIVE)) { + NCPAPIProvider.getNoCheatPlusAPI().addFeatureTags("checks", Arrays.asList(KeepAliveFrequency.class.getSimpleName())); + } + NCPAPIProvider.getNoCheatPlusAPI().addComponent(frequencyCheck); } @Override @@ -40,21 +50,14 @@ public class KeepAliveFrequency extends BaseAdapter { // Always update last received time. final NetData data = dataFactory.getData(player); data.lastKeepAliveTime = time; - // Check activation. final NetConfig cc = configFactory.getConfig(player); - if (!cc.keepAliveFrequencyActive) { - return; - } + + // Run check(s). + // TODO: Match vs. outgoing keep alive requests. // TODO: Better modeling of actual packet sequences (flying vs. keep alive vs. request/ping). // TODO: Better integration with god-mode check / trigger reset ndt. - data.keepAliveFreq.add(time, 1f); - final float first = data.keepAliveFreq.bucketScore(0); - if (first > 1f && !check.hasBypass(player)) { - // Trigger a violation. - final double vl = Math.max(first - 1f, data.keepAliveFreq.score(1f) - data.keepAliveFreq.numberOfBuckets()); - if (check.executeActions(player, vl, 1.0, cc.keepAliveFrequencyActions)) { - event.setCancelled(true); - } + if (cc.keepAliveFrequencyActive && frequencyCheck.check(player, time, data, cc)) { + event.setCancelled(true); } } diff --git a/NCPCompatProtocolLib/src/main/java/fr/neatmonster/nocheatplus/checks/net/protocollib/MovingFlying.java b/NCPCompatProtocolLib/src/main/java/fr/neatmonster/nocheatplus/checks/net/protocollib/MovingFlying.java index 0f6ed439..ce7df3e3 100644 --- a/NCPCompatProtocolLib/src/main/java/fr/neatmonster/nocheatplus/checks/net/protocollib/MovingFlying.java +++ b/NCPCompatProtocolLib/src/main/java/fr/neatmonster/nocheatplus/checks/net/protocollib/MovingFlying.java @@ -1,5 +1,6 @@ package fr.neatmonster.nocheatplus.checks.net.protocollib; +import java.util.Arrays; import java.util.List; import org.bukkit.entity.Player; @@ -15,6 +16,8 @@ import fr.neatmonster.nocheatplus.checks.net.FlyingFrequency; import fr.neatmonster.nocheatplus.checks.net.NetConfig; import fr.neatmonster.nocheatplus.checks.net.NetData; import fr.neatmonster.nocheatplus.checks.net.model.DataPacketFlying; +import fr.neatmonster.nocheatplus.config.ConfPaths; +import fr.neatmonster.nocheatplus.config.ConfigManager; import fr.neatmonster.nocheatplus.logging.Streams; import fr.neatmonster.nocheatplus.utilities.CheckUtils; @@ -55,6 +58,12 @@ public class MovingFlying extends BaseAdapter { PacketType.Play.Client.POSITION, PacketType.Play.Client.POSITION_LOOK }); + + // Add feature tags for checks. + if (ConfigManager.isTrueForAnyConfig(ConfPaths.NET_FLYINGFREQUENCY_ACTIVE)) { + NCPAPIProvider.getNoCheatPlusAPI().addFeatureTags("checks", Arrays.asList(FlyingFrequency.class.getSimpleName())); + } + NCPAPIProvider.getNoCheatPlusAPI().addComponent(flyingFrequency); } @Override diff --git a/NCPCompatProtocolLib/src/main/java/fr/neatmonster/nocheatplus/checks/net/protocollib/ProtocolLibComponent.java b/NCPCompatProtocolLib/src/main/java/fr/neatmonster/nocheatplus/checks/net/protocollib/ProtocolLibComponent.java index 5262b2fc..7e3cd197 100644 --- a/NCPCompatProtocolLib/src/main/java/fr/neatmonster/nocheatplus/checks/net/protocollib/ProtocolLibComponent.java +++ b/NCPCompatProtocolLib/src/main/java/fr/neatmonster/nocheatplus/checks/net/protocollib/ProtocolLibComponent.java @@ -73,7 +73,7 @@ public class ProtocolLibComponent implements DisableListener, INotifyReload, Joi } if (ConfigManager.isTrueForAnyConfig(ConfPaths.NET_KEEPALIVEFREQUENCY_ACTIVE) || ConfigManager.isTrueForAnyConfig(ConfPaths.FIGHT_GODMODE_CHECK)) { // (Set lastKeepAlive if this or fight.godmode is enabled.) - register("fr.neatmonster.nocheatplus.checks.net.protocollib.KeepAliveFrequency", plugin); + register("fr.neatmonster.nocheatplus.checks.net.protocollib.KeepAliveAdapter", plugin); } if (ConfigManager.isTrueForAnyConfig(ConfPaths.NET_SOUNDDISTANCE_ACTIVE)) { register("fr.neatmonster.nocheatplus.checks.net.protocollib.SoundDistance", plugin); @@ -84,7 +84,7 @@ public class ProtocolLibComponent implements DisableListener, INotifyReload, Joi names.add(adapter.getClass().getSimpleName()); } StaticLog.logInfo("Available (and activated) packet level hooks: " + StringUtil.join(names, " | ")); - NCPAPIProvider.getNoCheatPlusAPI().addFeatureTags("checks", names); + NCPAPIProvider.getNoCheatPlusAPI().addFeatureTags("packet-listeners", names); } else { StaticLog.logInfo("No packet level hooks activated."); } diff --git a/NCPCompatProtocolLib/src/main/java/fr/neatmonster/nocheatplus/checks/net/protocollib/UseEntityAdapter.java b/NCPCompatProtocolLib/src/main/java/fr/neatmonster/nocheatplus/checks/net/protocollib/UseEntityAdapter.java index c89bd72f..54935236 100644 --- a/NCPCompatProtocolLib/src/main/java/fr/neatmonster/nocheatplus/checks/net/protocollib/UseEntityAdapter.java +++ b/NCPCompatProtocolLib/src/main/java/fr/neatmonster/nocheatplus/checks/net/protocollib/UseEntityAdapter.java @@ -12,6 +12,7 @@ import com.comphenix.protocol.reflect.StructureModifier; import com.comphenix.protocol.wrappers.EnumWrappers.EntityUseAction; import fr.neatmonster.nocheatplus.NCPAPIProvider; +import fr.neatmonster.nocheatplus.checks.net.AttackFrequency; import fr.neatmonster.nocheatplus.checks.net.NetConfig; import fr.neatmonster.nocheatplus.checks.net.NetData; import fr.neatmonster.nocheatplus.config.ConfPaths; diff --git a/NCPCore/src/main/java/fr/neatmonster/nocheatplus/checks/net/KeepAliveFrequency.java b/NCPCore/src/main/java/fr/neatmonster/nocheatplus/checks/net/KeepAliveFrequency.java new file mode 100644 index 00000000..afedf3cf --- /dev/null +++ b/NCPCore/src/main/java/fr/neatmonster/nocheatplus/checks/net/KeepAliveFrequency.java @@ -0,0 +1,36 @@ +package fr.neatmonster.nocheatplus.checks.net; + +import org.bukkit.entity.Player; + +import fr.neatmonster.nocheatplus.checks.Check; +import fr.neatmonster.nocheatplus.checks.CheckType; +import fr.neatmonster.nocheatplus.utilities.CheckUtils; + +public class KeepAliveFrequency extends Check { + + public KeepAliveFrequency() { + super(CheckType.NET_KEEPALIVEFREQUENCY); + } + + /** + * Checks hasBypass on violation only. + * @param player + * @param time + * @param data + * @param cc + * @return If to cancel. + */ + public boolean check(final Player player, final long time, final NetData data, final NetConfig cc) { + data.keepAliveFreq.add(time, 1f); + final float first = data.keepAliveFreq.bucketScore(0); + if (first > 1f && !CheckUtils.hasBypass(CheckType.NET_KEEPALIVEFREQUENCY, player, data)) { + // Trigger a violation. + final double vl = Math.max(first - 1f, data.keepAliveFreq.score(1f) - data.keepAliveFreq.numberOfBuckets()); + if (executeActions(player, vl, 1.0, cc.keepAliveFrequencyActions)) { + return true; + } + } + return false; + } + +}