Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.rs2.net.login;
- import java.security.SecureRandom;
- import org.jboss.netty.buffer.ChannelBuffer;
- import org.jboss.netty.buffer.ChannelBuffers;
- import org.jboss.netty.channel.Channel;
- import org.jboss.netty.channel.ChannelHandlerContext;
- import org.jboss.netty.handler.codec.replay.ReplayingDecoder;
- import static org.rs2.net.util.ChannelBufferUtils.readString;
- /**
- * The login decoder.
- * @author Eric Hollander
- *
- */
- public class Rs2LoginDecoder extends ReplayingDecoder<Rs2LoginState> {
- /**
- * The session key exchange opcode.
- */
- public static final int SESSION_KEY_OPCODE = 14;
- /**
- * The connection opcode.
- */
- public static final int CONNECTION_OPCODE = 16;
- /**
- * The reconnection opcode.
- */
- public static final int RECONNECTION_OPCODE = 18;
- /**
- * The random generator.
- */
- public static final SecureRandom RANDOM = new SecureRandom();
- /**
- * The class constructor.
- * @param state The default state.
- */
- public Rs2LoginDecoder(Rs2LoginState state) {
- super(state);
- }
- @Override
- protected Object decode(ChannelHandlerContext ctx, Channel channel,
- ChannelBuffer in, Rs2LoginState state) throws Exception {
- /*
- * Prepare an outgoing buffer.
- */
- ChannelBuffer out = ChannelBuffers.dynamicBuffer();
- switch(state) {
- case OPCODE_REQUEST:
- /*
- * Read the opcode from the incoming buffer.
- */
- int opcode = in.readByte();
- switch(opcode) {
- case SESSION_KEY_OPCODE:
- /*
- * Prepare for session key request and call decode again.
- */
- checkpoint(Rs2LoginState.SESSION_KEY_REQUEST);
- break;
- case CONNECTION_OPCODE:
- /*
- * Prepare for connection request and call decode again.
- */
- checkpoint(Rs2LoginState.CONNECTION_REQUEST);
- break;
- case RECONNECTION_OPCODE:
- /*
- * Prepare for reconnection request and call decode again.
- */
- checkpoint(Rs2LoginState.RECONNECTION_REQUEST);
- break;
- }
- break;
- case SESSION_KEY_REQUEST:
- /*
- * Read the name part from the buffer (unknown function/meaning)
- */
- in.readByte();
- /*
- * Write 8 ignored bytes plus one response byte (0).
- */
- for(int i = 0; i < 9; i++) {
- out.writeByte(0);
- }
- /*
- * Write our key.
- */
- out.writeLong(RANDOM.nextLong());
- break;
- case CONNECTION_REQUEST:
- case RECONNECTION_REQUEST:
- /*
- * Reads 2 bytes, the size and the magic 255 byte.
- */
- in.readShort();
- /*
- * Read the client version.
- */
- int clientVersion = in.readShort();
- /*
- * Since we don't take highmem into account, just read it.
- */
- in.readByte();
- /*
- * Read 9 CRCs, ignored.
- */
- for(int i = 0; i < 9; i++) {
- in.readByte();
- }
- /*
- * Encrypted size + ignored byte (both ignored).
- */
- in.readShort();
- /*
- * Read the client + server keys. Used for ISAAC seeding.
- * TODO: Add ISAAC implementation.
- */
- long clientSessionKey = in.readLong();
- long serverSessionKey = in.readLong();
- /*
- * Read UID (ignored, unless you want cheaphax cheat client prevention).
- */
- in.readInt();
- /*
- * Read username + password from the buffer.
- */
- String username = readString(in);
- String password = readString(in);
- /*
- * Write response + player rights + flagged.
- */
- out.writeByte(2);
- out.writeByte(0);
- out.writeByte(0);
- /*
- * TODO: World loading via MySQL + valid auth checks.
- */
- break;
- }
- /*
- * Write the outgoing buffer to the connected channel.
- */
- channel.write(out);
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement