Advertisement
Guest User

Untitled

a guest
Jun 5th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.59 KB | None | 0 0
  1. package org.rs2.net.login;
  2.  
  3. import java.security.SecureRandom;
  4.  
  5. import org.jboss.netty.buffer.ChannelBuffer;
  6. import org.jboss.netty.buffer.ChannelBuffers;
  7. import org.jboss.netty.channel.Channel;
  8. import org.jboss.netty.channel.ChannelHandlerContext;
  9. import org.jboss.netty.handler.codec.replay.ReplayingDecoder;
  10.  
  11. import static org.rs2.net.util.ChannelBufferUtils.readString;
  12.  
  13. /**
  14.  * The login decoder.
  15.  * @author Eric Hollander
  16.  *
  17.  */
  18. public class Rs2LoginDecoder extends ReplayingDecoder<Rs2LoginState> {
  19.  
  20.     /**
  21.      * The session key exchange opcode.
  22.      */
  23.     public static final int SESSION_KEY_OPCODE = 14;
  24.    
  25.     /**
  26.      * The connection opcode.
  27.      */
  28.     public static final int CONNECTION_OPCODE = 16;
  29.    
  30.     /**
  31.      * The reconnection opcode.
  32.      */
  33.     public static final int RECONNECTION_OPCODE = 18;
  34.    
  35.     /**
  36.      * The random generator.
  37.      */
  38.     public static final SecureRandom RANDOM = new SecureRandom();
  39.    
  40.     /**
  41.      * The class constructor.
  42.      * @param state The default state.
  43.      */
  44.     public Rs2LoginDecoder(Rs2LoginState state) {
  45.         super(state);
  46.     }
  47.    
  48.     @Override
  49.     protected Object decode(ChannelHandlerContext ctx, Channel channel,
  50.             ChannelBuffer in, Rs2LoginState state) throws Exception {
  51.         /*
  52.          * Prepare an outgoing buffer.
  53.          */
  54.         ChannelBuffer out = ChannelBuffers.dynamicBuffer();
  55.         switch(state) {
  56.         case OPCODE_REQUEST:
  57.             /*
  58.              * Read the opcode from the incoming buffer.
  59.              */
  60.             int opcode = in.readByte();
  61.             switch(opcode) {
  62.             case SESSION_KEY_OPCODE:
  63.                 /*
  64.                  * Prepare for session key request and call decode again.
  65.                  */
  66.                 checkpoint(Rs2LoginState.SESSION_KEY_REQUEST);
  67.                 break;
  68.             case CONNECTION_OPCODE:
  69.                 /*
  70.                  * Prepare for connection request and call decode again.
  71.                  */
  72.                 checkpoint(Rs2LoginState.CONNECTION_REQUEST);
  73.                 break;
  74.             case RECONNECTION_OPCODE:
  75.                 /*
  76.                  * Prepare for reconnection request and call decode again.
  77.                  */
  78.                 checkpoint(Rs2LoginState.RECONNECTION_REQUEST);
  79.                 break;
  80.             }
  81.             break;
  82.         case SESSION_KEY_REQUEST:
  83.             /*
  84.              * Read the name part from the buffer (unknown function/meaning)
  85.              */
  86.             in.readByte();
  87.             /*
  88.              * Write 8 ignored bytes plus one response byte (0).
  89.              */
  90.             for(int i = 0; i < 9; i++) {
  91.                 out.writeByte(0);
  92.             }
  93.             /*
  94.              * Write our key.
  95.              */
  96.             out.writeLong(RANDOM.nextLong());
  97.             break;
  98.         case CONNECTION_REQUEST:
  99.         case RECONNECTION_REQUEST:
  100.             /*
  101.              * Reads 2 bytes, the size and the magic 255 byte.
  102.              */
  103.             in.readShort();
  104.             /*
  105.              * Read the client version.
  106.              */
  107.             int clientVersion = in.readShort();
  108.             /*
  109.              * Since we don't take highmem into account, just read it.
  110.              */
  111.             in.readByte();
  112.             /*
  113.              * Read 9 CRCs, ignored.
  114.              */
  115.             for(int i = 0; i < 9; i++) {
  116.                 in.readByte();
  117.             }
  118.             /*
  119.              * Encrypted size + ignored byte (both ignored).
  120.              */
  121.             in.readShort();
  122.             /*
  123.              * Read the client + server keys. Used for ISAAC seeding.
  124.              * TODO: Add ISAAC implementation.
  125.              */
  126.             long clientSessionKey = in.readLong();
  127.             long serverSessionKey = in.readLong();
  128.             /*
  129.              * Read UID (ignored, unless you want cheaphax cheat client prevention).
  130.              */
  131.             in.readInt();
  132.             /*
  133.              * Read username + password from the buffer.
  134.              */
  135.             String username = readString(in);
  136.             String password = readString(in);
  137.             /*
  138.              * Write response + player rights + flagged.
  139.              */
  140.             out.writeByte(2);
  141.             out.writeByte(0);
  142.             out.writeByte(0);
  143.             /*
  144.              * TODO: World loading via MySQL + valid auth checks.
  145.              */
  146.             break;
  147.         }
  148.         /*
  149.          * Write the outgoing buffer to the connected channel.
  150.          */
  151.         channel.write(out);
  152.         return null;
  153.     }
  154.  
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement