Skip to content

Commit

Permalink
misc: various code changes
Browse files Browse the repository at this point in the history
  • Loading branch information
MemencioPerez committed Nov 7, 2024
1 parent d34d233 commit 056f008
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import io.github._4drian3d.unsignedvelocity.configuration.Configuration;
import io.github._4drian3d.unsignedvelocity.listener.packet.ConfigurablePacketListener;

public final class ChatHeaderListener extends ConfigurablePacketListener {
public class ChatHeaderListener extends ConfigurablePacketListener {
@Inject
public ChatHeaderListener(Configuration configuration) {
super(PacketListenerPriority.LOWEST, configuration);
Expand All @@ -20,9 +20,9 @@ public boolean canBeLoaded() {
}

@Override
public void onPacketSend(final PacketSendEvent event) {
public void onPacketSend(PacketSendEvent event) {
if (event.isCancelled()) return;
final PacketTypeCommon packetType = event.getPacketType();
PacketTypeCommon packetType = event.getPacketType();
if (packetType == PacketType.Play.Server.PLAYER_CHAT_HEADER) {
event.setCancelled(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import io.github._4drian3d.unsignedvelocity.configuration.Configuration;
import io.github._4drian3d.unsignedvelocity.listener.packet.ConfigurablePacketListener;

public final class ChatSessionListener extends ConfigurablePacketListener {
public class ChatSessionListener extends ConfigurablePacketListener {
@Inject
public ChatSessionListener(Configuration configuration) {
super(PacketListenerPriority.LOWEST, configuration);
Expand All @@ -20,9 +20,9 @@ public boolean canBeLoaded() {
}

@Override
public void onPacketReceive(final PacketReceiveEvent event) {
public void onPacketReceive(PacketReceiveEvent event) {
if (event.isCancelled()) return;
final PacketTypeCommon packetType = event.getPacketType();
PacketTypeCommon packetType = event.getPacketType();
if (packetType == PacketType.Play.Client.CHAT_SESSION_UPDATE) {
event.setCancelled(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import java.time.Instant;

public final class ClientChatListener extends ConfigurablePacketListener {
public class ClientChatListener extends ConfigurablePacketListener {
@Inject
public ClientChatListener(Configuration configuration) {
super(PacketListenerPriority.LOWEST, configuration);
Expand All @@ -25,18 +25,17 @@ public boolean canBeLoaded() {
}

@Override
public void onPacketReceive(final PacketReceiveEvent event) {
public void onPacketReceive(PacketReceiveEvent event) {
if (event.isCancelled()) return;
final PacketTypeCommon packetType = event.getPacketType();
PacketTypeCommon packetType = event.getPacketType();
if (packetType == PacketType.Play.Client.CHAT_MESSAGE) {
final WrapperPlayClientChatMessage packet = new WrapperPlayClientChatMessage(event);
if (packet.getMessageSignData().isEmpty()) {
return;
WrapperPlayClientChatMessage packet = new WrapperPlayClientChatMessage(event);
if (packet.getMessageSignData().isPresent()) {
MessageSignData packetMessageSignData = packet.getMessageSignData().get();
Instant packetTimestamp = packetMessageSignData.getTimestamp();
packet.setMessageSignData(new MessageSignData(new SaltSignature(0L, new byte[0]), packetTimestamp));
event.markForReEncode(true);
}
MessageSignData packetMessageSignData = packet.getMessageSignData().get();
Instant packetTimestamp = packetMessageSignData.getTimestamp();
packet.setMessageSignData(new MessageSignData(new SaltSignature(0L, new byte[0]), packetTimestamp));
event.markForReEncode(true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@

import java.util.Optional;

public final class ServerChatListener extends ConfigurablePacketListener {
public class ServerChatListener extends ConfigurablePacketListener {
@Inject
public ServerChatListener(Configuration configuration) {
super(PacketListenerPriority.LOWEST, configuration);
}

private static @Nullable Component getComponentFromChatPacket(PacketSendEvent event) {
final WrapperPlayServerChatMessage packet = new WrapperPlayServerChatMessage(event);
WrapperPlayServerChatMessage packet = new WrapperPlayServerChatMessage(event);
ChatMessage chatMessage = packet.getMessage();
Component messageContent = chatMessage.getChatContent();
if (chatMessage instanceof ChatMessage_v1_19) {
Expand All @@ -48,18 +48,17 @@ public boolean canBeLoaded() {
}

@Override
public void onPacketSend(final PacketSendEvent event) {
public void onPacketSend(PacketSendEvent event) {
if (event.isCancelled()) return;
final PacketTypeCommon packetType = event.getPacketType();
ClientVersion version = event.getUser().getClientVersion();
PacketTypeCommon packetType = event.getPacketType();
if (packetType == PacketType.Play.Server.CHAT_MESSAGE) {
if (event.getUser().getClientVersion().isOlderThan(ClientVersion.V_1_19)) {
return;
if (version.isNewerThan(ClientVersion.V_1_18_2)) {
Component messageContent = getComponentFromChatPacket(event);
WrapperPlayServerSystemChatMessage newPacket = new WrapperPlayServerSystemChatMessage(false, messageContent);
event.getUser().sendPacketSilently(newPacket);
event.setCancelled(true);
}

Component messageContent = getComponentFromChatPacket(event);
final WrapperPlayServerSystemChatMessage newPacket = new WrapperPlayServerSystemChatMessage(false, messageContent);
event.getUser().sendPacketSilently(newPacket);
event.setCancelled(true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import java.time.Instant;

public final class CommandListener extends ConfigurablePacketListener {
public class CommandListener extends ConfigurablePacketListener {
@Inject
public CommandListener(Configuration configuration) {
super(PacketListenerPriority.LOWEST, configuration);
Expand All @@ -25,11 +25,11 @@ public boolean canBeLoaded() {
}

@Override
public void onPacketReceive(final PacketReceiveEvent event) {
public void onPacketReceive(PacketReceiveEvent event) {
if (event.isCancelled()) return;
final PacketTypeCommon packetType = event.getPacketType();
PacketTypeCommon packetType = event.getPacketType();
if (packetType == PacketType.Play.Client.CHAT_COMMAND) {
final WrapperPlayClientChatCommand packet = new WrapperPlayClientChatCommand(event);
WrapperPlayClientChatCommand packet = new WrapperPlayClientChatCommand(event);
MessageSignData packetMessageSignData = packet.getMessageSignData();
Instant packetTimestamp = packetMessageSignData.getTimestamp();
packet.setMessageSignData(new MessageSignData(new SaltSignature(0L, new byte[0]), packetTimestamp));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
import com.github.retrooper.packetevents.protocol.packettype.PacketType;
import com.github.retrooper.packetevents.protocol.packettype.PacketTypeCommon;
import com.github.retrooper.packetevents.protocol.player.ClientVersion;
import com.github.retrooper.packetevents.protocol.player.User;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerJoinGame;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerServerData;
import com.google.inject.Inject;
import io.github._4drian3d.unsignedvelocity.configuration.Configuration;
import io.github._4drian3d.unsignedvelocity.listener.packet.ConfigurablePacketListener;

public final class ServerDataListener extends ConfigurablePacketListener {
public class ServerDataListener extends ConfigurablePacketListener {
@Inject
public ServerDataListener(Configuration configuration) {
super(PacketListenerPriority.LOWEST, configuration);
Expand All @@ -24,24 +23,20 @@ public boolean canBeLoaded() {
}

@Override
public void onPacketSend(final PacketSendEvent event) {
public void onPacketSend(PacketSendEvent event) {
if (event.isCancelled()) return;
final User user = event.getUser();
final PacketTypeCommon packetType = event.getPacketType();
ClientVersion version = event.getUser().getClientVersion();
PacketTypeCommon packetType = event.getPacketType();
if (packetType == PacketType.Play.Server.SERVER_DATA) {
if (user.getClientVersion().isNewerThanOrEquals(ClientVersion.V_1_19_1) && user.getClientVersion().isOlderThanOrEquals(ClientVersion.V_1_20_3)) {
final WrapperPlayServerServerData packet = new WrapperPlayServerServerData(event);
if (!packet.isEnforceSecureChat()) {
packet.setEnforceSecureChat(true);
}
if (version.isNewerThan(ClientVersion.V_1_19) && version.isOlderThan(ClientVersion.V_1_20_5)) {
WrapperPlayServerServerData packet = new WrapperPlayServerServerData(event);
packet.setEnforceSecureChat(true);
event.markForReEncode(true);
}
} else if (packetType == PacketType.Play.Server.JOIN_GAME) {
if (user.getClientVersion().isNewerThanOrEquals(ClientVersion.V_1_20_5)) {
final WrapperPlayServerJoinGame packet = new WrapperPlayServerJoinGame(event);
if (!packet.isEnforcesSecureChat()) {
packet.setEnforcesSecureChat(true);
}
if (version.isNewerThan(ClientVersion.V_1_20_3)) {
WrapperPlayServerJoinGame packet = new WrapperPlayServerJoinGame(event);
packet.setEnforcesSecureChat(true);
event.markForReEncode(true);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.github.retrooper.packetevents.event.PacketListenerPriority;
import com.github.retrooper.packetevents.event.PacketReceiveEvent;
import com.github.retrooper.packetevents.event.PacketSendEvent;
import com.github.retrooper.packetevents.manager.server.VersionComparison;
import com.github.retrooper.packetevents.protocol.packettype.PacketType;
import com.github.retrooper.packetevents.protocol.packettype.PacketTypeCommon;
import com.github.retrooper.packetevents.protocol.player.ClientVersion;
Expand All @@ -23,7 +22,7 @@
import java.security.PublicKey;
import java.util.concurrent.TimeUnit;

public final class LoginListener extends ConfigurablePacketListener {
public class LoginListener extends ConfigurablePacketListener {
private final ProxyServer server;
private final Cache<User, byte[]> cache;

Expand Down Expand Up @@ -53,53 +52,46 @@ public boolean canBeLoaded() {

@Override
public void onPacketReceive(PacketReceiveEvent event) {
final User user = event.getUser();
final PacketTypeCommon packetType = event.getPacketType();
if (event.isCancelled()) return;
User user = event.getUser();
ClientVersion version = user.getClientVersion();
PacketTypeCommon packetType = event.getPacketType();
if (packetType == PacketType.Login.Client.LOGIN_START) {
if (!user.getClientVersion().is(VersionComparison.EQUALS, ClientVersion.V_1_19) && !user.getClientVersion().is(VersionComparison.EQUALS, ClientVersion.V_1_19_1)) {
return;
}

WrapperLoginClientLoginStart packet = new WrapperLoginClientLoginStart(event);

if (packet.getSignatureData().isEmpty()) {
return;
if (version.isNewerThan(ClientVersion.V_1_18_2) && version.isOlderThan(ClientVersion.V_1_19_3)) {
WrapperLoginClientLoginStart packet = new WrapperLoginClientLoginStart(event);
if (packet.getSignatureData().isPresent()) {
packet.setSignatureData(null);
event.markForReEncode(true);
}
}
packet.setSignatureData(null);
event.markForReEncode(true);
} else if (packetType == PacketType.Login.Client.ENCRYPTION_RESPONSE) {
if (!user.getClientVersion().is(VersionComparison.EQUALS, ClientVersion.V_1_19) && !user.getClientVersion().is(VersionComparison.EQUALS, ClientVersion.V_1_19_1)) {
return;
}

WrapperLoginClientEncryptionResponse packet = new WrapperLoginClientEncryptionResponse(event);

boolean cacheContainsUser = cache.getIfPresent(user) != null;
if (packet.getSaltSignature().isPresent() && cacheContainsUser) {
if (version.isNewerThan(ClientVersion.V_1_18_2) && version.isOlderThan(ClientVersion.V_1_19_3)) {
WrapperLoginClientEncryptionResponse packet = new WrapperLoginClientEncryptionResponse(event);
byte[] encryptedVerifyToken = cache.getIfPresent(user);

packet.setSaltSignature(null);
packet.setEncryptedVerifyToken(encryptedVerifyToken);
if (packet.getSaltSignature().isPresent() && encryptedVerifyToken != null) {
packet.setSaltSignature(null);
packet.setEncryptedVerifyToken(encryptedVerifyToken);
}
cache.invalidate(user);
event.markForReEncode(true);
}
cache.invalidate(user);
event.markForReEncode(true);
}
}

@Override
public void onPacketSend(PacketSendEvent event) {
if (event.isCancelled()) return;
final User user = event.getUser();
final PacketTypeCommon packetType = event.getPacketType();
User user = event.getUser();
ClientVersion version = user.getClientVersion();
PacketTypeCommon packetType = event.getPacketType();
if (packetType == PacketType.Login.Server.ENCRYPTION_REQUEST) {
if (!user.getClientVersion().is(VersionComparison.EQUALS, ClientVersion.V_1_19) && !user.getClientVersion().is(VersionComparison.EQUALS, ClientVersion.V_1_19_1)) {
return;
if (version.isNewerThan(ClientVersion.V_1_18_2) && version.isOlderThan(ClientVersion.V_1_19_3)) {
WrapperLoginServerEncryptionRequest packet = new WrapperLoginServerEncryptionRequest(event);
PublicKey serverPublicKey = packet.getPublicKey();
byte[] serverVerifyToken = packet.getVerifyToken();
byte[] encryptedVerifyToken = MinecraftEncryptionUtil.encryptRSA(serverPublicKey, serverVerifyToken);
cache.put(user, encryptedVerifyToken);
}
WrapperLoginServerEncryptionRequest packet = new WrapperLoginServerEncryptionRequest(event);
PublicKey serverPublicKey = packet.getPublicKey();
byte[] serverVerifyToken = packet.getVerifyToken();
byte[] encryptedVerifyToken = MinecraftEncryptionUtil.encryptRSA(serverPublicKey, serverVerifyToken);
cache.put(user, encryptedVerifyToken);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
import com.github.retrooper.packetevents.protocol.packettype.PacketType;
import com.github.retrooper.packetevents.protocol.packettype.PacketTypeCommon;
import com.github.retrooper.packetevents.protocol.player.ClientVersion;
import com.github.retrooper.packetevents.protocol.player.User;
import com.github.retrooper.packetevents.wrapper.status.server.WrapperStatusServerResponse;
import com.google.gson.JsonObject;
import com.google.inject.Inject;
import io.github._4drian3d.unsignedvelocity.configuration.Configuration;
import io.github._4drian3d.unsignedvelocity.listener.packet.ConfigurablePacketListener;

public final class ServerResponseListener extends ConfigurablePacketListener {
public class ServerResponseListener extends ConfigurablePacketListener {
@Inject
public ServerResponseListener(Configuration configuration) {
super(PacketListenerPriority.LOWEST, configuration);
Expand All @@ -24,12 +23,12 @@ public boolean canBeLoaded() {
}

@Override
public void onPacketSend(final PacketSendEvent event) {
public void onPacketSend(PacketSendEvent event) {
if (event.isCancelled()) return;
final User user = event.getUser();
final PacketTypeCommon packetType = event.getPacketType();
ClientVersion version = event.getUser().getClientVersion();
PacketTypeCommon packetType = event.getPacketType();
if (packetType == PacketType.Status.Server.RESPONSE) {
if (user.getClientVersion().isNewerThanOrEquals(ClientVersion.V_1_19)) {
if (version.isNewerThan(ClientVersion.V_1_18_2)) {
WrapperStatusServerResponse packet = new WrapperStatusServerResponse(event);
JsonObject component = packet.getComponent();
component.addProperty("preventsChatReports", true);
Expand Down

0 comments on commit 056f008

Please sign in to comment.