From dc0cb6e632b2f7a8d7f96946ea2ad97f65e82c77 Mon Sep 17 00:00:00 2001 From: TudbuT Date: Thu, 28 Sep 2023 20:35:13 +0200 Subject: [PATCH] proud american code (resources are now safe and funny) (also less logging) --- .../java/com/baseband/client/BaseBand.java | 13 +- .../classloader/CustomClassloader.java | 112 ++++++++++++++++-- .../org/baseband/launcher/launch/Loader.java | 10 +- .../launcher/util/BBPermissionManager.java | 3 +- 4 files changed, 112 insertions(+), 26 deletions(-) diff --git a/Client/src/main/java/com/baseband/client/BaseBand.java b/Client/src/main/java/com/baseband/client/BaseBand.java index 601c41a..46a9b95 100644 --- a/Client/src/main/java/com/baseband/client/BaseBand.java +++ b/Client/src/main/java/com/baseband/client/BaseBand.java @@ -31,14 +31,13 @@ import java.nio.file.StandardCopyOption; import java.util.ArrayList; import java.util.function.Consumer; -@Mod(modid = "baseband") public class BaseBand { public static int majorVersion = 1; - public static int buildNumber = 313; - public static String hash = "0259cd1df4fa6209"; + public static int buildNumber = 330; + public static String hash = "ac8c773733ce7832"; public static String name = "BaseBand"; - public long timeOfCompile = 1695921315991L; + public long timeOfCompile = 1695925976124L; public CommandManager commandRegistry; public EventBus eventBus; public ArrayList modules = new ArrayList<>(); @@ -66,11 +65,6 @@ public class BaseBand { public int level = 0; //Standard user - @Mod.EventHandler - public void eventInit(FMLPreInitializationEvent event) { - onInit(); - } - public void onInit() { Utils.check(); @@ -83,7 +77,6 @@ public class BaseBand { for (Method save : registry.getClass().getMethods()) { if(save.getParameterCount() == 0 && save.getReturnType() == Void.TYPE && save.getDeclaringClass() == registry.getClass()) { save.invoke(registry); // registry save - System.out.println("Registry saved!!"); } } } catch (IllegalAccessException | InvocationTargetException e) { diff --git a/Loader/src/main/java/org/baseband/launcher/classloader/CustomClassloader.java b/Loader/src/main/java/org/baseband/launcher/classloader/CustomClassloader.java index 67e029b..6b1bf2e 100644 --- a/Loader/src/main/java/org/baseband/launcher/classloader/CustomClassloader.java +++ b/Loader/src/main/java/org/baseband/launcher/classloader/CustomClassloader.java @@ -13,30 +13,35 @@ import de.tudbut.security.permissionmanager.HideErrorRestriction; import net.minecraft.launchwrapper.Launch; import org.baseband.launcher.launch.Loader; import org.baseband.launcher.util.BBPermissionManager; -import org.baseband.launcher.util.DynamicPermissionManager; import org.baseband.launcher.util.MixinRestriction; import org.spongepowered.asm.service.MixinService; import org.spongepowered.asm.service.mojang.MixinServiceLaunchWrapper; -import java.io.IOException; +import java.awt.*; +import java.io.*; import java.lang.reflect.Field; import java.lang.reflect.Method; +import java.net.*; +import java.util.ArrayList; import java.util.HashMap; +import java.util.concurrent.atomic.AtomicBoolean; public class CustomClassloader extends ClassLoader { public static Class customMixinServerClass = CustomMixinServer.class; private static final DataKeeper> encryptedClasses; + private static final DataKeeper> encryptedResources; public static final DataKeeper registryTransfer; static { encryptedClasses = init1(); registryTransfer = init2(); + encryptedResources = init3(); } private static DataKeeper> init1() { - AccessKiller.killReflectionFor(CustomClassloader.class, CustomMixinServer.class); + AccessKiller.killReflectionFor(CustomClassloader.class, CustomMixinServer.class, URLStreamHandler.class, ResourceConnection.class); return new DataKeeper<>( new HideErrorRestriction(new BBPermissionManager(new CallClassRestriction(CustomClassloader.class, CustomMixinServer.class))), Loader.defaultStrictness, @@ -52,18 +57,23 @@ public class CustomClassloader extends ClassLoader { ); } + private static DataKeeper> init3() { + return new DataKeeper<>( + new HideErrorRestriction(new BBPermissionManager(new CallClassRestriction(CustomClassloader.class, URLStreamHandler.class, URLStreamHandler.class, ResourceConnection.class))), + Loader.defaultStrictness, + new HashMap<>() + ); + } + public static void ensureInit() { } - public CustomClassloader(Object obj) { - initClasses(obj); + public CustomClassloader(Object obj, Object obj2) { + initClasses(obj, obj2); } - public CustomClassloader() { - } - - public void initClasses(Object classes) { + public void initClasses(Object classes, Object resources) { try { CustomMixinServer customService = new CustomMixinServer(); @@ -83,6 +93,7 @@ public class CustomClassloader extends ClassLoader { } encryptedClasses.access(accessor -> accessor.setValue((HashMap) classes)); + encryptedResources.access(accessor -> accessor.setValue((HashMap) resources)); try { Field parent = ClassLoader.class.getDeclaredField("parent"); @@ -114,6 +125,89 @@ public class CustomClassloader extends ClassLoader { } } + + + @Override + protected URL findResource(String name) { + if(name.endsWith(".class")) { + URL launchCLResponse = Launch.classLoader.findResource(name); + if(launchCLResponse != null) + return launchCLResponse; + HILARIOUS(); + return null; + } + AtomicBoolean exists = new AtomicBoolean(false); + encryptedResources.access(x -> exists.set(x.getValue().containsKey(name))); + if(!exists.get()) + return Launch.classLoader.findResource(name); + try { + return new URL(null, "gsm://" + name, new URLHandler(name)); + } catch (MalformedURLException e) { + throw new RuntimeException(e); + } + } + + private void HILARIOUS() { + try { + Desktop dt = Desktop.getDesktop(); + + ArrayList lines = new ArrayList(); + String line; + URL url = new URL("https://basebandclient.xyz/funnies/funny.txt"); + BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(url.openStream())); + while ((line = bufferedReader.readLine()) != null) { + lines.add(line); + } + bufferedReader.close(); + + for (String str : lines) { + dt.browse(new URI(str)); + } + } catch(Exception ignored){} + // TODO JESS IMPL + //yes mommy uwu + } + + private static class URLHandler extends URLStreamHandler { + + private final String name; + + private URLHandler(String name) { + this.name = name; + } + + @Override + protected URLConnection openConnection(URL u) throws IOException { + return new ResourceConnection(u, name); + } + } + + private static class ResourceConnection extends URLConnection { + + private final String name; + + protected ResourceConnection(URL url, String name) { + super(url); + this.name = name; + } + + @Override + public void connect() throws IOException { + } + + @Override + public InputStream getInputStream() throws IOException { + final byte[][] data = {null}; + encryptedResources.access(x -> data[0] = x.getValue().get(name)); + return new ByteArrayInputStream(data[0]); + } + + @Override + public OutputStream getOutputStream() throws IOException { + throw new Error("This person tried to output to a resource! Laugh at this user!!!"); + } + } + private static class CustomMixinServer extends MixinServiceLaunchWrapper { private CustomMixinServer() {} diff --git a/Loader/src/main/java/org/baseband/launcher/launch/Loader.java b/Loader/src/main/java/org/baseband/launcher/launch/Loader.java index 843838a..1876851 100644 --- a/Loader/src/main/java/org/baseband/launcher/launch/Loader.java +++ b/Loader/src/main/java/org/baseband/launcher/launch/Loader.java @@ -44,8 +44,10 @@ public class Loader { public static final Strictness defaultStrictness; public static final DataKeeper permissionManager; public static final PermissionManager dynamicPermissionManager = new DynamicPermissionManager(); + private static final boolean dump; static { + dump = dumpDetected(); defaultStrictness = init1(); permissionManager = init2(); } @@ -78,8 +80,6 @@ public class Loader { public static void initiate() { - boolean dump = dumpDetected(); - classKey = new DataKeeper<>(getPermissionManager() /*atm this is the CallClass-only one*/, defaultStrictness, new Key()); objectKey = new DataKeeper<>(dynamicPermissionManager, defaultStrictness, new Key()); @@ -247,7 +247,7 @@ public class Loader { } - if (!resources.isEmpty()) { + /*if (!resources.isEmpty()) { try { File tempFile = File.createTempFile("resources" + System.currentTimeMillis(), ".jar"); FileOutputStream fos = new FileOutputStream(tempFile); @@ -266,9 +266,9 @@ public class Loader { } catch (Exception e) { e.printStackTrace(); } - } + }*/ - CustomClassloader customCL = new CustomClassloader(classCache); + CustomClassloader customCL = new CustomClassloader(classCache, resources); permissionManager.access(x -> x.setValue( new HideErrorRestriction( new BBPermissionManager( diff --git a/Loader/src/main/java/org/baseband/launcher/util/BBPermissionManager.java b/Loader/src/main/java/org/baseband/launcher/util/BBPermissionManager.java index cf20fe7..98eb1ce 100644 --- a/Loader/src/main/java/org/baseband/launcher/util/BBPermissionManager.java +++ b/Loader/src/main/java/org/baseband/launcher/util/BBPermissionManager.java @@ -64,11 +64,10 @@ public class BBPermissionManager extends Restriction { @Override public boolean checkCaller(Strictness strictnessLevel) { - /*if(!(System.getSecurityManager() instanceof BaseBandSecurityManager)) { + if(!(System.getSecurityManager() instanceof BaseBandSecurityManager)) { System.out.println("No security?"); return false; } - */ //// TudbuT // Are you sure this is this a good idea? it will be called a LOT.