From 9e1177cb078306f6404ed020c45cea8ae98c976f Mon Sep 17 00:00:00 2001 From: Mike <76-Mike@users.noreply.git.krews.org> Date: Thu, 28 May 2020 22:21:31 +0200 Subject: [PATCH] Fixes MachineID sometimes not unique #620. --- .../habbohotel/gameclients/GameClient.java | 11 +---------- .../incoming/handshake/MachineIDEvent.java | 18 +++++++++++++++--- .../outgoing/handshake/MachineIDComposer.java | 11 ++++++----- src/main/java/com/eu/habbo/util/HexUtils.java | 14 ++++++++++++++ 4 files changed, 36 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/eu/habbo/habbohotel/gameclients/GameClient.java b/src/main/java/com/eu/habbo/habbohotel/gameclients/GameClient.java index 9f871543..a05ef0a0 100644 --- a/src/main/java/com/eu/habbo/habbohotel/gameclients/GameClient.java +++ b/src/main/java/com/eu/habbo/habbohotel/gameclients/GameClient.java @@ -73,17 +73,8 @@ public class GameClient { if (machineId == null) { throw new RuntimeException("Cannot set machineID to NULL"); } - this.machineId = machineId; - if (this.habbo != null) { - try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement("UPDATE users SET machine_id = ? WHERE id = ? LIMIT 1")) { - statement.setString(1, this.machineId); - statement.setInt(2, this.habbo.getHabboInfo().getId()); - statement.execute(); - } catch (SQLException e) { - LOGGER.error("Caught SQL exception", e); - } - } + this.machineId = machineId; } public void sendResponse(MessageComposer composer) { diff --git a/src/main/java/com/eu/habbo/messages/incoming/handshake/MachineIDEvent.java b/src/main/java/com/eu/habbo/messages/incoming/handshake/MachineIDEvent.java index 7be07c04..9949ffa5 100644 --- a/src/main/java/com/eu/habbo/messages/incoming/handshake/MachineIDEvent.java +++ b/src/main/java/com/eu/habbo/messages/incoming/handshake/MachineIDEvent.java @@ -3,14 +3,26 @@ package com.eu.habbo.messages.incoming.handshake; import com.eu.habbo.messages.NoAuthMessage; import com.eu.habbo.messages.incoming.MessageHandler; import com.eu.habbo.messages.outgoing.handshake.MachineIDComposer; +import com.eu.habbo.util.HexUtils; @NoAuthMessage public class MachineIDEvent extends MessageHandler { + private static final int HASH_LENGTH = 64; + @Override public void handle() throws Exception { - String unknown = this.packet.readString(); - this.client.setMachineId(this.packet.readString()); - this.client.sendResponse(new MachineIDComposer(this.client)); + String storedMachineId = this.packet.readString(); + String clientFingerprint = this.packet.readString(); + String capabilities = this.packet.readString(); + + // Update stored machine id if it doesn't match our requirements. + if (storedMachineId.startsWith("~") || storedMachineId.length() != HASH_LENGTH) { + storedMachineId = HexUtils.getRandom(HASH_LENGTH); + this.client.sendResponse(new MachineIDComposer(storedMachineId)); + } + + this.client.setMachineId(storedMachineId); } + } diff --git a/src/main/java/com/eu/habbo/messages/outgoing/handshake/MachineIDComposer.java b/src/main/java/com/eu/habbo/messages/outgoing/handshake/MachineIDComposer.java index 3e5736b2..f96abff6 100644 --- a/src/main/java/com/eu/habbo/messages/outgoing/handshake/MachineIDComposer.java +++ b/src/main/java/com/eu/habbo/messages/outgoing/handshake/MachineIDComposer.java @@ -1,21 +1,22 @@ package com.eu.habbo.messages.outgoing.handshake; -import com.eu.habbo.habbohotel.gameclients.GameClient; import com.eu.habbo.messages.ServerMessage; import com.eu.habbo.messages.outgoing.MessageComposer; import com.eu.habbo.messages.outgoing.Outgoing; public class MachineIDComposer extends MessageComposer { - private final GameClient client; - public MachineIDComposer(GameClient client) { - this.client = client; + private final String machineId; + + public MachineIDComposer(String machineId) { + this.machineId = machineId; } @Override protected ServerMessage composeInternal() { this.response.init(Outgoing.MachineIDComposer); - this.response.appendString(this.client.getMachineId()); + this.response.appendString(this.machineId); return this.response; } + } diff --git a/src/main/java/com/eu/habbo/util/HexUtils.java b/src/main/java/com/eu/habbo/util/HexUtils.java index 82ad1990..2e5bccb4 100644 --- a/src/main/java/com/eu/habbo/util/HexUtils.java +++ b/src/main/java/com/eu/habbo/util/HexUtils.java @@ -1,5 +1,8 @@ package com.eu.habbo.util; +import java.util.Random; +import java.util.concurrent.ThreadLocalRandom; + public class HexUtils { private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray(); @@ -24,4 +27,15 @@ public class HexUtils { return data; } + public static String getRandom(int length){ + Random r = ThreadLocalRandom.current(); + StringBuilder sb = new StringBuilder(); + + while(sb.length() < length){ + sb.append(Integer.toHexString(r.nextInt())); + } + + return sb.toString().substring(0, length); + } + }