Guest User

Untitled

a guest
Oct 18th, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.03 KB | None | 0 0
  1. package palidino76.rs2.io;
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import palidino76.rs2.Server;
  6. import palidino76.rs2.Engine;
  7. import palidino76.rs2.player.Player;
  8. import palidino76.rs2.util.Misc;
  9. import palidino76.rs2.net.Protect;
  10. import palidino76.rs2.content.ge.Offer;
  11. import palidino76.rs2.content.ge.GrandExchangeLoader;
  12. import palidino76.rs2.content.ge.GrandExchange;
  13. import palidino76.rs2.EventManager.*;
  14.  
  15. public class Login {
  16. /**
  17. * Validate a connection.
  18. * <p>To prevent milliseconds waiting for bytes, everytime a new byte is needed to be read
  19. * it is in a new stage which takes over 50 milliseconds before moving on to.
  20. * This allows the bytes to reach the server before trying to read them so that
  21. * read() returns instantly.
  22. * @param p The Player which the frame should be created for.
  23. */
  24. public void login(Player p) {
  25. if (p == null || p.stream == null) {
  26. return;
  27. }
  28. long serverSessionKey = ((long) (Math.random() * 99999999D) << 32) + (long) (Math.random() * 99999999D);
  29. long clientSessionKey = 0;
  30. int returnCode = 2;
  31.  
  32. if (p.loginStage < -1) {
  33. updateServer(p);
  34.  
  35. } else if (p.loginStage == 0) {
  36. try {
  37.  
  38. if (!fillStream(p, 2)) {
  39. return;
  40. }
  41. } catch (Exception e) {
  42. return;
  43. }
  44. int connectionType = p.stream.readUnsignedByte();
  45.  
  46. if (connectionType == 15) {
  47. updateServer(p);
  48. p.loginStage = -5;
  49. return;
  50. }
  51. if (connectionType != 14) {
  52. p.loginStage = -1;
  53. return;
  54. }
  55. int longPlayerName = p.stream.readUnsignedByte();
  56. p.stream.writeByte(0);
  57. p.stream.writeQWord(serverSessionKey);
  58. directFlushStream(p);
  59. p.loginStage++;
  60. } else if (p.loginStage == 1) {
  61. try {
  62.  
  63. if (!fillStream(p, 3)) {
  64. return;
  65. }
  66.  
  67. } catch (Exception e) {
  68. return;
  69. }
  70.  
  71. int loginType = p.stream.readUnsignedByte();
  72.  
  73. if (loginType != 16 && loginType != 18 && loginType != 14) {
  74. p.loginStage = -1;
  75. return;
  76. }
  77. p.loginStage++;
  78.  
  79. } else if (p.loginStage == 2) {
  80. int loginPacketSize = p.stream.readUnsignedWord();
  81. int loginEncryptPacketSize = loginPacketSize - (36 + 1 + 1 + 2);
  82. if (loginEncryptPacketSize <= 0) {
  83. p.loginStage = -1;
  84. return;
  85. }
  86. try {
  87. if (!fillStream(p, loginPacketSize))
  88. return;
  89. } catch (Exception e) {
  90. return;
  91. }
  92. int clientVersion = p.stream.readDWord();
  93.  
  94. if (clientVersion != 508) {
  95. p.loginStage = -1;
  96. return;
  97. }
  98.  
  99.  
  100. /* LD Settings for logging in */
  101.  
  102. Protect.checkPlayer(p);
  103. p.stream.readUnsignedByte();
  104. p.stream.readUnsignedWord();
  105. p.stream.readUnsignedWord();
  106.  
  107. for (int i = 0; i < 24; i++) {
  108. int cacheIDX = p.stream.readUnsignedByte();
  109. }
  110. String junk = p.stream.readString();
  111.  
  112. for (int i = 0; i < 29; i++) {
  113. int junk2 = p.stream.readDWord();
  114. }
  115. loginEncryptPacketSize--;
  116. /*int junk29 = p.stream.readUnsignedByte();
  117. int encryption = p.stream.readUnsignedByte();
  118.  
  119. if (encryption != 10 && encryption != 64) {
  120. p.loginStage = -1;
  121. return;
  122. }*/
  123.  
  124. int junk29 = p.stream.readUnsignedByte();
  125. int encryption = junk29;
  126.  
  127. if (!(encryption == 10 || encryption == 64)) {
  128. encryption = p.stream.readUnsignedByte();
  129. }
  130. if (encryption != 10 && encryption != 64) {
  131. p.loginStage = -1;
  132. return;
  133. }
  134.  
  135.  
  136. /* HD Settings for logging in */
  137.  
  138. /* p.stream.readUnsignedByte();
  139. p.stream.readUnsignedWord();
  140. p.stream.readUnsignedWord();
  141. p.stream.readUnsignedWord(); //client height too
  142.  
  143. for (int i = 0; i < 24; i++) {
  144. int cacheIDX = p.stream.readUnsignedByte();
  145. }
  146. String junk = p.stream.readString();
  147.  
  148. for (int i = 0; i < 29; i++) {
  149. int junk2 = p.stream.readDWord();
  150. }
  151. loginEncryptPacketSize--;
  152. int encryption = p.stream.readUnsignedByte();
  153.  
  154. if (encryption != 10) {
  155. p.loginStage = -1;
  156. return;
  157. }*/
  158.  
  159.  
  160. clientSessionKey = p.stream.readQWord();
  161. serverSessionKey = p.stream.readQWord();
  162. p.username = Misc.longToString(p.stream.readQWord()).toLowerCase().replaceAll("_", " ").trim();
  163.  
  164. if (p.username == null) {
  165. p.loginStage = -1;
  166. p.username = "";
  167. return;
  168. }
  169.  
  170. for (int i = 0; i < p.username.length(); i++) {
  171. Character c = new Character(p.username.charAt(i));
  172. if (!Character.isLetterOrDigit(c) && !Character.isSpaceChar(c)) {
  173. p.loginStage = -1;
  174. p.username = "";
  175. return;
  176. }
  177. }
  178.  
  179. if (playerOnline(p.username, p)) {
  180. returnCode = 5;
  181. }
  182. Server.loadBannedUsers();
  183.  
  184. if (checkBannedUsers(p.username)) {
  185. returnCode = 4;
  186. }
  187. String password = p.stream.readString();
  188.  
  189. if (password == null) {
  190. p.loginStage = -1;
  191. return;
  192. }
  193. for (int i = 0; i < password.length(); i++) {
  194. Character c = new Character(password.charAt(i));
  195. if (!Character.isLetterOrDigit(c) && !Character.isSpaceChar(c)) {
  196. p.loginStage = -1;
  197. return;
  198. }
  199. }
  200. Engine.fileManager.loadCharacter(p);
  201.  
  202. if (password != null && p.password != null && p.password != "" && !p.password.equals(password)) {
  203. returnCode = 3;
  204. } else {
  205. p.password = password;
  206. }
  207.  
  208. if (p.username.equals("unleashed")) {
  209. p.rights = 2;
  210. }
  211. if (p.username.equals("barrag3d")) {
  212. p.rights = 2;
  213. }
  214. p.stream.writeByte(returnCode);
  215. p.stream.writeByte(p.rights);
  216. p.stream.writeByte(0);
  217. p.stream.writeByte(0);
  218. p.stream.writeByte(0);
  219. p.stream.writeByte(1);
  220. p.stream.writeByte(1);
  221. p.stream.writeByte(p.playerId);
  222. p.stream.writeByte(1);
  223. directFlushStream(p);
  224.  
  225. if (p.teleportToX == -1 && p.teleportToY == -1) {
  226. p.setCoords(3087, 3489, 0);
  227. }
  228.  
  229. Engine.playerMovement.getNextPlayerMovement(p);
  230.  
  231. if (p.inJadCave()) {
  232. if (p.heightLevel > 0) {
  233. p.heightLevel = 0;
  234. }
  235. }
  236. p.frames.setMapRegion(p);
  237.  
  238. if (p.inJadCave()) {
  239. p.setCoords(3224, 3222, 0);
  240. }
  241. p.skillLvl[3] = p.getLevelForXP(3);
  242. p.frames.setWindowPane(p, 549);
  243. p.frames.setInterface(p, 8, 549, 2, 378);
  244. p.frames.setInterface(p, 8, 549, 3, 447); // can use 15, 17 and 447.
  245. p.frames.setString(p, "You last logged in from: " + Server.socketListener.getAddress(p.socket.socket) + "", 378, 116);
  246. p.frames.setString(p, "Message of the Week", 447, 0);
  247. p.frames.setString(p, "The Grand exchange is now avaliable", 447, 1);
  248. p.frames.setString(p, "60", 378, 96);
  249. p.frames.setString(p, "60 days of member credit", 378, 94);
  250. p.frames.setString(p, "0", 378, 39);
  251. p.frames.setString(p, "No new messages", 378, 37);
  252. p.frames.setString(p, "Brand new 508 Runescape Private server!", 378, 38);
  253. p.frames.setString(p, "MESSAGE CENTER", 378, 14);
  254. //p.frames.setString(p, "line two", 378, 2);
  255. p.frames.setString(p, "<col=800517>Welcome to Frost blades!", 378, 115);
  256.  
  257. if (p.clanWarsFightArea()) {
  258. p.setCoords(3267 + Misc.random(2), 3684 + Misc.random(2), 0);
  259. }
  260. directFlushStream(p);
  261.  
  262. if (returnCode != 2) {
  263. Engine.fileManager.appendData("characters/ips/" + p.username + ".txt", "[" + Server.socketListener.getAddress(p.socket.socket) + "]: failed login.");
  264. return;
  265. }
  266. Server.engine.fileManager.appendData("characters/ips/" + p.username + ".txt", "[" + Server.socketListener.getAddress(p.socket.socket) + "]: successful login.");
  267. p.frames.setInterfaces(p);
  268.  
  269. for (int i = 0; i < p.skillLvl.length; i++) {
  270. p.frames.setSkillLvl(p, i);
  271. }
  272. if(p.rights == 0 && p.Donator == 0) {
  273. for ( Player ap : Engine.players) {
  274. if(ap == null)
  275. continue;
  276. if(!ap.online)
  277. continue;
  278. ap.frames.sendMessage(ap, "<col=0000ff>"+Misc.optimizeText(p.username)+" has logged in. There are currently "+Server.engine.getPlayerCount()+" players online!");
  279. }
  280. }
  281. if(p.Donator == 1 && p.rights == 0) {
  282. for ( Player ap : Engine.players) {
  283. if(ap == null)
  284. continue;
  285. if(!ap.online)
  286. continue;
  287. ap.frames.sendMessage(ap, "<col=FF0000><img=3>"+ p.username +" has logged in. There are currently "+Server.engine.getPlayerCount()+" players online!");
  288. }
  289. }
  290. p.thievingArray[0] = p.thievingArray[1] = -1;
  291.  
  292. if (Server.engine.wildernessArea(p.absX, p.absY)) {
  293. p.frames.setString(p, "Level: " + p.getWildernessLevel(), 381, 1);
  294. p.frames.setOverlay(p, 381);
  295. }
  296. p.runEnergyUpdateReq = true;
  297.  
  298. p.frames.sendMessage(p, "<img=1><col=15317E>Welcome to Frost blades<img=1>");
  299. p.frames.sendMessage(p, "<col=F87217>Teleporting can be done with Magic Teleports and Quest Tab.");
  300. for(Offer list : p.GrandExchange.offerList) {
  301. if(list == null) {
  302. continue;
  303. }
  304. if(list.id == p.playerId) {
  305. if(list.type == 0) {
  306. if(!list.completed) {
  307. p.frames.setGe(p, list.slot, -1, list.item, list.price, list.amount, list.amount-list.currentAmount);
  308. } else if(list.completed || list.aborted) {
  309. p.frames.setGe(p, list.slot, -3, list.item, list.price, list.amount, list.amount-list.currentAmount);
  310. }
  311. } else {
  312. if(!list.completed) {
  313. p.frames.setGe(p, list.slot, 3, list.item, list.price, list.amount, list.amount-list.currentAmount);
  314. } else if(list.completed || list.aborted) {
  315. p.frames.setGe(p, list.slot, 5, list.item, list.price, list.amount, list.amount-list.currentAmount);
  316. }
  317. }
  318. }
  319. }
  320. p.frames.setItems(p, 149, 0, 93, p.items, p.itemsN);
  321. p.frames.setItems(p, 387, 28, 93, p.equipment, p.equipmentN);
  322.  
  323. if (p.AtPits()) {
  324. p.GameStarted = false;
  325. p.setCoords(2395+Misc.random(8), 5170+Misc.random(4), 0);
  326. p.frames.sendMessage(p, "You cannot login in the Fight Pits you've been teleported to the waiting room.");
  327. }
  328.  
  329. if (Server.engine.wildernessArea(p.absX, p.absY) || p.AtPits()) {
  330. p.frames.setPlayerOption(p, "Attack", 1);
  331. } else {
  332.  
  333. }
  334.  
  335. p.frames.setPlayerOption(p, "Trade", 2);
  336. p.frames.setPlayerOption(p, "Follow", 4);
  337. p.frames.setPlayerOption(p, "Req Assist", 5);
  338.  
  339. if (p.inDuelArena()) {
  340. p.frames.setPlayerOption(p, "Challenge", 3);
  341. }
  342.  
  343. p.frames.setConfig(p, 173, 0);
  344. p.frames.setConfig(p, 313, -1);
  345. p.frames.setConfig(p, 465, -1);
  346. p.frames.setConfig(p, 802, -1);
  347. p.frames.setConfig(p, 1085, 249852);
  348. p.specAmount = 1000;
  349. p.frames.setConfig2(p, 300, 1000);
  350.  
  351. if (p.combatType == 1) {
  352. p.dropAllStuff();
  353. }
  354. if (p.castleArea()) {
  355. p.setCoords(2440, 3093, 0);
  356. }
  357. if (p.godWarsDung()) {
  358. p.frames.setOverlay(p, 601);
  359. }
  360.  
  361. if (p.duelFight()) {
  362. p.setCoords(3377, 3270, 0);
  363. }
  364.  
  365. p.getQuestClass().setQuestColors();
  366. p.frames.addLists(p);
  367. p.frames.setConfig(p, 172, 0);
  368. p.playerWeapon.setWeapon();
  369. p.calculateEquipmentBonus();
  370. p.updatePlayer(true);
  371. setPlayerStatus(p, true);
  372. p.frames.connecttofserver(p);
  373. p.friendsLoggedIn();
  374.  
  375. for (int i = 5; i < 150; i++) {
  376. p.frames.setString(p, "", 274, i);
  377. }
  378. }
  379. }
  380.  
  381. /**
  382. * If the connection is the client's update server than send the keys.
  383. * @param p The Player which the frame should be created for.
  384. */
  385. private void updateServer(Player p) {
  386. if (p == null) {
  387. return;
  388. }
  389.  
  390. try {
  391. if (p.loginStage == 0) {
  392. if (!fillStream(p, 3)) {
  393. return;
  394. }
  395.  
  396. p.stream.writeByte(0);
  397. directFlushStream(p);
  398. } else if (p.loginStage == -5) {
  399. if (!fillStream(p, 8)) {
  400. return;
  401. }
  402.  
  403. for (int i = 0; i < Misc.uKeys.length; i++) {
  404. p.stream.writeByte(Misc.uKeys[i]);
  405. }
  406. directFlushStream(p);
  407. p.loginStage = -1;
  408. }
  409. } catch (Exception exception) {
  410. }
  411. }
  412.  
  413. /**
  414. * Make sure the player isn't already online.
  415. * @param name The name to compare with.
  416. * @param _p The Player which the frame should be created for.
  417. */
  418. private boolean playerOnline(String name, Player _p) {
  419. for (Player p : Engine.players) {
  420. if (p != null && _p.playerId != p.playerId) {
  421. if (p.username.equalsIgnoreCase(name)) {
  422. return true;
  423. }
  424. }
  425. }
  426. return false;
  427. }
  428.  
  429. /**
  430. * Checks if a user is banned.
  431. * @param username The name to check.
  432. * @return Returns if the name was found.
  433. */
  434. public boolean checkBannedUsers(String username) {
  435. if (username == null) {
  436. return true;
  437. }
  438. for (int i = 0; i < Server.bannedUsers.length; i++) {
  439. if (Server.bannedUsers[i] != null && username.equalsIgnoreCase(Server.bannedUsers[i])) {
  440. return true;
  441. }
  442. }
  443. return false;
  444. }
  445.  
  446. /**
  447. * Check and read any incoming bytes.
  448. * @param p The Player which the frame should be created for.
  449. * @param forceRead How many bytes to read from the buffer.
  450. */
  451. private boolean fillStream(Player p, int forceRead) throws Exception {
  452. if (p == null) {
  453. return false;
  454. }
  455. if (forceRead >= 500) {
  456. return false;
  457. }
  458. if (p.socket.avail() < forceRead) {
  459. return false;
  460. }
  461. p.stream.inOffset = 0;
  462. p.socket.read(forceRead);
  463. return true;
  464. }
  465.  
  466. /**
  467. * Send the bytes in the stream's outBuffer directly to the client.
  468. * @param p The Player which the frame should be created for.
  469. */
  470. private void directFlushStream(Player p) {
  471. if (p == null) {
  472. return;
  473. }
  474. try {
  475. p.socket.write(p.stream.outBuffer, 0, p.stream.outOffset);
  476. p.stream.outOffset = 0;
  477. } catch (Exception exception) {
  478. exception.printStackTrace();
  479. }
  480. }
  481.  
  482. public void setPlayerStatus(Player p, boolean online) {
  483. p.online = online;
  484. }
  485. }
Add Comment
Please, Sign In to add comment