From e93fd81215b10c0c9cb00a287ab85ae78803fc5e Mon Sep 17 00:00:00 2001 From: Tobias Neitzel Date: Fri, 12 Jan 2024 10:02:42 +0100 Subject: [PATCH] Add IActionProvider interface --- src/eu/tneitzel/rmg/Starter.java | 4 +++ .../rmg/internal/ArgumentHandler.java | 1 - src/eu/tneitzel/rmg/operations/Operation.java | 7 ++++++ .../tneitzel/rmg/plugin/IActionProvider.java | 18 +++++++++++++ src/eu/tneitzel/rmg/plugin/PluginSystem.java | 25 +++++++++++++++++++ src/eu/tneitzel/rmg/utils/RMGUtils.java | 20 +++++++++++++++ 6 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 src/eu/tneitzel/rmg/plugin/IActionProvider.java diff --git a/src/eu/tneitzel/rmg/Starter.java b/src/eu/tneitzel/rmg/Starter.java index a8b17f7..7279dd0 100644 --- a/src/eu/tneitzel/rmg/Starter.java +++ b/src/eu/tneitzel/rmg/Starter.java @@ -3,6 +3,7 @@ import eu.tneitzel.rmg.internal.ArgumentHandler; import eu.tneitzel.rmg.operations.Dispatcher; import eu.tneitzel.rmg.operations.Operation; +import eu.tneitzel.rmg.plugin.PluginSystem; import eu.tneitzel.rmg.utils.RMGUtils; /** @@ -20,6 +21,9 @@ public class Starter */ public static void main(String[] argv) { + String pluginPath = RMGUtils.getOption("--plugin", argv); + PluginSystem.init(pluginPath); + ArgumentHandler handler = new ArgumentHandler(argv); Operation operation = handler.getAction(); diff --git a/src/eu/tneitzel/rmg/internal/ArgumentHandler.java b/src/eu/tneitzel/rmg/internal/ArgumentHandler.java index ff366d7..0be3d4d 100644 --- a/src/eu/tneitzel/rmg/internal/ArgumentHandler.java +++ b/src/eu/tneitzel/rmg/internal/ArgumentHandler.java @@ -126,7 +126,6 @@ private void initialize() } checkPortRange(); - PluginSystem.init(RMGOption.GLOBAL_PLUGIN.getValue()); } /** diff --git a/src/eu/tneitzel/rmg/operations/Operation.java b/src/eu/tneitzel/rmg/operations/Operation.java index 79949fe..2d51041 100644 --- a/src/eu/tneitzel/rmg/operations/Operation.java +++ b/src/eu/tneitzel/rmg/operations/Operation.java @@ -9,6 +9,7 @@ import eu.tneitzel.argparse4j.inf.Subparsers; import eu.tneitzel.rmg.internal.ExceptionHandler; import eu.tneitzel.rmg.internal.RMGOption; +import eu.tneitzel.rmg.plugin.PluginSystem; /** * The Operation enum class contains one item for each possible rmg action. An enum item consists out of @@ -435,6 +436,12 @@ public static void addSubparsers(Subparsers argumentParser) Subparser parser = argumentParser.addParser(operation.name().toLowerCase()).help(operation.description); GlobalOption.addOptions(parser, operation); } + + for (IAction action : PluginSystem.getPluginActions()) + { + Subparser parser = argumentParser.addParser(action.getName().toLowerCase()).help(action.getDescription()); + GlobalOption.addOptions(parser, action); + } } @Override diff --git a/src/eu/tneitzel/rmg/plugin/IActionProvider.java b/src/eu/tneitzel/rmg/plugin/IActionProvider.java new file mode 100644 index 0000000..6d48084 --- /dev/null +++ b/src/eu/tneitzel/rmg/plugin/IActionProvider.java @@ -0,0 +1,18 @@ +package eu.tneitzel.rmg.plugin; + +import eu.tneitzel.argparse4j.global.IAction; + +/** + * The IActionProvider interface can be implemented by plugins to add custom actions to + * remote-method-guesser. All actions provided by the getActions method will be added to + * the command line. If the user decides to invoke such an action, the dispatch action is + * called with the selected action as argument. + * + * @author Tobias Neitzel (@qtc_de) + */ + +public interface IActionProvider +{ + IAction[] getActions(); + void dispatch(IAction action); +} diff --git a/src/eu/tneitzel/rmg/plugin/PluginSystem.java b/src/eu/tneitzel/rmg/plugin/PluginSystem.java index 138672a..10fe42b 100644 --- a/src/eu/tneitzel/rmg/plugin/PluginSystem.java +++ b/src/eu/tneitzel/rmg/plugin/PluginSystem.java @@ -9,6 +9,7 @@ import java.util.jar.JarInputStream; import java.util.jar.Manifest; +import eu.tneitzel.argparse4j.global.IAction; import eu.tneitzel.rmg.exceptions.MalformedPluginException; import eu.tneitzel.rmg.internal.ExceptionHandler; import eu.tneitzel.rmg.internal.RMGOption; @@ -31,6 +32,7 @@ public class PluginSystem { private static String manifestAttribute = "RmgPluginClass"; + private static IActionProvider actionProvider = null; private static IPayloadProvider payloadProvider = null; private static IResponseHandler responseHandler = null; private static IArgumentProvider argumentProvider = null; @@ -46,6 +48,7 @@ public class PluginSystem public static void init(String pluginPath) { DefaultProvider provider = new DefaultProvider(); + payloadProvider = provider; argumentProvider = provider; socketFactoryProvider = provider; @@ -122,6 +125,12 @@ private static void loadPlugin(String pluginPath) RMGUtils.exit(); } + if (pluginInstance instanceof IActionProvider) + { + actionProvider = (IActionProvider)pluginInstance; + inUse = true; + } + if (pluginInstance instanceof IPayloadProvider) { payloadProvider = (IPayloadProvider)pluginInstance; @@ -287,4 +296,20 @@ public static void setResponeHandler(IResponseHandler handler) { responseHandler = handler; } + + /** + * Return actions added by a user defined plugin. If no plugin was specified, + * an empty array of actions is returned. + * + * @return array of additional actions + */ + public static IAction[] getPluginActions() + { + if (actionProvider != null) + { + return actionProvider.getActions(); + } + + return new IAction[] {}; + } } diff --git a/src/eu/tneitzel/rmg/utils/RMGUtils.java b/src/eu/tneitzel/rmg/utils/RMGUtils.java index 807ed1f..61abcad 100644 --- a/src/eu/tneitzel/rmg/utils/RMGUtils.java +++ b/src/eu/tneitzel/rmg/utils/RMGUtils.java @@ -1335,4 +1335,24 @@ else if (type.isArray()) return Class.forName(type.getName()); } } + + /** + * Primitive argument parser for finding a single string value option on the command line. + * + * @param opt option name to find + * @param args command line + * @return the value of the specified option + */ + public static String getOption(String opt, String[] args) + { + for (int ctr = 0; ctr < args.length - 1; ctr++) + { + if (args[ctr].equals(opt)) + { + return args[ctr + 1]; + } + } + + return null; + } }