| 1 | package com.jellypudding.offlineclient.modules.misc;
|
| 2 |
|
| 3 | import com.jellypudding.offlineclient.event.Subscribe;
|
| 4 | import com.jellypudding.offlineclient.event.events.ClientTickEvent;
|
| 5 | import com.jellypudding.offlineclient.event.events.PacketSendEvent;
|
| 6 | import com.jellypudding.offlineclient.module.Category;
|
| 7 | import com.jellypudding.offlineclient.module.Module;
|
| 8 | import com.jellypudding.offlineclient.setting.BoolSetting;
|
| 9 | import com.jellypudding.offlineclient.setting.NumberSetting;
|
| 10 | import com.jellypudding.offlineclient.util.ChatUtil;
|
| 11 | import net.minecraft.client.multiplayer.ClientPacketListener;
|
| 12 | import net.minecraft.network.protocol.Packet;
|
| 13 | import net.minecraft.network.protocol.common.ServerboundClientInformationPacket;
|
| 14 | import net.minecraft.network.protocol.common.ServerboundCustomPayloadPacket;
|
| 15 | import net.minecraft.network.protocol.common.ServerboundKeepAlivePacket;
|
| 16 | import net.minecraft.network.protocol.common.ServerboundPongPacket;
|
| 17 | import net.minecraft.network.protocol.common.ServerboundResourcePackPacket;
|
| 18 | import net.minecraft.network.protocol.game.ServerboundAcceptTeleportationPacket;
|
| 19 | import net.minecraft.network.protocol.game.ServerboundChatAckPacket;
|
| 20 | import net.minecraft.network.protocol.game.ServerboundChatCommandPacket;
|
| 21 | import net.minecraft.network.protocol.game.ServerboundChatCommandSignedPacket;
|
| 22 | import net.minecraft.network.protocol.game.ServerboundChatPacket;
|
| 23 | import net.minecraft.network.protocol.game.ServerboundChunkBatchReceivedPacket;
|
| 24 | import net.minecraft.network.protocol.game.ServerboundClientCommandPacket;
|
| 25 | import net.minecraft.network.protocol.game.ServerboundClientTickEndPacket;
|
| 26 | import net.minecraft.network.protocol.game.ServerboundContainerClosePacket;
|
| 27 | import net.minecraft.network.protocol.game.ServerboundMovePlayerPacket;
|
| 28 | import net.minecraft.network.protocol.game.ServerboundMoveVehiclePacket;
|
| 29 | import net.minecraft.network.protocol.game.ServerboundPlayerLoadedPacket;
|
| 30 |
|
| 31 | import java.util.concurrent.ConcurrentLinkedQueue;
|
| 32 | import java.util.concurrent.atomic.AtomicInteger;
|
| 33 |
|
| 34 | // Paces outgoing packets. Anything the connection depends on is let straight through.
|
| 35 | public final class AntiPacketKick extends Module {
|
| 36 |
|
| 37 | private static final long WINDOW_MILLIS = 1000;
|
| 38 |
|
| 39 | private final NumberSetting limit = new NumberSetting("Limit",
|
| 40 | "Packets allowed out each second before the rest wait.", 300, 50, 1000, 10, " per second")
|
| 41 | .min(20);
|
| 42 | private final NumberSetting queueSize = new NumberSetting("Queue",
|
| 43 | "Most packets held back at once.",
|
| 44 | 200, 20, 1000, 10, " packets").min(1);
|
| 45 | private final BoolSetting notify = new BoolSetting("Notify",
|
| 46 | "Say in chat the first time a burst gets paced.", true);
|
| 47 | private final BoolSetting acceptHuge = new BoolSetting("Accept huge packets",
|
| 48 | "Reads incoming packets of any size instead of dropping the connection.", true);
|
| 49 | private final BoolSetting catchErrors = new BoolSetting("Catch errors",
|
| 50 | "Throws away a packet the client cannot read instead of disconnecting.", false);
|
| 51 | private final BoolSetting logErrors = new BoolSetting("Log errors",
|
| 52 | "Prints each caught error to chat.", true)
|
| 53 | .under(catchErrors);
|
| 54 |
|
| 55 | // Filled from the netty thread and drained on the main thread.
|
| 56 | private final ConcurrentLinkedQueue<Packet<?>> held = new ConcurrentLinkedQueue<>();
|
| 57 | private final AtomicInteger heldCount = new AtomicInteger();
|
| 58 | private final AtomicInteger sent = new AtomicInteger();
|
| 59 |
|
| 60 | private long windowStart;
|
| 61 | private int rate;
|
| 62 | private boolean warned;
|
| 63 | // The thread draining the queue. Only its own sends skip the cap.
|
| 64 | private volatile Thread drainer;
|
| 65 |
|
| 66 | public AntiPacketKick() {
|
| 67 | super("AntiPacketKick", "Spreads packet bursts out and keeps the server from dropping you.",
|
| 68 | Category.MISC);
|
| 69 | addSettings(limit, queueSize, notify, acceptHuge, catchErrors, logErrors);
|
| 70 | searchTags("flood", "throttle", "rate limit");
|
| 71 | }
|
| 72 |
|
| 73 | // Read from the netty thread whilst a packet is being decoded.
|
| 74 | public boolean acceptsHugePackets() {
|
| 75 | return isEnabled() && acceptHuge.isOn();
|
| 76 | }
|
| 77 |
|
| 78 | public boolean catchesErrors() {
|
| 79 | return isEnabled() && catchErrors.isOn();
|
| 80 | }
|
| 81 |
|
| 82 | public boolean logsErrors() {
|
| 83 | return logErrors.isOn();
|
| 84 | }
|
| 85 |
|
| 86 | @Override
|
| 87 | public String getSuffix() {
|
| 88 | int waiting = heldCount.get();
|
| 89 | return waiting > 0 ? rate + "/s " + waiting + " held" : rate + "/s";
|
| 90 | }
|
| 91 |
|
| 92 | @Override
|
| 93 | protected void onEnable() {
|
| 94 | held.clear();
|
| 95 | heldCount.set(0);
|
| 96 | sent.set(0);
|
| 97 | windowStart = System.currentTimeMillis();
|
| 98 | rate = 0;
|
| 99 | warned = false;
|
| 100 | }
|
| 101 |
|
| 102 | @Override
|
| 103 | protected void onDisable() {
|
| 104 | release(Integer.MAX_VALUE);
|
| 105 | held.clear();
|
| 106 | heldCount.set(0);
|
| 107 | }
|
| 108 |
|
| 109 | @Subscribe(priority = -100)
|
| 110 | private void onPacketSend(PacketSendEvent event) {
|
| 111 | if (Thread.currentThread() == drainer || event.isCancelled()) {
|
| 112 | return;
|
| 113 | }
|
| 114 | Packet<?> packet = event.getPacket();
|
| 115 | if (isCritical(packet)) {
|
| 116 | sent.incrementAndGet();
|
| 117 | return;
|
| 118 | }
|
| 119 | if (sent.get() < limit.getInt() && heldCount.get() == 0) {
|
| 120 | sent.incrementAndGet();
|
| 121 | return;
|
| 122 | }
|
| 123 | event.cancel();
|
| 124 | held.add(packet);
|
| 125 | if (heldCount.incrementAndGet() > queueSize.getInt() && held.poll() != null) {
|
| 126 | heldCount.decrementAndGet();
|
| 127 | }
|
| 128 | }
|
| 129 |
|
| 130 | @Subscribe
|
| 131 | private void onClientTick(ClientTickEvent event) {
|
| 132 | long now = System.currentTimeMillis();
|
| 133 | if (now - windowStart >= WINDOW_MILLIS) {
|
| 134 | windowStart = now;
|
| 135 | rate = sent.getAndSet(0);
|
| 136 | }
|
| 137 | int waiting = heldCount.get();
|
| 138 | if (waiting == 0) {
|
| 139 | return;
|
| 140 | }
|
| 141 | if (notify.isOn() && !warned) {
|
| 142 | warned = true;
|
| 143 | ChatUtil.message("§eHolding packets back to stay under " + limit.getInt() + " per second.");
|
| 144 | }
|
| 145 | release(limit.getInt() - sent.get());
|
| 146 | }
|
| 147 |
|
| 148 | private void release(int allowance) {
|
| 149 | if (allowance <= 0 || mc.player == null) {
|
| 150 | return;
|
| 151 | }
|
| 152 | ClientPacketListener connection = mc.player.connection;
|
| 153 | drainer = Thread.currentThread();
|
| 154 | try {
|
| 155 | for (int i = 0; i < allowance; i++) {
|
| 156 | Packet<?> packet = held.poll();
|
| 157 | if (packet == null) {
|
| 158 | break;
|
| 159 | }
|
| 160 | heldCount.decrementAndGet();
|
| 161 | connection.send(packet);
|
| 162 | sent.incrementAndGet();
|
| 163 | }
|
| 164 | } finally {
|
| 165 | drainer = null;
|
| 166 | }
|
| 167 | }
|
| 168 |
|
| 169 | private static boolean isCritical(Packet<?> packet) {
|
| 170 | return packet instanceof ServerboundKeepAlivePacket
|
| 171 | || packet instanceof ServerboundPongPacket
|
| 172 | || packet instanceof ServerboundAcceptTeleportationPacket
|
| 173 | || packet instanceof ServerboundMovePlayerPacket
|
| 174 | || packet instanceof ServerboundMoveVehiclePacket
|
| 175 | || packet instanceof ServerboundClientTickEndPacket
|
| 176 | || packet instanceof ServerboundChunkBatchReceivedPacket
|
| 177 | || packet instanceof ServerboundPlayerLoadedPacket
|
| 178 | || packet instanceof ServerboundClientCommandPacket
|
| 179 | || packet instanceof ServerboundClientInformationPacket
|
| 180 | || packet instanceof ServerboundContainerClosePacket
|
| 181 | || packet instanceof ServerboundResourcePackPacket
|
| 182 | || packet instanceof ServerboundCustomPayloadPacket
|
| 183 | || packet instanceof ServerboundChatPacket
|
| 184 | || packet instanceof ServerboundChatAckPacket
|
| 185 | || packet instanceof ServerboundChatCommandPacket
|
| 186 | || packet instanceof ServerboundChatCommandSignedPacket;
|
| 187 | }
|
| 188 | }
|
| 189 |
|