Skip to content

Add AntiXray layered obfuscation mode#8799

Merged
kennytv merged 3 commits into
PaperMC:masterfrom
DrexHD:feature/antixray_layer_obfuscation
Feb 9, 2023
Merged

Add AntiXray layered obfuscation mode#8799
kennytv merged 3 commits into
PaperMC:masterfrom
DrexHD:feature/antixray_layer_obfuscation

Conversation

@DrexHD
Copy link
Copy Markdown
Contributor

@DrexHD DrexHD commented Jan 19, 2023

Changes

  • Removes code duplication from obfuscateLayer
  • Adds a 3rd engine mode (layered obfuscation), this mode works the same as engine mode 2, but instead of randomizing every block, only randomizes blocks per chunk layer. This helps a lot with chunk packet compression and can reduce the network load when joining by a factor of ~2 (from very basic testing)

Testing
The tests have been run on a plain new paper server by joining three times in the same spot in spawn chunks. After every test, the server has been rebooted, and these results have shown to be reproducible.
Results: imgur

* Removes code duplication from obfuscateLayer
* Adds a 3rd engine mode (layered obfuscation), this mode works the same as engine mode 2, but instead of randomizing every blocks only randomizes block per chunk layer (https://github.com/DrexHD/AntiXray/blob/1.19.3/stable/media/enginemode-3.png). This helps a lot with chunk packet compression and can reduce the network load when joining by a factor of 2-3 (from very basic testing)
@DrexHD DrexHD requested a review from a team as a code owner January 19, 2023 15:51
@stonar96
Copy link
Copy Markdown
Contributor

  • Removes code duplication from obfuscateLayer

The code duplication is very much intended. You are touching the hottest code in Anti-Xray and with your changes you are introducing lots of branches into that code.

Also your changes can be implemented without even touching the obfuscateLayer method and avoiding the additional engineMode == EngineMode.OBFUSCATE_LAYER ? condition in the hot code. There's a reason why the random object is passed to this method.

I like the idea. The implementation of it should be a matter of changing like 2 or 3 lines tho.

@stonar96
Copy link
Copy Markdown
Contributor

stonar96 commented Jan 19, 2023

This will do it (not tested):

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;

    {
        while ((state = ThreadLocalRandom.current().nextInt()) == 0) ;
    }

    @Override
    public int getAsInt() {
        // https://en.wikipedia.org/wiki/Xorshift
        state ^= state << 13;
        state ^= state >>> 17;
        state ^= state << 5;
        // https://www.pcg-random.org/posts/bounded-rands.html
        return (int) ((Integer.toUnsignedLong(state) * numberOfBlocks) >>> 32);
    }
};

@FunctionalInterface
private static interface LayeredIntSupplier extends IntSupplier {
    default void nextLayer() {

    }
}

Then call random.nextLayer() before each obfuscateLayer(...). No additional branches and even faster than engine-mode: 2.

* Reverts code de-duplication for a slight performance improvement
* Implements layered obfuscation using a separate int supplier
Copy link
Copy Markdown
Contributor

@stonar96 stonar96 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything else looks good to me. Thanks!

Comment thread patches/server/0344-Anti-Xray.patch Outdated
Comment thread patches/server/0344-Anti-Xray.patch Outdated
@kennytv kennytv merged commit 0c9ace8 into PaperMC:master Feb 9, 2023
maestro-denery pushed a commit to maestro-denery/Paper that referenced this pull request Feb 19, 2023
imirochi referenced this pull request in PlazmaMC/PlazmaBukkit Mar 24, 2023
LeonTG pushed a commit to LeonTG/Paper that referenced this pull request May 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants