Merge branch 'fix/machine-id' into 'dev'

Fixes MachineID sometimes not unique #620.

See merge request morningstar/Arcturus-Community!213
This commit is contained in:
Mike 2020-06-02 19:57:40 -04:00
commit c0249a39ff
4 changed files with 36 additions and 18 deletions

View File

@ -73,17 +73,8 @@ public class GameClient {
if (machineId == null) { if (machineId == null) {
throw new RuntimeException("Cannot set machineID to NULL"); throw new RuntimeException("Cannot set machineID to NULL");
} }
this.machineId = machineId;
if (this.habbo != null) { this.machineId = machineId;
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);
}
}
} }
public void sendResponse(MessageComposer composer) { public void sendResponse(MessageComposer composer) {

View File

@ -3,14 +3,26 @@ package com.eu.habbo.messages.incoming.handshake;
import com.eu.habbo.messages.NoAuthMessage; import com.eu.habbo.messages.NoAuthMessage;
import com.eu.habbo.messages.incoming.MessageHandler; import com.eu.habbo.messages.incoming.MessageHandler;
import com.eu.habbo.messages.outgoing.handshake.MachineIDComposer; import com.eu.habbo.messages.outgoing.handshake.MachineIDComposer;
import com.eu.habbo.util.HexUtils;
@NoAuthMessage @NoAuthMessage
public class MachineIDEvent extends MessageHandler { public class MachineIDEvent extends MessageHandler {
private static final int HASH_LENGTH = 64;
@Override @Override
public void handle() throws Exception { public void handle() throws Exception {
String unknown = this.packet.readString(); String storedMachineId = this.packet.readString();
this.client.setMachineId(this.packet.readString()); String clientFingerprint = this.packet.readString();
this.client.sendResponse(new MachineIDComposer(this.client)); 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);
}
} }

View File

@ -1,21 +1,22 @@
package com.eu.habbo.messages.outgoing.handshake; 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.ServerMessage;
import com.eu.habbo.messages.outgoing.MessageComposer; import com.eu.habbo.messages.outgoing.MessageComposer;
import com.eu.habbo.messages.outgoing.Outgoing; import com.eu.habbo.messages.outgoing.Outgoing;
public class MachineIDComposer extends MessageComposer { public class MachineIDComposer extends MessageComposer {
private final GameClient client;
public MachineIDComposer(GameClient client) { private final String machineId;
this.client = client;
public MachineIDComposer(String machineId) {
this.machineId = machineId;
} }
@Override @Override
protected ServerMessage composeInternal() { protected ServerMessage composeInternal() {
this.response.init(Outgoing.MachineIDComposer); this.response.init(Outgoing.MachineIDComposer);
this.response.appendString(this.client.getMachineId()); this.response.appendString(this.machineId);
return this.response; return this.response;
} }
} }

View File

@ -1,5 +1,8 @@
package com.eu.habbo.util; package com.eu.habbo.util;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
public class HexUtils { public class HexUtils {
private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray(); private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
@ -24,4 +27,15 @@ public class HexUtils {
return data; 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);
}
} }