potential fix executing extensions X-platform

This commit is contained in:
sirjonasxx 2018-12-01 13:49:29 +01:00
parent e8f97e5a9f
commit 64af70e44a
2 changed files with 27 additions and 17 deletions

View File

@ -10,29 +10,35 @@ import java.util.Map;
*/ */
public class ExecutionInfo { public class ExecutionInfo {
private static Map<String, String> extensionTypeToExecutionCommand; private static Map<String, String[]> extensionTypeToExecutionCommand;
public final static List<String> ALLOWEDEXTENSIONTYPES; public final static List<String> ALLOWEDEXTENSIONTYPES;
public final static String EXTENSIONSDIRECTORY = "Extensions"; public final static String EXTENSIONSDIRECTORY = "Extensions";
static { static {
extensionTypeToExecutionCommand = new HashMap<>(); extensionTypeToExecutionCommand = new HashMap<>();
extensionTypeToExecutionCommand.put("*.jar","java -jar \"{path}\""); extensionTypeToExecutionCommand.put("*.jar", new String[]{"java", "-jar", "{path}"});
extensionTypeToExecutionCommand.put("*.py","python \"{path}\""); extensionTypeToExecutionCommand.put("*.py", new String[]{"python", "{path}"});
extensionTypeToExecutionCommand.put("*.py3","python3 \"{path}\""); extensionTypeToExecutionCommand.put("*.py3", new String[]{"python3", "{path}"});
extensionTypeToExecutionCommand.put("*.sh","\"{path}\""); extensionTypeToExecutionCommand.put("*.sh", new String[]{"{path}"});
extensionTypeToExecutionCommand.put("*.exe","\"{path}\""); extensionTypeToExecutionCommand.put("*.exe", new String[]{"{path}"});
String[] extraArgs = {"-p", "{port}", "-f", "{filename}", "-c", "{cookie}"};
for(String type : extensionTypeToExecutionCommand.keySet()) { for(String type : extensionTypeToExecutionCommand.keySet()) {
String[] commandShort = extensionTypeToExecutionCommand.get(type);
String[] combined = new String[extraArgs.length + commandShort.length];
System.arraycopy(commandShort, 0, combined, 0, commandShort.length);
System.arraycopy(extraArgs, 0, combined, commandShort.length, extraArgs.length);
extensionTypeToExecutionCommand.put( extensionTypeToExecutionCommand.put(
type, type,
extensionTypeToExecutionCommand.get(type) + " -p {port} -f \"{filename}\" -c {cookie}" combined
); );
} }
ALLOWEDEXTENSIONTYPES = new ArrayList<>(extensionTypeToExecutionCommand.keySet()); ALLOWEDEXTENSIONTYPES = new ArrayList<>(extensionTypeToExecutionCommand.keySet());
} }
public static String getExecutionCommand(String type) { public static String[] getExecutionCommand(String type) {
return extensionTypeToExecutionCommand.get(type); return extensionTypeToExecutionCommand.get(type);
} }

View File

@ -86,15 +86,19 @@ public class NormalExtensionRunner implements ExtensionRunner {
public void tryRunExtension(String path, int port) { public void tryRunExtension(String path, int port) {
try { try {
String filename = Paths.get(path).getFileName().toString(); String filename = Paths.get(path).getFileName().toString();
String execCommand = ExecutionInfo.getExecutionCommand(getFileExtension(path)) String[] execCommand = ExecutionInfo.getExecutionCommand(getFileExtension(path));
execCommand = Arrays.copyOf(execCommand, execCommand.length);
String cookie = Authenticator.generateCookieForExtension(filename);
for (int i = 0; i < execCommand.length; i++) {
execCommand[i] = execCommand[i]
.replace("{path}", path) .replace("{path}", path)
.replace("{port}", port+"") .replace("{port}", port+"")
.replace("{filename}", filename) .replace("{filename}", filename)
.replace("{cookie}", Authenticator.generateCookieForExtension(filename)); .replace("{cookie}", cookie);
Process proc = Runtime.getRuntime().exec(execCommand); }
ProcessBuilder pb = new ProcessBuilder(execCommand);
// Process proc = Runtime.getRuntime().exec(execCommand);
Process proc = pb.start();
if (Main.hasFlag(ExtensionRunner.SHOW_EXTENSIONS_LOG)) { if (Main.hasFlag(ExtensionRunner.SHOW_EXTENSIONS_LOG)) {
String sep = "" + System.lineSeparator(); String sep = "" + System.lineSeparator();