Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.dodian.network.login;
- import java.net.InetSocketAddress;
- 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.frame.FrameDecoder;
- import net.dodian.GameSettings;
- import net.dodian.game.World;
- import net.dodian.game.model.player.Player;
- import net.dodian.network.PlayerSession;
- import net.dodian.network.packet.PacketBuilder;
- import net.dodian.network.packet.codec.PacketDecoder;
- import net.dodian.network.packet.codec.PacketEncoder;
- import net.dodian.network.security.IsaacRandom;
- import net.dodian.network.sql.integration.ForumIntegration;
- import net.dodian.util.Misc;
- import net.dodian.util.StringUtils;
- /**
- * A {@link FrameDecoder} which decodes
- * the login requests.
- *
- * @author Gabriel Hannason
- */
- public final class LoginDecoder extends FrameDecoder {
- private static final int CONNECTED = 0;
- private static final int LOGGING_IN = 1;
- private int state = CONNECTED;
- private long seed;
- public static void sendReturnCode(Channel channel, int code) {
- channel.write(new PacketBuilder().put(code).toPacket())
- .addListener(arg0 -> arg0.getChannel().close());
- }
- @Override
- protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer)
- throws Exception {
- switch (state) {
- case CONNECTED:
- if (buffer.readableBytes() < 2) {
- sendReturnCode(channel, LoginResponses.LOGIN_COULD_NOT_COMPLETE);
- return null;
- }
- int request = buffer.readUnsignedByte();
- if (request != 14) {
- sendReturnCode(channel, LoginResponses.LOGIN_COULD_NOT_COMPLETE);
- return null;
- }
- buffer.readUnsignedByte();
- seed = new SecureRandom().nextLong();
- channel.write(new PacketBuilder().putLong(0).put((byte) 0).putLong(seed).toPacket());
- state = LOGGING_IN;
- return null;
- case LOGGING_IN:
- if (buffer.readableBytes() < 2) {
- sendReturnCode(channel, LoginResponses.LOGIN_COULD_NOT_COMPLETE);
- return null;
- }
- int loginType = buffer.readByte();
- if (loginType != 16 && loginType != 18) {
- sendReturnCode(channel, LoginResponses.LOGIN_COULD_NOT_COMPLETE);
- return null;
- }
- int blockLength = buffer.readByte() & 0xff;
- if (buffer.readableBytes() < blockLength) {
- sendReturnCode(channel, LoginResponses.LOGIN_COULD_NOT_COMPLETE);
- return null;
- }
- int magicId = buffer.readUnsignedByte();
- if (magicId != 0xFF) {
- sendReturnCode(channel, LoginResponses.LOGIN_COULD_NOT_COMPLETE);
- return null;
- }
- int clientVersion = buffer.readShort();
- if (clientVersion != GameSettings.GAME_VERSION) {
- sendReturnCode(channel, LoginResponses.OLD_CLIENT_VERSION);
- return null;
- }
- int memory = buffer.readByte();
- if (memory != 0 && memory != 1) {
- sendReturnCode(channel, LoginResponses.LOGIN_COULD_NOT_COMPLETE);
- return null;
- }
- int[] archiveCrcs = new int[9];
- for (int i = 0; i < 9; i++) {
- archiveCrcs[i] = buffer.readInt();
- }
- buffer.readUnsignedByte();
- /*ChannelBuffer rsaBuffer = buffer.readBytes(length);
- BigInteger bigInteger = new BigInteger(rsaBuffer.array());
- bigInteger = bigInteger.modPow(GameSettings.RSA_EXPONENT, GameSettings.RSA_MODULUS);
- rsaBuffer = ChannelBuffers.wrappedBuffer(bigInteger.toByteArray());*/
- int securityId = buffer.readByte();
- if (securityId != 10) {
- sendReturnCode(channel, LoginResponses.LOGIN_COULD_NOT_COMPLETE);
- return null;
- }
- long clientSeed = buffer.readLong();
- long seedReceived = buffer.readLong();
- if (seedReceived != seed) {
- sendReturnCode(channel, LoginResponses.LOGIN_BAD_SESSION_ID);
- return null;
- }
- int[] seed = new int[4];
- seed[0] = (int) (clientSeed >> 32);
- seed[1] = (int) clientSeed;
- seed[2] = (int) (this.seed >> 32);
- seed[3] = (int) this.seed;
- IsaacRandom decodingRandom = new IsaacRandom(seed);
- for (int i = 0; i < seed.length; i++) {
- seed[i] += 50;
- }
- int uid = buffer.readInt();
- String username = Misc.readString(buffer);
- String password = Misc.readString(buffer);
- String authCode = Misc.readString(buffer);
- String uuid = Misc.readString(buffer);
- if (username.length() > 12 || password.length() > 20) {
- sendReturnCode(channel, LoginResponses.LOGIN_INVALID_CREDENTIALS);
- return null;
- }
- username = Misc.formatText(username.toLowerCase());
- channel.getPipeline()
- .replace("encoder", "encoder", new PacketEncoder(new IsaacRandom(seed)));
- channel.getPipeline().replace("decoder", "decoder", new PacketDecoder(decodingRandom));
- return login(channel, new LoginDetailsMessage(username, password,
- ((InetSocketAddress) channel
- .getRemoteAddress())
- .getAddress().getHostAddress(), authCode, uuid,
- clientVersion, uid));
- }
- return null;
- }
- public Player login(Channel channel, LoginDetailsMessage msg) {
- PlayerSession session = new PlayerSession(channel);
- Player player = new Player(session).setUsername(msg.getUsername())
- .setLongUsername(StringUtils.stringToLong(msg.getUsername()))
- .setPassword(msg.getPassword()).setHostAddress(msg.getHost())
- .setSerialNumber(msg.getUuid());
- player.setAuthCode(msg.getAuthCode());
- session.setPlayer(player);
- int response = LoginResponses.getResponse(player, msg);
- if (msg.getUsername() != "Test") {
- // response = ForumIntegration.checkUser(player);
- }
- if (response == LoginResponses.LOGIN_SUCCESSFUL) {
- if (World.addLoginRequest(player)) {
- return player;
- } else {
- response = LoginResponses.LOGIN_ACCOUNT_ONLINE;
- }
- }
- sendReturnCode(channel, response);
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement