2021-12-06 10:49:40 +08:00
|
|
|
package com.janetfilter.core;
|
2021-11-30 17:06:48 +08:00
|
|
|
|
2021-12-06 10:49:40 +08:00
|
|
|
import com.janetfilter.core.commons.DebugInfo;
|
|
|
|
import com.janetfilter.core.plugin.PluginManager;
|
2021-11-30 17:06:48 +08:00
|
|
|
|
|
|
|
import java.lang.instrument.Instrumentation;
|
2021-12-02 10:58:59 +08:00
|
|
|
import java.util.Set;
|
2021-11-30 17:06:48 +08:00
|
|
|
|
|
|
|
public class Initializer {
|
2022-02-15 17:34:24 +08:00
|
|
|
public static void init(Environment environment) {
|
2022-01-20 14:55:03 +08:00
|
|
|
DebugInfo.useFile(environment.getLogsDir());
|
|
|
|
DebugInfo.info(environment.toString());
|
2022-01-13 19:31:41 +08:00
|
|
|
|
2022-01-25 15:40:39 +08:00
|
|
|
Dispatcher dispatcher = new Dispatcher(environment);
|
2022-02-15 17:34:24 +08:00
|
|
|
new PluginManager(dispatcher, environment).loadPlugins();
|
2021-11-30 17:06:48 +08:00
|
|
|
|
2022-02-15 17:34:24 +08:00
|
|
|
Instrumentation inst = environment.getInstrumentation();
|
2021-12-06 10:49:40 +08:00
|
|
|
inst.addTransformer(dispatcher, true);
|
2022-01-14 19:53:31 +08:00
|
|
|
inst.setNativeMethodPrefix(dispatcher, environment.getNativePrefix());
|
2021-12-01 11:35:06 +08:00
|
|
|
|
2021-12-06 10:49:40 +08:00
|
|
|
Set<String> classSet = dispatcher.getHookClassNames();
|
2021-11-30 17:06:48 +08:00
|
|
|
for (Class<?> c : inst.getAllLoadedClasses()) {
|
2021-12-01 12:33:37 +08:00
|
|
|
String name = c.getName();
|
2021-12-06 10:49:40 +08:00
|
|
|
if (!classSet.contains(name)) {
|
2021-12-01 12:33:37 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-11-30 17:06:48 +08:00
|
|
|
try {
|
2022-05-20 22:51:23 +08:00
|
|
|
c.getGenericSuperclass();
|
2021-11-30 17:06:48 +08:00
|
|
|
inst.retransformClasses(c);
|
2021-12-01 12:33:37 +08:00
|
|
|
} catch (Throwable e) {
|
2022-01-20 14:55:03 +08:00
|
|
|
DebugInfo.error("Retransform class failed: " + name, e);
|
2021-11-30 17:06:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|