Skip to content

Commit

Permalink
Add IActionProvider interface
Browse files Browse the repository at this point in the history
  • Loading branch information
qtc-de committed Jan 12, 2024
1 parent 84e16a1 commit e93fd81
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/eu/tneitzel/rmg/Starter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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();

Expand Down
1 change: 0 additions & 1 deletion src/eu/tneitzel/rmg/internal/ArgumentHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ private void initialize()
}

checkPortRange();
PluginSystem.init(RMGOption.GLOBAL_PLUGIN.getValue());
}

/**
Expand Down
7 changes: 7 additions & 0 deletions src/eu/tneitzel/rmg/operations/Operation.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions src/eu/tneitzel/rmg/plugin/IActionProvider.java
Original file line number Diff line number Diff line change
@@ -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);
}
25 changes: 25 additions & 0 deletions src/eu/tneitzel/rmg/plugin/PluginSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -46,6 +48,7 @@ public class PluginSystem
public static void init(String pluginPath)
{
DefaultProvider provider = new DefaultProvider();

payloadProvider = provider;
argumentProvider = provider;
socketFactoryProvider = provider;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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[] {};
}
}
20 changes: 20 additions & 0 deletions src/eu/tneitzel/rmg/utils/RMGUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit e93fd81

Please sign in to comment.