many custom types (gamemode, ...), fix network encryption, PacketJoinGame, better log with unknown packets

This commit is contained in:
bixilon 2020-06-03 02:34:08 +02:00
parent c044abbdc3
commit adbc77df2e
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
9 changed files with 189 additions and 26 deletions

View File

@ -0,0 +1,27 @@
package de.bixilon.minosoft.objects;
public enum Difficulty {
PEACEFUL(0),
EASY(1),
NORMAL(2),
HARD(3);
int id;
Difficulty(int id) {
this.id = id;
}
public static Difficulty byId(int id) {
for (Difficulty g : values()) {
if (g.getId() == id) {
return g;
}
}
return null;
}
public int getId() {
return id;
}
}

View File

@ -0,0 +1,26 @@
package de.bixilon.minosoft.objects;
public enum Dimension {
NETHER(-1),
OVERWORLD(0),
END(1);
int id;
Dimension(int id) {
this.id = id;
}
public static Dimension byId(int id) {
for (Dimension g : values()) {
if (g.getId() == id) {
return g;
}
}
return null;
}
public int getId() {
return id;
}
}

View File

@ -0,0 +1,27 @@
package de.bixilon.minosoft.objects;
public enum GameMode {
SURVIVAL(0),
CREATIVE(1),
ADVENTURE(3),
SPECTATOR(4);
int id;
GameMode(int id) {
this.id = id;
}
public static GameMode byId(int id) {
for (GameMode g : values()) {
if (g.getId() == id) {
return g;
}
}
return null;
}
public int getId() {
return id;
}
}

View File

@ -0,0 +1,30 @@
package de.bixilon.minosoft.objects;
public enum LevelType {
DEFAULT("default"),
FLAT("flat"),
LARGE_BIOMES("largeBiomes"),
AMPLIFIED("amplified"),
DEFAULT_1_1("default_1_1"),
CUSTOMIZED("customized"),
BUFFET("buffet");
String type;
LevelType(String type) {
this.type = type;
}
public static LevelType byType(String type) {
for (LevelType g : values()) {
if (g.getId().equals(type)) {
return g;
}
}
return null;
}
public String getId() {
return type;
}
}

View File

