Set up packet codec and channel initialization

This commit is contained in:
P0nk
2021-06-22 20:32:35 +02:00
parent 171215e653
commit 5431150362
7 changed files with 17 additions and 10 deletions

View File

@@ -26,7 +26,6 @@ import config.YamlConfig;
import constants.game.GameConstants; import constants.game.GameConstants;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.channel.ChannelInboundHandlerAdapter;
import net.netty.ClientCyphers;
import net.netty.InvalidPacketHeaderException; import net.netty.InvalidPacketHeaderException;
import net.server.Server; import net.server.Server;
import net.server.audit.locks.MonitoredLockType; import net.server.audit.locks.MonitoredLockType;
@@ -122,9 +121,7 @@ public class MapleClient extends ChannelInboundHandlerAdapter {
return lastPacket; return lastPacket;
} }
public MapleClient(ClientCyphers cyphers) { public MapleClient() {
this.send = cyphers.getSendCypher();
this.receive = cyphers.getReceiveCypher();
this.session = null; // TODO remove once the other constructor is removed this.session = null; // TODO remove once the other constructor is removed
} }

View File

@@ -59,7 +59,7 @@ public class MaplePacketDecoder extends CumulativeProtocolDecoder {
MapleAESOFB rcvdCrypto = client.getReceiveCrypto(); MapleAESOFB rcvdCrypto = client.getReceiveCrypto();
if (in.remaining() >= 4 && decoderState.packetlength == -1) { if (in.remaining() >= 4 && decoderState.packetlength == -1) {
int packetHeader = in.getInt(); int packetHeader = in.getInt();
if (!rcvdCrypto.checkPacket(packetHeader)) { if (!rcvdCrypto.isValidHeader(packetHeader)) {
MapleSessionCoordinator.getInstance().closeSession(session, true); MapleSessionCoordinator.getInstance().closeSession(session, true);
return false; return false;
} }

View File

@@ -14,7 +14,7 @@ public class ClientInitializer extends ChannelInitializer<SocketChannel> {
final String clientIp = socketChannel.remoteAddress().getHostName(); final String clientIp = socketChannel.remoteAddress().getHostName();
log.debug("Client initiated new connection from: {}", clientIp); log.debug("Client initiated new connection from: {}", clientIp);
MapleClient client = new MapleClient(ClientCyphers.generateNew()); socketChannel.pipeline().addLast("PacketCodec", new PacketCodec(ClientCyphers.generateNew()));
socketChannel.pipeline().addLast("MapleClient", client); socketChannel.pipeline().addLast("MapleClient", new MapleClient());
} }
} }

View File

@@ -0,0 +1,9 @@
package net.netty;
import io.netty.channel.CombinedChannelDuplexHandler;
public class PacketCodec extends CombinedChannelDuplexHandler<PacketDecoder, PacketEncoder> {
public PacketCodec(ClientCyphers clientCyphers) {
super(new PacketDecoder(clientCyphers.getReceiveCypher()), new PacketEncoder(clientCyphers.getSendCypher()));
}
}

View File

@@ -19,7 +19,7 @@ public class PacketDecoder extends ReplayingDecoder<Void> {
protected void decode(ChannelHandlerContext context, ByteBuf in, List<Object> out) { protected void decode(ChannelHandlerContext context, ByteBuf in, List<Object> out) {
final int header = in.readInt(); final int header = in.readInt();
if (!receiveCypher.checkPacket(header)) { if (!receiveCypher.isValidHeader(header)) {
throw new InvalidPacketHeaderException("Attempted to decode a packet with an invalid header", header); throw new InvalidPacketHeaderException("Attempted to decode a packet with an invalid header", header);
} }

View File

@@ -4,6 +4,7 @@ import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder; import io.netty.handler.codec.MessageToByteEncoder;
import net.mina.MapleCustomEncryption; import net.mina.MapleCustomEncryption;
import net.packet.OutPacket;
import tools.MapleAESOFB; import tools.MapleAESOFB;
public class PacketEncoder extends MessageToByteEncoder<OutPacket> { public class PacketEncoder extends MessageToByteEncoder<OutPacket> {

View File

@@ -158,12 +158,12 @@ public class MapleAESOFB {
return packetLength; return packetLength;
} }
public boolean checkPacket(byte[] packet) { private boolean checkPacket(byte[] packet) {
return ((((packet[0] ^ iv[2]) & 0xFF) == ((mapleVersion >> 8) & 0xFF)) && return ((((packet[0] ^ iv[2]) & 0xFF) == ((mapleVersion >> 8) & 0xFF)) &&
(((packet[1] ^ iv[3]) & 0xFF) == (mapleVersion & 0xFF))); (((packet[1] ^ iv[3]) & 0xFF) == (mapleVersion & 0xFF)));
} }
public boolean checkPacket(int packetHeader) { public boolean isValidHeader(int packetHeader) {
byte[] packetHeaderBuf = new byte[2]; byte[] packetHeaderBuf = new byte[2];
packetHeaderBuf[0] = (byte) ((packetHeader >> 24) & 0xFF); packetHeaderBuf[0] = (byte) ((packetHeader >> 24) & 0xFF);
packetHeaderBuf[1] = (byte) ((packetHeader >> 16) & 0xFF); packetHeaderBuf[1] = (byte) ((packetHeader >> 16) & 0xFF);