Skip to content

Commit

Permalink
Revert "fix auth"
Browse files Browse the repository at this point in the history
This reverts commit 696e96f.
  • Loading branch information
Wyvest committed Jul 10, 2023
1 parent 696e96f commit 1a3de70
Showing 1 changed file with 86 additions and 67 deletions.
153 changes: 86 additions & 67 deletions src/main/java/cc/woverflow/hytils/util/HypixelAPIUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,24 @@
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 {
public static String gexp;
public static String winstreak;
public static LocrawInfo locraw;
private int ticks = 0;
private static String token;

public HypixelAPIUtils() {
authorize();
Expand All @@ -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");
}


Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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());
}
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 1a3de70

Please sign in to comment.