From 905b16a361058afb1880ab55bf5b33d8b33a5653 Mon Sep 17 00:00:00 2001 From: Drex Date: Thu, 9 Feb 2023 18:38:00 +0100 Subject: [PATCH] Add AntiXray layered obfuscation mode (#8799) --- patches/server/Anti-Xray.patch | 36 ++++++++++++++++++++++++- patches/server/Paper-config-files.patch | 2 +- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/patches/server/Anti-Xray.patch b/patches/server/Anti-Xray.patch index 3bfb1f193e..8072a8e774 100644 --- a/patches/server/Anti-Xray.patch +++ b/patches/server/Anti-Xray.patch @@ -428,7 +428,31 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + bitStorageWriter.setBuffer(chunkPacketInfoAntiXray.getBuffer()); + int numberOfBlocks = presetBlockStateBits.length; + // Keep the lambda expressions as simple as possible. They are used very frequently. -+ IntSupplier random = numberOfBlocks == 1 ? (() -> 0) : new IntSupplier() { ++ LayeredIntSupplier random = numberOfBlocks == 1 ? (() -> 0) : engineMode == EngineMode.OBFUSCATE_LAYER ? new LayeredIntSupplier() { ++ // engine-mode: 3 ++ private int state; ++ private int next; ++ ++ { ++ while ((state = ThreadLocalRandom.current().nextInt()) == 0) ; ++ } ++ ++ @Override ++ public void nextLayer() { ++ // https://en.wikipedia.org/wiki/Xorshift ++ state ^= state << 13; ++ state ^= state >>> 17; ++ state ^= state << 5; ++ // https://www.pcg-random.org/posts/bounded-rands.html ++ next = (int) ((Integer.toUnsignedLong(state) * numberOfBlocks) >>> 32); ++ } ++ ++ @Override ++ public int getAsInt() { ++ return next; ++ } ++ } : new LayeredIntSupplier() { ++ // engine-mode: 2 + private int state; + + { @@ -509,6 +533,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + current = next; + next = nextNext; + nextNext = temp; ++ random.nextLayer(); + obfuscateLayer(y, bitStorageReader, bitStorageWriter, solidTemp, obfuscateTemp, presetBlockStateBitsTemp, current, next, nextNext, nearbyChunkSections, random); + } + @@ -534,6 +559,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + // There is nothing to read anymore + bitStorageReader.setBits(0); + solid[0] = true; ++ random.nextLayer(); + obfuscateLayer(15, bitStorageReader, bitStorageWriter, solid, obfuscateTemp, presetBlockStateBitsTemp, current, next, nextNext, nearbyChunkSections, random); + } + } else { @@ -546,6 +572,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + current = next; + next = nextNext; + nextNext = temp; ++ random.nextLayer(); + obfuscateLayer(15, bitStorageReader, bitStorageWriter, solidTemp, obfuscateTemp, presetBlockStateBitsTemp, current, next, nextNext, nearbyChunkSections, random); + } + @@ -838,6 +865,13 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + ((ServerLevel) level).getChunkSource().blockChanged(blockPos); + } + } ++ ++ @FunctionalInterface ++ private interface LayeredIntSupplier extends IntSupplier { ++ default void nextLayer() { ++ ++ } ++ } +} diff --git a/src/main/java/com/destroystokyo/paper/antixray/ChunkPacketInfo.java b/src/main/java/com/destroystokyo/paper/antixray/ChunkPacketInfo.java new file mode 100644 diff --git a/patches/server/Paper-config-files.patch b/patches/server/Paper-config-files.patch index c77b5f8c8e..95b2204dba 100644 --- a/patches/server/Paper-config-files.patch +++ b/patches/server/Paper-config-files.patch @@ -4013,7 +4013,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + +public enum EngineMode { + -+ HIDE(1, "hide ores"), OBFUSCATE(2, "obfuscate"); ++ HIDE(1, "hide ores"), OBFUSCATE(2, "obfuscate"), OBFUSCATE_LAYER(3, "obfuscate layer"); + + public static final ScalarSerializer SERIALIZER = new EngineModeSerializer(); +