forked from SteamWar/SteamWar
Normalize block entity IDs and handle bundled packets
- Normalize hidden block entity identifiers across versions - Add block break and tile entity handling for newer protocol versions - Preserve packet filtering through bundle packets and fix chunk section copying
This commit is contained in:
@@ -23,6 +23,7 @@ import de.steamwar.Reflection;
|
||||
import de.steamwar.Reflection.Field;
|
||||
import de.steamwar.core.Core;
|
||||
import io.netty.channel.*;
|
||||
import io.netty.util.ReferenceCountUtil;
|
||||
import lombok.Getter;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
@@ -37,6 +38,7 @@ import org.bukkit.plugin.Plugin;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.logging.Level;
|
||||
|
||||
@@ -57,17 +59,29 @@ public class TinyProtocol implements Listener {
|
||||
public static final Field<List> networkManagers = Reflection.getField(serverConnection, List.class, 0, networkManager);
|
||||
|
||||
private static final String HANDLER_NAME = "tiny-steamwar";
|
||||
private static final Class<?> bundlePacket = getOptionalClass("net.minecraft.network.protocol.BundlePacket");
|
||||
private static final Class<?> clientboundBundlePacket = getOptionalClass("net.minecraft.network.protocol.game.ClientboundBundlePacket");
|
||||
private static final Reflection.Method bundleSubPackets = bundlePacket == null ? null : Reflection.getTypedMethod(bundlePacket, null, Iterable.class);
|
||||
private static final Reflection.Constructor clientboundBundleConstructor = clientboundBundlePacket == null ? null : Reflection.getConstructor(clientboundBundlePacket, Iterable.class);
|
||||
public static final TinyProtocol instance = new TinyProtocol(Core.getInstance());
|
||||
|
||||
public static void init() {
|
||||
//enforce init
|
||||
}
|
||||
|
||||
private static Class<?> getOptionalClass(String className) {
|
||||
try {
|
||||
return Reflection.getClass(className);
|
||||
} catch (IllegalArgumentException ignored) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private final Plugin plugin;
|
||||
private final List<?> connections;
|
||||
private boolean closed;
|
||||
|
||||
private final Map<Class<?>, List<BiFunction<Player, Object, Object>>> packetFilters = new HashMap<>();
|
||||
private final Map<Class<?>, List<BiFunction<Player, Object, Object>>> packetFilters = new ConcurrentHashMap<>();
|
||||
@Getter
|
||||
private final Map<Player, PacketInterceptor> playerInterceptors = new HashMap<>();
|
||||
|
||||
@@ -207,31 +221,51 @@ public class TinyProtocol implements Listener {
|
||||
|
||||
@Override
|
||||
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
||||
Object originalMsg = msg;
|
||||
try {
|
||||
msg = filterPacket(player, msg);
|
||||
} catch (Exception e) {
|
||||
plugin.getLogger().log(Level.SEVERE, "Error during incoming packet processing", e);
|
||||
msg = null;
|
||||
}
|
||||
|
||||
if (msg != null) {
|
||||
super.channelRead(ctx, msg);
|
||||
} else {
|
||||
ReferenceCountUtil.release(originalMsg);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
|
||||
Object originalMsg = msg;
|
||||
try {
|
||||
msg = filterPacket(player, msg);
|
||||
} catch (Exception e) {
|
||||
plugin.getLogger().log(Level.SEVERE, "Error during outgoing packet processing", e);
|
||||
msg = null;
|
||||
}
|
||||
|
||||
if (msg != null) {
|
||||
super.write(ctx, msg, promise);
|
||||
} else {
|
||||
ReferenceCountUtil.release(originalMsg);
|
||||
promise.trySuccess();
|
||||
}
|
||||
}
|
||||
|
||||
private Object filterPacket(Player player, Object packet) {
|
||||
packet = filterSinglePacket(player, packet);
|
||||
if(packet == null)
|
||||
return null;
|
||||
|
||||
if(clientboundBundlePacket != null && clientboundBundlePacket.isInstance(packet))
|
||||
return filterBundlePacket(player, packet);
|
||||
|
||||
return packet;
|
||||
}
|
||||
|
||||
private Object filterSinglePacket(Player player, Object packet) {
|
||||
List<BiFunction<Player, Object, Object>> filters = packetFilters.getOrDefault(packet.getClass(), Collections.emptyList());
|
||||
|
||||
for(BiFunction<Player, Object, Object> filter : filters) {
|
||||
@@ -243,5 +277,33 @@ public class TinyProtocol implements Listener {
|
||||
|
||||
return packet;
|
||||
}
|
||||
|
||||
private Object filterBundlePacket(Player player, Object packet) {
|
||||
if(bundleSubPackets == null || clientboundBundleConstructor == null)
|
||||
throw new IllegalStateException("Cannot filter bundled packet " + packet.getClass().getName());
|
||||
|
||||
ArrayList<Object> filteredPackets = new ArrayList<>();
|
||||
boolean changed = false;
|
||||
for(Object subPacket : (Iterable<?>) bundleSubPackets.invoke(packet)) {
|
||||
Object filteredPacket = filterPacket(player, subPacket);
|
||||
if(filteredPacket == null) {
|
||||
changed = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if(filteredPacket != subPacket)
|
||||
changed = true;
|
||||
|
||||
filteredPackets.add(filteredPacket);
|
||||
}
|
||||
|
||||
if(!changed)
|
||||
return packet;
|
||||
|
||||
if(filteredPackets.isEmpty())
|
||||
return null;
|
||||
|
||||
return clientboundBundleConstructor.invoke(filteredPackets);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -92,7 +92,17 @@ public interface ChunkHider {
|
||||
}
|
||||
|
||||
public void copyBitsPerBlock() {
|
||||
bitsPerBlock = in.readByte();
|
||||
bitsPerBlock = in.readUnsignedByte();
|
||||
out.writeByte(bitsPerBlock);
|
||||
}
|
||||
|
||||
public int readBitsPerBlock() {
|
||||
bitsPerBlock = in.readUnsignedByte();
|
||||
return bitsPerBlock;
|
||||
}
|
||||
|
||||
public void writeBitsPerBlock(int bitsPerBlock) {
|
||||
this.bitsPerBlock = bitsPerBlock;
|
||||
out.writeByte(bitsPerBlock);
|
||||
}
|
||||
|
||||
@@ -108,6 +118,15 @@ public interface ChunkHider {
|
||||
copyVarInt();
|
||||
}
|
||||
|
||||
public int[] readPalette() {
|
||||
int paletteLength = ProtocolUtils.readVarInt(in);
|
||||
int[] palette = new int[paletteLength];
|
||||
for(int i = 0; i < paletteLength; i++)
|
||||
palette[i] = ProtocolUtils.readVarInt(in);
|
||||
|
||||
return palette;
|
||||
}
|
||||
|
||||
public void processPalette() {
|
||||
if(skipSection) {
|
||||
skipPalette();
|
||||
|
||||
@@ -98,10 +98,7 @@ public class ProtocolUtils {
|
||||
}
|
||||
|
||||
public static int posToChunk(int c){
|
||||
int chunk = c / 16;
|
||||
if(c < 0)
|
||||
chunk--;
|
||||
return chunk;
|
||||
return Math.floorDiv(c, 16);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
|
||||
@@ -31,6 +31,10 @@ public interface ProtocolWrapper {
|
||||
|
||||
boolean unfilteredTileEntityDataAction(Object packet);
|
||||
|
||||
default String tileEntityDataType(Object packet) {
|
||||
return null;
|
||||
}
|
||||
|
||||
BiFunction<Player, Object, Object> blockBreakHiderGenerator(Class<?> blockBreakPacket, TechHider techHider);
|
||||
|
||||
BiFunction<Player, Object, Object> multiBlockChangeGenerator(TechHider techHider);
|
||||
|
||||
@@ -27,6 +27,7 @@ import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.function.BiFunction;
|
||||
@@ -67,11 +68,12 @@ public class TechHider {
|
||||
private final Set<Integer> obfuscateIds;
|
||||
@Getter
|
||||
private final Set<String> hiddenBlockEntities;
|
||||
private boolean enabled;
|
||||
|
||||
public TechHider(LocationEvaluator locationEvaluator, Material obfuscationTarget, Set<Material> obfuscate, Set<String> hiddenBlockEntities) {
|
||||
this.locationEvaluator = locationEvaluator;
|
||||
this.obfuscateIds = obfuscate.stream().flatMap(m -> BlockIds.impl.materialToAllIds(m).stream()).collect(Collectors.toSet());
|
||||
this.hiddenBlockEntities = hiddenBlockEntities;
|
||||
this.hiddenBlockEntities = hiddenBlockEntities.stream().map(TechHider::normalizeBlockEntityId).collect(Collectors.toSet());
|
||||
this.obfuscationTarget = getBlockDataByBlock.invoke(getBlockByMaterial.invoke(null, obfuscationTarget));
|
||||
this.obfuscationTargetId = BlockIds.impl.materialToId(obfuscationTarget);
|
||||
|
||||
@@ -81,9 +83,11 @@ public class TechHider {
|
||||
techhiders.put(multiBlockChangePacket, ProtocolWrapper.impl.multiBlockChangeGenerator(this));
|
||||
techhiders.put(ChunkHider.impl.mapChunkPacket(), ChunkHider.impl.chunkHiderGenerator(this));
|
||||
|
||||
if(Core.getVersion() > 12 && Core.getVersion() < 19) {
|
||||
if(Core.getVersion() > 12) {
|
||||
Class<?> blockBreakClass = Reflection.getClass("net.minecraft.network.protocol.game.ClientboundBlockDestructionPacket");
|
||||
techhiders.put(blockBreakClass, ProtocolWrapper.impl.blockBreakHiderGenerator(blockBreakClass, this));
|
||||
BiFunction<Player, Object, Object> blockBreakHider = ProtocolWrapper.impl.blockBreakHiderGenerator(blockBreakClass, this);
|
||||
if(blockBreakHider != null)
|
||||
techhiders.put(blockBreakClass, blockBreakHider);
|
||||
}
|
||||
|
||||
if(Core.getVersion() > 8){
|
||||
@@ -94,10 +98,18 @@ public class TechHider {
|
||||
}
|
||||
|
||||
public void enable() {
|
||||
if(enabled)
|
||||
return;
|
||||
|
||||
enabled = true;
|
||||
techhiders.forEach(TinyProtocol.instance::addFilter);
|
||||
}
|
||||
|
||||
public void disable() {
|
||||
if(!enabled)
|
||||
return;
|
||||
|
||||
enabled = false;
|
||||
techhiders.forEach(TinyProtocol.instance::removeFilter);
|
||||
}
|
||||
|
||||
@@ -142,6 +154,8 @@ public class TechHider {
|
||||
case SKIP:
|
||||
return packet;
|
||||
case CHECK:
|
||||
if(isHiddenBlockEntity(ProtocolWrapper.impl.tileEntityDataType(packet)))
|
||||
return null;
|
||||
if(ProtocolWrapper.impl.unfilteredTileEntityDataAction(packet))
|
||||
return packet;
|
||||
default:
|
||||
@@ -149,6 +163,19 @@ public class TechHider {
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isHiddenBlockEntity(String id) {
|
||||
return hiddenBlockEntities.contains(normalizeBlockEntityId(id));
|
||||
}
|
||||
|
||||
public static String normalizeBlockEntityId(String id) {
|
||||
if(id == null)
|
||||
return "";
|
||||
|
||||
id = id.toLowerCase(Locale.ROOT);
|
||||
int namespaceSeparator = id.indexOf(':');
|
||||
return namespaceSeparator < 0 ? id : id.substring(namespaceSeparator + 1);
|
||||
}
|
||||
|
||||
public enum State {
|
||||
SKIP,
|
||||
CHECK,
|
||||
|
||||
Reference in New Issue
Block a user