11package me .ayunami2000 .ayunViaProxyEagUtils ;
22
33import com .google .common .primitives .Ints ;
4+ import com .viaversion .viaversion .util .ChatColorUtil ;
45import io .netty .buffer .ByteBuf ;
56import io .netty .buffer .ByteBufUtil ;
67import io .netty .buffer .Unpooled ;
1415import net .raphimc .vialegacy .protocols .release .protocol1_6_1to1_5_2 .ServerboundPackets1_5_2 ;
1516import net .raphimc .vialegacy .protocols .release .protocol1_7_2_5to1_6_4 .types .Types1_6_4 ;
1617import net .raphimc .vialoader .util .VersionEnum ;
18+ import net .raphimc .viaproxy .proxy .session .LegacyProxyConnection ;
1719import net .raphimc .viaproxy .proxy .session .ProxyConnection ;
1820import net .raphimc .viaproxy .proxy .util .ExceptionUtil ;
1921
20- import java .io .IOException ;
2122import java .nio .charset .StandardCharsets ;
2223import java .util .*;
2324
2425public class EaglerServerHandler extends MessageToMessageCodec <BinaryWebSocketFrame , ByteBuf > {
2526 private final VersionEnum version ;
2627 private final String password ;
2728 private final NetClient proxyConnection ;
28- private final Map <UUID , String > uuidStringMap = new HashMap <>();
2929 private final List <UUID > skinsBeingFetched = new ArrayList <>();
3030 private ByteBuf serverBoundPartialPacket = Unpooled .EMPTY_BUFFER ;
3131 private ByteBuf clientBoundPartialPacket = Unpooled .EMPTY_BUFFER ;
@@ -96,54 +96,18 @@ public void encode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
9696 }
9797
9898 public void encodeOld (ChannelHandlerContext ctx , ByteBuf in , List <Object > out ) {
99- if (handshakeState == 0 ) {
100- handshakeState = 1 ;
101- if (in .readableBytes () >= 2 && in .getUnsignedByte (0 ) == 2 ) {
102- in .setByte (1 , in .getUnsignedByte (1 ) + 8 );
103- }
99+ if (in .readableBytes () >= 2 && in .getUnsignedByte (0 ) == 2 ) {
100+ in .setByte (1 , in .getUnsignedByte (1 ) + 8 );
104101 }
105102 if (in .readableBytes () >= 1 && in .getUnsignedByte (0 ) == 0xFD ) {
106103 return ;
107104 }
108105 if (in .readableBytes () >= 3 && in .getUnsignedByte (0 ) == 250 ) {
109106 in .skipBytes (1 );
110107 String tag ;
111- byte [] msg ;
112108 try {
113109 tag = Types1_6_4 .STRING .read (in );
114110 if (tag .equals ("EAG|Skins-1.8" )) {
115- msg = new byte [in .readShort ()];
116- in .readBytes (msg );
117- if (msg .length == 0 ) {
118- throw new IOException ("Zero-length packet recieved" );
119- }
120- final int packetId = msg [0 ] & 0xFF ;
121- switch (packetId ) {
122- case 3 : {
123- if (msg .length != 17 ) {
124- throw new IOException ("Invalid length " + msg .length + " for skin request packet" );
125- }
126- final UUID searchUUID = SkinPackets .bytesToUUID (msg , 1 );
127- if (uuidStringMap .containsKey (searchUUID )) {
128- // skinsBeingFetched.add(searchUUID);
129- String name = uuidStringMap .get (searchUUID );
130- ByteBuf bb = ctx .alloc ().buffer ();
131- bb .writeByte ((byte ) 250 );
132- Types1_6_4 .STRING .write (bb , "EAG|FetchSkin" ); // todo: get to work
133- bb .writeByte ((byte ) 0 );
134- bb .writeByte ((byte ) 0 );
135- bb .writeBytes (name .getBytes (StandardCharsets .UTF_8 ));
136- out .add (new BinaryWebSocketFrame (bb ));
137- }
138- break ;
139- }
140- case 6 : {
141- break ;
142- }
143- default : {
144- throw new IOException ("Unknown packet type " + packetId );
145- }
146- }
147111 return ;
148112 }
149113 } catch (Exception ignored ) {
@@ -228,7 +192,18 @@ public void decodeOld(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
228192 try {
229193 String name = Types1_6_4 .STRING .read (in );
230194 UUID uuid = UUID .nameUUIDFromBytes (("OfflinePlayer:" + name ).getBytes (StandardCharsets .UTF_8 ));
231- uuidStringMap .put (uuid , name );
195+ skinsBeingFetched .add (uuid );
196+ ByteBuf bb = ctx .alloc ().buffer ();
197+ bb .writeByte ((byte ) 250 );
198+ Types1_6_4 .STRING .write (bb , "EAG|FetchSkin" );
199+ ByteBuf bbb = ctx .alloc ().buffer ();
200+ bbb .writeByte ((byte ) 0 );
201+ bbb .writeByte ((byte ) 0 );
202+ bbb .writeBytes (name .getBytes (StandardCharsets .UTF_8 ));
203+ bb .writeShort (bbb .readableBytes ());
204+ bb .writeBytes (bbb );
205+ bbb .release ();
206+ ctx .writeAndFlush (new BinaryWebSocketFrame (bb ));
232207 } catch (Exception ignored ) {
233208 }
234209 in .resetReaderIndex ();
@@ -240,20 +215,25 @@ public void decodeOld(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
240215 ctx .writeAndFlush (new BinaryWebSocketFrame (in .retain ()));
241216 return ;
242217 }
243- if (! skinsBeingFetched . isEmpty () && in .readableBytes () >= 3 && in .getUnsignedByte (0 ) == 250 ) {
218+ if (in .readableBytes () >= 3 && in .getUnsignedByte (0 ) == 250 ) {
244219 in .skipBytes (1 );
245220 String tag ;
246221 byte [] msg ;
247222 try {
248223 tag = Types1_6_4 .STRING .read (in );
249- // System.out.println(tag);
250224 if (tag .equals ("EAG|UserSkin" )) {
225+ if (skinsBeingFetched .isEmpty ()) {
226+ return ;
227+ }
251228 msg = new byte [in .readShort ()];
252229 in .readBytes (msg );
253- System .out .println (msg .length );
254- byte [] res = new byte [msg .length - 1 ];
230+ if (msg .length < 8192 ) {
231+ return ;
232+ }
233+ // TODO: FIX LOL!!
234+ byte [] res = new byte [msg .length > 16384 ? 16384 : 8192 ];
255235 System .arraycopy (msg , 1 , res , 0 , res .length );
256- if (res .length == 8192 ) {
236+ if (res .length < 16384 ) {
257237 final int [] tmp1 = new int [2048 ];
258238 final int [] tmp2 = new int [4096 ];
259239 for (int i = 0 ; i < tmp1 .length ; ++i ) {
@@ -273,11 +253,27 @@ public void decodeOld(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
273253 res [j ] = tmp3 ;
274254 }
275255 }
276- in .writerIndex (1 );
277- Types1_6_4 .STRING .write (in , "EAG|Skins-1.8" );
256+ ByteBuf bb = ctx .alloc ().buffer ();
257+ bb .writeByte ((byte ) 250 );
258+ Types1_6_4 .STRING .write (bb , "EAG|Skins-1.8" );
278259 byte [] data = SkinPackets .makeCustomResponse (skinsBeingFetched .remove (0 ), 0 , res );
279- in .writeShort (data .length );
280- in .writeBytes (data );
260+ bb .writeShort (data .length );
261+ bb .writeBytes (data );
262+ out .add (bb );
263+ return ;
264+ } else if (tag .equals ("EAG|Reconnect" )) {
265+ msg = new byte [in .readShort ()];
266+ in .readBytes (msg );
267+ in .resetReaderIndex ();
268+ in .resetWriterIndex ();
269+ in .writeByte ((byte ) 0xFF );
270+ Types1_6_4 .STRING .write (in , "Please use the IP: " + ChatColorUtil .COLOR_CHAR + "n" + new String (msg , StandardCharsets .UTF_8 ));
271+ in .resetReaderIndex ();
272+ ctx .fireChannelRead (in .retain ()).close ();
273+ if (!(proxyConnection instanceof ProxyConnection )) {
274+ ((LegacyProxyConnection ) proxyConnection ).getC2P ().close ();
275+ }
276+ return ;
281277 }
282278 } catch (Exception ignored ) {
283279 }
0 commit comments