Advertisement
Guest User

Untitled

a guest
Jan 10th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.73 KB | None | 0 0
  1. package com.inferno.net.event;
  2.  
  3. import java.math.BigInteger;
  4. import java.nio.ByteBuffer;
  5. import java.sql.SQLException;
  6. import java.util.Arrays;
  7.  
  8. import com.inferno.ForumIntegration;
  9. import com.inferno.NetworkConstants;
  10. import com.inferno.cache.Cache;
  11. import com.inferno.cache.crypto.ISAACCipher;
  12. import com.inferno.cache.crypto.ISAACPair;
  13. import com.inferno.cache.crypto.XTEACryption;
  14. import com.inferno.cache.misc.buffer.ByteBufferUtils;
  15. import com.inferno.game.node.entity.player.info.ClientInfo;
  16. import com.inferno.game.node.entity.player.info.DisplayMode;
  17. import com.inferno.game.node.entity.player.info.PlayerDetails;
  18. import com.inferno.game.node.entity.player.info.UIDInfo;
  19. import com.inferno.game.node.entity.player.info.login.LoginParser;
  20. import com.inferno.game.node.entity.player.info.login.LoginType;
  21. import com.inferno.game.node.entity.player.info.login.Response;
  22. import com.inferno.game.system.task.TaskExecutor;
  23. import com.inferno.net.IoReadEvent;
  24. import com.inferno.net.IoSession;
  25. import com.inferno.tools.StringUtils;
  26.  
  27. /**
  28. * Handles login reading events.
  29. *
  30. * @author Emperor
  31. */
  32. public final class LoginReadEvent extends IoReadEvent {
  33.  
  34. /**
  35. * The RSA modulus.
  36. */
  37. public static final BigInteger MODULUS = new BigInteger("165865706435016682110653568563251120094278686912987295809145491806194715902716739338411927793058925228087565434562948389222225588420069703784252638483569608159614392485969864899137973999614056797405232846059198315441808544524190866210655169682670028293787208173603935453834899795395794572295868565624049196373");
  38. /**
  39. * The RSA exponent.
  40. */
  41. public static final BigInteger RSA_KEY = new BigInteger("56600403721755849042871300293705052867669386058593586576677751174965876168398876338011266203274025417717477354449635744336265820214938719432244777866648612856409222657603092909839244312101542583618644127794582316068916809421286289618425813349038287935817476491851970456260849477664967448547154113206350117073");
  42.  
  43. /**
  44. * Constructs a new {@code LoginReadEvent}.
  45. *
  46. * @param session The session.
  47. * @param buffer The buffer with data to read from.
  48. */
  49. public LoginReadEvent(IoSession session, ByteBuffer buffer) {
  50. super(session, buffer);
  51. }
  52.  
  53. @Override
  54. public void read(IoSession session, ByteBuffer buffer) {
  55. int opcode = buffer.get() & 0xFF;
  56. if ((buffer.getShort()) != buffer.remaining()) {
  57. session.write(Response.BAD_SESSION_ID);
  58. return;
  59. }
  60. if (buffer.getInt() != NetworkConstants.REVISION) {
  61. session.write(Response.UPDATED);
  62. return;
  63. }
  64. int new1 = buffer.getInt();
  65. byte new2 = buffer.get();
  66. switch (opcode) {
  67. case 16: // Reconnect world login
  68. case 18: // World login
  69. decodeWorld(opcode, session, buffer);
  70. break;
  71. default:
  72. System.err.println("[Login] Unhandled login type [opcode=" + opcode + "]!");
  73. session.disconnect();
  74. break;
  75. }
  76. }
  77.  
  78. /**
  79. * Decodes a world login request.
  80. *
  81. * @param session The session.
  82. * @param rsa_buffer The buffer to read from.
  83. */
  84. private static void decodeWorld(final int opcode, final IoSession session, ByteBuffer buffer) {
  85. ByteBuffer rsa_buffer = getRSABlock(buffer);
  86. int[] xtea = new int[]{rsa_buffer.getInt(), rsa_buffer.getInt(), rsa_buffer.getInt(), rsa_buffer.getInt()};
  87. long var1 = rsa_buffer.getLong();
  88. int auth_type = rsa_buffer.get();
  89.  
  90. switch (auth_type) {
  91. case 0:
  92. case 3:
  93. int code = ByteBufferUtils.getTriByte(rsa_buffer);
  94. rsa_buffer.position(rsa_buffer.position() + 1);
  95. break;
  96. case 1:
  97. rsa_buffer.position(rsa_buffer.position() + 4);
  98. break;
  99. case 2:
  100. rsa_buffer.getInt();
  101. break;
  102. }
  103. int var2 = rsa_buffer.get();
  104. String password = ByteBufferUtils.getString(rsa_buffer);
  105. ByteBuffer xtea_buffer = XTEACryption.decrypt(buffer, xtea);
  106. String username = ByteBufferUtils.getString(xtea_buffer);
  107.  
  108. //int info = ForumIntegration.validate(username, password);
  109.  
  110. int displaySetting = xtea_buffer.get();
  111. int windowMode = (displaySetting >> 1);
  112. boolean lowMem = (displaySetting & 0xFF) == 1;
  113. int width = xtea_buffer.getShort();
  114. int height = xtea_buffer.getShort();
  115. xtea_buffer.position(xtea_buffer.position() + 24);
  116. String sessionToken1 = ByteBufferUtils.getString(xtea_buffer);
  117. int affiliateID = xtea_buffer.getInt();
  118. xtea_buffer.get(); // machine info opcode 6
  119. xtea_buffer.get(); // os type
  120. xtea_buffer.get(); // 64 bit
  121. xtea_buffer.get(); // os version
  122. xtea_buffer.get(); // vendor
  123. xtea_buffer.get(); // major
  124. xtea_buffer.get(); // minor
  125. xtea_buffer.get(); // patchcant type anything
  126. xtea_buffer.get(); // some flag
  127. xtea_buffer.getShort(); // max memory
  128. xtea_buffer.get();
  129. xtea_buffer.get();
  130. xtea_buffer.getShort();
  131. xtea_buffer.getShort();
  132. ByteBufferUtils.getJagString(xtea_buffer);
  133. ByteBufferUtils.getJagString(xtea_buffer);
  134. ByteBufferUtils.getJagString(xtea_buffer);
  135. ByteBufferUtils.getJagString(xtea_buffer);
  136. xtea_buffer.get();
  137. xtea_buffer.getShort();
  138. ByteBufferUtils.getJagString(xtea_buffer);
  139. ByteBufferUtils.getJagString(xtea_buffer);
  140. xtea_buffer.get();
  141. xtea_buffer.get();
  142. xtea_buffer.getInt();
  143. xtea_buffer.getInt();
  144. xtea_buffer.getInt();
  145. xtea_buffer.getInt();
  146. int sessionToken2 = xtea_buffer.get();
  147. xtea_buffer.getShort(); //mobile OS
  148. int crcBlock = xtea_buffer.getInt();
  149. final int[] clientCRC = new int[18];
  150. final int[] serverCRC = new int[18];
  151. for (int i = 0; i < 18; i++) {
  152. clientCRC[i] = xtea_buffer.getInt();
  153. serverCRC[i] = Cache.getIndexes()[i] == null ? 0 : Cache.getIndexes()[i].getInformation().getInformationContainer().getCrc();
  154. }
  155. ISAACCipher inCipher = new ISAACCipher(xtea);
  156. for (int i = 0; i < 4; i++) {
  157. xtea[i] += 50;
  158. }
  159. ISAACCipher outCipher = new ISAACCipher(xtea);
  160. session.setIsaacPair(new ISAACPair(inCipher, outCipher));
  161.  
  162. DisplayMode mode = DisplayMode.FIXED_MODE;
  163.  
  164. if(windowMode >= 1)
  165. {
  166. mode = DisplayMode.values()[windowMode-1];
  167. }
  168.  
  169. final PlayerDetails details = new PlayerDetails(username, null, session, mode);
  170. details.setClientInfo(new ClientInfo(width, height));
  171. final ByteBuffer b = xtea_buffer;
  172.  
  173. /*if (info != 2) {
  174. session.write(Response.INVALID_CREDENTIALS, true);
  175. return;
  176. } else {
  177. login(details, username, password, session, b, opcode);
  178. }*/
  179. login(details, username, password, session, b, opcode);
  180. }
  181.  
  182. /**
  183. * Handles the login procedure after we check an acc is registered.
  184. *
  185. * @param username the username.
  186. * @param password the password.
  187. * @param session the session.
  188. * @param buffer the byte buffer.
  189. * @param opcode the opcode.
  190. */
  191. private static void login(final PlayerDetails details, String username, String password, IoSession session, ByteBuffer buffer, int opcode) {
  192. LoginParser parser = new LoginParser(details, password, new UIDInfo(session.getAddress(), "none", "none", "none"), LoginType.fromType(opcode));
  193. TaskExecutor.execute(parser);
  194. }
  195.  
  196. /**
  197. * Gets the ISAAC seed from the buffer.
  198. *
  199. * @param buffer The buffer to read from.
  200. * @return The ISAAC seed.
  201. */
  202. public static int[] getISAACSeed(ByteBuffer buffer) {
  203. int[] seed = new int[4];
  204. for (int i = 0; i < 4; i++) {
  205. seed[i] = buffer.getInt();
  206. }
  207. return seed;
  208. }
  209.  
  210. /**
  211. * Gets the RSA block buffer.
  212. *
  213. * @param buffer The buffer to get the RSA block from.
  214. * @return The RSA block buffer.
  215. */
  216. public static ByteBuffer getRSABlock(ByteBuffer buffer) {
  217. byte[] rsaData = new byte[buffer.getShort()];
  218. buffer.get(rsaData);
  219. ByteBuffer block = ByteBuffer.wrap(new BigInteger(rsaData).modPow(RSA_KEY, MODULUS).toByteArray());
  220. int num = block.get();
  221. if (num != 1) {
  222. throw new IllegalArgumentException("Invalid RSA Magic Number " + num + "!");
  223. }
  224. return block;
  225. }
  226.  
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement