Files
src
build.gradle
gradle.properties
settings.gradle
Code
7 606
src/client/java/com/authycracker/client/AuthyCrackerClient.java
package com.authycracker.client;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Locale;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.message.v1.ClientReceiveMessageEvents;
import net.fabricmc.fabric.api.client.message.v1.ClientSendMessageEvents;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientPacketListener;
import net.minecraft.client.multiplayer.ServerData;
import net.minecraft.client.multiplayer.resolver.ServerAddress;
public final class AuthyCrackerClient implements ClientModInitializer {
private CredentialStore credentialStore;
private ClientPacketListener connection;
private String serverId;
private boolean onlineMode;
private boolean authChallengeSeen;
private boolean loginSent;
private boolean promptOpen;
private char[] pendingPassword;
private String pendingServerId;
private LoginPromptScreen activePrompt;
@Override
public void onInitializeClient() {
Path storageDirectory = Minecraft.getInstance().gameDirectory.toPath().resolve("config").resolve("authycracker");
try {
this.credentialStore = CredentialStore.open(storageDirectory);
} catch (IOException exception) {
this.credentialStore = null;
}
ClientPlayConnectionEvents.JOIN.register((listener, sender, client) -> {
this.connection = listener;
this.serverId = normalizeServerId(client.getCurrentServer());
this.onlineMode = listener.onlineMode();
this.authChallengeSeen = false;
this.loginSent = false;
this.promptOpen = false;
this.clearPendingPrompt();
this.activePrompt = null;
});
ClientPlayConnectionEvents.DISCONNECT.register((listener, client) -> {
if (this.connection != listener) {
return;
}
LoginPromptScreen prompt = this.activePrompt;
this.clearPendingPrompt();
this.connection = null;
this.serverId = null;
this.onlineMode = false;
this.authChallengeSeen = false;
this.loginSent = false;
if (prompt != null && client.gui.screen() == prompt) {
client.gui.setScreen(null);
}
});
ClientReceiveMessageEvents.GAME.register((message, overlay) -> this.observeServerMessage(message.getString()));
ClientSendMessageEvents.ALLOW_COMMAND.register(command -> {
this.observeCommand(command);
return true;
});
}
private void observeServerMessage(String message) {
if (this.connection == null || this.serverId == null || this.onlineMode || this.credentialStore == null) {
return;
}
if (isAuthenticationFailureMessage(message)) {
this.clearPendingPrompt();
return;
}
if (isAuthenticationPrompt(message)) {
this.authChallengeSeen = true;
if (this.loginSent || this.promptOpen) {
return;
}
this.loginSent = true;
ClientPacketListener currentConnection = this.connection;
String currentServerId = this.serverId;
Minecraft client = Minecraft.getInstance();
client.execute(() -> {
if (this.connection != currentConnection || this.onlineMode || !this.authChallengeSeen) {
return;
}
try {
char[] password = this.credentialStore.getPassword(currentServerId);
if (password == null) {
return;
}
try {
currentConnection.sendCommand("login " + new String(password));
} finally {
Arrays.fill(password, '\0');
}
} catch (IOException | java.security.GeneralSecurityException ignored) {
}
});
return;
}
if (isAuthenticationSuccessMessage(message)) {
this.offerSavePrompt();
}
}
private void observeCommand(String command) {
if (this.connection == null || this.serverId == null || this.onlineMode || !this.authChallengeSeen
|| this.credentialStore == null || this.promptOpen || this.credentialStore.hasCredential(this.serverId)
|| this.credentialStore.isNeverAsk(this.serverId)) {
return;
}
int start = 0;
while (start < command.length() && Character.isWhitespace(command.charAt(start))) {
start++;
}
if (start < command.length() && command.charAt(start) == '/') {
start++;
}
int verbStart = start;
while (start < command.length() && !Character.isWhitespace(command.charAt(start))) {
start++;
}
if (start - verbStart != 5 || !command.regionMatches(true, verbStart, "login", 0, 5)) {
return;
}
while (start < command.length() && Character.isWhitespace(command.charAt(start))) {
start++;
}
int end = command.length();
while (end > start && Character.isWhitespace(command.charAt(end - 1))) {
end--;
}
if (start == end) {
return;
}
char[] password = new char[end - start];
command.getChars(start, end, password, 0);
if (this.pendingPassword != null) {
Arrays.fill(this.pendingPassword, '\0');
}
this.pendingPassword = password;
this.pendingServerId = this.serverId;
}
private void offerSavePrompt() {
if (this.pendingPassword == null || this.pendingServerId == null || this.promptOpen || this.onlineMode
|| this.credentialStore == null || !this.pendingServerId.equals(this.serverId)
|| this.credentialStore.hasCredential(this.serverId) || this.credentialStore.isNeverAsk(this.serverId)) {
return;
}
this.promptOpen = true;
ClientPacketListener currentConnection = this.connection;
String currentServerId = this.serverId;
Minecraft client = Minecraft.getInstance();
client.execute(() -> {
if (this.connection != currentConnection || this.onlineMode || !this.authChallengeSeen
|| this.pendingPassword == null || !currentServerId.equals(this.pendingServerId)
|| this.credentialStore == null || this.credentialStore.hasCredential(currentServerId)
|| this.credentialStore.isNeverAsk(currentServerId)) {
this.clearPendingPrompt();
return;
}
this.activePrompt = new LoginPromptScreen(
() -> this.savePendingPassword(currentServerId),
() -> { },
() -> this.neverAskForServer(currentServerId),
this::clearPendingPrompt
);
client.gui.setScreen(this.activePrompt);
});
}
private void savePendingPassword(String currentServerId) {
if (this.credentialStore == null || this.pendingPassword == null) {
return;
}
try {
this.credentialStore.save(currentServerId, this.pendingPassword);
} catch (IOException | java.security.GeneralSecurityException ignored) {
}
}
private void neverAskForServer(String currentServerId) {
if (this.credentialStore == null) {
return;
}
try {
this.credentialStore.neverAsk(currentServerId);
} catch (IOException ignored) {