Guest User

Untitled

a guest
Jul 2nd, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.42 KB | None | 0 0
  1. package com.augustus.net.codec;
  2.  
  3. import java.security.SecureRandom;
  4.  
  5. import org.jboss.netty.buffer.ChannelBuffer;
  6. import org.jboss.netty.channel.Channel;
  7. import org.jboss.netty.channel.ChannelHandlerContext;
  8. import org.jboss.netty.handler.codec.replay.ReplayingDecoder;
  9.  
  10. import com.augustus.model.player.Player;
  11. import com.augustus.net.util.BufferUtils;
  12. import com.augustus.net.util.NameUtils;
  13. import com.augustus.net.util.ResponseCodeUtils;
  14. import com.augustus.packet.PacketBuilder;
  15.  
  16. /**
  17.  * Performs decoding measures which decodes a new connection into
  18.  * a login request which is then processed by the server to enable
  19.  * game access for the user.
  20.  *
  21.  * @author Ryley Kimmel
  22.  */
  23. public class LoginDecoder extends ReplayingDecoder<LoginDecoder.LoginState> {
  24.  
  25.     /**
  26.      * We checkpoint to the Request state.
  27.      */
  28.     public LoginDecoder() {
  29.     checkpoint(LoginState.REQUEST);
  30.     }
  31.  
  32.     /**
  33.      * The states of the decoding process.
  34.      *
  35.      * @author Ryley Kimmel
  36.      */
  37.     public enum LoginState {
  38.     /**
  39.      * At this state the client sends a decode request
  40.      * to the server. Here the server identifies what state
  41.      * it needs to perform by reading an unsigned byte.
  42.      */
  43.    
  44.     REQUEST,
  45.    
  46.     /**
  47.      * At this state the server decides the best login server
  48.      * choice for the user.
  49.      */
  50.    
  51.     HANDSHAKE,
  52.    
  53.     /**
  54.      * At this state the server has finished decoding and now
  55.      * provides the client with information to login after
  56.      * authenticating the user name and password sent by the client.
  57.      *
  58.      * The server at this time swaps the pipeline's decoder to
  59.      * the message decoder.
  60.      */
  61.    
  62.     GAME;
  63.     }
  64.  
  65.     /**
  66.      * Initial response, we send back 8 bytes.
  67.      */
  68.     private static final byte[] INITIAL_RESPONSE = {
  69.     0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0
  70.     };
  71.  
  72.     /**
  73.      * Generates random numbers.
  74.      */
  75.     private static final SecureRandom serverKeyGenerator = new SecureRandom();
  76.  
  77.     @Override
  78.     protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer, LoginState state) throws Exception {
  79.     switch(state) {
  80.     case REQUEST:
  81.  
  82.         if(buffer.readableBytes() < 1) {
  83.         return false;
  84.         }
  85.  
  86.         switch(buffer.readUnsignedByte()) {
  87.  
  88.         case 14:
  89.         checkpoint(LoginState.HANDSHAKE);
  90.         return true;
  91.         }
  92.     case HANDSHAKE:
  93.  
  94.         if(buffer.readableBytes() < 1) {
  95.         return false;
  96.         }
  97.  
  98.         @SuppressWarnings("unused")
  99.         int nameHash = buffer.readUnsignedByte();
  100.  
  101.         PacketBuilder response = new PacketBuilder();
  102.  
  103.         response.writeBytes(INITIAL_RESPONSE);
  104.  
  105.         response.writeByte(0);
  106.  
  107.         response.writeLong(serverKeyGenerator.nextLong());
  108.  
  109.         channel.write(response);
  110.  
  111.         checkpoint(LoginState.GAME);
  112.         return true;
  113.     case GAME:
  114.  
  115.         PacketBuilder loginResponse = new PacketBuilder();
  116.  
  117.         if(buffer.readableBytes() < 2) {
  118.         return false;
  119.         }
  120.  
  121.         int loginOpcode = buffer.readUnsignedByte();
  122.         switch(loginOpcode) {
  123.         case 16:
  124.         case 18:
  125.  
  126.         int loginSize = buffer.readUnsignedByte();
  127.  
  128.         int loginSizeE = loginSize - (36 + 1 + 1 + 2);
  129.  
  130.         if (loginSizeE < 1) {
  131.             channel.close();
  132.             return false;
  133.         }
  134.        
  135.         if (buffer.readableBytes() < loginSize) {
  136.             return false;
  137.         }
  138.  
  139.         if (buffer.readUnsignedByte() != 255) {
  140.             channel.close();
  141.             return false;
  142.         }
  143.  
  144.         if(buffer.readShort() != 317) {
  145.             channel.write(loginResponse.writeByte(ResponseCodeUtils.OUTDATED_CLIENT));
  146.             return false;
  147.         }
  148.        
  149.         @SuppressWarnings("unused")
  150.         int memory = buffer.readUnsignedByte();
  151.  
  152.         for (int i = 0; i < 9; i++) buffer.readInt();
  153.    
  154.         loginSizeE--;
  155.  
  156.         if (buffer.readUnsignedByte() != loginSizeE) {
  157.             channel.close();
  158.             return false;
  159.         }
  160.  
  161.         if (buffer.readUnsignedByte() != 10) {
  162.             channel.close();
  163.             return false;
  164.         }
  165.        
  166.         @SuppressWarnings("unused")
  167.         int clientId = buffer.readUnsignedByte();
  168.  
  169.         String username = NameUtils.formatName(BufferUtils.readRS2String(buffer));
  170.  
  171.         String password = BufferUtils.readRS2String(buffer);
  172.  
  173.         channel.write(loginResponse.writeByte(ResponseCodeUtils.SUCCESS));
  174.  
  175.         Player player = new Player(username, password, channel);
  176.  
  177.         loginResponse.writeByte(player.getPlayerRights());
  178.  
  179.         loginResponse.writeByte(player.isUserAccountFlagged() ? 0 : 1);
  180.  
  181.         channel.write(loginResponse);
  182.        
  183.         return true;
  184.         }
  185.         return true;
  186.         /** anything else cannot be serviced **/
  187.     default:
  188.         return false;
  189.     }
  190.     }
  191.  
  192. }
Add Comment
Please, Sign In to add comment