Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class LoginProtocolDecoder extends ReplayingDecoder<LoginProtocolDecoder.LoginState> {
- public enum LoginState {
- GAME, HANDSHAKE, REQUEST
- }
- private static final byte[] INITIAL_RESPONSE = {
- 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0
- };
- private static final SecureRandom sessionKey = new SecureRandom();
- public LoginProtocolDecoder() {
- checkpoint(LoginState.REQUEST);
- }
- @Override
- protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer, LoginState state) {
- 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;
- }
- int nameHash = buffer.readUnsignedByte();
- PacketConstuctor response = new PacketConstuctor();
- response.writeBytes(INITIAL_RESPONSE);
- response.writeByte(0);
- response.writeLong(sessionKey.nextLong());
- channel.write(response);
- checkpoint(LoginState.GAME);
- return true;
- case GAME:
- PacketConstuctor loginResponse = new PacketConstuctor();
- if (buffer.readableBytes() < 2) {
- return false;
- }
- int loginCode = buffer.readUnsignedByte();
- switch (loginCode) {
- case 16:
- case 18:
- int loginSize = buffer.readUnsignedByte();
- int encryptedLoginSize = loginSize - (36 + 1 + 1 + 2);
- if (encryptedLoginSize < 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(6));
- return false;
- }
- int memory = buffer.readUnsignedByte();
- System.out.println(memory); //0 = low 1 = high
- for (int i = 0; i < 9; i++) {
- buffer.readInt();
- }
- encryptedLoginSize--;
- if (buffer.readUnsignedByte() != encryptedLoginSize) {
- channel.close();
- return false;
- }
- if (buffer.readUnsignedByte() != 10) {
- channel.close();
- return false;
- }
- //int clientId = buffer.readUnsignedByte();
- final long clientSessionKey = buffer.readLong();
- final long serverSessionKey = buffer.readLong();
- final int[] isaacSeed = {
- (int) (clientSessionKey >> 32),
- (int) clientSessionKey,
- (int) (serverSessionKey >> 32),
- (int) serverSessionKey };
- final int userID = buffer.readInt();
- final String username = Utils.formatName(Utils.readRS2String(buffer));
- final String password = Utils.readRS2String(buffer);
- final IsaacRandom inCipher = new IsaacRandom(isaacSeed);
- for (int i = 0; i < isaacSeed.length; i++)
- isaacSeed[i] += 50;
- channel.write(loginResponse.writeByte(2));
- //loginResponse.writeByte(0); //logout..
- Client c = new Client(ctx);
- c.bindCharacter(new Character(password, password));
- c.getCharacter().setUsername(username);
- c.getCharacter().setPassword(password);
- c.load();
- loginResponse.writeByte(c.getCharacter().getCharRights());
- //channel.write(loginResponse.writeByte(0));
- channel.getPipeline().replace("decoder", "decoder", new RS2Decoder(inCipher));
- channel.write(loginResponse);
- //return valide(ctx, username, password);
- }
- return true;
- default:
- return null;
- }
- }
- /*
- private static boolean valide(ChannelHandlerContext ctx, String username, String password) {
- //TEMP LOADER!
- Client c = new Client(ctx);
- c.bindCharacter(new Character(password, password));
- c.getCharacter().setUsername(username);
- c.getCharacter().setPassword(password);
- //client.load();
- return true;
- }
- */
- }
Add Comment
Please, Sign In to add comment