Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package dragonkk.rs2rsps.net.codec;
- import java.io.File;
- import java.util.GregorianCalendar;
- import org.jboss.netty.channel.ChannelFutureListener;
- import dragonkk.rs2rsps.HostList;
- import dragonkk.rs2rsps.Server;
- import dragonkk.rs2rsps.io.InStream;
- import dragonkk.rs2rsps.io.OutStream;
- import dragonkk.rs2rsps.model.World;
- import dragonkk.rs2rsps.model.player.Player;
- import dragonkk.rs2rsps.util.Constants;
- import dragonkk.rs2rsps.util.Logger;
- import dragonkk.rs2rsps.util.Misc;
- import dragonkk.rs2rsps.util.Serializer;
- public class LoginDecoder {
- private static File AccountFile(String name) {
- return new File(
- "Data/savedgames/"
- + name + ".ser");
- }
- @SuppressWarnings("unused")
- public static void decode(ConnectionHandler p, InStream in) {
- String before = "" + p.getChannel().getRemoteAddress();
- String[] ipString = before.replaceAll("/", "").replaceAll(":", " ").split(" ");
- if (HostList.containsIp("" + ipString[0] + "")) {
- return;
- }
- if (p.getConnectionStage() == Constants.LOGIN_START) {
- OutStream outstream = new OutStream();
- outstream.writeByte(0);
- outstream.writeLong(p.getSessionKey());
- p.write(outstream);
- p.setConnectionStage(Constants.LOGIN_CYPTION);
- } else if (p.getConnectionStage() == Constants.LOGIN_CYPTION) {
- if (3 > in.remaining()) {
- return;
- }
- int loginType = in.readUnsignedByte();
- if (loginType != 16 && loginType != 18) {
- p.setConnectionStage(Constants.DISCONNECT);
- return;
- }
- int loginPacketSize = in.readUnsignedShort();
- if (loginPacketSize > in.remaining()) {
- return;
- }
- int clientVersion = in.readInt();
- if (clientVersion != Constants.REVISION) {
- p.setConnectionStage(Constants.DISCONNECT);
- return;
- }
- int unknown0 = in.readUnsignedByte();
- int displayMode = in.readUnsignedByte();
- p.setDisplayMode(displayMode);
- int screenSizeX = in.readUnsignedShort();
- int screenSizeY = in.readUnsignedShort();
- int unknown3 = in.readUnsignedByte();
- InStream inStream1 = new InStream(24); // new dunno
- inStream1.addBytes(in.buffer(), in.offset(), 24);
- in.skip(24);
- String settings = in.readRS2String();
- int unknown4 = in.readInt();
- int size = in.readUnsignedByte();
- InStream inStream2 = new InStream(size); // options etc
- inStream2.addBytes(in.buffer(), in.offset(), size);
- in.skip(size);
- InStream inStream3 = new InStream(14); // new dunno
- inStream1.addBytes(in.buffer(), in.offset(), 14);
- in.skip(14);
- in.readShort(); // new dunno
- in.readLong(); // new dunno
- int[] idxSizes = new int[33];
- for (int index = 0; index < idxSizes.length; index++) {
- idxSizes[index] = in.readInt();
- }
- in.skip(6);
- if (in.readUnsignedByte() != 10) {
- p.setConnectionStage(Constants.DISCONNECT);
- return;
- }
- int sessionKey[] = new int[4];
- for (int i = 0; i < 4; i++) {
- sessionKey[i] = in.readInt();
- }
- long l = in.readLong();
- int hash = (int) (31 & l >> 16);
- if (hash != p.getNameHash()) {
- p.setConnectionStage(Constants.DISCONNECT);
- return;
- }
- String Username = Misc.formatPlayerNameForProtocol(Misc.longToString(l));
- String Password = in.readRS2String();
- for (int i = 0; i < 4; i++) {
- sessionKey[i] += 50;
- }
- byte OpCode = 0;
- File account = AccountFile(Username);
- if (account == null || Username == null || Password == null) {
- OpCode = Constants.INVALID_PASSWORD;
- } else if (World.isOnList(Username)) {
- OpCode = Constants.ALREADY_ONLINE;
- }
- int returnCode = Constants.LOGIN_OK;
- String details[] = new String[]{Username, Password};
- File accountFile = account;
- Object serializedJavaObject = null;
- if (accountFile.exists()) {
- serializedJavaObject = Serializer.LoadAccount(accountFile);
- if (serializedJavaObject == null) {
- Logger.log("Nulled account", "Account " + details[0] + " is nulled.");
- if (Serializer.backupExists(Username)) {
- Logger.log("Backup", "A backup of this player exists. Attempting to load");
- try {
- serializedJavaObject = Serializer.loadBackup(Username);
- } catch (Exception e) {
- Logger.log("BackupService", "Restore from backup failed. An IO Error occoured while reading the file. Creating new Player!");
- serializedJavaObject = new Player(Username, Password,
- new GregorianCalendar(), new GregorianCalendar(),
- (short) 1, "", (byte) 1);
- ((Player) serializedJavaObject).wasReset = true;
- }
- if (serializedJavaObject == null) {
- Logger.log("BackupService", "Restore from backup failed. The file was corrupted. Creating new Player!");
- serializedJavaObject = new Player(Username, Password,
- new GregorianCalendar(), new GregorianCalendar(),
- (short) 1, "", (byte) 1);
- ((Player) serializedJavaObject).wasReset = true;
- }
- } else {
- Logger.log("BackupService", "A backup file did not exist for the nulled player. Creating new instance!");
- serializedJavaObject = new Player(Username, Password,
- new GregorianCalendar(), new GregorianCalendar(),
- (short) 1, "", (byte) 1);
- ((Player) serializedJavaObject).wasReset = true;
- }
- }
- }
- Player loadedPlayer = (Player) serializedJavaObject;
- if (returnCode == 2) {
- if (!accountFile.exists()) {
- loadedPlayer = new Player(Username, Password,
- new GregorianCalendar(), new GregorianCalendar(),
- (short) 1, "", (byte) 1);
- }
- loadedPlayer.setPassword(details[1]);
- }
- if (account == null || Username == null || Password == null) {
- OpCode = Constants.INVALID_PASSWORD;
- }
- if (World.isOnList(Username)) {
- OpCode = Constants.ALREADY_ONLINE;
- }
- OpCode = (byte) returnCode;
- if (Server.bannedIps.contains(ipString[0])) {
- OpCode = Constants.BANNED;
- Logger.log("Login decoder", "Login request denied, " + ipString[0] + " is contained in the ban list.");
- }
- p.setName(details[0]);
- if (World.isOnline(details[0])) {
- OpCode = Constants.ALREADY_ONLINE;
- }
- OutStream outstream = new OutStream();
- outstream.writeByte(OpCode);
- if (OpCode != 2) {
- p.writeInstant(outstream).addListener(ChannelFutureListener.CLOSE);
- return;
- } else {
- p.writeInstant(outstream);
- }
- p.setConnectionStage(Constants.REMOVE_ID);
- p.setPlayer(loadedPlayer);
- World.registerConnection(p);
- } else {
- p.setConnectionStage(Constants.DISCONNECT);
- }
- }
- }
Add Comment
Please, Sign In to add comment