Stop creating a thread for each auth request

This commit is contained in:
themode 2020-12-28 00:18:25 +01:00
parent 28c5e39f8b
commit 5975f8d838

View File

@ -15,12 +15,10 @@ import org.jetbrains.annotations.NotNull;
import javax.crypto.SecretKey; import javax.crypto.SecretKey;
import java.math.BigInteger; import java.math.BigInteger;
import java.util.Arrays; import java.util.Arrays;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.CompletableFuture;
public class EncryptionResponsePacket implements ClientPreplayPacket { public class EncryptionResponsePacket implements ClientPreplayPacket {
private final static String THREAD_NAME = "Mojang Auth Thread";
private static final AtomicInteger UNIQUE_THREAD_ID = new AtomicInteger(0);
private byte[] sharedSecret; private byte[] sharedSecret;
private byte[] verifyToken; private byte[] verifyToken;
@ -33,38 +31,35 @@ public class EncryptionResponsePacket implements ClientPreplayPacket {
} }
final NettyPlayerConnection nettyConnection = (NettyPlayerConnection) connection; final NettyPlayerConnection nettyConnection = (NettyPlayerConnection) connection;
new Thread(THREAD_NAME + " #" + UNIQUE_THREAD_ID.incrementAndGet()) { CompletableFuture.runAsync(() -> {
try {
final String loginUsername = nettyConnection.getLoginUsername();
if (!Arrays.equals(nettyConnection.getNonce(), getNonce())) {
MinecraftServer.LOGGER.error("{} tried to login with an invalid nonce!", loginUsername);
return;
}
if (!loginUsername.isEmpty()) {
public void run() { final byte[] digestedData = MojangCrypt.digestData("", MojangAuth.getKeyPair().getPublic(), getSecretKey());
try {
final String loginUsername = nettyConnection.getLoginUsername(); if (digestedData == null) {
if (!Arrays.equals(nettyConnection.getNonce(), getNonce())) { // Incorrect key, probably because of the client
MinecraftServer.LOGGER.error("{} tried to login with an invalid nonce!", loginUsername); MinecraftServer.LOGGER.error("Connection {} failed initializing encryption.", nettyConnection.getRemoteAddress());
connection.disconnect();
return; return;
} }
if (!loginUsername.isEmpty()) {
final byte[] digestedData = MojangCrypt.digestData("", MojangAuth.getKeyPair().getPublic(), getSecretKey()); final String string3 = new BigInteger(digestedData).toString(16);
final GameProfile gameProfile = MojangAuth.getSessionService().hasJoinedServer(new GameProfile(null, loginUsername), string3);
nettyConnection.setEncryptionKey(getSecretKey());
if (digestedData == null) { MinecraftServer.LOGGER.info("UUID of player {} is {}", loginUsername, gameProfile.getId());
// Incorrect key, probably because of the client CONNECTION_MANAGER.startPlayState(connection, gameProfile.getId(), gameProfile.getName());
MinecraftServer.LOGGER.error("Connection {} failed initializing encryption.", nettyConnection.getRemoteAddress());
connection.disconnect();
return;
}
final String string3 = new BigInteger(digestedData).toString(16);
final GameProfile gameProfile = MojangAuth.getSessionService().hasJoinedServer(new GameProfile(null, loginUsername), string3);
nettyConnection.setEncryptionKey(getSecretKey());
MinecraftServer.LOGGER.info("UUID of player {} is {}", loginUsername, gameProfile.getId());
CONNECTION_MANAGER.startPlayState(connection, gameProfile.getId(), gameProfile.getName());
}
} catch (AuthenticationUnavailableException e) {
e.printStackTrace();
} }
} catch (AuthenticationUnavailableException e) {
e.printStackTrace();
} }
}.start(); });
} }
@Override @Override