Advertisement
Guest User

Untitled

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