Guest User

Untitled

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