From ea62001d41a47c961c1d3d7125c75f637944a517 Mon Sep 17 00:00:00 2001 From: TudbuT Date: Thu, 20 Jun 2024 03:15:31 +0200 Subject: [PATCH] properly implement download speed checking --- .../java/de/com/baseband/launcher/Loader.java | 40 +++++++++++++++++-- .../de/com/baseband/launcher/Tweaker.java | 3 +- .../classloader/CustomClassLoader.java | 9 ----- .../baseband/launcher/util/SecondCounter.java | 39 ------------------ 4 files changed, 38 insertions(+), 53 deletions(-) delete mode 100644 Loader/src/main/java/de/com/baseband/launcher/util/SecondCounter.java diff --git a/Loader/src/main/java/de/com/baseband/launcher/Loader.java b/Loader/src/main/java/de/com/baseband/launcher/Loader.java index ee2b3b0..482876c 100644 --- a/Loader/src/main/java/de/com/baseband/launcher/Loader.java +++ b/Loader/src/main/java/de/com/baseband/launcher/Loader.java @@ -11,7 +11,9 @@ import de.tudbut.io.TypedInputStream; import de.tudbut.io.TypedOutputStream; import de.tudbut.net.http.HTTPUtils; import de.tudbut.parsing.TCN; +import de.tudbut.security.AccessKiller; import de.tudbut.tools.Hasher; +import de.tudbut.tools.SimpleLock; import de.tudbut.tools.Tools; import de.tudbut.tools.encryption.Key; import de.tudbut.tools.encryption.RawKey; @@ -29,6 +31,7 @@ import java.net.Socket; import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.HashMap; +import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; @@ -59,8 +62,14 @@ public class Loader implements Util { private static CustomClassLoader classLoader; public static void run() { + AccessKiller.killReflectionFor( + Tweaker.class, + Loader.class, + CustomClassLoader.class, + CustomClassLoader.CustomMixinServer.class); + try(Socket client = new Socket("baseband.com.de", 40000)) { - client.setSoTimeout(15000); + client.setSoTimeout(5000); client.getOutputStream().write(0); TypedInputStream inputStream = new TypedInputStream(client.getInputStream()); TypedOutputStream outputStream = new TypedOutputStream(client.getOutputStream()); @@ -92,12 +101,37 @@ public class Loader implements Util { TCN clientData = TCN.readMap(Tools.stringToMap(key.decryptString(inputStream.readString()))); HashMap data = new HashMap<>(); + SimpleLock downloadUpdated = new SimpleLock(); + SimpleLock continueDownload = new SimpleLock(); + AtomicInteger bytes = new AtomicInteger(); + new Thread(() -> { + long lastUpdate = System.currentTimeMillis(); + while (classLoader == null) { + downloadUpdated.waitHere(2000); + if(downloadUpdated.isLocked()) { + LOGGER.warn("No new BaseBand chunk has been downloaded in 2 seconds. Consider restarting your game."); + } + else if(System.currentTimeMillis() - lastUpdate >= 500) { + LOGGER.info("Downloading at {}KB/s...", (int) ((bytes.get() / 1024f) * (System.currentTimeMillis() - lastUpdate) / 1000f)); + lastUpdate = System.currentTimeMillis(); + bytes.set(0); + } + downloadUpdated.lock(); + continueDownload.unlock(); + } + }, "Download guard").start(); RawKey rk = new RawKey(key.toBytes()); int n = inputStream.readInt(); for (int i = 0; i < n; i++) { - data.put(rk.decryptString(inputStream.readString()), rk.decryptBytes(inputStream.readByteArray())); + String name = inputStream.readString(); + byte[] classBytes = inputStream.readByteArray(); + data.put(rk.decryptString(name), rk.decryptBytes(classBytes)); + bytes.getAndAdd(classBytes.length); + continueDownload.lock(); + downloadUpdated.unlock(); + continueDownload.waitHere(); } - LOGGER.info("BaseBand downloaded: {} classes.", data.size()); + LOGGER.info("BaseBand downloaded: {} chunks.", data.size()); classLoader = new CustomClassLoader(data); classLoader.inject(); classLoader.informClient(clientData); diff --git a/Loader/src/main/java/de/com/baseband/launcher/Tweaker.java b/Loader/src/main/java/de/com/baseband/launcher/Tweaker.java index deabd69..f217ce5 100644 --- a/Loader/src/main/java/de/com/baseband/launcher/Tweaker.java +++ b/Loader/src/main/java/de/com/baseband/launcher/Tweaker.java @@ -5,7 +5,6 @@ package de.com.baseband.launcher; -import de.com.baseband.launcher.util.Util; import net.minecraft.launchwrapper.ITweaker; import net.minecraft.launchwrapper.LaunchClassLoader; import net.minecraftforge.common.ForgeVersion; @@ -22,7 +21,7 @@ import java.util.Map; @SuppressWarnings("unused") @IFMLLoadingPlugin.Name("baseband") @IFMLLoadingPlugin.MCVersion(ForgeVersion.mcVersion) -public class Tweaker implements ITweaker, IFMLLoadingPlugin, Util { +public class Tweaker implements ITweaker, IFMLLoadingPlugin { public static void loaded(Class baseBandClass) { Loader.loaded(baseBandClass); diff --git a/Loader/src/main/java/de/com/baseband/launcher/classloader/CustomClassLoader.java b/Loader/src/main/java/de/com/baseband/launcher/classloader/CustomClassLoader.java index c1f5627..c339931 100644 --- a/Loader/src/main/java/de/com/baseband/launcher/classloader/CustomClassLoader.java +++ b/Loader/src/main/java/de/com/baseband/launcher/classloader/CustomClassLoader.java @@ -1,7 +1,5 @@ package de.com.baseband.launcher.classloader; -import de.com.baseband.launcher.url.ByteURLHandler; -import de.com.baseband.launcher.url.ResourceConnection; import de.com.baseband.launcher.url.URLWrapper; import de.com.baseband.launcher.util.Util; import de.tudbut.parsing.TCN; @@ -43,13 +41,6 @@ public class CustomClassLoader extends ClassLoader implements Util { } public CustomClassLoader(Map data) { - AccessKiller.killReflectionFor( - CustomClassLoader.class, - CustomMixinServer.class, - URLWrapper.class, - ByteURLHandler.class, - ResourceConnection.class); - names.addAll(data.keySet()); this.binaryKeeper = new DataKeeper<>( new PermissionOR(new ClassLoaderRestriction(this), new CallClassRestriction(CustomClassLoader.class, CustomMixinServer.class)), diff --git a/Loader/src/main/java/de/com/baseband/launcher/util/SecondCounter.java b/Loader/src/main/java/de/com/baseband/launcher/util/SecondCounter.java deleted file mode 100644 index c765ad1..0000000 --- a/Loader/src/main/java/de/com/baseband/launcher/util/SecondCounter.java +++ /dev/null @@ -1,39 +0,0 @@ -package de.com.baseband.launcher.util; - -import java.util.LinkedList; -import java.util.Queue; - -public class SecondCounter { - - private static class TimeStampedByte { - long timestamp; - int bytes; - - TimeStampedByte(long timestamp, int bytes) { - this.timestamp = timestamp; - this.bytes = bytes; - } - } - - private final Queue count = new LinkedList<>(); - - public void increment(int bytes) { - count.add(new TimeStampedByte(System.currentTimeMillis() + 1000L, bytes)); - } - - public int getCount() { - long time = System.currentTimeMillis(); - try { - while (!count.isEmpty() && count.peek().timestamp < time) { - count.remove(); - } - } catch (Exception e) { - // empty catch block - } - int byteCount = 0; - for (TimeStampedByte tsb : count) { - byteCount += tsb.bytes; - } - return byteCount; - } -} \ No newline at end of file