diff --git a/beanshooter/src/eu/tneitzel/beanshooter/networking/LoopbackSocketFactory.java b/beanshooter/src/eu/tneitzel/beanshooter/networking/LoopbackSocketFactory.java index 7aa79eb..bb94605 100644 --- a/beanshooter/src/eu/tneitzel/beanshooter/networking/LoopbackSocketFactory.java +++ b/beanshooter/src/eu/tneitzel/beanshooter/networking/LoopbackSocketFactory.java @@ -32,9 +32,10 @@ * * @author Tobias Neitzel (@qtc_de) */ -public class LoopbackSocketFactory extends RMISocketFactory { - +public class LoopbackSocketFactory extends RMISocketFactory +{ private String host; + private int port; private RMISocketFactory fac; private boolean printInfo = true; private boolean followRedirect = false; @@ -43,12 +44,14 @@ public class LoopbackSocketFactory extends RMISocketFactory { * Creates a new LoopbackSocketFactory. * * @param host remote host that is expected to get all further RMI connections + * @param port if not set to zero, redirect connections to the specified port * @param fac original socket factory to create sockets from * @param followRedirect if true, connections are not redirected to the expected host */ - public LoopbackSocketFactory(String host, RMISocketFactory fac, boolean followRedirect) + public LoopbackSocketFactory(String host, int port, RMISocketFactory fac, boolean followRedirect) { this.host = host; + this.port = port; this.fac = fac; this.followRedirect= followRedirect; } @@ -67,38 +70,54 @@ public Socket createSocket(String host, int port) throws IOException { Socket sock = null; - if(!this.host.equals(host)) { - - if( printInfo && BeanshooterOption.GLOBAL_VERBOSE.getBool() ) { + if (!this.host.equals(host)) + { + if (printInfo && BeanshooterOption.GLOBAL_VERBOSE.getBool()) + { Logger.printInfoBox(); Logger.printlnMixedBlue("RMI object tries to connect to different remote host:", host); } - if( this.followRedirect ) { - if( printInfo && BeanshooterOption.GLOBAL_VERBOSE.getBool() ) + if (this.followRedirect) + { + if (printInfo && BeanshooterOption.GLOBAL_VERBOSE.getBool()) + { Logger.println("Following redirect to new target..."); + } - } else { + } + else + { host = this.host; - if( printInfo && BeanshooterOption.GLOBAL_VERBOSE.getBool() ) { + if (printInfo && BeanshooterOption.GLOBAL_VERBOSE.getBool()) + { Logger.printlnMixedBlue("Redirecting the connection back to", host); Logger.printlnMixedYellow("You can use", "--follow", "to prevent this."); } } - if( printInfo && BeanshooterOption.GLOBAL_VERBOSE.getBool() ) { + if (printInfo && BeanshooterOption.GLOBAL_VERBOSE.getBool()) + { Logger.decreaseIndent(); } this.printInfo = false; } - try { + if (this.port > 0 && this.port != port) + { + port = this.port; + } + + try + { sock = fac.createSocket(host, port); + } - } catch( UnknownHostException e ) { + catch (UnknownHostException e) + { ExceptionHandler.unknownHost(e, host, true); } diff --git a/beanshooter/src/eu/tneitzel/beanshooter/networking/LoopbackSslSocketFactory.java b/beanshooter/src/eu/tneitzel/beanshooter/networking/LoopbackSslSocketFactory.java index 05b024c..c1fc72c 100644 --- a/beanshooter/src/eu/tneitzel/beanshooter/networking/LoopbackSslSocketFactory.java +++ b/beanshooter/src/eu/tneitzel/beanshooter/networking/LoopbackSslSocketFactory.java @@ -33,9 +33,10 @@ * * @author Tobias Neitzel (@qtc_de) */ -public class LoopbackSslSocketFactory extends SSLSocketFactory { - +public class LoopbackSslSocketFactory extends SSLSocketFactory +{ public static String host = ""; + public static int port = 0; public static SSLSocketFactory fac = null; public static boolean printInfo = true; public static boolean followRedirect = false; @@ -50,38 +51,54 @@ public Socket createSocket(String target, int port) throws IOException { Socket sock = null; - if(!host.equals(target)) { - - if( printInfo && BeanshooterOption.GLOBAL_VERBOSE.getBool() ) { + if (!host.equals(target)) + { + if (printInfo && BeanshooterOption.GLOBAL_VERBOSE.getBool()) + { Logger.printInfoBox(); Logger.printlnMixedBlue("RMI object tries to connect to different remote host:", target); } - if( followRedirect ) { - if( printInfo && BeanshooterOption.GLOBAL_VERBOSE.getBool() ) + if (followRedirect) + { + if (printInfo && BeanshooterOption.GLOBAL_VERBOSE.getBool()) + { Logger.println("Following SSL redirect to new target..."); + } - } else { + } + else + { target = host; - if( printInfo && BeanshooterOption.GLOBAL_VERBOSE.getBool() ) { + if (printInfo && BeanshooterOption.GLOBAL_VERBOSE.getBool()) + { Logger.printlnMixedBlue("Redirecting the SSL connection back to", host); Logger.printlnMixedYellow("You can use", "--follow", "to prevent this."); } } - if( printInfo && BeanshooterOption.GLOBAL_VERBOSE.getBool() ) { + if (printInfo && BeanshooterOption.GLOBAL_VERBOSE.getBool()) + { Logger.decreaseIndent(); } printInfo = false; } - try { + if (LoopbackSslSocketFactory.port > 0 && LoopbackSslSocketFactory.port != port) + { + port = LoopbackSslSocketFactory.port; + } + + try + { sock = fac.createSocket(host, port); + } - } catch( UnknownHostException e ) { + catch (UnknownHostException e) + { ExceptionHandler.unknownHost(e, host, true); } diff --git a/beanshooter/src/eu/tneitzel/beanshooter/networking/RMIRegistryEndpoint.java b/beanshooter/src/eu/tneitzel/beanshooter/networking/RMIRegistryEndpoint.java index 2aea1e3..61f1546 100644 --- a/beanshooter/src/eu/tneitzel/beanshooter/networking/RMIRegistryEndpoint.java +++ b/beanshooter/src/eu/tneitzel/beanshooter/networking/RMIRegistryEndpoint.java @@ -75,13 +75,18 @@ public RMIRegistryEndpoint(RMIEndpoint rmi) private synchronized static void SocketFactorySetup(String host, int port) { - if( setupComplete ) + if (setupComplete) + { return; + } - try { + try + { RMISocketFactory.setSocketFactory(PluginSystem.getDefaultRMISocketFactory(host, port)); + } - } catch (IOException e) { + catch (IOException e) + { Logger.eprintlnMixedBlue("Unable to set custom", "RMISocketFactory.", "Host redirection will probably not work."); ExceptionHandler.showStackTrace(e); Logger.eprintln(""); diff --git a/beanshooter/src/eu/tneitzel/beanshooter/operation/BeanshooterOperation.java b/beanshooter/src/eu/tneitzel/beanshooter/operation/BeanshooterOperation.java index d3969f0..3dd4e19 100644 --- a/beanshooter/src/eu/tneitzel/beanshooter/operation/BeanshooterOperation.java +++ b/beanshooter/src/eu/tneitzel/beanshooter/operation/BeanshooterOperation.java @@ -30,6 +30,8 @@ public enum BeanshooterOperation implements Operation { BeanshooterOption.TARGET_BOUND_NAME, BeanshooterOption.TARGET_OBJID_SERVER, BeanshooterOption.TARGET_OBJID_CONNECTION, + BeanshooterOption.TARGET_OVERWRITE_HOST, + BeanshooterOption.TARGET_OVERWRITE_PORT, BeanshooterOption.CONN_FOLLOW, BeanshooterOption.CONN_SSL, BeanshooterOption.CONN_JMXMP, @@ -58,6 +60,8 @@ public enum BeanshooterOperation implements Operation { BeanshooterOption.TARGET_PORT, BeanshooterOption.TARGET_BOUND_NAME, BeanshooterOption.TARGET_OBJID_SERVER, + BeanshooterOption.TARGET_OVERWRITE_HOST, + BeanshooterOption.TARGET_OVERWRITE_PORT, BeanshooterOption.CONN_FOLLOW, BeanshooterOption.CONN_SSL, BeanshooterOption.CONN_JMXMP, @@ -89,6 +93,8 @@ public enum BeanshooterOperation implements Operation { BeanshooterOption.TARGET_BOUND_NAME, BeanshooterOption.TARGET_OBJID_SERVER, BeanshooterOption.TARGET_OBJID_CONNECTION, + BeanshooterOption.TARGET_OVERWRITE_HOST, + BeanshooterOption.TARGET_OVERWRITE_PORT, BeanshooterOption.CONN_FOLLOW, BeanshooterOption.CONN_SSL, BeanshooterOption.CONN_JMXMP, @@ -121,6 +127,8 @@ public enum BeanshooterOperation implements Operation { BeanshooterOption.TARGET_BOUND_NAME, BeanshooterOption.TARGET_OBJID_SERVER, BeanshooterOption.TARGET_OBJID_CONNECTION, + BeanshooterOption.TARGET_OVERWRITE_HOST, + BeanshooterOption.TARGET_OVERWRITE_PORT, BeanshooterOption.CONN_JMXMP, BeanshooterOption.CONN_JOLOKIA, BeanshooterOption.CONN_JOLOKIA_ENDPOINT, @@ -146,6 +154,8 @@ public enum BeanshooterOperation implements Operation { BeanshooterOption.TARGET_BOUND_NAME, BeanshooterOption.TARGET_OBJID_SERVER, BeanshooterOption.TARGET_OBJID_CONNECTION, + BeanshooterOption.TARGET_OVERWRITE_HOST, + BeanshooterOption.TARGET_OVERWRITE_PORT, BeanshooterOption.CONN_JMXMP, BeanshooterOption.CONN_JOLOKIA, BeanshooterOption.CONN_JOLOKIA_ENDPOINT, @@ -176,6 +186,8 @@ public enum BeanshooterOperation implements Operation { BeanshooterOption.TARGET_BOUND_NAME, BeanshooterOption.TARGET_OBJID_SERVER, BeanshooterOption.TARGET_OBJID_CONNECTION, + BeanshooterOption.TARGET_OVERWRITE_HOST, + BeanshooterOption.TARGET_OVERWRITE_PORT, BeanshooterOption.CONN_FOLLOW, BeanshooterOption.CONN_SSL, BeanshooterOption.CONN_JMXMP, @@ -225,6 +237,8 @@ public enum BeanshooterOperation implements Operation { BeanshooterOption.TARGET_BOUND_NAME, BeanshooterOption.TARGET_OBJID_SERVER, BeanshooterOption.TARGET_OBJID_CONNECTION, + BeanshooterOption.TARGET_OVERWRITE_HOST, + BeanshooterOption.TARGET_OVERWRITE_PORT, BeanshooterOption.CONN_JMXMP, BeanshooterOption.CONN_JOLOKIA, BeanshooterOption.CONN_JOLOKIA_ENDPOINT, @@ -252,6 +266,8 @@ public enum BeanshooterOperation implements Operation { BeanshooterOption.TARGET_BOUND_NAME, BeanshooterOption.TARGET_OBJID_SERVER, BeanshooterOption.TARGET_OBJID_CONNECTION, + BeanshooterOption.TARGET_OVERWRITE_HOST, + BeanshooterOption.TARGET_OVERWRITE_PORT, BeanshooterOption.CONN_FOLLOW, BeanshooterOption.CONN_SSL, BeanshooterOption.CONN_JMXMP, @@ -283,6 +299,8 @@ public enum BeanshooterOperation implements Operation { BeanshooterOption.TARGET_BOUND_NAME, BeanshooterOption.TARGET_OBJID_SERVER, BeanshooterOption.TARGET_OBJID_CONNECTION, + BeanshooterOption.TARGET_OVERWRITE_HOST, + BeanshooterOption.TARGET_OVERWRITE_PORT, BeanshooterOption.CONN_FOLLOW, BeanshooterOption.CONN_SSL, BeanshooterOption.CONN_JMXMP, @@ -311,6 +329,8 @@ public enum BeanshooterOperation implements Operation { BeanshooterOption.TARGET_BOUND_NAME, BeanshooterOption.TARGET_OBJID_SERVER, BeanshooterOption.TARGET_OBJID_CONNECTION, + BeanshooterOption.TARGET_OVERWRITE_HOST, + BeanshooterOption.TARGET_OVERWRITE_PORT, BeanshooterOption.CONN_FOLLOW, BeanshooterOption.CONN_JOLOKIA, BeanshooterOption.CONN_JOLOKIA_ENDPOINT, @@ -356,6 +376,8 @@ public enum BeanshooterOperation implements Operation { BeanshooterOption.TARGET_BOUND_NAME, BeanshooterOption.TARGET_OBJID_SERVER, BeanshooterOption.TARGET_OBJID_CONNECTION, + BeanshooterOption.TARGET_OVERWRITE_HOST, + BeanshooterOption.TARGET_OVERWRITE_PORT, BeanshooterOption.CONN_FOLLOW, BeanshooterOption.CONN_JOLOKIA, BeanshooterOption.CONN_JOLOKIA_ENDPOINT, diff --git a/beanshooter/src/eu/tneitzel/beanshooter/operation/BeanshooterOption.java b/beanshooter/src/eu/tneitzel/beanshooter/operation/BeanshooterOption.java index f3d1e6e..afd8549 100644 --- a/beanshooter/src/eu/tneitzel/beanshooter/operation/BeanshooterOption.java +++ b/beanshooter/src/eu/tneitzel/beanshooter/operation/BeanshooterOption.java @@ -68,6 +68,20 @@ public enum BeanshooterOption implements Option { ArgType.INT, "port"), + TARGET_OVERWRITE_HOST("--overwrite-host", + "overwrite the host a boundname points to", + Arguments.store(), + OptionGroup.TARGET, + ArgType.STRING, + "host"), + + TARGET_OVERWRITE_PORT("--overwrite-port", + "overwrite the port a boundname points to", + Arguments.store(), + OptionGroup.TARGET, + ArgType.INT, + "port"), + TARGET_BOUND_NAME("--bound-name", "target bound name within an RMI registry", Arguments.store(), diff --git a/beanshooter/src/eu/tneitzel/beanshooter/operation/Dispatcher.java b/beanshooter/src/eu/tneitzel/beanshooter/operation/Dispatcher.java index 1911286..65764c1 100644 --- a/beanshooter/src/eu/tneitzel/beanshooter/operation/Dispatcher.java +++ b/beanshooter/src/eu/tneitzel/beanshooter/operation/Dispatcher.java @@ -265,7 +265,7 @@ else if (!enumerated) if (BeanshooterOption.CONN_JOLOKIA.getBool()) enumHelper.enumJolokiaVersion(); - else if (!BeanshooterOption.CONN_JNDI.getValue().contains("service:jmx:remote+")) + else if (!BeanshooterOption.CONN_JNDI.getValue("").contains("service:jmx:remote+")) enumHelper.enumSerial(); if (!access) diff --git a/beanshooter/src/eu/tneitzel/beanshooter/plugin/providers/SocketFactoryProvider.java b/beanshooter/src/eu/tneitzel/beanshooter/plugin/providers/SocketFactoryProvider.java index 8136f0c..262d2d5 100644 --- a/beanshooter/src/eu/tneitzel/beanshooter/plugin/providers/SocketFactoryProvider.java +++ b/beanshooter/src/eu/tneitzel/beanshooter/plugin/providers/SocketFactoryProvider.java @@ -46,24 +46,38 @@ public RMIClientSocketFactory getRMIClientSocketFactory(String host, int port) * are looked up from the RMI registry use the RMISocketFactory.getDefaultSocketFactory function to * obtain a SocketFactory. This factory is then used for explicit calls (method invocations) and for * implicit calls (DGC actions like clean or dirty). + * + * Since beanshooter v5.0.0, LoopbackSocketFactory also performs port redirection if the + * corresponding command line arguments have been specified. */ @Override public RMISocketFactory getDefaultRMISocketFactory(String host, int port) { RMISocketFactory fac = RMISocketFactory.getDefaultSocketFactory(); - return new LoopbackSocketFactory(host, fac, BeanshooterOption.CONN_FOLLOW.getBool()); + + String factoryHost = BeanshooterOption.TARGET_OVERWRITE_HOST.getValue(host); + int factoryPort = BeanshooterOption.TARGET_OVERWRITE_PORT.getValue(0); + + return new LoopbackSocketFactory(factoryHost, factoryPort, fac, BeanshooterOption.CONN_FOLLOW.getBool()); } /** * The default SSLRMISocketFactory used by beanshooter is the LoopbackSslSocketFactory, which * redirects all connection to the original target and thus prevents unwanted RMI redirections. + * + * Since beanshooter v5.0.0, LoopbackSslSocketFactory also performs port redirection if the + * corresponding command line arguments have been specified. */ @Override public String getDefaultSSLSocketFactoryClass(String host, int port) { TrustAllSocketFactory trustAllFax = new TrustAllSocketFactory(); - LoopbackSslSocketFactory.host = host; + String factoryHost = BeanshooterOption.TARGET_OVERWRITE_HOST.getValue(host); + int factoryPort = BeanshooterOption.TARGET_OVERWRITE_PORT.getValue(0); + + LoopbackSslSocketFactory.host = factoryHost; + LoopbackSslSocketFactory.port = factoryPort; LoopbackSslSocketFactory.fac = trustAllFax.getSSLSocketFactory(); LoopbackSslSocketFactory.followRedirect = BeanshooterOption.CONN_FOLLOW.getBool();