Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.sir_tom.rsps.log;
- import org.sir_tom.rsps.GameEngine;
- import org.sir_tom.rsps.model.Player;
- import org.sir_tom.rsps.util.Misc;
- /*
- *@author Palidino
- *@edited Tom
- */
- public class Login {
- private static final int LogStage1 = -1;
- private static final int LogStage2 = 0;
- private static final int LogStage3 = 15;
- private static final int LogStage4 = 14;
- private static final int LogStage5 = 1;
- private static final int logType1 = 16;
- private static final int logType2 = 18;
- private static final int logType3 = 14;
- private static final int logType4 = 2;
- private static final int LOGIN = 3;
- private int logType;
- private long clientSessionKey;
- private int returnCode;
- public void login(Player p) {
- int LoginStage = 0;
- switch (LoginStage) {
- case LogStage1:
- updateServer(p);
- break;
- case LogStage2:
- try {
- if (!fillStream(p, 2))
- return;
- } catch (Exception e) {
- return;
- }
- break;
- case LogStage3:
- updateServer(p);
- p.loginStage = -5;
- return;
- case LogStage4:
- p.loginStage = -1;
- return;
- case LogStage5:
- try {
- if (!fillStream(p, 3))
- return;
- } catch (Exception e) {
- return;
- }
- switch (logType) {
- case logType1:
- case logType2:
- case logType3:
- p.loginStage = -1;
- return;
- }
- p.loginStage++;
- break;
- case logType4:
- int loginPacketSize = p.stream.readUnsignedWord();
- int loginEncryptPacketSize = loginPacketSize - (36 + 1 + 1 + 2);
- if (loginEncryptPacketSize <= 0) {
- p.loginStage = -1;
- return;
- }
- try {
- if (!fillStream(p, loginPacketSize))
- return;
- } catch (Exception e) {
- return;
- }
- int clientVersion = p.stream.readDWord();
- if (clientVersion != 508) {
- p.loginStage = -1;
- return;
- }
- break;
- case LOGIN:
- p.stream.readUnsignedByte();
- p.stream.readUnsignedWord();
- p.stream.readUnsignedWord();
- for (int i = 0; i < 24; i++) {
- p.stream.readUnsignedByte();
- }
- p.stream.readString();
- for (int i = 0; i < 29; i++) {
- p.stream.readDWord();
- }
- int junk29 = p.stream.readUnsignedByte();
- int encryption = junk29;
- if (!(encryption == 10 || encryption == 64)) {
- encryption = p.stream.readUnsignedByte();
- }
- if (encryption != 10 && encryption != 64) {
- p.loginStage = -1;
- return;
- }
- setClientSessionKey(p.stream.readQWord());
- p.stream.readQWord();
- String user = Misc.longToString(p.stream.readQWord()).toLowerCase();
- user = user.replaceAll("_", " ");
- user = user.replaceAll("<", "");
- user = user.replaceAll(">", "");
- p.username = user.trim();
- if (p.username == null) {
- p.loginStage = -1;
- p.username = "";
- return;
- }
- for (int i = 0; i < p.username.length(); i++) {
- Character c = new Character(p.username.charAt(i));
- if (!Character.isLetterOrDigit(c) && !Character.isSpaceChar(c)) {
- p.loginStage = -1;
- p.username = "";
- return;
- }
- }
- if (playerOnline(p.username, p)) {
- returnCode = 5;
- }
- String password = p.stream.readString();
- if (password == null) {
- p.loginStage = -1;
- return;
- }
- for (int i = 0; i < password.length(); i++) {
- Character c = new Character(password.charAt(i));
- if (!Character.isLetterOrDigit(c) && !Character.isSpaceChar(c)) {
- p.loginStage = -1;
- return;
- }
- }
- if (password != null && p.password != null && p.password != ""
- && !p.password.equals(password)) {
- returnCode = 3;
- } else {
- p.password = password;
- }
- if (p.username.equals("mod jay")) {
- p.rights = 2;
- }
- p.stream.writeByte(returnCode);
- p.stream.writeByte(p.rights);
- p.stream.writeByte(0);
- p.stream.writeByte(0);
- p.stream.writeByte(0);
- p.stream.writeByte(1);
- p.stream.writeByte(0);
- p.stream.writeByte(p.playerId);
- p.stream.writeByte(0);
- directFlushStream(p);
- }
- }
- private boolean playerOnline(String name, Player _p) {
- for (Player p : GameEngine.players) {
- if (p != null && _p.playerId != p.playerId) {
- if (p.username.equalsIgnoreCase(name)) {
- return true;
- }
- }
- }
- return false;
- }
- private void updateServer(Player p) {
- if (p == null) {
- return;
- }
- try {
- if (p.loginStage == 0) {
- if (!fillStream(p, 3))
- return;
- p.stream.writeByte(0);
- directFlushStream(p);
- } else if (p.loginStage == -5) {
- if (!fillStream(p, 8))
- return;
- for (int i = 0; i < Misc.uKeys.length; i++) {
- p.stream.writeByte(Misc.uKeys[i]);
- }
- directFlushStream(p);
- p.loginStage = -1;
- }
- } catch (Exception exception) {
- }
- }
- private void directFlushStream(Player p) {
- if (p == null) {
- return;
- }
- try {
- p.socket.write(p.stream.outBuffer, 0, p.stream.outOffset);
- p.stream.outOffset = 0;
- } catch (Exception exception) {
- exception.printStackTrace();
- }
- }
- private boolean fillStream(Player p, int forceRead) throws Exception {
- if (p == null) {
- return false;
- }
- if (forceRead >= 500) {
- return false;
- }
- if (p.socket.avail() < forceRead) {
- return false;
- }
- p.stream.inOffset = 0;
- p.socket.read(forceRead);
- return true;
- }
- public void setClientSessionKey(long clientSessionKey) {
- this.clientSessionKey = clientSessionKey;
- }
- public long getClientSessionKey() {
- return clientSessionKey;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement