Skip to content

Commit

Permalink
Fixed proxy via batch (runing on Windoze, needs testing on Nux).
Browse files Browse the repository at this point in the history
  • Loading branch information
melezov committed May 27, 2013
1 parent 8bd0368 commit 843802c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 53 deletions.
31 changes: 0 additions & 31 deletions jsad/src/main/java/io/jvm/jsad/RandomTag.java

This file was deleted.

73 changes: 51 additions & 22 deletions jsad/src/main/java/io/jvm/jsad/RunnerProxy.java
Expand Up @@ -4,7 +4,7 @@
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayDeque;
import java.util.Random;

public class RunnerProxy extends RunnerBase {
private final RunnerDirect runnerDirect;
Expand Down Expand Up @@ -67,70 +67,99 @@ public Output exec(
return runnerDirect.exec(params, input, workingDir);
}

final ArrayDeque<String> paramList = new ArrayDeque<String>();
for(final String prefix: proxyPrefix) paramList.add(prefix);
for(final String param: params) paramList.add(param);
final OsProxy osProxy = OsProxy.getOsProxy();
final StringBuilder proxyBody = new StringBuilder(osProxy.header);

final String processTag =
String.format("%016X-%016X", System.currentTimeMillis(), random.nextLong());

final RandomTag tag = new RandomTag();
final File inputFile, outputFile, errorFile;

if (proxyInput && input != null) {
inputFile = new File(workingDir, tag.toString());
inputFile = new File(workingDir, processTag + ".input");
write(inputFile, input);
paramList.add("<");
paramList.add(inputFile.getAbsolutePath());
proxyBody.append(" < \"").append(inputFile.getAbsolutePath()).append('"');
}
else {
inputFile = null;
}

if (proxyOutput) {
outputFile = new File(workingDir, tag.toString());
paramList.add(">");
paramList.add(outputFile.getAbsolutePath());
outputFile = new File(workingDir, processTag + ".output");
proxyBody.append(" > \"").append(outputFile.getAbsolutePath()).append('"');
}
else {
outputFile = null;
}

if (proxyError) {
errorFile = new File(workingDir, tag.toString());
paramList.add("2>");
paramList.add(errorFile.getAbsolutePath());
errorFile = new File(workingDir, processTag + ".error");
proxyBody.append(" 2> \"").append(errorFile.getAbsolutePath()).append('"');
}
else {
errorFile = null;
}

final String[] newParams = paramList.toArray(new String[paramList.size()]);
final File proxyFile = new File(workingDir, processTag + osProxy.extension);
write(proxyFile, proxyBody.toString().getBytes(osProxy.encoding));

final String[] newParams = new String[osProxy.shell.length + params.length + 1];
newParams[0] = proxyFile.getAbsolutePath();
System.arraycopy(osProxy.shell, 0, newParams, 1, osProxy.shell.length);
System.arraycopy(params, 0, newParams, 1 + osProxy.shell.length, params.length);

final byte[] newInput = proxyInput ? null : input;

final Output output = runnerDirect.exec(newParams, newInput, workingDir);
proxyFile.delete();
if (inputFile != null) inputFile.delete();

final Output output = runnerDirect.exec(newParams, newInput, workingDir);
if (outputFile == null && errorFile == null) {
return output;
}

final Output newOutput = new Output(
output.code,
outputFile == null ? slurp(outputFile) : output.output,
errorFile == null ? slurp(errorFile) : output.error);
outputFile != null ? slurp(outputFile) : output.output,
errorFile != null ? slurp(errorFile) : output.error);

return newOutput;
}

// -----------------------------------------------------------------------------

private static boolean isWindows =
private static enum OsProxy {
WINDOWS(new String[] { "cmd", "/c" }, "@echo off\r\n%*", "cp1250", ".bat"),
LINUX (new String[] { "/bin/sh" }, "#!/bin/sh\n$@", "UTF-8", ".sh" );

public final String[] shell;
public final String header;
public final String encoding;
public final String extension;

private OsProxy(
final String[] shell,
final String header,
final String encoding,
final String extension) {
this.shell = shell;
this.header = header;
this.encoding = encoding;
this.extension = extension;
}

private static boolean isWindows =
"\\".equals(System.getProperty("file.separator"));

private static String[] proxyPrefix = isWindows
? new String[] { "cmd", "/c" }
: new String[] { "/bin/sh" };
public static OsProxy getOsProxy() {
return isWindows ? WINDOWS : LINUX;
}
}

// -----------------------------------------------------------------------------

private static Random random = new Random();

private static void write(final File file, final byte[] body) throws IOException {
final FileOutputStream fos = new FileOutputStream(file);
try {
Expand Down

0 comments on commit 843802c

Please sign in to comment.