Guest User

Untitled

a guest
Mar 17th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.20 KB | None | 0 0
  1. package dragonkk.rs2rsps.net.codec;
  2.  
  3. import java.io.File;
  4. import java.util.GregorianCalendar;
  5.  
  6. import org.jboss.netty.channel.ChannelFutureListener;
  7.  
  8. import dragonkk.rs2rsps.HostList;
  9. import dragonkk.rs2rsps.Server;
  10. import dragonkk.rs2rsps.io.InStream;
  11. import dragonkk.rs2rsps.io.OutStream;
  12. import dragonkk.rs2rsps.model.World;
  13. import dragonkk.rs2rsps.model.player.Player;
  14. import dragonkk.rs2rsps.util.Constants;
  15. import dragonkk.rs2rsps.util.Logger;
  16. import dragonkk.rs2rsps.util.Misc;
  17. import dragonkk.rs2rsps.util.Serializer;
  18.  
  19. public class LoginDecoder {
  20.  
  21. private static File AccountFile(String name) {
  22. return new File(
  23. "Data/savedgames/"
  24. + name + ".ser");
  25. }
  26.  
  27. @SuppressWarnings("unused")
  28. public static void decode(ConnectionHandler p, InStream in) {
  29. String before = "" + p.getChannel().getRemoteAddress();
  30. String[] ipString = before.replaceAll("/", "").replaceAll(":", " ").split(" ");
  31. if (HostList.containsIp("" + ipString[0] + "")) {
  32. return;
  33. }
  34. if (p.getConnectionStage() == Constants.LOGIN_START) {
  35. OutStream outstream = new OutStream();
  36. outstream.writeByte(0);
  37. outstream.writeLong(p.getSessionKey());
  38. p.write(outstream);
  39. p.setConnectionStage(Constants.LOGIN_CYPTION);
  40. } else if (p.getConnectionStage() == Constants.LOGIN_CYPTION) {
  41. if (3 > in.remaining()) {
  42. return;
  43. }
  44. int loginType = in.readUnsignedByte();
  45. if (loginType != 16 && loginType != 18) {
  46. p.setConnectionStage(Constants.DISCONNECT);
  47. return;
  48. }
  49. int loginPacketSize = in.readUnsignedShort();
  50. if (loginPacketSize > in.remaining()) {
  51. return;
  52. }
  53. int clientVersion = in.readInt();
  54. if (clientVersion != Constants.REVISION) {
  55. p.setConnectionStage(Constants.DISCONNECT);
  56. return;
  57. }
  58. int unknown0 = in.readUnsignedByte();
  59. int displayMode = in.readUnsignedByte();
  60. p.setDisplayMode(displayMode);
  61. int screenSizeX = in.readUnsignedShort();
  62. int screenSizeY = in.readUnsignedShort();
  63. int unknown3 = in.readUnsignedByte();
  64. InStream inStream1 = new InStream(24); // new dunno
  65. inStream1.addBytes(in.buffer(), in.offset(), 24);
  66. in.skip(24);
  67. String settings = in.readRS2String();
  68. int unknown4 = in.readInt();
  69. int size = in.readUnsignedByte();
  70. InStream inStream2 = new InStream(size); // options etc
  71. inStream2.addBytes(in.buffer(), in.offset(), size);
  72. in.skip(size);
  73. InStream inStream3 = new InStream(14); // new dunno
  74. inStream1.addBytes(in.buffer(), in.offset(), 14);
  75. in.skip(14);
  76. in.readShort(); // new dunno
  77. in.readLong(); // new dunno
  78. int[] idxSizes = new int[33];
  79. for (int index = 0; index < idxSizes.length; index++) {
  80. idxSizes[index] = in.readInt();
  81. }
  82. in.skip(6);
  83. if (in.readUnsignedByte() != 10) {
  84. p.setConnectionStage(Constants.DISCONNECT);
  85. return;
  86. }
  87. int sessionKey[] = new int[4];
  88. for (int i = 0; i < 4; i++) {
  89. sessionKey[i] = in.readInt();
  90. }
  91. long l = in.readLong();
  92. int hash = (int) (31 & l >> 16);
  93. if (hash != p.getNameHash()) {
  94. p.setConnectionStage(Constants.DISCONNECT);
  95. return;
  96. }
  97. String Username = Misc.formatPlayerNameForProtocol(Misc.longToString(l));
  98. String Password = in.readRS2String();
  99. for (int i = 0; i < 4; i++) {
  100. sessionKey[i] += 50;
  101. }
  102. byte OpCode = 0;
  103. File account = AccountFile(Username);
  104. if (account == null || Username == null || Password == null) {
  105. OpCode = Constants.INVALID_PASSWORD;
  106. } else if (World.isOnList(Username)) {
  107. OpCode = Constants.ALREADY_ONLINE;
  108. }
  109. int returnCode = Constants.LOGIN_OK;
  110. String details[] = new String[]{Username, Password};
  111. File accountFile = account;
  112. Object serializedJavaObject = null;
  113. if (accountFile.exists()) {
  114. serializedJavaObject = Serializer.LoadAccount(accountFile);
  115. if (serializedJavaObject == null) {
  116. Logger.log("Nulled account", "Account " + details[0] + " is nulled.");
  117. if (Serializer.backupExists(Username)) {
  118. Logger.log("Backup", "A backup of this player exists. Attempting to load");
  119. try {
  120. serializedJavaObject = Serializer.loadBackup(Username);
  121. } catch (Exception e) {
  122. Logger.log("BackupService", "Restore from backup failed. An IO Error occoured while reading the file. Creating new Player!");
  123. serializedJavaObject = new Player(Username, Password,
  124. new GregorianCalendar(), new GregorianCalendar(),
  125. (short) 1, "", (byte) 1);
  126. ((Player) serializedJavaObject).wasReset = true;
  127. }
  128. if (serializedJavaObject == null) {
  129. Logger.log("BackupService", "Restore from backup failed. The file was corrupted. Creating new Player!");
  130. serializedJavaObject = new Player(Username, Password,
  131. new GregorianCalendar(), new GregorianCalendar(),
  132. (short) 1, "", (byte) 1);
  133. ((Player) serializedJavaObject).wasReset = true;
  134. }
  135. } else {
  136. Logger.log("BackupService", "A backup file did not exist for the nulled player. Creating new instance!");
  137. serializedJavaObject = new Player(Username, Password,
  138. new GregorianCalendar(), new GregorianCalendar(),
  139. (short) 1, "", (byte) 1);
  140. ((Player) serializedJavaObject).wasReset = true;
  141. }
  142. }
  143. }
  144. Player loadedPlayer = (Player) serializedJavaObject;
  145. if (returnCode == 2) {
  146. if (!accountFile.exists()) {
  147. loadedPlayer = new Player(Username, Password,
  148. new GregorianCalendar(), new GregorianCalendar(),
  149. (short) 1, "", (byte) 1);
  150. }
  151. loadedPlayer.setPassword(details[1]);
  152. }
  153. if (account == null || Username == null || Password == null) {
  154. OpCode = Constants.INVALID_PASSWORD;
  155. }
  156. if (World.isOnList(Username)) {
  157. OpCode = Constants.ALREADY_ONLINE;
  158. }
  159. OpCode = (byte) returnCode;
  160. if (Server.bannedIps.contains(ipString[0])) {
  161. OpCode = Constants.BANNED;
  162. Logger.log("Login decoder", "Login request denied, " + ipString[0] + " is contained in the ban list.");
  163. }
  164. p.setName(details[0]);
  165. if (World.isOnline(details[0])) {
  166. OpCode = Constants.ALREADY_ONLINE;
  167. }
  168. OutStream outstream = new OutStream();
  169. outstream.writeByte(OpCode);
  170. if (OpCode != 2) {
  171. p.writeInstant(outstream).addListener(ChannelFutureListener.CLOSE);
  172. return;
  173. } else {
  174. p.writeInstant(outstream);
  175. }
  176. p.setConnectionStage(Constants.REMOVE_ID);
  177. p.setPlayer(loadedPlayer);
  178. World.registerConnection(p);
  179. } else {
  180. p.setConnectionStage(Constants.DISCONNECT);
  181. }
  182. }
  183. }
Add Comment
Please, Sign In to add comment