diff --git a/src/openonyx/connection/Account.java b/src/openonyx/connection/Account.java new file mode 100644 index 0000000..2a756b5 --- /dev/null +++ b/src/openonyx/connection/Account.java @@ -0,0 +1,135 @@ +package openonyx.connection; + +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import java.util.UUID; +import net.minecraft.util.Session; +import openonyx.connection.internal.AuthException; + +public abstract class Account { + protected String i; + private final AccountType accountType; + protected long H; + protected UUID uuid; + + protected static long L(JsonObject jsonObject, String string, long l) { + JsonElement jsonElement; + return (jsonElement = jsonObject.get(string)) != null && jsonElement.isJsonPrimitive() && jsonElement.getAsJsonPrimitive().isNumber() + ? jsonElement.getAsLong() + : l; + } + + protected Account(AccountType accountType, String string) { + this.accountType = accountTypex; + this.i = string; + this.H = System.currentTimeMillis(); + } + + public String D() { + return this.i; + } + + public abstract void L() throws AuthException; + + public UUID getUuid() { + return this.uuid; + } + + public String d() { + return null; + } + + public String L() { + return null; + } + + protected static String L(JsonObject jsonObject, String string) { + JsonElement jsonElement; + return (jsonElement = jsonObject.get(string)) != null && jsonElement.isJsonPrimitive() && jsonElement.getAsJsonPrimitive().isString() + ? jsonElement.getAsString() + : null; + } + + public long d() { + return 0L; + } + + public abstract Session L() throws AuthException; + + public final JsonObject L() { + Account account1 = this; + JsonObject jsonObject = new JsonObject(); + jsonObject.addProperty("type", account1.accountType.d()); + jsonObject.addProperty("name", account1.i); + jsonObject.addProperty("addedAt", account1.H); + if (account1.uuid != null) { + jsonObject.addProperty("uuid", account1.uuid.toString()); + } + + account1.d(jsonObject); + return jsonObject; + } + + protected abstract void d(JsonObject jsonObject); + + protected static boolean L(JsonObject jsonObject, String string, boolean bl) { + JsonElement jsonElement; + return (jsonElement = jsonObject.get(string)) != null && jsonElement.isJsonPrimitive() && jsonElement.getAsJsonPrimitive().isBoolean() + ? jsonElement.getAsBoolean() + : bl; + } + + public long L() { + return this.H; + } + + public static Account L(JsonObject jsonObject) { + JsonObject jsonObject1 = jsonObject; + AccountType accountTypex; + if ((accountTypex = AccountType.L(L(jsonObject1, "type"))) == null) { + return null; + } + + String string; + if ((string = L(jsonObject1, "name")) != null && !string.isBlank()) { + JsonObject jsonObject2; + label37: { + account = switch (accountTypex) { + case OFFLINE -> new OfflineAccount(string); + case ACCESS_TOKEN -> new TokenAccount(string); + case MICROSOFT -> new MicrosoftAccount(string); + }; + String string1; + label36: + if ((string1 = L(jsonObject1, "uuid")) != null) { + try { + account.uuid = UUID.fromString(string1); + } catch (IllegalArgumentException illegalArgumentException) { + break label36; + } + + jsonObject2 = jsonObject1; + break label37; + } + + jsonObject2 = jsonObject1; + } + + JsonElement jsonElement; + if ((jsonElement = jsonObject2.get("addedAt")) != null && jsonElement.isJsonPrimitive() && jsonElement.getAsJsonPrimitive().isNumber()) { + account.H = jsonElement.getAsLong(); + } + + account.L(jsonObject1); + return account; + } else { + return null; + } + } + + protected abstract void L(JsonObject jsonObject); + + public AccountType getAccountType() { + return this.accountType; + } +} diff --git a/src/openonyx/connection/AccountManager.java b/src/openonyx/connection/AccountManager.java new file mode 100644 index 0000000..5d0eadc --- /dev/null +++ b/src/openonyx/connection/AccountManager.java @@ -0,0 +1,254 @@ +package openonyx.connection; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import java.io.File; +import java.io.IOException; +import java.net.Proxy; +import java.nio.charset.StandardCharsets; +import java.nio.file.AtomicMoveNotSupportedException; +import java.nio.file.CopyOption; +import java.nio.file.Files; +import java.nio.file.LinkOption; +import java.nio.file.OpenOption; +import java.nio.file.Path; +import java.nio.file.StandardCopyOption; +import java.nio.file.attribute.FileAttribute; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.UUID; +import java.util.concurrent.CompletableFuture; +import net.minecraft.util.Session; +import openonyx.connection.internal.AuthException; +import openonyx.core.MinecraftAccess; +import openonyx.core.OnyxClient; +import openonyx.utilities.AsyncExecutor; +import org.apache.logging.log4j.Logger; + +public class AccountManager implements MinecraftAccess { + private static final String m = "accounts.json"; + private final ProxySettings proxySettings; + private static final int l = 1; + private static final String I = UUID.randomUUID().toString(); + private final List accounts = new ArrayList<>(); + private Session session; + private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create(); + private static final String H = "accounts.json.tmp"; + private Account account; + + public Proxy d() { + return this.proxySettings.d(); + } + + public void d() { + AccountManager accountManager1 = this; + Path path = accountManager1.L(); + LinkOption[] linkOptions = new LinkOption[0]; + boolean bl = true; + if (Files.isRegularFile(path, linkOptions)) { + JsonObject jsonObject; + try { + String string = new String(Files.readAllBytes(accountManager1.L()), StandardCharsets.UTF_8); + jsonObject = new JsonParser().parse(string).getAsJsonObject(); + } catch (IOException | RuntimeException iOException) { + OnyxClient.LOGGER.error("Failed to read accounts", iOException); + return; + } + + JsonElement jsonElement; + if ((jsonElement = jsonObject.get("proxy")) != null && jsonElement.isJsonObject()) { + accountManager1.proxySettings.L(jsonElement.getAsJsonObject()); + } + + JsonElement jsonElement2; + if ((jsonElement2 = jsonObject.get("accounts")) != null && jsonElement2.isJsonArray()) { + accountManager1.accounts.clear(); + Iterator iterator = jsonElement2.getAsJsonArray().iterator(); + + label43: + while (true) { + for (Iterator iterator1 = iterator; iterator1.hasNext(); iterator1 = iterator) { + JsonElement jsonElement1; + if ((jsonElement1 = (JsonElement)iterator.next()).isJsonObject()) { + try { + Account accountx; + if ((accountx = Account.L(jsonElement1.getAsJsonObject())) != null) { + accountManager1.accounts.add(accountx); + } + } catch (RuntimeException runtimeException) { + OnyxClient.LOGGER.warn("Skipping malformed account entry", runtimeException); + } + continue label43; + } + } + + return; + } + } + } + } + + public Account getAccount() { + return this.account; + } + + private void L(Session session) { + if (this.session == null) { + this.session = mc.getSession(); + } + + mc.setSession(sessionx); + } + + public void i(Account account) { + this.accounts.add(accountx); + this.L(); + } + + public boolean L() { + AccountManager accountManager1 = this; + JsonArray jsonArray = new JsonArray(); + synchronized (accountManager1.accounts) { + Iterator iterator; + Iterator iterator1 = iterator = accountManager1.accounts.iterator(); + + while (iterator1.hasNext()) { + Account accountx = (Account)iterator.next(); + iterator1 = iterator; + jsonArray.add(accountx.L()); + } + } + + JsonObject jsonObject = new JsonObject(); + jsonObject.addProperty("version", 1); + jsonObject.add("accounts", jsonArray); + jsonObject.add("proxy", accountManager1.proxySettings.L()); + Path path = accountManager1.d().resolve("accounts.json.tmp"); + + try { + Path path1 = accountManager1.d(); + FileAttribute[] fileAttributes = new FileAttribute[0]; + boolean bl = true; + Files.createDirectories(path1, fileAttributes); + byte[] b = GSON.toJson(jsonObject).getBytes(StandardCharsets.UTF_8); + OpenOption[] openOptions = new OpenOption[0]; + boolean bl1 = true; + Files.write(path, b, openOptions); + + try { + Path path3 = accountManager1.L(); + CopyOption[] copyOptions1 = new CopyOption[2]; + bl1 = true; + copyOptions1[0] = StandardCopyOption.ATOMIC_MOVE; + copyOptions1[1] = StandardCopyOption.REPLACE_EXISTING; + Files.move(path, path3, copyOptions1); + } catch (AtomicMoveNotSupportedException atomicMoveNotSupportedException) { + Path path2 = accountManager1.L(); + CopyOption[] copyOptions = new CopyOption[1]; + bl1 = true; + copyOptions[0] = StandardCopyOption.REPLACE_EXISTING; + Files.move(path, path2, copyOptions); + } + + return true; + } catch (IOException iOException) { + OnyxClient.LOGGER.error("Failed to save accounts", iOException); + return false; + } + } + + public Path d() { + return new File(mc.mcDataDir, "onyx").toPath(); + } + + public AccountManager() { + this.proxySettings = new ProxySettings(); + } + + public Path L() { + return this.d().resolve("accounts.json"); + } + + public ProxySettings getProxySettings() { + return this.proxySettings; + } + + public Proxy L() { + return this.proxySettings.D(); + } + + public List L() { + return List.copyOf(this.accounts); + } + + public static String L() { + return I; + } + + public CompletableFuture L() { + AccountManager accountManager1 = this; + Account accountx = accountManager1.account; + return accountManager1.account == null ? CompletableFuture.completedFuture(null) : accountManager1.d(accountx); + } + + public CompletableFuture d(Account account) { + return CompletableFuture.runAsync(() -> { + AccountManager accountManager2 = this; + + Session sessionx; + try { + sessionx = accountx.L(); + } catch (AuthException authException) { + throw new AccountManagerPart(authException.getMessage()); + } catch (RuntimeException runtimeException) { + Logger logger = OnyxClient.LOGGER; + Object[] objects = new Object[2]; + boolean bl = true; + objects[0] = accountx.D(); + objects[1] = runtimeException; + logger.error("Unexpected error logging into '{}'", objects); + throw new AccountManagerPart(new StringBuilder().insert(0, "Unexpected error: ").append(runtimeException).toString()); + } + + accountManager2.L(); + mc.addScheduledTask(() -> { + this.L(sessionx); + this.account = accountx; + }); + }, AsyncExecutor.getExecutorservice()); + } + + public CompletableFuture L(Account account) { + return CompletableFuture.runAsync(() -> { + try { + accountx.L(); + } catch (AuthException authException) { + throw new AccountManagerPart(authException.getMessage()); + } + + this.L(); + }, AsyncExecutor.getExecutorservice()); + } + + public void L(Account account) { + if (this.accounts.remove(accountx)) { + if (this.account == accountx) { + this.account = null; + } + + this.L(); + } + } + + public void L() { + if (this.session != null) { + mc.setSession(this.session); + this.account = null; + } + } +} diff --git a/src/openonyx/connection/AccountManagerPart.java b/src/openonyx/connection/AccountManagerPart.java new file mode 100644 index 0000000..9a6491a --- /dev/null +++ b/src/openonyx/connection/AccountManagerPart.java @@ -0,0 +1,7 @@ +package openonyx.connection; + +public class AccountManagerPart extends RuntimeException { + public AccountManagerPart(String string) { + super(string); + } +} diff --git a/src/openonyx/connection/AccountPart.java b/src/openonyx/connection/AccountPart.java new file mode 100644 index 0000000..1fe526d --- /dev/null +++ b/src/openonyx/connection/AccountPart.java @@ -0,0 +1,25 @@ +package openonyx.connection; + +// $VF: synthetic class +class AccountPart { + static { + int[] i = new int[AccountType.values().length]; + boolean bl = true; + e = i; + + try { + e[AccountType.OFFLINE.ordinal()] = 1; + } catch (NoSuchFieldError noSuchFieldError2) { + } + + try { + e[AccountType.ACCESS_TOKEN.ordinal()] = 2; + } catch (NoSuchFieldError noSuchFieldError1) { + } + + try { + e[AccountType.MICROSOFT.ordinal()] = 3; + } catch (NoSuchFieldError noSuchFieldError) { + } + } +} diff --git a/src/openonyx/connection/AccountType.java b/src/openonyx/connection/AccountType.java new file mode 100644 index 0000000..e2bcd3a --- /dev/null +++ b/src/openonyx/connection/AccountType.java @@ -0,0 +1,53 @@ +package openonyx.connection; + +public enum AccountType { + ACCESS_TOKEN("access_token", "Access Token", true), + MICROSOFT("microsoft", "Microsoft", true), + OFFLINE("offline", "Offline", false); + + private final String d; + private final String i; + private final boolean H; + + public String d() { + return this.d; + } + + private static AccountType[] L() { + AccountType[] accountTypes = new AccountType[3]; + boolean bl = true; + accountTypes[0] = OFFLINE; + accountTypes[1] = ACCESS_TOKEN; + accountTypes[2] = MICROSOFT; + return accountTypes; + } + + AccountType(String string1, String string2, boolean bl) { + this.d = string1; + this.i = string2; + this.H = bl; + } + + public static AccountType L(String string) { + String string1 = string; + AccountType[] accountTypes; + int ix = (accountTypes = values()).length; + + for (int k = j = 0; k < ix; k = ++j) { + AccountType accountType; + if ((accountType = accountTypes[j]).d.equalsIgnoreCase(string1)) { + return accountType; + } + } + + return null; + } + + public boolean L() { + return this.H; + } + + public String L() { + return this.i; + } +}