Restructure "net" packages

This commit is contained in:
P0nk
2021-07-18 14:26:23 +02:00
parent 11e83522d6
commit 9e3f3cc036
16 changed files with 93 additions and 99 deletions

View File

@@ -1,28 +0,0 @@
package net.netty;
import constants.net.ServerConstants;
import tools.MapleAESOFB;
public class ClientCyphers {
private final MapleAESOFB send;
private final MapleAESOFB receive;
private ClientCyphers(MapleAESOFB send, MapleAESOFB receive) {
this.send = send;
this.receive = receive;
}
public static ClientCyphers of(InitializationVector sendIv, InitializationVector receiveIv) {
MapleAESOFB send = new MapleAESOFB(sendIv, (short) (0xFFFF - ServerConstants.VERSION));
MapleAESOFB receive = new MapleAESOFB(receiveIv, ServerConstants.VERSION);
return new ClientCyphers(send, receive);
}
public MapleAESOFB getSendCypher() {
return send;
}
public MapleAESOFB getReceiveCypher() {
return receive;
}
}

View File

@@ -1,27 +0,0 @@
package net.netty;
public class InitializationVector {
private final byte[] bytes;
private InitializationVector(byte[] bytes) {
this.bytes = bytes;
}
public byte[] getBytes() {
return bytes;
}
public static InitializationVector generateSend() {
byte[] ivSend = {82, 48, 120, getRandomByte()};
return new InitializationVector(ivSend);
}
public static InitializationVector generateReceive() {
byte[] ivRecv = {70, 114, 122, getRandomByte()};
return new InitializationVector(ivRecv);
}
private static byte getRandomByte() {
return (byte) (Math.random() * 255);
}
}

View File

@@ -1,9 +0,0 @@
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

@@ -1,49 +0,0 @@
package net.netty;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ReplayingDecoder;
import net.MapleCustomEncryption;
import net.packet.ByteBufInPacket;
import tools.MapleAESOFB;
import java.util.List;
public class PacketDecoder extends ReplayingDecoder<Void> {
private final MapleAESOFB receiveCypher;
public PacketDecoder(MapleAESOFB receiveCypher) {
this.receiveCypher = receiveCypher;
}
@Override
protected void decode(ChannelHandlerContext context, ByteBuf in, List<Object> out) {
final int header = in.readInt();
if (!receiveCypher.isValidHeader(header)) {
throw new InvalidPacketHeaderException("Attempted to decode a packet with an invalid header", header);
}
final int packetLength = decodePacketLength(header);
byte[] packet = new byte[packetLength];
in.readBytes(packet);
receiveCypher.crypt(packet);
MapleCustomEncryption.decryptData(packet);
out.add(new ByteBufInPacket(Unpooled.wrappedBuffer(packet)));
}
/**
* @param header Packet header - the first 4 bytes of the packet
* @return Packet size in bytes
*/
private static int decodePacketLength(byte[] header) {
return (((header[1] ^ header[3]) & 0xFF) << 8) | ((header[0] ^ header[2]) & 0xFF);
}
private static int decodePacketLength(int header) {
int length = ((header >>> 16) ^ (header & 0xFFFF));
length = ((length << 8) & 0xFF00) | ((length >>> 8) & 0xFF);
return length;
}
}

View File

@@ -1,30 +0,0 @@
package net.netty;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import net.MapleCustomEncryption;
import net.packet.OutPacket;
import tools.MapleAESOFB;
public class PacketEncoder extends MessageToByteEncoder<OutPacket> {
private final MapleAESOFB sendCypher;
public PacketEncoder(MapleAESOFB sendCypher) {
this.sendCypher = sendCypher;
}
@Override
protected void encode(ChannelHandlerContext ctx, OutPacket in, ByteBuf out) {
byte[] packet = in.getBytes();
out.writeBytes(getEncodedHeader(packet.length));
MapleCustomEncryption.encryptData(packet);
sendCypher.crypt(packet);
out.writeBytes(packet);
}
private byte[] getEncodedHeader(int length) {
return sendCypher.getPacketHeader(length);
}
}

View File

@@ -9,6 +9,9 @@ import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.timeout.IdleStateHandler;
import net.encryption.ClientCyphers;
import net.encryption.InitializationVector;
import net.encryption.PacketCodec;
import net.packet.logging.InPacketLogger;
import net.packet.logging.OutPacketLogger;
import tools.MaplePacketCreator;