Advertisement
Guest User

Untitled

a guest
Apr 2nd, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.60 KB | None | 0 0
  1. public class LoginDecoder extends ByteToMessageDecoder {
  2.  
  3. private LoginType type;
  4.  
  5. int size;
  6.  
  7. @Override
  8. protected void decode(ChannelHandlerContext context, ByteBuf buffer, List<Object> out) throws Exception {
  9. if (!buffer.isReadable()) {
  10. throw new IllegalStateException("The data in the buffer could not be read!");
  11. }
  12.  
  13. int loginType = buffer.readUnsignedByte();
  14.  
  15. if (loginType != 16 && loginType != 19 && loginType != 18) {
  16. throw new ProtocolException("Invalid login type received: " + loginType + ".");
  17. }
  18.  
  19. type = loginType == 19 ? LoginType.LOBBY : LoginType.WORLD;
  20.  
  21. size = buffer.readUnsignedShort();
  22.  
  23. if (buffer.readableBytes() < size) {
  24. throw new IllegalStateException("Not enough readable bytes from the buffer!");
  25. }
  26.  
  27. int revision = buffer.readInt();
  28. int subrevision = buffer.readInt();
  29. if (revision != Constants.REVISION && subrevision != Constants.SUB_REVISION) {
  30. throw new IllegalStateException("Invalid client version received: " + revision + ", " + subrevision + ".");
  31. }
  32.  
  33. if (type.equals(LoginType.WORLD)) {
  34. buffer.readByte();
  35. }
  36.  
  37. if (type.equals(LoginType.LOBBY)) {
  38. decodeLobbyPayload(context, buffer, out);
  39. } else if (type.equals(LoginType.WORLD)) {
  40. //decodeWorldPayload(context, buffer, out);
  41. }
  42. }
  43.  
  44. private void decodeLobbyPayload(ChannelHandlerContext context, ByteBuf buffer, List<Object> out) throws ProtocolException {
  45. if (buffer.readableBytes() < 2) {
  46. throw new IllegalStateException("Not enough readable bytes from the buffer!");
  47. }
  48.  
  49. int secureBufferSize = buffer.readUnsignedShort();
  50. if (buffer.readableBytes() < secureBufferSize) {
  51. throw new IllegalStateException("Not enough readable bytes from the buffer!");
  52. }
  53.  
  54. byte[] secureBytes = new byte[secureBufferSize];
  55. buffer.readBytes(secureBytes);
  56. ByteBuf secureBuffer = Unpooled.wrappedBuffer(new BigInteger(secureBytes).modPow(Protocol.LOGIN_EXPONENT, Protocol.LOGIN_MODULUS).toByteArray());
  57.  
  58. int block = secureBuffer.readUnsignedByte();
  59. if (block != 10) {
  60. throw new ProtocolException("Invalid block opcode received: " + block + ".");
  61. }
  62.  
  63. int[] xteaKey = new int [4];
  64. for (int i = 0; i < xteaKey.length; i++) {
  65. xteaKey[i] = secureBuffer.readInt();
  66. }
  67.  
  68. long loginHash = secureBuffer.readLong();
  69. if (loginHash != 0) {
  70. throw new ProtocolException("Invalid login hash received: " + loginHash + ".");
  71. }
  72.  
  73. String password = BufferUtility.readString(secureBuffer);
  74.  
  75. long[] loginSeeds = new long[2];
  76. for (int i = 0; i < loginSeeds.length; i++) {
  77. loginSeeds[i] = secureBuffer.readLong();
  78. }
  79.  
  80. byte[] xteaBlock = new byte[buffer.readableBytes()];
  81. buffer.readBytes(xteaBlock);
  82.  
  83. XTEACipher xtea = new XTEACipher(xteaKey);
  84. xtea.decrypt(xteaBlock, 0, xteaBlock.length);
  85.  
  86. ByteBuf xteaBuffer = Unpooled.wrappedBuffer(xteaBlock);
  87.  
  88. boolean decodeAsString = xteaBuffer.readByte() == 1;
  89. String username = decodeAsString ? BufferUtility.readString(xteaBuffer) : Base37Utility.decodeBase37(xteaBuffer.readLong());
  90.  
  91. xteaBuffer.readUnsignedByte(); // Game identifier.
  92. xteaBuffer.readUnsignedByte(); // Language identifier.
  93. xteaBuffer.readUnsignedByte(); // Frame type (Fixed, Resizable, or Full Screen).
  94.  
  95. xteaBuffer.readUnsignedShort(); // Canvas width.
  96. xteaBuffer.readUnsignedShort(); // Canvas height.
  97. xteaBuffer.readUnsignedByte();
  98.  
  99. int[] randomData = new int[24];
  100. for (int index = 0; index < randomData.length; index++) {
  101. randomData[index] = xteaBuffer.readUnsignedByte();
  102. }
  103.  
  104. String token = BufferUtility.readString(xteaBuffer);
  105. if (token != Protocol.LOGIN_TOKEN) {
  106. throw new ProtocolException("Invalid login token received: " + token + ".");
  107. }
  108.  
  109. int length = xteaBuffer.readUnsignedByte();
  110.  
  111. byte[] machineData = new byte[length];
  112. for (int data = 0; data < machineData.length; data++) {
  113. machineData[data] = (byte) xteaBuffer.readUnsignedByte();
  114. }
  115.  
  116. xteaBuffer.readInt();
  117. BufferUtility.readString(xteaBuffer);
  118. xteaBuffer.readInt();
  119. xteaBuffer.readInt();
  120. BufferUtility.readString(xteaBuffer);
  121.  
  122. xteaBuffer.readUnsignedByte();
  123.  
  124. int[] crc = new int[36];
  125. for (int index = 0; index < crc.length; index++) {
  126. crc[index] = xteaBuffer.readInt();
  127. }
  128.  
  129. int[] serverKeys = new int[xteaKey.length];
  130. for (int i = 0; i < serverKeys.length; i++) {
  131. serverKeys[i] = xteaKey[i] + 50;
  132. }
  133.  
  134. out.add(new LoginRequestMessage(context.channel(), UsernameUtility.formatForProtocol(username), password, username.contains("@"), new ISAACCipher(serverKeys), new ISAACCipher(xteaKey), type));
  135. }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement