vproxy/proxy/src/main/java/proxy/network/LoginHandler.java
2025-06-04 16:07:20 +02:00

96 lines
2.9 KiB
Java
Executable file

package proxy.network;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import proxy.Proxy;
import proxy.packet.L00PacketLoginStart;
import proxy.packet.L01PacketEncryptionResponse;
import proxy.packet.R00PacketDisconnect;
import proxy.packet.R02PacketLoginSuccess;
import proxy.packet.R03PacketEnableCompression;
import proxy.util.Formatter;
import proxy.util.Log;
public class LoginHandler implements Handler {
private static enum LoginState {
INIT, WAITING, DONE;
}
private final Proxy proxy;
private final Connection connection;
private LoginState state = LoginState.INIT;
private int timer;
private String username;
public LoginHandler(Proxy proxy, Connection connection) {
this.proxy = proxy;
this.connection = connection;
}
public void update(Connection connection) {
if(this.state == LoginState.WAITING) {
this.tryAcceptPlayer();
}
if(this.timer++ == 600) {
this.disconnect("Took too long to log in");
}
}
public void disconnect(String reason) {
try {
Log.info("Disconnecting " + this.getConnectionInfo() + ": " + reason);
this.connection.sendPacket(new R00PacketDisconnect(reason));
this.connection.closeChannel(reason);
}
catch(Exception e) {
Log.error(e, "Error whilst disconnecting player");
}
}
public void tryAcceptPlayer() {
if(this.proxy.isLoggedIn(this.username)) {
this.disconnect("That username is already taken");
return;
}
this.state = LoginState.DONE;
if(this.proxy.getCompression() >= 0) {
this.connection.sendPacket(new R03PacketEnableCompression(this.proxy.getCompression()), new ChannelFutureListener() {
public void operationComplete(ChannelFuture p_operationComplete_1_) throws Exception {
LoginHandler.this.connection.setCompressionTreshold(LoginHandler.this.proxy.getCompression());
}
});
}
ProxyHandler handler = new ProxyHandler(this.proxy, this.connection, this.username);
this.connection.setNetHandler(handler);
this.connection.sendPacket(new R02PacketLoginSuccess(Proxy.getOfflineUUID(this.username), this.username));
handler.setupPlayer();
}
public void onDisconnect(Connection connection, String reason) {
Log.info(this.getConnectionInfo() + " lost connection: " + reason);
}
public String getConnectionInfo() {
return this.username != null ? this.username + " (" + this.connection.getRemoteAddress().toString() + ")"
: String.valueOf(this.connection.getRemoteAddress());
}
public void processLoginStart(L00PacketLoginStart packet) {
if(this.state != LoginState.INIT)
throw new IllegalStateException("Unexpected hello packet");
this.username = packet.getProfile();
if(this.username.length() < 3 || this.username.length() > 16 || !Formatter.isValidUser(this.username)) {
this.disconnect("Invalid username");
return;
}
this.state = LoginState.WAITING;
}
public void processEncryptionResponse(L01PacketEncryptionResponse packet) {
throw new IllegalStateException("Unexpected key packet");
}
}