From 1a3de70e4ec156ef6f35e18d3d315c289d061874 Mon Sep 17 00:00:00 2001 From: Wyvest Date: Mon, 10 Jul 2023 23:30:54 +0900 Subject: [PATCH] Revert "fix auth" This reverts commit 696e96f1cae9f42c9c705d8e16fc79c0750ead5e. --- .../hytils/util/HypixelAPIUtils.java | 153 ++++++++++-------- 1 file changed, 86 insertions(+), 67 deletions(-) diff --git a/src/main/java/cc/woverflow/hytils/util/HypixelAPIUtils.java b/src/main/java/cc/woverflow/hytils/util/HypixelAPIUtils.java index 804aafb..a6b0ff9 100644 --- a/src/main/java/cc/woverflow/hytils/util/HypixelAPIUtils.java +++ b/src/main/java/cc/woverflow/hytils/util/HypixelAPIUtils.java @@ -22,26 +22,17 @@ import cc.polyfrost.oneconfig.events.event.Stage; import cc.polyfrost.oneconfig.events.event.TickEvent; import cc.polyfrost.oneconfig.libs.eventbus.Subscribe; -import cc.polyfrost.oneconfig.utils.JsonUtils; import cc.polyfrost.oneconfig.utils.NetworkUtils; import cc.polyfrost.oneconfig.utils.hypixel.LocrawInfo; import cc.woverflow.hytils.HytilsReborn; import cc.woverflow.hytils.handlers.cache.HeightHandler; import cc.woverflow.hytils.util.ranks.RankType; -import com.google.gson.JsonElement; import com.google.gson.JsonObject; -import com.mojang.authlib.GameProfile; import com.mojang.authlib.exceptions.AuthenticationException; import net.minecraft.client.Minecraft; import net.minecraft.util.EnumChatFormatting; -import org.apache.commons.io.IOUtils; -import java.io.IOException; -import java.io.InputStreamReader; import java.math.BigInteger; -import java.net.HttpURLConnection; -import java.net.URL; -import java.nio.charset.StandardCharsets; import java.util.*; public class HypixelAPIUtils { @@ -49,7 +40,6 @@ public class HypixelAPIUtils { public static String winstreak; public static LocrawInfo locraw; private int ticks = 0; - private static String token; public HypixelAPIUtils() { authorize(); @@ -63,19 +53,16 @@ private static boolean authorize() { BigInteger serverBi = random1Bi.xor(random2Bi); String serverId = serverBi.toString(16); try { - GameProfile profile = Minecraft.getMinecraft().getSession().getProfile(); - String token = Minecraft.getMinecraft().getSession().getToken(); - Minecraft.getMinecraft().getSessionService().joinServer(profile, token, serverId); + Minecraft.getMinecraft().getSessionService().joinServer(Minecraft + .getMinecraft() + .getSession() + .getProfile(), Minecraft.getMinecraft().getSession().getToken(), serverId); } catch (AuthenticationException e) { e.printStackTrace(); return false; } - JsonObject response = NetworkUtils.getJsonElement("https://api.polyfrost.cc/hypixel/v1/authentication?username=" + Minecraft.getMinecraft().getSession().getUsername() + "&serverId=" + serverId).getAsJsonObject(); - if (response.has("error")) { - return false; - } - token = response.get("token").getAsString(); - return true; + JsonObject response = NetworkUtils.getJsonElement("https://api.polyfrost.cc/hypixel/authentication?username=" + Minecraft.getMinecraft().getSession().getUsername() + "&serverId=" + serverId).getAsJsonObject(); + return !response.has("error"); } @@ -85,9 +72,17 @@ private static boolean authorize() { * @return Whether the "getting" was successful. */ public static boolean getGEXP() { - JsonObject exp = getJsonObjectAuth("https://api.polyfrost.cc/hypixel/v1/guild-exp"); + JsonObject exp = NetworkUtils.getJsonElement("https://api.polyfrost.cc/hypixel/guild-exp").getAsJsonObject(); if (exp.has("error")) { - return false; + if (Objects.equals(exp.get("error").getAsString(), "INVALID_AUTHORIZATION")) { + if (authorize()) { + return getGEXP(); + } else { + return false; + } + } else { + return false; + } } gexp = exp.get("exp").getAsString(); return true; @@ -100,9 +95,17 @@ public static boolean getGEXP() { * @return Whether the "getting" was successful. */ public static boolean getGEXP(String username) { - JsonObject exp = getJsonObjectAuth("https://api.polyfrost.cc/hypixel/v1/guild-exp?username=" + username); + JsonObject exp = NetworkUtils.getJsonElement("https://api.polyfrost.cc/hypixel/guild-exp?username=" + username).getAsJsonObject(); if (exp.has("error")) { - return false; + if (Objects.equals(exp.get("error").getAsString(), "INVALID_AUTHORIZATION")) { + if (authorize()) { + return getGEXP(username); + } else { + return false; + } + } else { + return false; + } } gexp = exp.get("exp").getAsString(); return true; @@ -114,9 +117,17 @@ public static boolean getGEXP(String username) { * @return Whether the "getting" was successful. */ public static boolean getWeeklyGEXP() { - JsonObject exp = getJsonObjectAuth("https://api.polyfrost.cc/hypixel/v1/guild-exp"); + JsonObject exp = NetworkUtils.getJsonElement("https://api.polyfrost.cc/hypixel/guild-exp").getAsJsonObject(); if (exp.has("error")) { - return false; + if (Objects.equals(exp.get("error").getAsString(), "INVALID_AUTHORIZATION")) { + if (authorize()) { + return getWeeklyGEXP(); + } else { + return false; + } + } else { + return false; + } } gexp = exp.get("weeklyExp").getAsString(); return true; @@ -129,9 +140,17 @@ public static boolean getWeeklyGEXP() { * @return Whether the "getting" was successful. */ public static boolean getWeeklyGEXP(String username) { - JsonObject exp = getJsonObjectAuth("https://api.polyfrost.cc/hypixel/v1/guild-exp?username=" + username); + JsonObject exp = NetworkUtils.getJsonElement("https://api.polyfrost.cc/hypixel/guild-exp?username=" + username).getAsJsonObject(); if (exp.has("error")) { - return false; + if (Objects.equals(exp.get("error").getAsString(), "INVALID_AUTHORIZATION")) { + if (authorize()) { + return getWeeklyGEXP(username); + } else { + return false; + } + } else { + return false; + } } gexp = exp.get("weeklyExp").getAsString(); return true; @@ -159,9 +178,17 @@ public static boolean getWinstreak() { return false; } } - JsonObject winstreak = getJsonObjectAuth("https://api.polyfrost.cc/hypixel/v1/winstreak?game=" + gameType); + JsonObject winstreak = NetworkUtils.getJsonElement("https://api.polyfrost.cc/hypixel/winstreak?game=" + gameType).getAsJsonObject(); if (winstreak.has("error")) { - return false; + if (Objects.equals(winstreak.get("error").getAsString(), "INVALID_AUTHORIZATION")) { + if (authorize()) { + return getWinstreak(); + } else { + return false; + } + } else { + return false; + } } HypixelAPIUtils.winstreak = String.valueOf(winstreak.get("winstreak").getAsInt()); return true; @@ -190,9 +217,17 @@ public static boolean getWinstreak(String username) { return false; } } - JsonObject winstreak = getJsonObjectAuth("https://api.polyfrost.cc/hypixel/v1/winstreak?game=" + gameType + "&username=" + username); + JsonObject winstreak = NetworkUtils.getJsonElement("https://api.polyfrost.cc/hypixel/winstreak?game=" + gameType + "&username=" + username).getAsJsonObject(); if (winstreak.has("error")) { - return false; + if (Objects.equals(winstreak.get("error").getAsString(), "INVALID_AUTHORIZATION")) { + if (authorize()) { + return getWinstreak(username); + } else { + return false; + } + } else { + return false; + } } HypixelAPIUtils.winstreak = String.valueOf(winstreak.get("winstreak").getAsInt()); return true; @@ -209,9 +244,17 @@ public static boolean getWinstreak(String username, String game) { if (!Objects.equals(game, "bedwars") && !Objects.equals(game, "skywars") && !Objects.equals(game, "duels")) { return false; } - JsonObject winstreak = getJsonObjectAuth("https://api.polyfrost.cc/hypixel/v1/winstreak?game=" + game + "&username=" + username); + JsonObject winstreak = NetworkUtils.getJsonElement("https://api.polyfrost.cc/hypixel/winstreak?game=" + game + "&username=" + username).getAsJsonObject(); if (winstreak.has("error")) { - return false; + if (Objects.equals(winstreak.get("error").getAsString(), "INVALID_AUTHORIZATION")) { + if (authorize()) { + return getWinstreak(username, game); + } else { + return false; + } + } else { + return false; + } } HypixelAPIUtils.winstreak = String.valueOf(winstreak.get("winstreak").getAsInt()); return true; @@ -224,9 +267,17 @@ public static boolean getWinstreak(String username, String game) { */ public static RankType getRank() { JsonObject playerRank = - getJsonObjectAuth("https://api.polyfrost.cc/hypixel/v1/rank"); + NetworkUtils.getJsonElement("https://api.polyfrost.cc/hypixel/rank").getAsJsonObject(); if (playerRank.has("error")) { - return RankType.UNKNOWN; + if (Objects.equals(playerRank.get("error").getAsString(), "INVALID_AUTHORIZATION")) { + if (authorize()) { + return getRank(); + } else { + return RankType.UNKNOWN; + } + } else { + return RankType.UNKNOWN; + } } return RankType.getRank(playerRank.get("rank").getAsString()); } @@ -255,38 +306,6 @@ public static String getUUID(String username) { } } - public static JsonObject getJsonObjectAuth(String url) { - HttpURLConnection connection; - try { - connection = ((HttpURLConnection) new URL(url).openConnection()); - connection.setRequestMethod("GET"); - connection.setUseCaches(false); - connection.addRequestProperty("User-Agent", "Hytils Reborn/" + HytilsReborn.VERSION); - connection.setReadTimeout(5000); - connection.setConnectTimeout(5000); - connection.setDoOutput(true); - connection.addRequestProperty("Authorization", token); - } catch (IOException e) { - e.printStackTrace(); - return null; - } - try (InputStreamReader input = new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8)) { - JsonElement element = JsonUtils.parseString(IOUtils.toString(input)); - if (element == null || !element.isJsonObject()) { - return null; - } - return element.getAsJsonObject(); - } catch (IOException e) { - if (e.getMessage().contains("403")) { - if (!authorize()) { - return null; - } - return getJsonObjectAuth(url); - } - return null; - } - } - @Subscribe private void onTick(TickEvent event) { if (event.stage == Stage.START) {