From 82bf11fcbc7186f2a13ef925952b95200881f24b Mon Sep 17 00:00:00 2001 From: Mats Date: Tue, 10 May 2016 20:53:37 +0200 Subject: [PATCH] Fix anti-xray for 1.9 (#393) * Fix anti-xray for 1.9 * Bye NMS. * Perform * Add config option --- .../us/myles/ViaVersion/ViaVersionPlugin.java | 5 +++++ .../ViaVersion/api/ViaVersionConfig.java | 7 +++++++ .../storage/ClientChunks.java | 20 +++++++++++++++++++ .../myles/ViaVersion/util/ReflectionUtil.java | 7 +++++++ src/main/resources/config.yml | 4 +++- 5 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java b/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java index f7bdb2533..c03a1928d 100644 --- a/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java +++ b/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java @@ -442,6 +442,11 @@ public class ViaVersionPlugin extends JavaPlugin implements ViaVersionAPI, ViaVe return getConfig().getString("tracking-max-kick-msg", "You are sending too many packets, :("); } + @Override + public boolean isAntiXRay() { + return getConfig().getBoolean("anti-xray-patch", true); + } + public boolean isAutoTeam() { // Collision has to be enabled first return isPreventCollision() && getConfig().getBoolean("auto-team", true); diff --git a/src/main/java/us/myles/ViaVersion/api/ViaVersionConfig.java b/src/main/java/us/myles/ViaVersion/api/ViaVersionConfig.java index 5f294077a..4ddd89c14 100644 --- a/src/main/java/us/myles/ViaVersion/api/ViaVersionConfig.java +++ b/src/main/java/us/myles/ViaVersion/api/ViaVersionConfig.java @@ -142,4 +142,11 @@ public interface ViaVersionConfig { * @return Kick message, with colour codes using '&' */ String getMaxWarningsKickMessage(); + + /** + * Is anti-xray enabled? + * + * @return A boolean + */ + boolean isAntiXRay(); } diff --git a/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/storage/ClientChunks.java b/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/storage/ClientChunks.java index 8db47f107..2adcc1ddb 100644 --- a/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/storage/ClientChunks.java +++ b/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/storage/ClientChunks.java @@ -4,10 +4,12 @@ import com.google.common.collect.Lists; import com.google.common.collect.Sets; import lombok.Getter; import org.bukkit.Bukkit; +import us.myles.ViaVersion.api.ViaVersion; import us.myles.ViaVersion.api.data.StoredObject; import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.util.ReflectionUtil; +import java.lang.reflect.Method; import java.util.List; import java.util.Set; import java.util.logging.Level; @@ -17,11 +19,15 @@ public class ClientChunks extends StoredObject { // Reflection private static ReflectionUtil.ClassReflection mapChunkBulkRef; private static ReflectionUtil.ClassReflection mapChunkRef; + private static Method obfuscateRef; + private static Class worldRef; static { try { mapChunkBulkRef = new ReflectionUtil.ClassReflection(ReflectionUtil.nms("PacketPlayOutMapChunkBulk")); mapChunkRef = new ReflectionUtil.ClassReflection(ReflectionUtil.nms("PacketPlayOutMapChunk")); + obfuscateRef = Class.forName("org.spigotmc.AntiXray").getMethod("obfuscate", int.class, int.class, int.class, byte[].class, ReflectionUtil.nms("World")); + worldRef = ReflectionUtil.nms("World"); } catch (Exception e) { Bukkit.getLogger().log(Level.WARNING, "Failed to initialise chunk reflection", e); } @@ -44,6 +50,20 @@ public class ClientChunks extends StoredObject { int[] xcoords = mapChunkBulkRef.getFieldValue("a", packet, int[].class); int[] zcoords = mapChunkBulkRef.getFieldValue("b", packet, int[].class); Object[] chunkMaps = mapChunkBulkRef.getFieldValue("c", packet, Object[].class); + + if (ViaVersion.getConfig().isAntiXRay()) { //Spigot anti-xray patch + Object world = mapChunkBulkRef.getFieldValue("world", packet, Object.class); + + for (int i = 0; i < xcoords.length; ++i) { + Object spigotConfig = ReflectionUtil.getPublic(world, "spigotConfig", Object.class); + Object antiXrayInstance = ReflectionUtil.getPublic(spigotConfig, "antiXrayInstance", Object.class); + + Object b = ReflectionUtil.get(chunkMaps[i], "b", Object.class); + Object a = ReflectionUtil.get(chunkMaps[i], "a", Object.class); + + obfuscateRef.invoke(antiXrayInstance, xcoords[i], zcoords[i], b, a, world); + } + } for (int i = 0; i < chunkMaps.length; i++) { int x = xcoords[i]; int z = zcoords[i]; diff --git a/src/main/java/us/myles/ViaVersion/util/ReflectionUtil.java b/src/main/java/us/myles/ViaVersion/util/ReflectionUtil.java index 9bc115a82..307ae2601 100644 --- a/src/main/java/us/myles/ViaVersion/util/ReflectionUtil.java +++ b/src/main/java/us/myles/ViaVersion/util/ReflectionUtil.java @@ -60,6 +60,13 @@ public class ReflectionUtil { return (T) field.get(o); } + public static T getPublic(Object o, String f, Class t) throws NoSuchFieldException, IllegalAccessException { + Field field = o.getClass().getField(f); + field.setAccessible(true); + return (T) field.get(o); + } + + public static void set(Object o, String f, Object value) throws NoSuchFieldException, IllegalAccessException { Field field = o.getClass().getDeclaredField(f); field.setAccessible(true); diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 5422bbd55..867fa8e04 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -51,4 +51,6 @@ tracking-warning-pps: 120 # How many warnings over the interval can we have # This can never be higher than "tracking-interval" tracking-max-warnings: 4 -tracking-max-kick-msg: "You are sending too many packets, :(" \ No newline at end of file +tracking-max-kick-msg: "You are sending too many packets, :(" +# Patch the Anti xray to work on 1.9 (If your server is 1.8) This can cost more performance, so disable it if you don't use it. +anti-xray-patch: true \ No newline at end of file