diff --git a/build.gradle.kts b/build.gradle.kts index 7d13ffb..e9ad888 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -71,6 +71,7 @@ sourceSets.main { tasks.withType(Jar::class) { from(shade.map { if(it.isDirectory) it else zipTree(it) }) + duplicatesStrategy = DuplicatesStrategy.EXCLUDE manifest { attributes( "ForceLoadAsMod" to true, diff --git a/src/main/java/gq/malwarefight/nosession/win/WindowsSandbox.java b/src/main/java/gq/malwarefight/nosession/win/WindowsSandbox.java new file mode 100644 index 0000000..04e9bf4 --- /dev/null +++ b/src/main/java/gq/malwarefight/nosession/win/WindowsSandbox.java @@ -0,0 +1,48 @@ +package gq.malwarefight.nosession.win; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.file.Files; + +public class WindowsSandbox { + /** + * Run a process sandboxed + * @return success or failure + */ + public static native boolean runProcess(String[] rwMounts, String[] roMounts, String[] args); + + /** + * Determines if we are running in a sandbox + * @return whether we are running in a sandbox + */ + public static native boolean isSandboxed(); + + /** + * This method was pulled from the Utils class so people can use this module as a standalone library + */ + private static void copy(InputStream i, OutputStream o) throws IOException { + byte[] buffer = new byte[16384]; + int read; + while ((read = i.read(buffer)) > 0) { + o.write(buffer, 0, read); + } + i.close(); + o.close(); + } + + static { + try { + File tempFile = Files.createTempFile("minecraft_sandbox", ".dll").toFile(); + try (InputStream is = WindowsSandbox.class.getResourceAsStream("/native/" + System.getProperty("os.arch") + "/windows/" + System.mapLibraryName("windows_sandbox"))) { + assert is != null: "Native library not compiled"; + copy(is, Files.newOutputStream(tempFile.toPath())); + } + System.load(tempFile.getAbsolutePath()); + tempFile.deleteOnExit(); + } catch (Exception e) { + throw new RuntimeException("Cannot instantiate WindowsSandbox"); + } + } +}