Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.augustus.net.codec;
- import java.security.SecureRandom;
- import org.jboss.netty.buffer.ChannelBuffer;
- import org.jboss.netty.channel.Channel;
- import org.jboss.netty.channel.ChannelHandlerContext;
- import org.jboss.netty.handler.codec.replay.ReplayingDecoder;
- import com.augustus.model.player.Player;
- import com.augustus.net.util.BufferUtils;
- import com.augustus.net.util.NameUtils;
- import com.augustus.net.util.ResponseCodeUtils;
- import com.augustus.packet.PacketBuilder;
- /**
- * Performs decoding measures which decodes a new connection into
- * a login request which is then processed by the server to enable
- * game access for the user.
- *
- * @author Ryley Kimmel
- */
- public class LoginDecoder extends ReplayingDecoder<LoginDecoder.LoginState> {
- /**
- * We checkpoint to the Request state.
- */
- public LoginDecoder() {
- checkpoint(LoginState.REQUEST);
- }
- /**
- * The states of the decoding process.
- *
- * @author Ryley Kimmel
- */
- public enum LoginState {
- /**
- * At this state the client sends a decode request
- * to the server. Here the server identifies what state
- * it needs to perform by reading an unsigned byte.
- */
- REQUEST,
- /**
- * At this state the server decides the best login server
- * choice for the user.
- */
- HANDSHAKE,
- /**
- * At this state the server has finished decoding and now
- * provides the client with information to login after
- * authenticating the user name and password sent by the client.
- *
- * The server at this time swaps the pipeline's decoder to
- * the message decoder.
- */
- GAME;
- }
- /**
- * Initial response, we send back 8 bytes.
- */
- private static final byte[] INITIAL_RESPONSE = {
- 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0
- };
- /**
- * Generates random numbers.
- */
- private static final SecureRandom serverKeyGenerator = new SecureRandom();
- @Override
- protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer, LoginState state) throws Exception {
- switch(state) {
- case REQUEST:
- if(buffer.readableBytes() < 1) {
- return false;
- }
- switch(buffer.readUnsignedByte()) {
- case 14:
- checkpoint(LoginState.HANDSHAKE);
- return true;
- }
- case HANDSHAKE:
- if(buffer.readableBytes() < 1) {
- return false;
- }
- @SuppressWarnings("unused")
- int nameHash = buffer.readUnsignedByte();
- PacketBuilder response = new PacketBuilder();
- response.writeBytes(INITIAL_RESPONSE);
- response.writeByte(0);
- response.writeLong(serverKeyGenerator.nextLong());
- channel.write(response);
- checkpoint(LoginState.GAME);
- return true;
- case GAME:
- PacketBuilder loginResponse = new PacketBuilder();
- if(buffer.readableBytes() < 2) {
- return false;
- }
- int loginOpcode = buffer.readUnsignedByte();
- switch(loginOpcode) {
- case 16:
- case 18:
- int loginSize = buffer.readUnsignedByte();
- int loginSizeE = loginSize - (36 + 1 + 1 + 2);
- if (loginSizeE < 1) {
- channel.close();
- return false;
- }
- if (buffer.readableBytes() < loginSize) {
- return false;
- }
- if (buffer.readUnsignedByte() != 255) {
- channel.close();
- return false;
- }
- if(buffer.readShort() != 317) {
- channel.write(loginResponse.writeByte(ResponseCodeUtils.OUTDATED_CLIENT));
- return false;
- }
- @SuppressWarnings("unused")
- int memory = buffer.readUnsignedByte();
- for (int i = 0; i < 9; i++) buffer.readInt();
- loginSizeE--;
- if (buffer.readUnsignedByte() != loginSizeE) {
- channel.close();
- return false;
- }
- if (buffer.readUnsignedByte() != 10) {
- channel.close();
- return false;
- }
- @SuppressWarnings("unused")
- int clientId = buffer.readUnsignedByte();
- String username = NameUtils.formatName(BufferUtils.readRS2String(buffer));
- String password = BufferUtils.readRS2String(buffer);
- channel.write(loginResponse.writeByte(ResponseCodeUtils.SUCCESS));
- Player player = new Player(username, password, channel);
- loginResponse.writeByte(player.getPlayerRights());
- loginResponse.writeByte(player.isUserAccountFlagged() ? 0 : 1);
- channel.write(loginResponse);
- return true;
- }
- return true;
- /** anything else cannot be serviced **/
- default:
- return false;
- }
- }
- }
Add Comment
Please, Sign In to add comment