Skip to content

Commit

Permalink
enforce non-encryption packets to be encrypted
Browse files Browse the repository at this point in the history
  • Loading branch information
acrylic-style committed Apr 14, 2022
1 parent 83e0c1e commit a094d4e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ public T getHandler() {
* @return true if the message was sent successfully, false otherwise.
*/
public boolean sendPacket(@NotNull PacketSender sender, @NotNull M msg) {
if (id != 0 && !sender.isEncrypted()) {
throw new IllegalStateException("Cannot send packet " + id + " without encryption");
}
try (ByteArrayOutputStream bout = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(bout)) {
out.writeByte(id);
Expand Down Expand Up @@ -148,6 +151,9 @@ public static void handleProxySide(ServerConnection server, byte[] rawData) {
try (ByteArrayInputStream bin = new ByteArrayInputStream(data);
DataInputStream in = new DataInputStream(bin)) {
byte id = (byte) (in.readByte() & 0xFF);
if (id != 0 && !server.isEncrypted()) {
throw new RuntimeException("Packet " + id + " must be sent encrypted (server: " + server + ")");
}
Protocol<?, ?> protocol = Protocol.getById(PacketFlow.TO_PROXY, id);
if (protocol == null) {
Logger.getCurrentLogger().warn(
Expand Down Expand Up @@ -190,6 +196,9 @@ public static void handleServerSide(@NotNull PacketSender sender, byte[] rawData
try (ByteArrayInputStream bin = new ByteArrayInputStream(data);
DataInputStream in = new DataInputStream(bin)) {
byte id = (byte) (in.readByte() & 0xFF);
if (id != 0 && !sender.isEncrypted()) {
throw new RuntimeException("Packet " + id + " must be sent encrypted (sender: " + sender + ")");
}
Protocol<?, ?> protocol = Protocol.getById(PacketFlow.TO_SERVER, id);
if (protocol == null) {
Logger.getCurrentLogger().warn("Received unknown protocol id from {}: {}", sender, id);
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group = "net.azisaba.azipluginmessaging"
version = "2.0.1"
version = "2.0.2"

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

public class PlayerImpl implements Player, PacketSender {
private static final Map<UUID, PlayerImpl> MAP = new ConcurrentHashMap<>();
private final org.bukkit.entity.Player handle;
private org.bukkit.entity.Player handle;
private KeyPair keyPair;
private PublicKey remotePublicKey;
private boolean encrypted = false;
Expand All @@ -33,7 +33,9 @@ private PlayerImpl(@Nullable org.bukkit.entity.Player handle) {

@NotNull
public static PlayerImpl of(@NotNull org.bukkit.entity.Player player) {
return MAP.computeIfAbsent(player.getUniqueId(), u -> new PlayerImpl(player));
PlayerImpl p = MAP.computeIfAbsent(player.getUniqueId(), u -> new PlayerImpl(player));
p.handle = player;
return p;
}

@NotNull
Expand Down

0 comments on commit a094d4e

Please sign in to comment.