mirror of
https://github.com/ReaJason/MemShellParty.git
synced 2026-09-25 16:31:53 +08:00
refactor: simplify code
This commit is contained in:
+70
-159
@@ -34,189 +34,99 @@ public class TomcatFilterProbe {
|
|||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("all")
|
|
||||||
private List<Map<String, String>> collectFiltersData(Object context) {
|
private List<Map<String, String>> collectFiltersData(Object context) {
|
||||||
List<Map<String, String>> filtersList = new ArrayList<Map<String, String>>();
|
Map<String, Map<String, Object>> aggregatedData = new LinkedHashMap<>();
|
||||||
Object[] filterMaps = (Object[]) invokeMethod(context, "findFilterMaps", null, null);
|
|
||||||
if (filterMaps == null || filterMaps.length == 0) {
|
|
||||||
return filtersList;
|
|
||||||
}
|
|
||||||
Object[] filterDefs = (Object[]) invokeMethod(context, "findFilterDefs", null, null);
|
|
||||||
Map<String, Object> filterConfigs = null;
|
|
||||||
try {
|
|
||||||
filterConfigs = (Map<String, Object>) getFieldValue(context, "filterConfigs");
|
|
||||||
} catch (Exception ignored) {
|
|
||||||
}
|
|
||||||
Map<String, Object> filterDefMap = new HashMap<String, Object>();
|
|
||||||
if (filterDefs != null) {
|
|
||||||
for (Object filterDef : filterDefs) {
|
|
||||||
String filterName = (String) invokeMethod(filterDef, "getFilterName", null, null);
|
|
||||||
if (filterName != null) {
|
|
||||||
filterDefMap.put(filterName, filterDef);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Set<String> processedFilters = new HashSet<String>();
|
|
||||||
for (Object filterMap : filterMaps) {
|
|
||||||
String filterName = (String) invokeMethod(filterMap, "getFilterName", null, null);
|
|
||||||
if (filterName == null || processedFilters.contains(filterName)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
processedFilters.add(filterName);
|
|
||||||
|
|
||||||
Map<String, String> filterInfo = new LinkedHashMap<String, String>();
|
try {
|
||||||
filterInfo.put("filterName", filterName);
|
Object[] filterMaps = (Object[]) invokeMethod(context, "findFilterMaps");
|
||||||
String filterClass = null;
|
if (filterMaps == null || filterMaps.length == 0) return new ArrayList<>();
|
||||||
Object filterDef = filterDefMap.get(filterName);
|
|
||||||
if (filterDef != null) {
|
Object[] filterDefs = (Object[]) invokeMethod(context, "findFilterDefs");
|
||||||
|
Map<?, ?> filterConfigs = (Map<?, ?>) getFieldValue(context, "filterConfigs");
|
||||||
|
|
||||||
|
for (Object fm : filterMaps) {
|
||||||
|
String name = (String) invokeMethod(fm, "getFilterName");
|
||||||
|
if (name == null) continue;
|
||||||
|
if (!aggregatedData.containsKey(name)) {
|
||||||
|
String filterClass = "N/A";
|
||||||
|
if (filterDefs != null) {
|
||||||
|
for (Object def : filterDefs) {
|
||||||
|
if (!name.equals(invokeMethod(def, "getFilterName"))) continue;
|
||||||
|
String cls = (String) invokeMethod(def, "getFilterClass");
|
||||||
|
if ((cls == null || "N/A".equals(cls)) && filterConfigs != null) {
|
||||||
|
Object config = filterConfigs.get(name);
|
||||||
|
Object filter = config != null ? invokeMethod(config, "getFilter") : null;
|
||||||
|
if (filter != null) cls = filter.getClass().getName();
|
||||||
|
}
|
||||||
|
if (cls != null) filterClass = cls;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Map<String, Object> info = new HashMap<>();
|
||||||
|
info.put("filterName", name);
|
||||||
|
info.put("filterClass", filterClass);
|
||||||
|
info.put("urlPatterns", new LinkedHashSet<String>());
|
||||||
|
info.put("servletNames", new LinkedHashSet<String>());
|
||||||
|
aggregatedData.put(name, info);
|
||||||
|
}
|
||||||
|
Map<String, Object> info = aggregatedData.get(name);
|
||||||
|
String[] urls = null;
|
||||||
try {
|
try {
|
||||||
filterClass = (String) invokeMethod(filterDef, "getFilterClass", null, null);
|
urls = (String[]) invokeMethod(fm, "getURLPatterns");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
try {
|
try {
|
||||||
Object clazz = invokeMethod(filterDef, "getFilterClass", null, null);
|
Object urlPattern = getFieldValue(fm, "urlPattern");
|
||||||
if (clazz instanceof Class) {
|
if (urlPattern instanceof String) {
|
||||||
filterClass = ((Class<?>) clazz).getName();
|
urls = new String[] { (String) urlPattern };
|
||||||
}
|
}
|
||||||
} catch (Exception ignored) {
|
} catch (Exception ignored) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (urls != null) ((Set<String>) info.get("urlPatterns")).addAll(Arrays.asList(urls));
|
||||||
}
|
}
|
||||||
if (filterClass == null || filterClass.isEmpty() || filterClass.equals("N/A")) {
|
} catch (Exception ignored) {}
|
||||||
try {
|
List<Map<String, String>> result = new ArrayList<>();
|
||||||
if (filterConfigs != null) {
|
for (Map<String, Object> entry : aggregatedData.values()) {
|
||||||
Object filterConfig = filterConfigs.get(filterName);
|
Map<String, String> finalInfo = new HashMap<>();
|
||||||
if (filterConfig != null) {
|
finalInfo.put("filterName", (String) entry.get("filterName"));
|
||||||
Object filter = invokeMethod(filterConfig, "getFilter", null, null);
|
finalInfo.put("filterClass", (String) entry.get("filterClass"));
|
||||||
if (filter != null) {
|
Set<?> urls = (Set<?>) entry.get("urlPatterns");
|
||||||
filterClass = filter.getClass().getName();
|
finalInfo.put("urlPatterns", urls.isEmpty() ? "" : urls.toString());
|
||||||
}
|
result.add(finalInfo);
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (Exception ignored) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
filterInfo.put("filterClass", filterClass != null ? filterClass : "N/A");
|
|
||||||
|
|
||||||
List<String> urlPatternsList = new ArrayList<String>();
|
|
||||||
List<String> servletNamesList = new ArrayList<String>();
|
|
||||||
|
|
||||||
for (Object fm : filterMaps) {
|
|
||||||
String mappingFilterName = (String) invokeMethod(fm, "getFilterName", null, null);
|
|
||||||
if (filterName.equals(mappingFilterName)) {
|
|
||||||
String[] urlPatterns = null;
|
|
||||||
try {
|
|
||||||
urlPatterns = (String[]) invokeMethod(fm, "getURLPatterns", null, null);
|
|
||||||
} catch (Exception e) {
|
|
||||||
try {
|
|
||||||
Object urlPattern = getFieldValue(fm, "urlPattern");
|
|
||||||
if (urlPattern instanceof String) {
|
|
||||||
urlPatterns = new String[] { (String) urlPattern };
|
|
||||||
}
|
|
||||||
} catch (Exception ignored) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (urlPatterns != null && urlPatterns.length > 0) {
|
|
||||||
for (String pattern : urlPatterns) {
|
|
||||||
urlPatternsList.add(pattern);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
String[] servletNames = null;
|
|
||||||
try {
|
|
||||||
servletNames = (String[]) invokeMethod(fm, "getServletNames", null, null);
|
|
||||||
} catch (Exception e) {
|
|
||||||
try {
|
|
||||||
Object servletName = getFieldValue(fm, "servletName");
|
|
||||||
if (servletName instanceof String) {
|
|
||||||
servletNames = new String[] { (String) servletName };
|
|
||||||
}
|
|
||||||
} catch (Exception ignored) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (servletNames != null && servletNames.length > 0) {
|
|
||||||
for (String servletName : servletNames) {
|
|
||||||
servletNamesList.add(servletName);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!urlPatternsList.isEmpty()) {
|
|
||||||
StringBuilder patterns = new StringBuilder();
|
|
||||||
for (int j = 0; j < urlPatternsList.size(); j++) {
|
|
||||||
patterns.append(urlPatternsList.get(j));
|
|
||||||
if (j < urlPatternsList.size() - 1) {
|
|
||||||
patterns.append(", ");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
filterInfo.put("urlPatterns", patterns.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!servletNamesList.isEmpty()) {
|
|
||||||
StringBuilder servletNames = new StringBuilder();
|
|
||||||
for (int j = 0; j < servletNamesList.size(); j++) {
|
|
||||||
servletNames.append(servletNamesList.get(j));
|
|
||||||
if (j < servletNamesList.size() - 1) {
|
|
||||||
servletNames.append(", ");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
filterInfo.put("servletNames", servletNames.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
filtersList.add(filterInfo);
|
|
||||||
}
|
}
|
||||||
|
return result;
|
||||||
return filtersList;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@SuppressWarnings("all")
|
@SuppressWarnings("all")
|
||||||
private String formatFiltersData(Map<String, List<Map<String, String>>> allFiltersData) {
|
private String formatFiltersData(Map<String, List<Map<String, String>>> allFiltersData) {
|
||||||
StringBuilder output = new StringBuilder();
|
StringBuilder output = new StringBuilder();
|
||||||
for (Map.Entry<String, List<Map<String, String>>> entry : allFiltersData.entrySet()) {
|
for (Map.Entry<String, List<Map<String, String>>> entry : allFiltersData.entrySet()) {
|
||||||
String contextRoot = entry.getKey();
|
String context = entry.getKey();
|
||||||
List<Map<String, String>> filters = entry.getValue();
|
List<Map<String, String>> filters = entry.getValue();
|
||||||
|
output.append("Context: ").append(context).append("\n");
|
||||||
output.append("Context: ").append(contextRoot).append("\n");
|
|
||||||
|
|
||||||
if (filters.isEmpty()) {
|
if (filters.isEmpty()) {
|
||||||
output.append("No filters found\n");
|
output.append("No filters found\n");
|
||||||
continue;
|
} else if (filters.size() == 1 && filters.get(0).containsKey("error")) {
|
||||||
}
|
|
||||||
|
|
||||||
if (filters.size() == 1 && filters.get(0).containsKey("error")) {
|
|
||||||
output.append(filters.get(0).get("error")).append("\n");
|
output.append(filters.get(0).get("error")).append("\n");
|
||||||
continue;
|
} else {
|
||||||
}
|
for (Map<String, String> info : filters) {
|
||||||
|
appendIfPresent(output, "", info.get("filterName"), "");
|
||||||
for (Map<String, String> filterInfo : filters) {
|
appendIfPresent(output, " -> ", info.get("filterClass"), "");
|
||||||
String filterName = filterInfo.get("filterName");
|
appendIfPresent(output, " -> URL:", info.get("urlPatterns"), "");
|
||||||
String filterClass = filterInfo.get("filterClass");
|
output.append("\n");
|
||||||
String urlPatterns = filterInfo.get("urlPatterns");
|
|
||||||
String servletNames = filterInfo.get("servletNames");
|
|
||||||
|
|
||||||
if (filterName != null) {
|
|
||||||
output.append(filterName);
|
|
||||||
}
|
}
|
||||||
if (filterClass != null) {
|
|
||||||
output.append(" -> ").append(filterClass);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (urlPatterns != null && !urlPatterns.isEmpty()) {
|
|
||||||
output.append(" -> URL:[").append(urlPatterns).append("]");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (servletNames != null && !servletNames.isEmpty()) {
|
|
||||||
output.append(" -> Servlet:[").append(servletNames).append("]");
|
|
||||||
}
|
|
||||||
|
|
||||||
output.append("\n");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return output.toString();
|
return output.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void appendIfPresent(StringBuilder sb, String prefix, String value, String suffix) {
|
||||||
|
if (value != null && !value.isEmpty()) {
|
||||||
|
sb.append(prefix).append(value).append(suffix);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@SuppressWarnings("all")
|
@SuppressWarnings("all")
|
||||||
private static String repeatString(String str, int count) {
|
private static String repeatString(String str, int count) {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
@@ -230,8 +140,7 @@ public class TomcatFilterProbe {
|
|||||||
private String getContextRoot(Object context) {
|
private String getContextRoot(Object context) {
|
||||||
String r = null;
|
String r = null;
|
||||||
try {
|
try {
|
||||||
r = (String) invokeMethod(invokeMethod(context, "getServletContext", null, null), "getContextPath", null,
|
r = (String) invokeMethod(invokeMethod(context, "getServletContext"), "getContextPath");
|
||||||
null);
|
|
||||||
} catch (Exception ignored) {
|
} catch (Exception ignored) {
|
||||||
}
|
}
|
||||||
String c = context.getClass().getName();
|
String c = context.getClass().getName();
|
||||||
@@ -286,7 +195,9 @@ public class TomcatFilterProbe {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("all")
|
@SuppressWarnings("all")
|
||||||
public static Object invokeMethod(Object obj, String methodName, Class<?>[] paramClazz, Object[] param) {
|
public static Object invokeMethod(Object obj, String methodName) {
|
||||||
|
Class<?>[] paramClazz = null;
|
||||||
|
Object[] param = null;
|
||||||
try {
|
try {
|
||||||
Class<?> clazz = (obj instanceof Class) ? (Class<?>) obj : obj.getClass();
|
Class<?> clazz = (obj instanceof Class) ? (Class<?>) obj : obj.getClass();
|
||||||
Method method = null;
|
Method method = null;
|
||||||
|
|||||||
Reference in New Issue
Block a user