add encryption and enforce authentication

This commit is contained in:
Sen 2025-05-18 17:39:22 +02:00
parent 2ea3267e3a
commit 8d4b4b3619
16 changed files with 452 additions and 73 deletions

View file

@ -137,7 +137,7 @@ import common.packet.CPacketAction;
import common.packet.CPacketCheat;
import common.packet.CPacketMessage;
import common.packet.HPacketHandshake;
import common.packet.LPacketPasswordResponse;
import common.packet.LPacketLogin;
import common.packet.CPacketAction.Action;
import common.potion.Potion;
import common.potion.PotionEffect;
@ -488,9 +488,9 @@ public class Client implements IThreadListener {
try
{
connection = createNetworkManagerAndConnect(address == null ? InetAddress.getLoopbackAddress() : InetAddress.getByName(IDN.toASCII(address)), port);
connection.setNetHandler(new ClientLoginHandler(connection, this));
connection.setNetHandler(new ClientLoginHandler(connection, this, user, access, pass));
connection.sendPacket(new HPacketHandshake(Config.PROTOCOL));
connection.sendPacket(new LPacketPasswordResponse(user, access, pass));
connection.sendPacket(new LPacketLogin());
}
catch (UnknownHostException u)
{

View file

@ -1,21 +1,37 @@
package client.network;
import java.security.PublicKey;
import javax.crypto.SecretKey;
import client.Client;
import common.network.IClientLoginHandler;
import common.network.NetConnection;
import common.network.NetHandler;
import common.network.PacketRegistry;
import common.packet.LPacketPasswordResponse;
import common.packet.LPacketStartEncrypt;
import common.packet.RPacketDisconnect;
import common.packet.RPacketEnableCompression;
import common.packet.RPacketLoginSuccess;
import common.packet.RPacketRequestEncrypt;
import common.util.EncryptUtil;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
public class ClientLoginHandler extends NetHandler implements IClientLoginHandler {
private final Client gm;
private final NetConnection networkManager;
private final String user;
private final String access;
private final String password;
public ClientLoginHandler(NetConnection conn, Client gmIn) {
public ClientLoginHandler(NetConnection conn, Client gmIn, String userIn, String accessIn, String passwordIn) {
this.networkManager = conn;
this.gm = gmIn;
this.user = userIn;
this.access = accessIn;
this.password = passwordIn;
}
public void onDisconnect(String reason)
@ -23,10 +39,21 @@ public class ClientLoginHandler extends NetHandler implements IClientLoginHandle
this.gm.disconnected(reason);
}
public final void handleDisconnect(RPacketDisconnect packetIn)
public void handleDisconnect(RPacketDisconnect packetIn)
{
this.networkManager.closeChannel(packetIn.getReason());
}
public void handleEncrypt(RPacketRequestEncrypt packet) {
final SecretKey secret = EncryptUtil.createNewSharedKey();
PublicKey pubkey = packet.getKey();
this.networkManager.sendPacket(new LPacketStartEncrypt(secret, pubkey, packet.getToken()), new GenericFutureListener < Future <? super Void >> () {
public void operationComplete(Future <? super Void > u) throws Exception {
ClientLoginHandler.this.networkManager.startEncryption(secret);
ClientLoginHandler.this.networkManager.sendPacket(new LPacketPasswordResponse(ClientLoginHandler.this.user, ClientLoginHandler.this.access, ClientLoginHandler.this.password));
}
});
}
public void handleLoginSuccess(RPacketLoginSuccess packetIn)
{
@ -35,24 +62,8 @@ public class ClientLoginHandler extends NetHandler implements IClientLoginHandle
this.networkManager.setNetHandler(new ClientPlayer(this.gm, this.networkManager));
}
public final void handleEnableCompression(RPacketEnableCompression packetIn)
public void handleEnableCompression(RPacketEnableCompression packetIn)
{
this.networkManager.setCompressionTreshold(packetIn.getValue());
}
// public void handlePasswordRequest(RPacketPasswordRequest packetIn) {
// if(this.server == null) {
// this.networkManager.sendPacket(new LPacketPasswordResponse(this.user, "", ""));
// }
// else if((packetIn.getPasswordRequested() && this.server.pass.isEmpty()) ||
// (packetIn.getPasswordProtected() && this.server.access.isEmpty())) {
//// this.toChange = this.gm.getConnected();
// this.accessRequired = packetIn.getPasswordProtected() && this.server.access.isEmpty();
// this.passwordRequired = packetIn.getPasswordRequested() && this.server.pass.isEmpty();
// this.networkManager.closeChannel("");
// }
// else {
// this.networkManager.sendPacket(new LPacketPasswordResponse(this.user, this.access, this.pass));
// }
// }
}