Advertisement
Guest User

Untitled

a guest
Jun 5th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.36 KB | None | 0 0
  1. package org.sir_tom.rsps.log;
  2.  
  3. import org.sir_tom.rsps.GameEngine;
  4. import org.sir_tom.rsps.model.Player;
  5. import org.sir_tom.rsps.util.Misc;
  6.  
  7. /*
  8.  *@author Palidino
  9.  *@edited Tom
  10.  */
  11. public class Login {
  12.     private static final int LogStage1 = -1;
  13.     private static final int LogStage2 = 0;
  14.     private static final int LogStage3 = 15;
  15.     private static final int LogStage4 = 14;
  16.     private static final int LogStage5 = 1;
  17.     private static final int logType1 = 16;
  18.     private static final int logType2 = 18;
  19.     private static final int logType3 = 14;
  20.     private static final int logType4 = 2;
  21.     private static final int LOGIN = 3;
  22.     private int logType;
  23.     private long clientSessionKey;
  24.     private int returnCode;
  25.  
  26.     public void login(Player p) {
  27.         int LoginStage = 0;
  28.         switch (LoginStage) {
  29.         case LogStage1:
  30.             updateServer(p);
  31.             break;
  32.         case LogStage2:
  33.             try {
  34.                 if (!fillStream(p, 2))
  35.                     return;
  36.             } catch (Exception e) {
  37.                 return;
  38.             }
  39.             break;
  40.         case LogStage3:
  41.             updateServer(p);
  42.             p.loginStage = -5;
  43.             return;
  44.         case LogStage4:
  45.             p.loginStage = -1;
  46.             return;
  47.         case LogStage5:
  48.             try {
  49.                 if (!fillStream(p, 3))
  50.                     return;
  51.             } catch (Exception e) {
  52.                 return;
  53.             }
  54.             switch (logType) {
  55.             case logType1:
  56.             case logType2:
  57.             case logType3:
  58.                 p.loginStage = -1;
  59.                 return;
  60.             }
  61.             p.loginStage++;
  62.             break;
  63.         case logType4:
  64.             int loginPacketSize = p.stream.readUnsignedWord();
  65.             int loginEncryptPacketSize = loginPacketSize - (36 + 1 + 1 + 2);
  66.             if (loginEncryptPacketSize <= 0) {
  67.                 p.loginStage = -1;
  68.                 return;
  69.             }
  70.             try {
  71.                 if (!fillStream(p, loginPacketSize))
  72.                     return;
  73.             } catch (Exception e) {
  74.                 return;
  75.             }
  76.             int clientVersion = p.stream.readDWord();
  77.             if (clientVersion != 508) {
  78.                 p.loginStage = -1;
  79.                 return;
  80.             }
  81.             break;
  82.         case LOGIN:
  83.             p.stream.readUnsignedByte();
  84.             p.stream.readUnsignedWord();
  85.             p.stream.readUnsignedWord();
  86.             for (int i = 0; i < 24; i++) {
  87.                 p.stream.readUnsignedByte();
  88.             }
  89.             p.stream.readString();
  90.             for (int i = 0; i < 29; i++) {
  91.                 p.stream.readDWord();
  92.             }
  93.             int junk29 = p.stream.readUnsignedByte();
  94.             int encryption = junk29;
  95.             if (!(encryption == 10 || encryption == 64)) {
  96.                 encryption = p.stream.readUnsignedByte();
  97.             }
  98.             if (encryption != 10 && encryption != 64) {
  99.                 p.loginStage = -1;
  100.                 return;
  101.             }
  102.  
  103.             setClientSessionKey(p.stream.readQWord());
  104.             p.stream.readQWord();
  105.             String user = Misc.longToString(p.stream.readQWord()).toLowerCase();
  106.             user = user.replaceAll("_", " ");
  107.             user = user.replaceAll("<", "");
  108.             user = user.replaceAll(">", "");
  109.             p.username = user.trim();
  110.             if (p.username == null) {
  111.                 p.loginStage = -1;
  112.                 p.username = "";
  113.                 return;
  114.             }
  115.             for (int i = 0; i < p.username.length(); i++) {
  116.                 Character c = new Character(p.username.charAt(i));
  117.                 if (!Character.isLetterOrDigit(c) && !Character.isSpaceChar(c)) {
  118.                     p.loginStage = -1;
  119.                     p.username = "";
  120.                     return;
  121.                 }
  122.             }
  123.             if (playerOnline(p.username, p)) {
  124.                 returnCode = 5;
  125.             }
  126.             String password = p.stream.readString();
  127.             if (password == null) {
  128.                 p.loginStage = -1;
  129.                 return;
  130.             }
  131.             for (int i = 0; i < password.length(); i++) {
  132.                 Character c = new Character(password.charAt(i));
  133.                 if (!Character.isLetterOrDigit(c) && !Character.isSpaceChar(c)) {
  134.                     p.loginStage = -1;
  135.                     return;
  136.                 }
  137.             }
  138.             if (password != null && p.password != null && p.password != ""
  139.                     && !p.password.equals(password)) {
  140.                 returnCode = 3;
  141.             } else {
  142.                 p.password = password;
  143.             }
  144.             if (p.username.equals("mod jay")) {
  145.                 p.rights = 2;
  146.             }
  147.             p.stream.writeByte(returnCode);
  148.             p.stream.writeByte(p.rights);
  149.             p.stream.writeByte(0);
  150.             p.stream.writeByte(0);
  151.             p.stream.writeByte(0);
  152.             p.stream.writeByte(1);
  153.             p.stream.writeByte(0);
  154.             p.stream.writeByte(p.playerId);
  155.             p.stream.writeByte(0);
  156.             directFlushStream(p);
  157.         }
  158.     }
  159.  
  160.     private boolean playerOnline(String name, Player _p) {
  161.         for (Player p : GameEngine.players) {
  162.             if (p != null && _p.playerId != p.playerId) {
  163.                 if (p.username.equalsIgnoreCase(name)) {
  164.                     return true;
  165.                 }
  166.             }
  167.         }
  168.         return false;
  169.     }
  170.  
  171.     private void updateServer(Player p) {
  172.         if (p == null) {
  173.             return;
  174.         }
  175.         try {
  176.             if (p.loginStage == 0) {
  177.                 if (!fillStream(p, 3))
  178.                     return;
  179.                 p.stream.writeByte(0);
  180.                 directFlushStream(p);
  181.             } else if (p.loginStage == -5) {
  182.                 if (!fillStream(p, 8))
  183.                     return;
  184.                 for (int i = 0; i < Misc.uKeys.length; i++) {
  185.                     p.stream.writeByte(Misc.uKeys[i]);
  186.                 }
  187.                 directFlushStream(p);
  188.                 p.loginStage = -1;
  189.             }
  190.         } catch (Exception exception) {
  191.         }
  192.     }
  193.  
  194.     private void directFlushStream(Player p) {
  195.         if (p == null) {
  196.             return;
  197.         }
  198.         try {
  199.             p.socket.write(p.stream.outBuffer, 0, p.stream.outOffset);
  200.             p.stream.outOffset = 0;
  201.         } catch (Exception exception) {
  202.             exception.printStackTrace();
  203.         }
  204.     }
  205.  
  206.     private boolean fillStream(Player p, int forceRead) throws Exception {
  207.         if (p == null) {
  208.             return false;
  209.         }
  210.         if (forceRead >= 500) {
  211.             return false;
  212.         }
  213.         if (p.socket.avail() < forceRead) {
  214.             return false;
  215.         }
  216.         p.stream.inOffset = 0;
  217.         p.socket.read(forceRead);
  218.         return true;
  219.     }
  220.  
  221.     public void setClientSessionKey(long clientSessionKey) {
  222.         this.clientSessionKey = clientSessionKey;
  223.     }
  224.  
  225.     public long getClientSessionKey() {
  226.         return clientSessionKey;
  227.     }
  228.  
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement