From 4dde8dbd219c081c43609e049f59513668f21a9f Mon Sep 17 00:00:00 2001 From: Gabriel Tofvesson Date: Tue, 16 May 2017 00:04:11 +0200 Subject: [PATCH] Initial commit --- .idea/misc.xml | 22 +++++ .idea/modules.xml | 8 ++ JSafe.iml | 11 +++ src/net/tofvesson/jsafe/JSafe.java | 86 +++++++++++++++++++ .../jsafe/command/ProcessInteractor.java | 32 +++++++ 5 files changed, 159 insertions(+) create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 JSafe.iml create mode 100644 src/net/tofvesson/jsafe/JSafe.java create mode 100644 src/net/tofvesson/jsafe/command/ProcessInteractor.java diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..2e47c90 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,22 @@ + + + + + + + + + + 1.8 + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..11438e2 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/JSafe.iml b/JSafe.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/JSafe.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/net/tofvesson/jsafe/JSafe.java b/src/net/tofvesson/jsafe/JSafe.java new file mode 100644 index 0000000..1070f2e --- /dev/null +++ b/src/net/tofvesson/jsafe/JSafe.java @@ -0,0 +1,86 @@ +package net.tofvesson.jsafe; + +import javax.tools.JavaCompiler; +import javax.tools.ToolProvider; +import java.io.*; +import java.net.URL; + +@SuppressWarnings("ALL") +public class JSafe { + + // Special values in class + public static final String CLASSNAME = "&LOADEDCLASS;"; + public static final String AMPERSAND = "&"; + Process p; + + public JSafe(String code, String... args){ + Process sand = null; + try { + File f = File.createTempFile("exec", ".java"); + code = code.replace(CLASSNAME, f.getName().substring(0, f.getName().length()-5)).replace(AMPERSAND, "&"); + OutputStream o = new FileOutputStream(f); + o.write(code.getBytes()); + o.close(); + JavaCompiler j = ToolProvider.getSystemJavaCompiler(); + StringBuilder sb = new StringBuilder(); + for(String s : args) sb.append(s).append(' '); + if(sb.length()>0) sb.setLength(sb.length()-1); + if(j.run(null, null, null, f.getAbsolutePath())==0){ + f.delete(); + File f2 = new File(f.getAbsolutePath().substring(0, f.getAbsolutePath().length()-4)+"class"); + File f1 = new File(f2.getAbsolutePath().substring(0, f2.getAbsolutePath().length()-f2.getName().length())); + sand = Runtime.getRuntime().exec("java "+f2.getName().substring(0, f2.getName().length()-6), new String[]{}, f1.getAbsoluteFile()); + p = sand; + if(p!=null){ + Thread t = new Thread(() -> { + try{ + InputStream i = p.getInputStream(); + Thread.sleep(200); + while(p.isAlive() || i.available()>0){ + if(i.available()>0) System.out.write(i.read()); + else Thread.sleep(1); + } + f2.delete(); + }catch(Exception e){ e.printStackTrace(); } + }); + t.start(); + } + } + } catch (IOException e) { + e.printStackTrace(); + //} catch (InterruptedException e) { + e.printStackTrace(); + } finally{ if(sand==null) p = null; } + } + + + /* + f.delete(); + f = new File(f.getAbsolutePath()); + SandboxClassLoader loader = new SandboxClassLoader(null, "net.tofvesson.jsafe.JSafe"); + ArrayList a = new ArrayList<>(); + byte[] b = new byte[4096]; + int i; + while() + */ + class SandboxClassLoader extends ClassLoader{ + + protected final String[] blacklist; + protected final String[] packageBlacklist; + + public SandboxClassLoader(String[] packageBlacklist, String... blacklist){ + this.packageBlacklist = packageBlacklist==null?new String[]{}:packageBlacklist; + this.blacklist = blacklist; + } + + @Override + protected Class loadClass(String name, boolean resolve) throws ClassNotFoundException { + return super.loadClass(name, resolve); + } + + @Override + protected Package definePackage(String name, String specTitle, String specVersion, String specVendor, String implTitle, String implVersion, String implVendor, URL sealBase) throws IllegalArgumentException { + return super.definePackage(name, specTitle, specVersion, specVendor, implTitle, implVersion, implVendor, sealBase); + } + } +} diff --git a/src/net/tofvesson/jsafe/command/ProcessInteractor.java b/src/net/tofvesson/jsafe/command/ProcessInteractor.java new file mode 100644 index 0000000..f797387 --- /dev/null +++ b/src/net/tofvesson/jsafe/command/ProcessInteractor.java @@ -0,0 +1,32 @@ +package net.tofvesson.jsafe.command; + +import net.tofvesson.jsafe.JSafe; + +import java.io.IOException; +import java.io.OutputStream; + +public class ProcessInteractor { + + public ProcessInteractor(JSafe sandbox){ + + } + + final class CommandStream extends OutputStream { + + final String enc; + final char commandCode, notCommand; + boolean commandMode = false; + + public CommandStream(char commandCode){ + this.commandCode = commandCode; + notCommand = commandCode=='&'?'#':'&'; + enc = notCommand+"cmd;"; + } + + @Override + public void write(int b) throws IOException { + + } + + } +}