Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
518
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.82 KB | None | 0 0
  1. package net.com.codeusa.net.codec;
  2.  
  3. import net.com.codeusa.Server;
  4. import net.com.codeusa.Engine;
  5. import net.com.codeusa.model.Player;
  6. import net.com.codeusa.util.Misc;
  7. import net.com.codeusa.util.Protect;
  8. import net.com.codeusa.net.IOHostList;
  9. import net.com.codeusa.net.FloodDetect;
  10.  
  11. /**
  12. * @author Codeusa <codeusa@live.com> <rune-server.org> <codeusa.net
  13. * @info login information is here
  14. */
  15.  
  16. public class RS2LoginProtocol {
  17. /**
  18. * Validate a connection.
  19. * <p>To prevent milliseconds waiting for bytes, everytime a new byte is needed to be read
  20. * it is in a new stage which takes over 50 milliseconds before moving on to.
  21. * This allows the bytes to reach the server before trying to read them so that
  22. * read() returns instantly.
  23. * @param p The Player which the frame should be created for.
  24. */
  25. public void login(Player p) {
  26. if (p == null || p.stream == null) {
  27. return;
  28. }
  29. long serverSessionKey = ((long) (Math.random() * 99999999D) << 32) + (long) (Math.random() * 99999999D);
  30. long clientSessionKey = 8;
  31. int returnCode = 2;
  32. FloodDetect.checkClient(p);
  33. if (p.loginStage < -1) {
  34. updateServer(p);
  35. } else if (p.loginStage == 0) {
  36. try {
  37. if (!fillStream(p, 2))
  38. return;
  39. } catch (Exception e) {
  40. return;
  41. }
  42. int connectionType = p.stream.readUnsignedByte();
  43. if (connectionType == 15) {
  44. updateServer(p);
  45. p.loginStage = -5;
  46. return;
  47. }
  48. if (connectionType != 14) {
  49. p.loginStage = -1;
  50. return;
  51. }
  52. int longPlayerName = p.stream.readUnsignedByte();
  53. p.stream.writeByte(0);
  54. p.stream.writeQWord(serverSessionKey);
  55. directFlushStream(p);
  56. p.loginStage++;
  57. } else if (p.loginStage == 1) {
  58. try {
  59. if (!fillStream(p, 3))
  60. return;
  61. } catch (Exception e) {
  62. return;
  63. }
  64. int loginType = p.stream.readUnsignedByte();
  65. if (loginType != 16 && loginType != 18 && loginType != 14) {
  66. p.loginStage = -1;
  67. return;
  68. }
  69. p.loginStage++;
  70. } else if (p.loginStage == 2) {
  71. int loginPacketSize = p.stream.readUnsignedWord();
  72. int loginEncryptPacketSize = loginPacketSize - (36 + 1 + 1 + 2);
  73. if (loginEncryptPacketSize <= 0) {
  74. p.loginStage = -1;
  75. return;
  76. }
  77. try {
  78. if (!fillStream(p, loginPacketSize))
  79. return;
  80. } catch (Exception e) {
  81. return;
  82. }
  83. int clientVersion = p.stream.readDWord();
  84. if (clientVersion != 508) {
  85. p.loginStage = -1;
  86. return;
  87. }
  88. p.stream.readUnsignedByte();
  89. p.stream.readUnsignedWord();
  90. p.stream.readUnsignedWord();
  91. for (int i = 0; i < 24; i++) {
  92. int cacheIDX = p.stream.readUnsignedByte();
  93. if(cacheIDX == 0) {
  94. p.loginStage = -1;
  95. return;
  96. }
  97. }
  98. String junk = p.stream.readString();
  99. for (int i = 0; i < 29; i++) {
  100. int junk2 = p.stream.readDWord();
  101. }
  102. loginEncryptPacketSize--;
  103. int junk29 = p.stream.readUnsignedByte();
  104. int encryption = junk29;
  105. if(!(encryption == 10 || encryption == 64)) {
  106. encryption = p.stream.readUnsignedByte();
  107. }
  108. if (encryption != 10 && encryption != 64) {
  109. p.loginStage = -1;
  110. return;
  111. }
  112.  
  113. clientSessionKey = p.stream.readQWord();
  114. serverSessionKey = p.stream.readQWord();
  115. String user = Misc.longToString(p.stream.readQWord()).toLowerCase();
  116. user = user.replaceAll("_", " ");
  117. user = user.replaceAll("<", "");
  118. user = user.replaceAll(">", "");
  119. p.username = user.trim();
  120. if (p.username == null) {
  121. p.loginStage = -1;
  122. p.username = "";
  123. return;
  124. }
  125. for (int i = 0; i < p.username.length(); i++) {
  126. Character c = new Character(p.username.charAt(i));
  127. if (!Character.isLetterOrDigit(c) && !Character.isSpaceChar(c)) {
  128. p.loginStage = -1;
  129. p.username = "";
  130. return;
  131. }
  132. }
  133. if (p.systemUpdating) {
  134. returnCode = 14;
  135. }
  136. if (p.username.equalsIgnoreCase("Ankit")) {
  137. returnCode = 2;
  138. }
  139. if (p.username.equalsIgnoreCase("Euphoria")) {
  140. returnCode = 2;
  141. }
  142. if (playerOnline(p.username, p)) {
  143. returnCode = 5;
  144. // Protect.checkPlayer(p);
  145. }
  146. Server.loadBannedUsers();
  147. if (checkBannedUsers(p.username)) {
  148. returnCode = 4;
  149. }
  150. String password = p.stream.readString();
  151. if (password == null) {
  152. p.loginStage = -1;
  153. return;
  154. }
  155. for (int i = 0; i < password.length(); i++) {
  156. Character c = new Character(password.charAt(i));
  157. if (!Character.isLetterOrDigit(c) && !Character.isSpaceChar(c)) {
  158. p.loginStage = -1;
  159. return;
  160. }
  161. }
  162. Engine.fileManager.loadCharacter(p);
  163. if (password != null && p.password != null && p.password != "" && !p.password.equals(password)) {
  164. returnCode = 3;
  165. } else {
  166. p.password = password;
  167. }
  168. p.stream.writeByte(returnCode);
  169. p.stream.writeByte(p.rights);
  170. p.stream.writeByte(0);
  171. p.stream.writeByte(0);
  172. p.stream.writeByte(0);
  173. p.stream.writeByte(1);
  174. p.stream.writeByte(0);
  175. p.stream.writeByte(p.playerId);
  176. p.stream.writeByte(1);
  177. directFlushStream(p);
  178. if (p.teleportToX == -1 && p.teleportToY == -1)
  179. p.setCoords(2862, 5678, 0);
  180. Engine.playerMovement.getNextPlayerMovement(p);
  181. if (p.heightLevel != p.lastHeight && p.lastHeight < 4) {
  182. p.heightLevel = p.lastHeight;
  183. p.getActionSender().setMapRegion(p);
  184. p.updateReq = true;
  185. } else {
  186. p.heightLevel = 0;
  187. p.lastHeight = 0;
  188. p.getActionSender().setMapRegion(p);
  189. p.updateReq = true;
  190. }
  191. if(p.InBounty == 1) {
  192. Engine.BountyHunter.enter(p, 0);
  193. }
  194. p.getActionSender().setMapRegion(p);
  195. if (p.inJadCave())
  196. p.setCoords(3681, 2975, 0);
  197. p.getActionSender().setWindowPane(p, 548);
  198. if (p.clanWarsFightArea())
  199. p.setCoords(3267 + Misc.random(2), 3684 + Misc.random(2), 0);
  200. p.getActionSender().connecttofserver(p);
  201. p.friendsLoggedIn();
  202. directFlushStream(p);
  203. if (returnCode != 2) {
  204. Engine.fileManager.appendData("characters/ips/" + p.username + ".txt", "[" + Server.socketListener.getAddress(p.socket.socket) + "]: failed login.");
  205. return;
  206. }
  207. try {
  208. Engine.fileManager.savePassword(p);
  209. } catch (Exception e) {
  210. }
  211. Server.engine.fileManager.appendData("characters/ips/" + p.username + ".txt", "[" + Server.socketListener.getAddress(p.socket.socket) + "]: successful login.");
  212. p.getActionSender().setInterfaces(p);
  213. for (int i = 0; i < p.skillLvl.length; i++) {
  214. p.getActionSender().setSkillLvl(p, i);
  215. }
  216. p.thievingArray[0] = p.thievingArray[1] = -1;
  217. p.getWorldLoader().addPlayerFeatures();
  218. p.runEnergyUpdateReq = true;
  219. }
  220. }
  221.  
  222.  
  223.  
  224. /**
  225. * If the connection is the client's update server than send the keys.
  226. * @param p The Player which the frame should be created for.
  227. */
  228. private void updateServer(Player p) {
  229. if (p == null) {
  230. return;
  231. }
  232. try {
  233. if (p.loginStage == 0) {
  234. if (!fillStream(p, 3))
  235. return;
  236. p.stream.writeByte(0);
  237. directFlushStream(p);
  238. } else if (p.loginStage == -5) {
  239. if (!fillStream(p, 8))
  240. return;
  241. for (int i = 0; i < Misc.uKeys.length; i++) {
  242. p.stream.writeByte(Misc.uKeys[i]);
  243. }
  244. directFlushStream(p);
  245. p.loginStage = -1;
  246. }
  247. } catch (Exception exception) {
  248. }
  249. }
  250.  
  251. /**
  252. * Make sure the player isn't already online.
  253. * @param name The name to compare with.
  254. * @param _p The Player which the frame should be created for.
  255. */
  256. private boolean playerOnline(String name, Player _p) {
  257. for (Player p : Engine.players) {
  258. if (p != null && _p.playerId != p.playerId) {
  259. if (p.username.equalsIgnoreCase(name)) {
  260. return true;
  261. }
  262. }
  263. }
  264. return false;
  265. }
  266.  
  267. /**
  268. * Checks if a user is banned.
  269. * @param username The name to check.
  270. * @return Returns if the name was found.
  271. */
  272. public boolean checkBannedUsers(String username) {
  273. if (username == null) {
  274. return true;
  275. }
  276. for (int i = 0; i < Server.bannedUsers.length; i++) {
  277. if (Server.bannedUsers[i] != null && username.equalsIgnoreCase(Server.bannedUsers[i])) {
  278. return true;
  279. }
  280. }
  281. return false;
  282. }
  283.  
  284. /**
  285. * Check and read any incoming bytes.
  286. * @param p The Player which the frame should be created for.
  287. * @param forceRead How many bytes to read from the buffer.
  288. */
  289. private boolean fillStream(Player p, int forceRead) throws Exception {
  290. if (p == null) {
  291. return false;
  292. }
  293. if (forceRead >= 500) {
  294. return false;
  295. }
  296. if (p.socket.avail() < forceRead) {
  297. return false;
  298. }
  299. p.stream.inOffset = 0;
  300. p.socket.read(forceRead);
  301. return true;
  302. }
  303.  
  304. /**
  305. * Send the bytes in the stream's outBuffer directly to the client.
  306. * @param p The Player which the frame should be created for.
  307. */
  308. private void directFlushStream(Player p) {
  309. if (p == null) {
  310. return;
  311. }
  312. try {
  313. p.socket.write(p.stream.outBuffer, 0, p.stream.outOffset);
  314. p.stream.outOffset = 0;
  315. } catch (Exception exception) {
  316. exception.printStackTrace();
  317. }
  318. }
  319. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement