-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Shade WindowsSandbox into the main project
- Loading branch information
1 parent
02ab131
commit 563ae18
Showing
2 changed files
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/main/java/gq/malwarefight/nosession/win/WindowsSandbox.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
} | ||
} | ||
} |