|
| 1 | +package me.ayunami2000.ayunViaProxyEagUtils; |
| 2 | + |
| 3 | +import io.netty.buffer.ByteBuf; |
| 4 | +import io.netty.channel.ChannelHandlerContext; |
| 5 | +import io.netty.channel.ChannelInboundHandlerAdapter; |
| 6 | +import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame; |
| 7 | +import net.raphimc.vialegacy.protocols.release.protocol1_7_2_5to1_6_4.types.Types1_6_4; |
| 8 | +import net.raphimc.viaproxy.proxy.util.ExceptionUtil; |
| 9 | + |
| 10 | +import java.io.ByteArrayOutputStream; |
| 11 | +import java.io.DataOutputStream; |
| 12 | +import java.io.IOException; |
| 13 | +import java.nio.charset.StandardCharsets; |
| 14 | +import java.util.HashMap; |
| 15 | +import java.util.UUID; |
| 16 | +import java.util.concurrent.ConcurrentHashMap; |
| 17 | + |
| 18 | +public class EaglerSkinHandler extends ChannelInboundHandlerAdapter { |
| 19 | + public static final ConcurrentHashMap<UUID, byte[]> skinCollection; |
| 20 | + private static final ConcurrentHashMap<UUID, byte[]> capeCollection; |
| 21 | + private static final ConcurrentHashMap<UUID, Long> lastSkinLayerUpdate; |
| 22 | + private static final int[] SKIN_DATA_SIZE; |
| 23 | + private static final int[] CAPE_DATA_SIZE; |
| 24 | + private static final ConcurrentHashMap<UUID, ChannelHandlerContext> users; |
| 25 | + private final String user; |
| 26 | + |
| 27 | + private static void sendData(final ChannelHandlerContext ctx, final String channel, final byte[] data) throws IOException { |
| 28 | + final ByteBuf bb = ctx.alloc().buffer(); |
| 29 | + bb.writeByte(250); |
| 30 | + try { |
| 31 | + Types1_6_4.STRING.write(bb, channel); |
| 32 | + } catch (Exception e) { |
| 33 | + throw new IOException(e); |
| 34 | + } |
| 35 | + bb.writeShort(data.length); |
| 36 | + bb.writeBytes(data); |
| 37 | + ctx.writeAndFlush(new BinaryWebSocketFrame(bb)); |
| 38 | + } |
| 39 | + |
| 40 | + public EaglerSkinHandler(final String username) { |
| 41 | + this.user = username; |
| 42 | + } |
| 43 | + |
| 44 | + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { |
| 45 | + ExceptionUtil.handleNettyException(ctx, cause, null); |
| 46 | + } |
| 47 | + |
| 48 | + public void channelRead(final ChannelHandlerContext ctx, final Object obj) throws Exception { |
| 49 | + final UUID uuid = UUID.nameUUIDFromBytes(("OfflinePlayer:" + this.user).getBytes(StandardCharsets.UTF_8)); |
| 50 | + if (!EaglerSkinHandler.users.containsKey(uuid) && ctx.channel().isActive()) { |
| 51 | + EaglerSkinHandler.users.put(uuid, ctx); |
| 52 | + } |
| 53 | + if (obj instanceof BinaryWebSocketFrame) { |
| 54 | + final ByteBuf bb = ((BinaryWebSocketFrame) obj).content(); |
| 55 | + if (bb.readableBytes() >= 3 && bb.readByte() == -6) { |
| 56 | + String tag; |
| 57 | + byte[] msg; |
| 58 | + try { |
| 59 | + tag = Types1_6_4.STRING.read(bb); |
| 60 | + msg = new byte[bb.readShort()]; |
| 61 | + bb.readBytes(msg); |
| 62 | + } catch (Exception e) { |
| 63 | + bb.resetReaderIndex(); |
| 64 | + super.channelRead(ctx, obj); |
| 65 | + return; |
| 66 | + } |
| 67 | + try { |
| 68 | + if ("EAG|MySkin".equals(tag)) { |
| 69 | + if (!EaglerSkinHandler.skinCollection.containsKey(uuid)) { |
| 70 | + final int t = msg[0] & 0xFF; |
| 71 | + if (t < EaglerSkinHandler.SKIN_DATA_SIZE.length && msg.length == EaglerSkinHandler.SKIN_DATA_SIZE[t] + 1) { |
| 72 | + EaglerSkinHandler.skinCollection.put(uuid, msg); |
| 73 | + } |
| 74 | + } |
| 75 | + bb.release(); |
| 76 | + return; |
| 77 | + } |
| 78 | + if ("EAG|MyCape".equals(tag)) { |
| 79 | + if (!EaglerSkinHandler.capeCollection.containsKey(uuid)) { |
| 80 | + final int t = msg[0] & 0xFF; |
| 81 | + if (t < EaglerSkinHandler.CAPE_DATA_SIZE.length && msg.length == EaglerSkinHandler.CAPE_DATA_SIZE[t] + 2) { |
| 82 | + EaglerSkinHandler.capeCollection.put(uuid, msg); |
| 83 | + } |
| 84 | + } |
| 85 | + bb.release(); |
| 86 | + return; |
| 87 | + } |
| 88 | + if ("EAG|FetchSkin".equals(tag)) { |
| 89 | + if (msg.length > 2) { |
| 90 | + final String fetch = new String(msg, 2, msg.length - 2, StandardCharsets.UTF_8); |
| 91 | + final UUID uuidFetch = UUID.nameUUIDFromBytes(("OfflinePlayer:" + fetch).getBytes(StandardCharsets.UTF_8)); |
| 92 | + byte[] data; |
| 93 | + if ((data = EaglerSkinHandler.skinCollection.get(uuidFetch)) != null) { |
| 94 | + byte[] conc = new byte[data.length + 2]; |
| 95 | + conc[0] = msg[0]; |
| 96 | + conc[1] = msg[1]; |
| 97 | + System.arraycopy(data, 0, conc, 2, data.length); |
| 98 | + if ((data = EaglerSkinHandler.capeCollection.get(uuidFetch)) != null) { |
| 99 | + final byte[] conc2 = new byte[conc.length + data.length]; |
| 100 | + System.arraycopy(conc, 0, conc2, 0, conc.length); |
| 101 | + System.arraycopy(data, 0, conc2, conc.length, data.length); |
| 102 | + conc = conc2; |
| 103 | + } |
| 104 | + sendData(ctx, "EAG|UserSkin", conc); |
| 105 | + } |
| 106 | + } |
| 107 | + bb.release(); |
| 108 | + return; |
| 109 | + } |
| 110 | + if ("EAG|SkinLayers".equals(tag)) { |
| 111 | + final long millis = System.currentTimeMillis(); |
| 112 | + final Long lsu = EaglerSkinHandler.lastSkinLayerUpdate.get(uuid); |
| 113 | + if (lsu != null && millis - lsu < 700L) { |
| 114 | + return; |
| 115 | + } |
| 116 | + EaglerSkinHandler.lastSkinLayerUpdate.put(uuid, millis); |
| 117 | + byte[] conc2; |
| 118 | + if ((conc2 = EaglerSkinHandler.capeCollection.get(uuid)) != null) { |
| 119 | + conc2[1] = msg[0]; |
| 120 | + } else { |
| 121 | + conc2 = new byte[]{2, msg[0], 0}; |
| 122 | + EaglerSkinHandler.capeCollection.put(uuid, conc2); |
| 123 | + } |
| 124 | + final ByteArrayOutputStream bao = new ByteArrayOutputStream(); |
| 125 | + final DataOutputStream dd = new DataOutputStream(bao); |
| 126 | + dd.write(msg[0]); |
| 127 | + dd.writeUTF(this.user); |
| 128 | + final byte[] bpacket = bao.toByteArray(); |
| 129 | + for (final UUID pl : EaglerSkinHandler.users.keySet()) { |
| 130 | + if (!pl.equals(uuid)) { |
| 131 | + sendData(EaglerSkinHandler.users.get(pl), "EAG|SkinLayers", bpacket); |
| 132 | + } |
| 133 | + } |
| 134 | + bb.release(); |
| 135 | + return; |
| 136 | + } |
| 137 | + } catch (Throwable var18) { |
| 138 | + var18.printStackTrace(); |
| 139 | + } |
| 140 | + } |
| 141 | + bb.resetReaderIndex(); |
| 142 | + } |
| 143 | + super.channelRead(ctx, obj); |
| 144 | + } |
| 145 | + |
| 146 | + public void channelInactive(final ChannelHandlerContext ctx) throws Exception { |
| 147 | + super.channelInactive(ctx); |
| 148 | + final UUID uuid = UUID.nameUUIDFromBytes(("OfflinePlayer:" + this.user).getBytes(StandardCharsets.UTF_8)); |
| 149 | + EaglerSkinHandler.users.remove(uuid); |
| 150 | + EaglerSkinHandler.skinCollection.remove(uuid); |
| 151 | + EaglerSkinHandler.capeCollection.remove(uuid); |
| 152 | + EaglerSkinHandler.lastSkinLayerUpdate.remove(uuid); |
| 153 | + } |
| 154 | + |
| 155 | + static { |
| 156 | + skinCollection = new ConcurrentHashMap<>(); |
| 157 | + capeCollection = new ConcurrentHashMap<>(); |
| 158 | + lastSkinLayerUpdate = new ConcurrentHashMap<>(); |
| 159 | + SKIN_DATA_SIZE = new int[]{8192, 16384, -9, -9, 1, 16384, -9}; |
| 160 | + CAPE_DATA_SIZE = new int[]{4096, -9, 1}; |
| 161 | + users = new ConcurrentHashMap<>(); |
| 162 | + } |
| 163 | +} |
0 commit comments