Advertisement
Guest User

Untitled

a guest
Oct 2nd, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.35 KB | None | 0 0
  1. package net.netty;
  2.  
  3. import io.LoadRequest;
  4. import net.ISAACCipher;
  5. import net.packet.Packet;
  6.  
  7. import org.jboss.netty.buffer.ChannelBuffer;
  8. import org.jboss.netty.buffer.ChannelBuffers;
  9. import org.jboss.netty.channel.Channel;
  10. import org.jboss.netty.channel.ChannelFuture;
  11. import org.jboss.netty.channel.ChannelFutureListener;
  12. import org.jboss.netty.channel.ChannelHandlerContext;
  13. import org.jboss.netty.handler.codec.frame.FrameDecoder;
  14.  
  15. import world.World;
  16.  
  17. public class LoginProtocolDecoder extends FrameDecoder {
  18.  
  19. private static enum LoginStages {
  20. REQUEST, LOGIN_REQUEST_PART_ONE, LOGIN_REQUEST_PART_TWO, LOGIN_REQUEST_PART_THREE
  21. }
  22.  
  23. private LoginStages loginStage = LoginStages.REQUEST;
  24. private int encryptedBlockSize;
  25.  
  26. private long serverKeyPart;
  27.  
  28. private static byte[] junkBytes = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 };
  29.  
  30. static ChannelFutureListener closeFuture = new ChannelFutureListener() {
  31.  
  32. @Override
  33. public void operationComplete(ChannelFuture future) throws Exception {
  34. future.getChannel().disconnect();
  35. }
  36.  
  37. };
  38.  
  39. static void sendReturnCode(int i, Channel channel, boolean closeChannel) {
  40. ChannelBuffer buf = ChannelBuffers.buffer(1);
  41. buf.writeByte(i);
  42. if (closeChannel) {
  43. channel.write(buf).addListener(closeFuture);
  44. } else {
  45. channel.write(buf);
  46. }
  47.  
  48. }
  49.  
  50. @Override
  51. protected Object decode(ChannelHandlerContext ctx, final Channel channel,
  52. ChannelBuffer buffer) throws Exception {
  53.  
  54. if (buffer.readableBytes() <= 0)
  55. return null;
  56.  
  57. switch (loginStage) {
  58. case REQUEST:
  59. if (buffer.readableBytes() >= 2) {
  60. buffer.markReaderIndex();
  61. int requestType = buffer.readUnsignedByte();
  62.  
  63. if (requestType == 14) {
  64. @SuppressWarnings("unused")
  65. int nameHash = buffer.readUnsignedByte();
  66. serverKeyPart = ((long) (java.lang.Math.random() * 99999999D) << 32)
  67. + (long) (java.lang.Math.random() * 99999999D);
  68. ChannelBuffer loginResponce = ChannelBuffers.buffer(17);
  69. loginResponce.writeBytes(junkBytes);
  70.  
  71. loginResponce.writeByte(0);
  72.  
  73. loginResponce.writeLong(serverKeyPart);
  74. this.loginStage = LoginStages.LOGIN_REQUEST_PART_ONE;
  75. channel.write(loginResponce);
  76. return null;
  77. }
  78.  
  79. /*
  80. * Not a supported type. Lets close the channel.
  81. */
  82. channel.close();
  83.  
  84. return null;
  85. } else {
  86. return null;
  87. }
  88.  
  89. case LOGIN_REQUEST_PART_ONE:
  90. if (buffer.readableBytes() >= 42) {
  91. int loginType = buffer.readByte() & 0xff;
  92. if (loginType != 16 && loginType != 18) { // 16 normal, 18
  93. // reconnecting
  94. sendReturnCode((byte) 0, channel, true); // TODO response
  95. // code.
  96. return null;
  97. }
  98. encryptedBlockSize = buffer.readByte() - 40;
  99.  
  100. // Here we read the 40 extra bytes which are added the the
  101. // encrypted block size above^^
  102.  
  103. int verificationByte = buffer.readByte() & 0xff;
  104.  
  105. if (verificationByte != 255) {
  106. sendReturnCode(0, channel, true); // TODO return code;
  107. return null;
  108. }
  109.  
  110. int clientVersion = buffer.readShort();
  111.  
  112. if (clientVersion != 377) {
  113. sendReturnCode(0, channel, true); // RuneScape has since
  114. // been updated.
  115. return null;
  116. }
  117.  
  118. @SuppressWarnings("unused")
  119. boolean lowMemoryVersion = buffer.readByte() == 0;
  120.  
  121. for (int i = 0; i < 9; i++) {
  122. buffer.readInt(); // We should make a note of what these are
  123. // and then compare them against
  124. // connecting clients. We should then
  125. // send a RuneScape has since been
  126. // updated message if they do not match.
  127. }
  128.  
  129. encryptedBlockSize--;
  130.  
  131. if (encryptedBlockSize <= 0) {
  132. sendReturnCode(0, channel, true);
  133. return null;
  134. }
  135.  
  136. loginStage = LoginStages.LOGIN_REQUEST_PART_TWO;
  137. return true; // Return true so that this method is instantly
  138. // called again in case we have the data needed
  139. // to continue.
  140. }
  141.  
  142. case LOGIN_REQUEST_PART_TWO:
  143. if (buffer.readableBytes() >= encryptedBlockSize) {
  144.  
  145. int remainingBytes = buffer.readByte();
  146.  
  147. if (remainingBytes != encryptedBlockSize) {
  148. sendReturnCode(0, channel, true);
  149. return null;
  150. }
  151.  
  152. int verificationByte = buffer.readByte() & 0xff;
  153.  
  154. if (verificationByte != 10) {
  155. sendReturnCode(0, channel, true);
  156. System.out.println("k4:" + verificationByte);
  157. return null;
  158. }
  159.  
  160. long clientKeyPart = buffer.readLong();
  161. long serverKeyPart = buffer.readLong();
  162.  
  163. if (serverKeyPart != this.serverKeyPart) {
  164. sendReturnCode(0, channel, true);
  165. System.out.println("k5");
  166. return null;
  167. }
  168.  
  169. int uid = buffer.readInt();
  170.  
  171. String username = Packet.getString(buffer);
  172. String password = Packet.getString(buffer);
  173.  
  174. int sessionKey[] = new int[4];
  175. sessionKey[0] = (int) (clientKeyPart >> 32);
  176. sessionKey[1] = (int) clientKeyPart;
  177. sessionKey[2] = (int) (serverKeyPart >> 32);
  178. sessionKey[3] = (int) serverKeyPart;
  179.  
  180. ISAACCipher inCipher = new ISAACCipher(sessionKey);
  181. for (int i = 0; i < 4; i++)
  182. sessionKey[i] += 50;
  183. ISAACCipher outCipher = new ISAACCipher(sessionKey);
  184.  
  185. LoadRequest lr = new LoadRequest(channel, username, password,
  186. uid, inCipher, outCipher);
  187.  
  188. World.getWorld().getAccountManager().loadPlayer(lr);
  189.  
  190. }
  191. return null;
  192.  
  193. }
  194.  
  195. return null;
  196.  
  197. }
  198.  
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement