From 3bc65a3eb0c38174f867895a1418406bd7699c42 Mon Sep 17 00:00:00 2001 From: kirillsaint Date: Wed, 2 Aug 2023 14:12:26 +0600 Subject: [PATCH 1/3] Test Account Changer --- .../java/net/silentclient/client/Client.java | 19 ++-- .../components/AccountPicker.java | 40 +++++++- .../client/mixin/ducks/MinecraftExt.java | 5 + .../client/mixin/mixins/MinecraftMixin.java | 17 +++- .../client/utils/AccountManager.java | 94 +++++++++++++++++++ .../silentclient/client/utils/Requests.java | 46 +++++++++ .../client/utils/types/UserData.java | 5 + 7 files changed, 214 insertions(+), 12 deletions(-) create mode 100644 src/main/java/net/silentclient/client/mixin/ducks/MinecraftExt.java create mode 100644 src/main/java/net/silentclient/client/utils/AccountManager.java diff --git a/src/main/java/net/silentclient/client/Client.java b/src/main/java/net/silentclient/client/Client.java index 3fbeca3..786f0ad 100644 --- a/src/main/java/net/silentclient/client/Client.java +++ b/src/main/java/net/silentclient/client/Client.java @@ -88,6 +88,7 @@ public class Client { private MouseCursorHandler mouseCursorHandler; private GlobalSettings globalSettings; private File globalSettingsFile; + private AccountManager accountManager; public static void memoryDebug(String paramString) { LogManager.getLogger().info("-- Start Memory Debug -- " + paramString); @@ -242,6 +243,9 @@ public class Client { EventManager.register(cpsTracker); logger.info("STARTING > cosmetics"); cosmetics.init(); + logger.info("STARTING > account-manager"); + accountManager = new AccountManager(); + accountManager.init(); logger.info("STARTING > entity-culling"); EventManager.register(new EntityCulling()); EntityCulling.SUPPORT_NEW_GL = GLContext.getCapabilities().OpenGL33; @@ -272,11 +276,6 @@ public class Client { Minecraft.getMinecraft().displayGuiScreen(new GuiError("Authorization Error. Try restarting the game")); return; } - if(!Client.getInstance().getAccount().getUsername().equals(Minecraft.getMinecraft().getSession().getUsername().toLowerCase())) { - logger.info("STARTING > ERROR: " + "Usernames SC and MC account do not match"); - Minecraft.getMinecraft().displayGuiScreen(new GuiError("Usernames SC and MC account do not match")); - return; - } if(Client.getInstance().getAccount().isBanned()) { logger.info("STARTING > ERROR: " + "Account is banned"); Minecraft.getMinecraft().displayGuiScreen(new GuiError("Your account is banned")); @@ -396,7 +395,7 @@ public class Client { } } - private PlayerResponse updateAccount() { + public PlayerResponse updateAccount() { try { URL url = new URL("https://api.silentclient.net/account"); HttpsURLConnection con = (HttpsURLConnection) url.openConnection(); @@ -520,6 +519,14 @@ public class Client { } // Instances + public String getApiUrl() { + return "http://localhost:" + getUserData().port; + } + + public AccountManager getAccountManager() { + return accountManager; + } + public FriendsResponse getFriends() { return friends; } diff --git a/src/main/java/net/silentclient/client/gui/silentmainmenu/components/AccountPicker.java b/src/main/java/net/silentclient/client/gui/silentmainmenu/components/AccountPicker.java index cfcc3bf..45363cc 100644 --- a/src/main/java/net/silentclient/client/gui/silentmainmenu/components/AccountPicker.java +++ b/src/main/java/net/silentclient/client/gui/silentmainmenu/components/AccountPicker.java @@ -1,6 +1,10 @@ package net.silentclient.client.gui.silentmainmenu.components; import net.minecraft.client.Minecraft; +import net.silentclient.client.Client; +import net.silentclient.client.gui.font.SilentFontRenderer; +import net.silentclient.client.gui.lite.clickgui.utils.MouseUtils; +import net.silentclient.client.utils.AccountManager; public class AccountPicker { private boolean open; @@ -14,10 +18,44 @@ public class AccountPicker { } public void draw(Minecraft mc, int mouseX, int mouseY) { -// Client.getInstance().getSilentFontRenderer().drawCenteredString(Client.getInstance().getAccount().original_username, x, y, 14, SilentFontRenderer.FontType.TITLE); + Client.getInstance().getSilentFontRenderer().drawCenteredString(Client.getInstance().getAccount().original_username, x, y, 14, SilentFontRenderer.FontType.TITLE); + if(this.open) { + int accountY = y + 15; + for(AccountManager.AccountType account : Client.getInstance().getAccountManager().getAccounts()) { + if(Client.getInstance().getAccount().getUsername().equalsIgnoreCase(account.username)) { + continue; + } + Client.getInstance().getSilentFontRenderer().drawCenteredString(account.username, x, accountY, 14, SilentFontRenderer.FontType.TITLE); + accountY += 15; + } + } } public void onClick(Minecraft mc, int mouseX, int mouseY) { + if(MouseUtils.isInside(mouseX, mouseY, x - (Client.getInstance().getSilentFontRenderer().getStringWidth(Client.getInstance().getAccount().original_username, 14, SilentFontRenderer.FontType.TITLE) / 2), y, Client.getInstance().getSilentFontRenderer().getStringWidth(Client.getInstance().getAccount().original_username, 14, SilentFontRenderer.FontType.TITLE), 14)) { + this.open = !this.open; + (new Thread(() -> { + Client.getInstance().getAccountManager().updateAccounts(); + })).start(); + return; + } + if(this.open) { + int accountY = y + 15; + int accountIndex = 0; + for(AccountManager.AccountType account : Client.getInstance().getAccountManager().getAccounts()) { + if(Client.getInstance().getAccount().getUsername().equalsIgnoreCase(account.username)) { + accountIndex += 1; + continue; + } + if(MouseUtils.isInside(mouseX, mouseY, x - (Client.getInstance().getSilentFontRenderer().getStringWidth(account.username, 14, SilentFontRenderer.FontType.TITLE) / 2), accountY, Client.getInstance().getSilentFontRenderer().getStringWidth(account.username, 14, SilentFontRenderer.FontType.TITLE), 14)) { + Client.getInstance().getAccountManager().setSelected(account, accountIndex, false); + this.open = false; + break; + } + accountY += 15; + accountIndex += 1; + } + } } } diff --git a/src/main/java/net/silentclient/client/mixin/ducks/MinecraftExt.java b/src/main/java/net/silentclient/client/mixin/ducks/MinecraftExt.java new file mode 100644 index 0000000..e5948d9 --- /dev/null +++ b/src/main/java/net/silentclient/client/mixin/ducks/MinecraftExt.java @@ -0,0 +1,5 @@ +package net.silentclient.client.mixin.ducks; + +public interface MinecraftExt { + void setSession(Object session); +} diff --git a/src/main/java/net/silentclient/client/mixin/mixins/MinecraftMixin.java b/src/main/java/net/silentclient/client/mixin/mixins/MinecraftMixin.java index 965e7e5..2f97dfe 100644 --- a/src/main/java/net/silentclient/client/mixin/mixins/MinecraftMixin.java +++ b/src/main/java/net/silentclient/client/mixin/mixins/MinecraftMixin.java @@ -15,6 +15,7 @@ import net.minecraft.item.EnumAction; import net.minecraft.item.ItemBlock; import net.minecraft.util.IChatComponent; import net.minecraft.util.ResourceLocation; +import net.minecraft.util.Session; import net.minecraft.world.WorldSettings; import net.silentclient.client.Client; import net.silentclient.client.event.impl.*; @@ -22,6 +23,7 @@ import net.silentclient.client.gui.lite.LiteMainMenu; import net.silentclient.client.gui.silentmainmenu.MainMenuConcept; import net.silentclient.client.gui.util.BackgroundPanorama; import net.silentclient.client.hooks.MinecraftHook; +import net.silentclient.client.mixin.ducks.MinecraftExt; import net.silentclient.client.mods.player.ZoomMod; import net.silentclient.client.mods.render.AnimationsMod; import net.silentclient.client.mods.settings.FPSBoostMod; @@ -32,10 +34,7 @@ import org.lwjgl.input.Keyboard; import org.lwjgl.input.Mouse; import org.lwjgl.opengl.Display; import org.lwjgl.opengl.GL11; -import org.spongepowered.asm.mixin.Final; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Overwrite; -import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.*; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.Redirect; @@ -45,7 +44,7 @@ import java.io.File; import java.io.IOException; @Mixin(Minecraft.class) -public abstract class MinecraftMixin { +public abstract class MinecraftMixin implements MinecraftExt { @Inject(method = "startGame", at = @At("HEAD")) public void initClient(CallbackInfo callbackInfo) throws IOException { Client.getInstance().init(); @@ -67,6 +66,11 @@ public abstract class MinecraftMixin { // } } + @Override + public void setSession(Object session) { + this.session = (Session) session; + } + @Inject(method = "shutdownMinecraftApplet", at = @At("HEAD")) public void shutdownClient(CallbackInfo callbackInfo) throws Throwable { Client.getInstance().shutdown(); @@ -121,6 +125,9 @@ public abstract class MinecraftMixin { @Shadow public GameSettings gameSettings; + @Mutable + @Shadow @Final private Session session; + @Inject(method = "displayGuiScreen", at = @At("RETURN"), cancellable = true) public void displayGuiScreenInject(GuiScreen guiScreenIn, CallbackInfo ci) { if(Client.backgroundPanorama == null) { diff --git a/src/main/java/net/silentclient/client/utils/AccountManager.java b/src/main/java/net/silentclient/client/utils/AccountManager.java new file mode 100644 index 0000000..cc51e1c --- /dev/null +++ b/src/main/java/net/silentclient/client/utils/AccountManager.java @@ -0,0 +1,94 @@ +package net.silentclient.client.utils; + +import com.google.common.reflect.TypeToken; +import net.minecraft.client.Minecraft; +import net.minecraft.util.Session; +import net.silentclient.client.Client; +import net.silentclient.client.gui.silentmainmenu.MainMenuConcept; +import net.silentclient.client.mixin.ducks.MinecraftExt; +import net.silentclient.client.utils.types.PlayerResponse; +import org.json.JSONObject; + +import java.lang.reflect.Type; +import java.util.ArrayList; + +public class AccountManager { + private ArrayList accounts; + private AccountType selected; + + public void init() { + Client.logger.info("[ACCOUNT-MANAGER]: Getting accounts"); + updateAccounts(); + + Client.logger.info("[ACCOUNT-MANAGER]: Getting selected account"); + String accountText = Requests.httpGet(Client.getInstance().getApiUrl() + "/selected"); + if(accountText != null) { + try { + AccountType account = Client.getInstance().getGson().fromJson(accountText, AccountType.class); + Client.logger.info("[ACCOUNT-MANAGER]: Selected Account Username: " + account.username); + setSelected(account, 0, true); + } catch (Exception err) { + Client.logger.catching(err); + } + } + } + + public void updateAccounts() { + String accountsText = Requests.httpGet(Client.getInstance().getApiUrl() + "/accounts"); + if(accountsText != null) { + try { + Type listType = new TypeToken>(){}.getType(); + + accounts = Client.getInstance().getGson().fromJson(accountsText, listType); + } catch (Exception err) { + Client.logger.catching(err); + } + } + } + + public void setSelected(AccountType account, int index, boolean force) { + if(!force) { + Requests.httpPost(Client.getInstance().getApiUrl() + "/set_selected", new JSONObject().put("selected", index).toString()); + } + selected = account; + Session session = new Session(account.username, nameToUuid(account.username), account.mc_access_token != null ? account.mc_access_token : "0", account.mc_access_token != null ? "msa" : "legacy"); + ((MinecraftExt) Minecraft.getMinecraft()).setSession(session); + Client.getInstance().getUserData().setAccessToken(selected.access_token); + Client.getInstance().getSilentSocket().Disconnect(); + Client.getInstance().getSilentSocket().Connect(selected.access_token); + PlayerResponse acc = Client.getInstance().updateAccount(); + if(acc != null) { + Client.getInstance().setAccount(acc.getAccount()); + } + + MainMenuConcept.imageLocation = null; + MainMenuConcept.image = null; + MainMenuConcept.initSkin = false; + } + + public static String nameToUuid(String username) { + try { + String content = Requests.httpGet(Client.getInstance().getApiUrl() + "/uuid?username="+username); + + return Client.getInstance().getGson().fromJson(content, UuidResponse.class).uuid; + } catch (Exception err) { + Client.logger.catching(err); + return "0"; + } + } + + public ArrayList getAccounts() { + return accounts; + } + + public class AccountType { + public String username; + public String access_token; + public String mc_access_token; + public String mc_refresh_token; + } + + public class UuidResponse { + public String uuid; + } +} diff --git a/src/main/java/net/silentclient/client/utils/Requests.java b/src/main/java/net/silentclient/client/utils/Requests.java index 9376e1b..5b7e997 100644 --- a/src/main/java/net/silentclient/client/utils/Requests.java +++ b/src/main/java/net/silentclient/client/utils/Requests.java @@ -7,6 +7,7 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; +import java.net.HttpURLConnection; import java.net.URL; public class Requests { @@ -54,4 +55,49 @@ public class Requests { return null; } } + + public static String httpGet(String url) { + return httpRequest("GET", url, null); + } + + public static String httpPost(String url, String data) { + return httpRequest("POST", url, data); + } + + public static String httpPost(String url) { + return httpRequest("POST", url, null); + } + + public static String httpRequest(String method, String urlOriginal, String data) { + try { + Client.logger.info("Sending a HTTP " + method + " Request to a URL: " + urlOriginal + " with data: " + data); + URL url = new URL(urlOriginal); + HttpURLConnection con = (HttpURLConnection) url.openConnection(); + con.setRequestMethod(method); + con.setRequestProperty("User-Agent", "SilentClient"); + con.setRequestProperty("Authorization", "Bearer " + Client.getInstance().getUserData().getAccessToken()); + if(data != null) { + con.setRequestProperty("Content-Type", "application/json"); + con.setRequestProperty("Accept", "application/json"); + con.setDoOutput(true); + try(OutputStream os = con.getOutputStream()) { + byte[] input = data.getBytes("utf-8"); + os.write(input, 0, input.length); + } + } + BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); + String inputLine; + StringBuffer content = new StringBuffer(); + while ((inputLine = in.readLine()) != null) { + content.append(inputLine); + } + in.close(); + con.disconnect(); + Client.logger.info("HTTP " + method + " Request ("+ urlOriginal +") Response: " + content.toString()); + return content.toString(); + } catch (IOException e) { + e.printStackTrace(); + return null; + } + } } diff --git a/src/main/java/net/silentclient/client/utils/types/UserData.java b/src/main/java/net/silentclient/client/utils/types/UserData.java index 193188f..15fc264 100644 --- a/src/main/java/net/silentclient/client/utils/types/UserData.java +++ b/src/main/java/net/silentclient/client/utils/types/UserData.java @@ -2,6 +2,7 @@ package net.silentclient.client.utils.types; public class UserData { public String access_token; + public int port; public UserData() { this.access_token = ""; @@ -14,4 +15,8 @@ public class UserData { public String getAccessToken() { return access_token; } + + public void setAccessToken(String access_token) { + this.access_token = access_token; + } } From 27e6a840f2277f6294644f3167aee41606114d94 Mon Sep 17 00:00:00 2001 From: kirillsaint Date: Wed, 2 Aug 2023 14:18:54 +0600 Subject: [PATCH 2/3] Fix --- src/main/java/net/silentclient/client/Client.java | 2 +- src/main/java/net/silentclient/client/utils/types/UserData.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/silentclient/client/Client.java b/src/main/java/net/silentclient/client/Client.java index 786f0ad..58945f0 100644 --- a/src/main/java/net/silentclient/client/Client.java +++ b/src/main/java/net/silentclient/client/Client.java @@ -520,7 +520,7 @@ public class Client { // Instances public String getApiUrl() { - return "http://localhost:" + getUserData().port; + return "http://localhost:" + getUserData().server_port; } public AccountManager getAccountManager() { diff --git a/src/main/java/net/silentclient/client/utils/types/UserData.java b/src/main/java/net/silentclient/client/utils/types/UserData.java index 15fc264..8c0df41 100644 --- a/src/main/java/net/silentclient/client/utils/types/UserData.java +++ b/src/main/java/net/silentclient/client/utils/types/UserData.java @@ -2,7 +2,7 @@ package net.silentclient.client.utils.types; public class UserData { public String access_token; - public int port; + public int server_port; public UserData() { this.access_token = ""; From aa8484c91f6229256907d994eae3cc7f72c6cc06 Mon Sep 17 00:00:00 2001 From: kirillsaint Date: Wed, 2 Aug 2023 15:43:29 +0600 Subject: [PATCH 3/3] Fixes --- .../client/cosmetics/AbstractShieldRenderer.java | 4 ++-- .../gui/silentmainmenu/components/AccountPicker.java | 9 ++++++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/main/java/net/silentclient/client/cosmetics/AbstractShieldRenderer.java b/src/main/java/net/silentclient/client/cosmetics/AbstractShieldRenderer.java index 3b60eb7..e7dc935 100644 --- a/src/main/java/net/silentclient/client/cosmetics/AbstractShieldRenderer.java +++ b/src/main/java/net/silentclient/client/cosmetics/AbstractShieldRenderer.java @@ -75,7 +75,7 @@ public class AbstractShieldRenderer extends ModelBase implements LayerRenderer