Advertisement
Guest User

Untitled

a guest
Jan 7th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.52 KB | None | 0 0
  1. package net.dodian.network.login;
  2.  
  3. import java.net.InetSocketAddress;
  4. import java.security.SecureRandom;
  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 net.dodian.GameSettings;
  12. import net.dodian.game.World;
  13. import net.dodian.game.model.player.Player;
  14. import net.dodian.network.PlayerSession;
  15. import net.dodian.network.packet.PacketBuilder;
  16. import net.dodian.network.packet.codec.PacketDecoder;
  17. import net.dodian.network.packet.codec.PacketEncoder;
  18. import net.dodian.network.security.IsaacRandom;
  19. import net.dodian.network.sql.integration.ForumIntegration;
  20. import net.dodian.util.Misc;
  21. import net.dodian.util.StringUtils;
  22.  
  23. /**
  24. * A {@link FrameDecoder} which decodes
  25. * the login requests.
  26. *
  27. * @author Gabriel Hannason
  28. */
  29. public final class LoginDecoder extends FrameDecoder {
  30.  
  31. private static final int CONNECTED = 0;
  32.  
  33. private static final int LOGGING_IN = 1;
  34.  
  35. private int state = CONNECTED;
  36.  
  37. private long seed;
  38.  
  39. public static void sendReturnCode(Channel channel, int code) {
  40. channel.write(new PacketBuilder().put(code).toPacket())
  41. .addListener(arg0 -> arg0.getChannel().close());
  42. }
  43.  
  44. @Override
  45. protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer)
  46. throws Exception {
  47. switch (state) {
  48. case CONNECTED:
  49. if (buffer.readableBytes() < 2) {
  50. sendReturnCode(channel, LoginResponses.LOGIN_COULD_NOT_COMPLETE);
  51. return null;
  52. }
  53.  
  54. int request = buffer.readUnsignedByte();
  55. if (request != 14) {
  56. sendReturnCode(channel, LoginResponses.LOGIN_COULD_NOT_COMPLETE);
  57. return null;
  58. }
  59. buffer.readUnsignedByte();
  60. seed = new SecureRandom().nextLong();
  61. channel.write(new PacketBuilder().putLong(0).put((byte) 0).putLong(seed).toPacket());
  62. state = LOGGING_IN;
  63. return null;
  64. case LOGGING_IN:
  65. if (buffer.readableBytes() < 2) {
  66. sendReturnCode(channel, LoginResponses.LOGIN_COULD_NOT_COMPLETE);
  67. return null;
  68. }
  69. int loginType = buffer.readByte();
  70. if (loginType != 16 && loginType != 18) {
  71. sendReturnCode(channel, LoginResponses.LOGIN_COULD_NOT_COMPLETE);
  72. return null;
  73. }
  74. int blockLength = buffer.readByte() & 0xff;
  75. if (buffer.readableBytes() < blockLength) {
  76. sendReturnCode(channel, LoginResponses.LOGIN_COULD_NOT_COMPLETE);
  77. return null;
  78. }
  79. int magicId = buffer.readUnsignedByte();
  80. if (magicId != 0xFF) {
  81. sendReturnCode(channel, LoginResponses.LOGIN_COULD_NOT_COMPLETE);
  82. return null;
  83. }
  84. int clientVersion = buffer.readShort();
  85.  
  86. if (clientVersion != GameSettings.GAME_VERSION) {
  87. sendReturnCode(channel, LoginResponses.OLD_CLIENT_VERSION);
  88. return null;
  89. }
  90.  
  91. int memory = buffer.readByte();
  92. if (memory != 0 && memory != 1) {
  93. sendReturnCode(channel, LoginResponses.LOGIN_COULD_NOT_COMPLETE);
  94. return null;
  95. }
  96.  
  97.  
  98. int[] archiveCrcs = new int[9];
  99. for (int i = 0; i < 9; i++) {
  100. archiveCrcs[i] = buffer.readInt();
  101. }
  102.  
  103.  
  104. buffer.readUnsignedByte();
  105.  
  106.  
  107. /*ChannelBuffer rsaBuffer = buffer.readBytes(length);
  108. BigInteger bigInteger = new BigInteger(rsaBuffer.array());
  109. bigInteger = bigInteger.modPow(GameSettings.RSA_EXPONENT, GameSettings.RSA_MODULUS);
  110. rsaBuffer = ChannelBuffers.wrappedBuffer(bigInteger.toByteArray());*/
  111.  
  112.  
  113.  
  114. int securityId = buffer.readByte();
  115. if (securityId != 10) {
  116. sendReturnCode(channel, LoginResponses.LOGIN_COULD_NOT_COMPLETE);
  117. return null;
  118. }
  119.  
  120. long clientSeed = buffer.readLong();
  121. long seedReceived = buffer.readLong();
  122. if (seedReceived != seed) {
  123. sendReturnCode(channel, LoginResponses.LOGIN_BAD_SESSION_ID);
  124. return null;
  125. }
  126.  
  127. int[] seed = new int[4];
  128. seed[0] = (int) (clientSeed >> 32);
  129. seed[1] = (int) clientSeed;
  130. seed[2] = (int) (this.seed >> 32);
  131. seed[3] = (int) this.seed;
  132.  
  133. IsaacRandom decodingRandom = new IsaacRandom(seed);
  134. for (int i = 0; i < seed.length; i++) {
  135. seed[i] += 50;
  136. }
  137.  
  138.  
  139. int uid = buffer.readInt();
  140. String username = Misc.readString(buffer);
  141. String password = Misc.readString(buffer);
  142. String authCode = Misc.readString(buffer);
  143. String uuid = Misc.readString(buffer);
  144.  
  145. if (username.length() > 12 || password.length() > 20) {
  146. sendReturnCode(channel, LoginResponses.LOGIN_INVALID_CREDENTIALS);
  147. return null;
  148. }
  149.  
  150. username = Misc.formatText(username.toLowerCase());
  151. channel.getPipeline()
  152. .replace("encoder", "encoder", new PacketEncoder(new IsaacRandom(seed)));
  153. channel.getPipeline().replace("decoder", "decoder", new PacketDecoder(decodingRandom));
  154.  
  155. return login(channel, new LoginDetailsMessage(username, password,
  156. ((InetSocketAddress) channel
  157. .getRemoteAddress())
  158. .getAddress().getHostAddress(), authCode, uuid,
  159. clientVersion, uid));
  160. }
  161. return null;
  162. }
  163.  
  164. public Player login(Channel channel, LoginDetailsMessage msg) {
  165.  
  166. PlayerSession session = new PlayerSession(channel);
  167.  
  168. Player player = new Player(session).setUsername(msg.getUsername())
  169. .setLongUsername(StringUtils.stringToLong(msg.getUsername()))
  170. .setPassword(msg.getPassword()).setHostAddress(msg.getHost())
  171. .setSerialNumber(msg.getUuid());
  172. player.setAuthCode(msg.getAuthCode());
  173.  
  174. session.setPlayer(player);
  175.  
  176. int response = LoginResponses.getResponse(player, msg);
  177.  
  178.  
  179. if (msg.getUsername() != "Test") {
  180. // response = ForumIntegration.checkUser(player);
  181. }
  182.  
  183. if (response == LoginResponses.LOGIN_SUCCESSFUL) {
  184. if (World.addLoginRequest(player)) {
  185. return player;
  186. } else {
  187. response = LoginResponses.LOGIN_ACCOUNT_ONLINE;
  188. }
  189. }
  190.  
  191. sendReturnCode(channel, response);
  192. return null;
  193. }
  194.  
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement