Guest User

Untitled

a guest
Aug 25th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.78 KB | None | 0 0
  1. public class LoginProtocolDecoder extends ReplayingDecoder<LoginProtocolDecoder.LoginState> {
  2.    
  3.  
  4.     public enum LoginState {
  5.         GAME, HANDSHAKE, REQUEST
  6.     }
  7.  
  8.     private static final byte[] INITIAL_RESPONSE = {
  9.         0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0
  10.     };
  11.  
  12.     private static final SecureRandom sessionKey = new SecureRandom();
  13.  
  14.     public LoginProtocolDecoder() {
  15.         checkpoint(LoginState.REQUEST);
  16.     }
  17.  
  18.     @Override
  19.     protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer, LoginState state) {
  20.         switch (state) {
  21.         case REQUEST:
  22.             if (buffer.readableBytes() < 1) {
  23.                 return false;
  24.             }
  25.             switch (buffer.readUnsignedByte()) {
  26.             case 14:
  27.                 checkpoint(LoginState.HANDSHAKE);
  28.                 return true;
  29.             }
  30.         case HANDSHAKE:
  31.             if (buffer.readableBytes() < 1) {
  32.                 return false;
  33.             }
  34.             int nameHash = buffer.readUnsignedByte();
  35.             PacketConstuctor response = new PacketConstuctor();
  36.             response.writeBytes(INITIAL_RESPONSE);
  37.             response.writeByte(0);
  38.             response.writeLong(sessionKey.nextLong());
  39.             channel.write(response);
  40.             checkpoint(LoginState.GAME);
  41.             return true;
  42.         case GAME:
  43.             PacketConstuctor loginResponse = new PacketConstuctor();
  44.             if (buffer.readableBytes() < 2) {
  45.                 return false;
  46.             }
  47.             int loginCode = buffer.readUnsignedByte();
  48.             switch (loginCode) {
  49.             case 16:
  50.             case 18:
  51.                 int loginSize = buffer.readUnsignedByte();
  52.                 int encryptedLoginSize = loginSize - (36 + 1 + 1 + 2);
  53.                 if (encryptedLoginSize < 1) {
  54.                     channel.close();
  55.                     return false;
  56.                 }
  57.                 if (buffer.readableBytes() < loginSize) {
  58.                     return false;
  59.                 }
  60.                 if (buffer.readUnsignedByte() != 255) {
  61.                     channel.close();
  62.                     return false;
  63.                 }
  64.                 if (buffer.readShort() != 317) {
  65.                     channel.write(loginResponse.writeByte(6));
  66.                     return false;
  67.                 }
  68.                 int memory = buffer.readUnsignedByte();
  69.                 System.out.println(memory); //0 = low 1 = high
  70.                 for (int i = 0; i < 9; i++) {
  71.                     buffer.readInt();
  72.                 }
  73.                 encryptedLoginSize--;
  74.                 if (buffer.readUnsignedByte() != encryptedLoginSize) {
  75.                     channel.close();
  76.                     return false;
  77.                 }
  78.                 if (buffer.readUnsignedByte() != 10) {
  79.                     channel.close();
  80.                     return false;
  81.                 }
  82.                
  83.                 //int clientId = buffer.readUnsignedByte();
  84.                 final long clientSessionKey = buffer.readLong();
  85.                 final long serverSessionKey = buffer.readLong();
  86.                 final int[] isaacSeed = {
  87.                         (int) (clientSessionKey >> 32),
  88.                         (int) clientSessionKey,
  89.                         (int) (serverSessionKey >> 32),
  90.                         (int) serverSessionKey };
  91.                
  92.                 final int userID = buffer.readInt();
  93.                 final String username = Utils.formatName(Utils.readRS2String(buffer));
  94.                 final String password = Utils.readRS2String(buffer);
  95.                
  96.                 final IsaacRandom inCipher = new IsaacRandom(isaacSeed);
  97.                 for (int i = 0; i < isaacSeed.length; i++)
  98.                     isaacSeed[i] += 50;
  99.                 channel.write(loginResponse.writeByte(2));
  100.                 //loginResponse.writeByte(0); //logout..
  101.                
  102.                 Client c = new Client(ctx);
  103.                 c.bindCharacter(new Character(password, password));
  104.                 c.getCharacter().setUsername(username);
  105.                 c.getCharacter().setPassword(password);
  106.                 c.load();
  107.                 loginResponse.writeByte(c.getCharacter().getCharRights());
  108.                 //channel.write(loginResponse.writeByte(0));
  109.                 channel.getPipeline().replace("decoder", "decoder", new RS2Decoder(inCipher));
  110.                 channel.write(loginResponse);
  111.                 //return valide(ctx, username, password);
  112.             }
  113.             return true;
  114.            
  115.         default:
  116.             return null;
  117.         }
  118.     }
  119.     /*
  120.     private static boolean valide(ChannelHandlerContext ctx, String username, String password) {
  121.         //TEMP LOADER!
  122.         Client c = new Client(ctx);
  123.         c.bindCharacter(new Character(password, password));
  124.         c.getCharacter().setUsername(username);
  125.         c.getCharacter().setPassword(password);
  126.         //client.load();
  127.         return true;
  128.     }
  129.     */
  130. }
Add Comment
Please, Sign In to add comment