@ -7,9 +7,7 @@ import de.bixilon.minosoft.protocol.packets.serverbound.login.PacketEncryptionRe
import de.bixilon.minosoft.protocol.protocol.*;
import de.bixilon.minosoft.util.Util;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.SecretKey;
import java.io.DataInputStream;
import java.io.DataOutputStream;
@ -124,24 +122,16 @@ public class Network {
byte[] raw = p.write(connection.getVersion()).getOutBytes();
if (encryptionEnabled) {
// encrypt
byte[] encrypted;
try {
encrypted = cipherEncrypt.doFinal(raw);
} catch (IllegalBlockSizeException | BadPaddingException e) {
Log.fatal("Failed to encrypt!");
e.printStackTrace();
binQueue.remove(0);
continue;
}
byte[] encrypted = cipherEncrypt.update(raw);
binQueue.add(encrypted);
} else {
if (p instanceof PacketEncryptionResponse) {
// enable encryption
enableEncryption(((PacketEncryptionResponse) p).getSecretKey());
}
binQueue.add(raw);
}
queue.remove(0);
if (p instanceof PacketEncryptionResponse) {
// enable encryption
enableEncryption(((PacketEncryptionResponse) p).getSecretKey());
}
}
while (binQueueIn.size() > 0) {
@ -150,23 +140,16 @@ public class Network {
InPacketBuffer inPacketBuffer;
if (encryptionEnabled) {
// decrypt
byte[] decrypted;
try {
decrypted = cipherDecrypt.doFinal(raw);
} catch (IllegalBlockSizeException | BadPaddingException e) {
Log.fatal("Failed to decrypt!");
e.printStackTrace();
binQueueIn.remove(0);
continue;
}
byte[] decrypted = cipherDecrypt.update(raw);
inPacketBuffer = new InPacketBuffer(decrypted);
} else {
inPacketBuffer = new InPacketBuffer(raw);
}
Class<? extends ClientboundPacket> clazz = Protocol.getPacketByPacket(connection.getVersion().getProtocol().getPacketByCommand(connection.getConnectionState(), inPacketBuffer.getCommand()));
Packets.Clientbound p = connection.getVersion().getProtocol().getPacketByCommand(connection.getConnectionState(), inPacketBuffer.getCommand());
Class<? extends ClientboundPacket> clazz = Protocol.getPacketByPacket(p);
if (clazz == null) {
Log.warn("[IN] Unknown packet with command " + inPacketBuffer.getCommand());
Log.warn(String.format("[IN] Unknown packet with command %x (%s)", inPacketBuffer.getCommand(), ((p != null) ? p.name() : "UNKNOWN")));
binQueueIn.remove(0);
continue;
}

View File

@ -0,0 +1,53 @@
package de.bixilon.minosoft.protocol.packets.clientbound.play;
import de.bixilon.minosoft.logging.Log;
import de.bixilon.minosoft.objects.Difficulty;
import de.bixilon.minosoft.objects.Dimension;
import de.bixilon.minosoft.objects.GameMode;
import de.bixilon.minosoft.objects.LevelType;
import de.bixilon.minosoft.protocol.packets.ClientboundPacket;
import de.bixilon.minosoft.protocol.protocol.InPacketBuffer;
import de.bixilon.minosoft.protocol.protocol.PacketHandler;
import de.bixilon.minosoft.protocol.protocol.ProtocolVersion;
import de.bixilon.minosoft.util.BitByte;
public class PacketJoinGame implements ClientboundPacket {
int entityId;
boolean hardcore;
GameMode gameMode;
Dimension dimension;
Difficulty difficulty;
int maxPlayers;
LevelType levelType;
@Override
public void read(InPacketBuffer buffer, ProtocolVersion v) {
switch (v) {
case VERSION_1_7_10:
this.entityId = buffer.readInteger();
byte gameModeRaw = buffer.readByte();
hardcore = BitByte.isBitSet(gameModeRaw, 3);
// remove hardcore bit and get gamemode
gameModeRaw &= ~0x8;
gameMode = GameMode.byId(gameModeRaw);
dimension = Dimension.byId(buffer.readByte());
difficulty = Difficulty.byId(buffer.readByte());
maxPlayers = buffer.readByte();
levelType = LevelType.byType(buffer.readString());
break;
}
log();
}
@Override
public void log() {
Log.protocol(String.format("Receiving join game packet (entityId=%s, gameMode=%s, dimension=%s, difficulty=%s)", entityId, gameMode.name(), dimension.name(), difficulty.name()));
}
@Override
public void handle(PacketHandler h) {
h.handle(this);
}
}

View File

@ -4,6 +4,7 @@ import de.bixilon.minosoft.logging.Log;
import de.bixilon.minosoft.protocol.network.Connection;
import de.bixilon.minosoft.protocol.packets.clientbound.login.PacketEncryptionKeyRequest;
import de.bixilon.minosoft.protocol.packets.clientbound.login.PacketLoginSuccess;
import de.bixilon.minosoft.protocol.packets.clientbound.play.PacketJoinGame;
import de.bixilon.minosoft.protocol.packets.clientbound.status.PacketStatusPong;
import de.bixilon.minosoft.protocol.packets.clientbound.status.PacketStatusResponse;
import de.bixilon.minosoft.protocol.packets.serverbound.login.PacketEncryptionResponse;
@ -44,4 +45,7 @@ public class PacketHandler {
// now we are playing
connection.setConnectionState(ConnectionState.PLAY);
}
public void handle(PacketJoinGame pkg) {
}
}

View File

@ -3,6 +3,7 @@ package de.bixilon.minosoft.protocol.protocol;
import de.bixilon.minosoft.protocol.packets.ClientboundPacket;
import de.bixilon.minosoft.protocol.packets.clientbound.login.PacketEncryptionKeyRequest;
import de.bixilon.minosoft.protocol.packets.clientbound.login.PacketLoginSuccess;
import de.bixilon.minosoft.protocol.packets.clientbound.play.PacketJoinGame;
import de.bixilon.minosoft.protocol.packets.clientbound.status.PacketStatusPong;
import de.bixilon.minosoft.protocol.packets.clientbound.status.PacketStatusResponse;
@ -30,5 +31,7 @@ public interface Protocol {
packetClassMapping.put(Packets.Clientbound.STATUS_PONG, PacketStatusPong.class);
packetClassMapping.put(Packets.Clientbound.LOGIN_ENCRYPTION_REQUEST, PacketEncryptionKeyRequest.class);
packetClassMapping.put(Packets.Clientbound.LOGIN_LOGIN_SUCCESS, PacketLoginSuccess.class);
packetClassMapping.put(Packets.Clientbound.PLAY_JOIN_GAME, PacketJoinGame.class);
}
}

View File

@ -0,0 +1,10 @@
package de.bixilon.minosoft.util;
public class BitByte {
public static boolean isBitSet(byte in, int pos) {
boolean bitSet;
int mask = 1 << pos;
bitSet = ((in & mask) == mask);
return bitSet;
}
}