Advertisement
Guest User

Untitled

a guest
May 20th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1. package org.hannes;
  2.  
  3. import java.util.Random;
  4. import java.util.logging.Level;
  5. import java.util.logging.Logger;
  6.  
  7. import org.hannes.Player.LoginState;
  8. import org.hannes.Player.MemoryVersion;
  9.  
  10. public class LoginDecoder {
  11.  
  12. public static final int SESSION_KEY_COUNT = 9;
  13.  
  14. public static final int EXPECTED_MAGIC_ID = 255;
  15. public static final int VERSION_ID = 289;
  16.  
  17. public static final int UPDATE_REQUEST = 15;
  18. public static final int LOGIN_REQUEST = 14;
  19.  
  20. public static final int CONNECT = 16;
  21. public static final int RECONNECT = 18;
  22.  
  23. private static final Random RANDOM_GENERATOR = new Random();
  24. private static final Logger logger = Logger.getLogger(LoginDecoder.class.getName());
  25.  
  26. @SuppressWarnings("unused")
  27. public static void decode(final Player player) {
  28. try {
  29. player.setLoginState(LoginState.DECODE);
  30. final int[] sessionKeys = new int[SESSION_KEY_COUNT];
  31. final long serverSessionKey = RANDOM_GENERATOR.nextLong();
  32. player.forceRead(2);
  33. final int request = player.getInBuffer().get() & 0xFF;
  34. if(request != LOGIN_REQUEST && request != UPDATE_REQUEST) {
  35. log("Wrong requestId: " + request, player);
  36. return;
  37. }
  38. final int nameHash = player.getInBuffer().get() & 0xFF;
  39. for(int i = 0; i < 8; i++)
  40. player.getSocket().getOutputStream().write(0);
  41. player.getSocket().getOutputStream().write(0); // Don't skip RSA checks.
  42. player.getOutBuffer().putLong(serverSessionKey);
  43. player.getSocket().getOutputStream().write(player.getOutBuffer().getBuffer(), 0, player.getOutBuffer().getOffset());
  44. player.forceRead(2);
  45. final int connectionType = player.getInBuffer().get() & 0xFF;
  46. if(connectionType != RECONNECT && connectionType != CONNECT) {
  47. log("Wrong connectionType: " + connectionType, player);
  48. return;
  49. }
  50. final int loginPacketSize = player.getInBuffer().get() & 0xFF;
  51. player.forceRead(loginPacketSize);
  52. final int magicId = player.getInBuffer().get() & 0xFF;
  53. final int versionId = player.getInBuffer().getShort() & 0xFFFF;
  54. if(magicId != EXPECTED_MAGIC_ID || versionId != VERSION_ID) {
  55. log("Wrong client version: (" + magicId + ", " + versionId + ")", player);
  56. return;
  57. }
  58. final boolean lowMemory = (player.getInBuffer().get() & 0xFF) == 1;
  59. player.setMemoryVersion(lowMemory ? MemoryVersion.LOW : MemoryVersion.HIGH);
  60. for(int i = 0; i < SESSION_KEY_COUNT; i++) {
  61. sessionKeys[i] = player.getInBuffer().getInt();
  62. }
  63.  
  64. byte[] bytes = player.getInBuffer().getBuffer();
  65. for(int i = 0; i < 200; i++) {
  66. System.out.print((bytes[i] & 0xFF) + ",");
  67. if(i % 20 == 19) {
  68. System.out.println();
  69. }
  70. }
  71.  
  72. /*final int RSACheck = player.getInBuffer().get();
  73. if(RSACheck != loginPacketSize - 41) {
  74. log("Wrong RSACheck: " + RSACheck, player);
  75. return;
  76. }*/
  77. final int opcode = player.getInBuffer().get() & 0xFF;
  78. if(opcode != 10) {
  79. log("Wrong opcode: " + opcode, player);
  80. return;
  81. }
  82. final int[] clientKeys = new int[2];
  83. final int[] serverKeys = new int[2];
  84. clientKeys[0] = player.getInBuffer().getInt();
  85. clientKeys[1] = player.getInBuffer().getInt();
  86. serverKeys[0] = player.getInBuffer().getInt();
  87. serverKeys[1] = player.getInBuffer().getInt();
  88. final int uid = player.getInBuffer().getInt();
  89. final String username = player.getInBuffer().getRSString();
  90. final String password = player.getInBuffer().getRSString();
  91. log(username + " : " + password, player);
  92. player.getSocket().getOutputStream().write((byte) 2);
  93. player.getSocket().getOutputStream().write((byte) 0);
  94. player.getSocket().getOutputStream().write((byte) 0);
  95. player.setLoginState(LoginState.LOGGED_IN);
  96. } catch(Exception ex) {
  97. logger.log(Level.SEVERE, "Error in playerlogin.", ex);
  98. } finally {
  99. logger.info("Login procedure done.");
  100. }
  101. }
  102.  
  103. public static void log(String string, Player player) {
  104. logger.info("Player " + player.getPlayerId() + ", " + player.getWorldId() + ": " + string);
  105. }
  106.  
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement