Skip to content

Commit

Permalink
Improvements for forcedOfflinePlayers and logging
Browse files Browse the repository at this point in the history
  • Loading branch information
NikitaCartes committed Jun 14, 2023
1 parent c7b9cde commit 7ed8ed7
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 23 deletions.
7 changes: 4 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,11 @@ modrinth {
token.set(System.getenv("MODRINTH_TOKEN"))
projectId.set("aZj58GfX")
versionNumber.set("${project.version}")
versionName = "[1.20] ${project.version}"
versionName = "[1.20, 1.20.1] ${project.version}"
versionType = "beta"
changelog.set("Release notes and Changelog: \nhttps://github.com/NikitaCartes/EasyAuth/releases/tag/${project.version}")
uploadFile = remapJar
gameVersions.addAll("1.20")
gameVersions.addAll("1.20", "1.20.1")
loaders = ["fabric", "quilt"]
dependencies {
required.project("P7dR8mSH") // Fabric API
Expand All @@ -130,9 +130,10 @@ curseforge {
addGameVersion("Java 17")

addGameVersion("1.20")
addGameVersion("1.20.1")

mainArtifact(remapJar) {
displayName = "[1.20] ${project.version}"
displayName = "[1.20, 1.20.1] ${project.version}"
relations {
requiredDependency("fabric-api")
embeddedLibrary 'server-translation-api'
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Mod
mod_name=EasyAuth
mod_id=easyauth
mod_version=3.0.0-16
mod_version=3.0.0-17

# Fabric
minecraft_version=1.20
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
import xyz.nikitacartes.easyauth.storage.AuthConfig;
import xyz.nikitacartes.easyauth.storage.PlayerCache;
import xyz.nikitacartes.easyauth.storage.database.DBApiException;
import xyz.nikitacartes.easyauth.storage.database.LevelDB;
import xyz.nikitacartes.easyauth.storage.database.MongoDB;
import xyz.nikitacartes.easyauth.storage.database.MySQL;
import xyz.nikitacartes.easyauth.utils.AuthHelper;
import xyz.nikitacartes.easyauth.utils.TranslationHelper;

Expand Down Expand Up @@ -364,6 +361,7 @@ private static int addPlayerToForcedOffline(ServerCommandSource source, String p

THREADPOOL.submit(() -> {
config.main.forcedOfflinePlayers.add(player.toLowerCase(Locale.ROOT));
config.experimental.verifiedOnlinePlayer.remove(player.toLowerCase(Locale.ROOT));
config.save(new File("./mods/EasyAuth/config.json"));
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.regex.Pattern;

import static xyz.nikitacartes.easyauth.EasyAuth.*;
import static xyz.nikitacartes.easyauth.utils.EasyLogger.LogDebug;
import static xyz.nikitacartes.easyauth.utils.EasyLogger.LogError;

@Mixin(ServerLoginNetworkHandler.class)
Expand Down Expand Up @@ -60,22 +61,33 @@ private void acceptPlayer(CallbackInfo ci) {
private void checkPremium(LoginHelloC2SPacket packet, CallbackInfo ci) {
if (config.main.premiumAutologin) {
try {
// Todo: use config
String playername = packet.name().toLowerCase();
Pattern pattern = Pattern.compile("^[a-z0-9_]{3,16}$");
Matcher matcher = pattern.matcher(playername);
if (config.main.forcedOfflinePlayers.contains(playername)) {
LogDebug("Player " + playername + " is forced to be offline");
mojangAccountNamesCache.remove(playername);
state = ServerLoginNetworkHandler.State.READY_TO_ACCEPT;

this.profile = new GameProfile(null, packet.name());
ci.cancel();
return;
}
if (mojangAccountNamesCache.contains(playername) || config.experimental.verifiedOnlinePlayer.contains(playername)) {
LogDebug("Player " + playername + " is cached as online player. Authentication continues as vanilla");
mojangAccountNamesCache.add(playername);
return;
}
if ((playerCacheMap.containsKey(Uuids.getOfflinePlayerUuid(playername).toString()) || !matcher.matches() || config.main.forcedOfflinePlayers.contains(playername))) {
if ((playerCacheMap.containsKey(Uuids.getOfflinePlayerUuid(playername).toString()) || !matcher.matches())) {
// Player definitely doesn't have a mojang account
LogDebug("Player " + playername + " is cached as offline player");
state = ServerLoginNetworkHandler.State.READY_TO_ACCEPT;

this.profile = new GameProfile(null, packet.name());
ci.cancel();
} else {
// Checking account status from API
LogDebug("Checking player " + playername + " for premium status");
HttpsURLConnection httpsURLConnection = (HttpsURLConnection) new URL("https://api.mojang.com/users/profiles/minecraft/" + playername).openConnection();
httpsURLConnection.setRequestMethod("GET");
httpsURLConnection.setConnectTimeout(5000);
Expand All @@ -85,7 +97,7 @@ private void checkPremium(LoginHelloC2SPacket packet, CallbackInfo ci) {
if (response == HttpURLConnection.HTTP_OK) {
// Player has a Mojang account
httpsURLConnection.disconnect();

LogDebug("Player " + playername + " has a Mojang account");

// Caches the request
mojangAccountNamesCache.add(playername);
Expand All @@ -95,6 +107,7 @@ private void checkPremium(LoginHelloC2SPacket packet, CallbackInfo ci) {
} else if (response == HttpURLConnection.HTTP_NO_CONTENT || response == HttpURLConnection.HTTP_NOT_FOUND) {
// Player doesn't have a Mojang account
httpsURLConnection.disconnect();
LogDebug("Player " + playername + " doesn't have a Mojang account");
state = ServerLoginNetworkHandler.State.READY_TO_ACCEPT;

this.profile = new GameProfile(null, packet.name());
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/xyz/nikitacartes/easyauth/storage/AuthConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,11 @@
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.regex.Pattern;

import static xyz.nikitacartes.easyauth.EasyAuth.serverProp;
import static xyz.nikitacartes.easyauth.EasyAuth.DB;
import static xyz.nikitacartes.easyauth.utils.EasyLogger.LogError;
import static xyz.nikitacartes.easyauth.utils.EasyLogger.LogInfo;
import static xyz.nikitacartes.easyauth.utils.EasyLogger.*;

public class AuthConfig {
private static final Gson gson = new GsonBuilder()
Expand Down Expand Up @@ -66,9 +64,13 @@ public static AuthConfig load(File file) {
config.experimental.forcedOfflineUuids = false;
}
if (config.main.premiumAutologin) {
LogInfo("You cannot use server in offline mode and premiumAutologin! Disabling the latter.");
LogWarn("You cannot use server in offline mode and premiumAutologin! Disabling the latter.");
config.main.premiumAutologin = false;
}
} else {
if (!config.main.premiumAutologin) {
LogWarn("With online-mode enabled and premiumAutoLogin disabled, offline players will not be able to join.");
}
}
if (config.experimental.enableServerSideTranslation && !FabricLoader.getInstance().isModLoaded("server_translations_api")) {
config.experimental.enableServerSideTranslation = false;
Expand Down Expand Up @@ -211,7 +213,7 @@ public static class MainConfig {
* This protects premium usernames from being stolen, since cracked players
* with name that is found in Mojang database, are kicked.
*/
public boolean premiumAutologin = false;
public boolean premiumAutologin = true;
/**
* Whether bedrock players should skip the authentication process.
* You have to set online-mode to true in server.properties!
Expand Down
12 changes: 4 additions & 8 deletions src/main/java/xyz/nikitacartes/easyauth/utils/EasyLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,14 @@ public static void LogWarn(String message, Throwable e) {
}

public static void LogDebug(String message) {
if (config == null) {
log(Level.DEBUG, message);
} else if (config.experimental.debugMode) {
log(Level.DEBUG, message);
if (config != null && config.experimental.debugMode) {
log(Level.INFO, "[DEBUG]: " + message);
}
}

public static void LogDebug(String message, Throwable e) {
if (config == null) {
log(Level.DEBUG, message);
} else if (config.experimental.debugMode) {
log(Level.DEBUG, message, e);
if (config != null && config.experimental.debugMode) {
log(Level.INFO, "[DEBUG]: " + message, e);
}
}

Expand Down

0 comments on commit 7ed8ed7

Please sign in to comment.