Advertisement
Guest User

Untitled

a guest
Jun 19th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. package zeus.rs2.net.codec;
  2.  
  3. import java.security.SecureRandom;
  4. import java.util.logging.Logger;
  5.  
  6. import org.jboss.netty.buffer.ChannelBuffer;
  7. import org.jboss.netty.channel.Channel;
  8. import org.jboss.netty.channel.ChannelHandlerContext;
  9. import org.jboss.netty.handler.codec.frame.FrameDecoder;
  10.  
  11. import zeus.rs2.model.player.Player;
  12. import zeus.rs2.net.packet.MessageBuilder;
  13. import zeus.rs2.util.BufferUtils;
  14.  
  15. /**
  16. *
  17. * @author Karl
  18. *
  19. */
  20. public class RS2LoginDecoder extends FrameDecoder {
  21.  
  22. private static final Logger logger = Logger.getLogger(RS2LoginDecoder.class.getName());
  23. private SecureRandom random = new SecureRandom();
  24.  
  25. public enum State {
  26. STATE_OPCODE, STATE_RESPONSE, STATE_UPDATE, STATE_LOGIN;
  27. }
  28.  
  29. private State state = State.STATE_OPCODE;
  30. private static final int GAME_OPCODE = 14;
  31. private static final int UPDATE_OPCODE = 15;
  32. private static final byte[] INITIAL_RESPONSE = new byte[] {
  33. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0
  34. };
  35.  
  36. @Override
  37. protected Object decode(ChannelHandlerContext chc, Channel channel,
  38. ChannelBuffer channelBuffer) throws Exception {
  39. switch(state) {
  40. case STATE_OPCODE:
  41. if(channelBuffer.readableBytes() >= 1) {
  42. int opcode = channelBuffer.readByte();
  43. switch(opcode) {
  44. case GAME_OPCODE:
  45. state = State.STATE_RESPONSE;
  46. return true;
  47. case UPDATE_OPCODE:
  48. state = State.STATE_UPDATE;
  49. return true;
  50. default:
  51. logger.info("Invalid opcode:" + opcode);
  52. channel.close();
  53. return false;
  54. }
  55. } else {
  56. return false;
  57. }
  58. case STATE_RESPONSE:
  59. if(channelBuffer.readableBytes() >= 1) {
  60. long serverKey = random.nextLong();
  61. channel.write(new MessageBuilder().writeBytes(INITIAL_RESPONSE).writeByte((byte) 0).writeLong(serverKey).toMessage());
  62. state = State.STATE_LOGIN;
  63. return true;
  64. } else {
  65. return false;
  66. }
  67. case STATE_UPDATE:
  68. if(channelBuffer.readableBytes() >= 4) {
  69. return true;
  70. } else {
  71. return false;
  72. }
  73. case STATE_LOGIN:
  74. if(channelBuffer.readableBytes() >= 1) {
  75. long clientKey = channelBuffer.readLong();
  76. long serverKey = channelBuffer.readLong();
  77. int isaacSeed[] = new int[4];
  78. isaacSeed[0] = (int) (clientKey >> 32);
  79. isaacSeed[1] = (int) clientKey;
  80. isaacSeed[2] = (int) (serverKey >> 32);
  81. isaacSeed[3] = (int) serverKey;
  82. int rsaOpcode = channelBuffer.readByte() & 0xFF;
  83. if(rsaOpcode != 10) {
  84. logger.info("Invalid rsa opcode:" + rsaOpcode);
  85. channel.close();
  86. return false;
  87. }
  88. int connectionType = channelBuffer.readByte() & 0xFF;
  89. if(connectionType != 16 && connectionType != 18) {
  90. logger.info("Invalid connection type:" + connectionType);
  91. channel.close();
  92. return false;
  93. }
  94. int loginPacket = channelBuffer.readByte() & 0xFF;
  95. if(loginPacket != 255) {
  96. logger.info("Invalid login packet:" + loginPacket);
  97. channel.close();
  98. return false;
  99. }
  100. int clientVersion = channelBuffer.readShort() & 0xFF;
  101. if(clientVersion != 319) {
  102. logger.info("Invalid client version:" + clientVersion);
  103. channel.close();
  104. return false;
  105. }
  106. for(int i = 0; i < 9; i++) {
  107. channelBuffer.readInt();
  108. }
  109. for(int i = 0; i < 4; i++) {
  110. isaacSeed[2] += 50;
  111. }
  112. String username = BufferUtils.readRS2String(channelBuffer);
  113. String password = BufferUtils.readRS2String(channelBuffer);
  114. Player player = new Player(username, password);
  115. player.addPlayer(player);
  116. logger.info("Registered player: username: "+username+" password: "+password+".");
  117. return true;
  118. } else {
  119. return false;
  120. }
  121. }
  122. return null;
  123. }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement