Search in sources :

Example 1 with MapleAESOFB

use of tools.MapleAESOFB in project HeavenMS by ronancpl.

the class MaplePacketEncoder method encode.

@Override
public void encode(final IoSession session, final Object message, final ProtocolEncoderOutput out) throws Exception {
    final MapleClient client = (MapleClient) session.getAttribute(MapleClient.CLIENT_KEY);
    try {
        client.lockClient();
        try {
            final MapleAESOFB send_crypto = client.getSendCrypto();
            final byte[] input = (byte[]) message;
            final byte[] unencrypted = new byte[input.length];
            System.arraycopy(input, 0, unencrypted, 0, input.length);
            final byte[] ret = new byte[unencrypted.length + 4];
            final byte[] header = send_crypto.getPacketHeader(unencrypted.length);
            MapleCustomEncryption.encryptData(unencrypted);
            send_crypto.crypt(unencrypted);
            System.arraycopy(header, 0, ret, 0, 4);
            System.arraycopy(unencrypted, 0, ret, 4, unencrypted.length);
            out.write(IoBuffer.wrap(ret));
        } finally {
            client.unlockClient();
        }
    // System.arraycopy(unencrypted, 0, ret, 4, unencrypted.length);
    // out.write(ByteBuffer.wrap(ret));
    } catch (NullPointerException npe) {
        out.write(IoBuffer.wrap(((byte[]) message)));
    }
}
Also used : MapleAESOFB(tools.MapleAESOFB) MapleClient(client.MapleClient)

Example 2 with MapleAESOFB

use of tools.MapleAESOFB in project HeavenMS by ronancpl.

the class MapleServerHandler method sessionOpened.

@Override
public void sessionOpened(IoSession session) {
    if (!Server.getInstance().isOnline()) {
        session.close(true);
        return;
    }
    if (channel > -1 && world > -1) {
        if (Server.getInstance().getChannel(world, channel) == null) {
            session.close(true);
            return;
        }
    } else {
        FilePrinter.print(FilePrinter.SESSION, "IoSession with " + session.getRemoteAddress() + " opened on " + sdf.format(Calendar.getInstance().getTime()), false);
    }
    byte[] key = { 0x13, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, (byte) 0xB4, 0x00, 0x00, 0x00, 0x1B, 0x00, 0x00, 0x00, 0x0F, 0x00, 0x00, 0x00, 0x33, 0x00, 0x00, 0x00, 0x52, 0x00, 0x00, 0x00 };
    byte[] ivRecv = { 70, 114, 122, 82 };
    byte[] ivSend = { 82, 48, 120, 115 };
    ivRecv[3] = (byte) (Math.random() * 255);
    ivSend[3] = (byte) (Math.random() * 255);
    MapleAESOFB sendCypher = new MapleAESOFB(key, ivSend, (short) (0xFFFF - ServerConstants.VERSION));
    MapleAESOFB recvCypher = new MapleAESOFB(key, ivRecv, (short) ServerConstants.VERSION);
    MapleClient client = new MapleClient(sendCypher, recvCypher, session);
    client.setWorld(world);
    client.setChannel(channel);
    // Generates a reasonable session id.
    client.setSessionId(sessionId.getAndIncrement());
    session.write(MaplePacketCreator.getHello(ServerConstants.VERSION, ivSend, ivRecv));
    session.setAttribute(MapleClient.CLIENT_KEY, client);
}
Also used : MapleAESOFB(tools.MapleAESOFB) MapleClient(client.MapleClient)

Example 3 with MapleAESOFB

use of tools.MapleAESOFB in project HeavenMS by ronancpl.

the class MaplePacketDecoder method doDecode.

@Override
protected boolean doDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception {
    final MapleClient client = (MapleClient) session.getAttribute(MapleClient.CLIENT_KEY);
    if (client == null) {
        session.close(true);
        return false;
    }
    DecoderState decoderState = (DecoderState) session.getAttribute(DECODER_STATE_KEY);
    if (decoderState == null) {
        decoderState = new DecoderState();
        session.setAttribute(DECODER_STATE_KEY, decoderState);
    }
    MapleAESOFB rcvdCrypto = client.getReceiveCrypto();
    if (in.remaining() >= 4 && decoderState.packetlength == -1) {
        int packetHeader = in.getInt();
        if (!rcvdCrypto.checkPacket(packetHeader)) {
            session.close(true);
            return false;
        }
        decoderState.packetlength = MapleAESOFB.getPacketLength(packetHeader);
    } else if (in.remaining() < 4 && decoderState.packetlength == -1) {
        return false;
    }
    if (in.remaining() >= decoderState.packetlength) {
        byte[] decryptedPacket = new byte[decoderState.packetlength];
        in.get(decryptedPacket, 0, decoderState.packetlength);
        decoderState.packetlength = -1;
        rcvdCrypto.crypt(decryptedPacket);
        MapleCustomEncryption.decryptData(decryptedPacket);
        out.write(decryptedPacket);
        return true;
    }
    return false;
}
Also used : MapleAESOFB(tools.MapleAESOFB) MapleClient(client.MapleClient)

Aggregations

MapleClient (client.MapleClient)3 MapleAESOFB (tools.MapleAESOFB)3