Skip to content

Commit

Permalink
Add custom socket factory obj to ExampleServer
Browse files Browse the repository at this point in the history
A new RemoteObject was added to port 9010 of the ExampleServer. This
remote object is registred with a custom RMISocketFactory to allow tests
of the dynamic SockerFactory creation feature of remote-method-guesser.
  • Loading branch information
qtc-de committed Nov 12, 2023
1 parent e30f52c commit 298f003
Show file tree
Hide file tree
Showing 12 changed files with 218 additions and 77 deletions.
19 changes: 19 additions & 0 deletions docker/example-server/resources/server/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).


## v4.1.0 - Nov 12, 2023

### Added

* Add an additional RemoteObject using a custom socket factory class


## v4.0.0 and before

Changelog entries can be found within the global [CHANGELOG.md](/CHANGELOG.md) file
of remote-method-guesser.
7 changes: 6 additions & 1 deletion docker/example-server/resources/server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>de.qtc.rmg.server.ExampleServer</groupId>
<artifactId>rmg-example-server</artifactId>
<version>3.3.0</version>
<version>4.1.0</version>
<name>rmg-example-server</name>
<description>RMG Example Server</description>

Expand Down Expand Up @@ -49,6 +49,11 @@
<manifest>
<mainClass>de.qtc.rmg.server.ExampleServer</mainClass>
</manifest>
<manifestEntries>
<Built-By>
Tobias Neitzel (@qtc_de)
</Built-By>
</manifestEntries>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import javax.rmi.ssl.SslRMIServerSocketFactory;

import de.qtc.rmg.server.activation.ActivationServer;
import de.qtc.rmg.server.factory.CustomSocketFactoryServer;
import de.qtc.rmg.server.interfaces.IPlainServer;
import de.qtc.rmg.server.interfaces.ISecureServer;
import de.qtc.rmg.server.interfaces.ISslServer;
Expand All @@ -22,28 +23,40 @@
import de.qtc.rmg.server.utils.Logger;
import de.qtc.rmg.server.utils.Utils;

public class ExampleServer {
public class ExampleServer
{
private static final int registryPort = 1090;
private static final int activatorPort = 1098;
private static final int plainRegistryPort = 9010;

private static int registryPort = 1090;
private static Remote remoteObject1 = null;
private static Remote remoteObject2 = null;
private static Remote remoteObject3 = null;
private static Remote remoteObjectOne;
private static Remote remoteObjectTwo;
private static Remote remoteObjectThree;

private static final String boundNameOne = "plain-server";
private static final String boundNameTwo = "ssl-server";
private static final String boundNameThree = "secure-server";

public static void main(String[] argv)
{
String disableColor = System.getProperty("de.qtc.rmg.server.disableColor");

if (disableColor != null && disableColor.equalsIgnoreCase("true"))
{
Logger.disableColor();
}

Logger.println("Initializing Java RMI Server:");
Logger.println("");
Logger.increaseIndent();

if (System.getSecurityManager() == null) {
if (System.getSecurityManager() == null)
{
System.setSecurityManager(new SecurityManager());
}

try {
try
{
SslRMIClientSocketFactory csf = new SslRMIClientSocketFactory();
SslRMIServerSocketFactory ssf = new SslRMIServerSocketFactory();

Expand All @@ -53,30 +66,47 @@ public static void main(String[] argv)
Logger.println("");

Logger.printlnMixedBlue("Creating", "PlainServer", "object.");
remoteObject1 = new PlainServer();
IPlainServer stub = (IPlainServer)UnicastRemoteObject.exportObject(remoteObject1, 0);
Utils.bindToRegistry(stub, registry, "plain-server");
remoteObjectOne = new PlainServer();
IPlainServer stub = (IPlainServer)UnicastRemoteObject.exportObject(remoteObjectOne, 0);
Utils.bindToRegistry(stub, registry, boundNameOne);

Logger.printlnMixedBlue("Creating", "SSLServer", "object.");
remoteObject2 = new SslServer();
ISslServer stub2 = (ISslServer)UnicastRemoteObject.exportObject(remoteObject2, 0, csf, ssf);
Utils.bindToRegistry(stub2, registry, "ssl-server");
remoteObjectTwo = new SslServer();
ISslServer stub2 = (ISslServer)UnicastRemoteObject.exportObject(remoteObjectTwo, 0, csf, ssf);
Utils.bindToRegistry(stub2, registry, boundNameTwo);

Logger.printlnMixedBlue("Creating", "SecureServer", "object.");
remoteObject3 = new SecureServer();
ISecureServer stub3 = (ISecureServer)UnicastRemoteObject.exportObject(remoteObject3, 0);
Utils.bindToRegistry(stub3, registry, "secure-server");
remoteObjectThree = new SecureServer();
ISecureServer stub3 = (ISecureServer)UnicastRemoteObject.exportObject(remoteObjectThree, 0);
Utils.bindToRegistry(stub3, registry, boundNameThree);

Logger.decreaseIndent();
Logger.println("");
Logger.println("Server setup finished.");
Logger.println("Initializing legacy server.");
Logger.println("Initializing LegacyServer.");
Logger.println("");

LegacyServer.init(plainRegistryPort);

Logger.println("LegacyServer setup finished.");
Logger.println("Initializing ActivationServer.");
Logger.println("");

LegacyServer.init();
ActivationServer.init();
ActivationServer.init(activatorPort);

Logger.println("ActivationServer setup finished.");
Logger.println("Initializing CustomSocketFactoryServer.");
Logger.println("");

CustomSocketFactoryServer.startServer(plainRegistryPort);

Logger.println("Setup finished.");
Logger.println("Waiting for incoming connections.");
Logger.println("");
}

} catch (RemoteException | AlreadyBoundException | NotBoundException e) {
catch (RemoteException | AlreadyBoundException | NotBoundException e)
{
Logger.eprintln("Unexpected RMI Error:");
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,13 @@
@SuppressWarnings("unused")
public class ActivationServer
{
private static int activationSystemPort = 1098;
private static Remote remoteObject1 = null;
private static Remote remoteObject2 = null;
private static Remote remoteObject3 = null;
private static Remote remoteObjectOne;
private static Remote remoteObjectTwo;
private static Remote remoteObjectThree;

private static final String boundNameOne = "activation-test";
private static final String boundNameTwo = "activation-test2";
private static final String boundNameThree = "plain-server";

private final static String codebase = "file:///opt/example-server.jar";

Expand All @@ -42,11 +45,12 @@ public class ActivationServer
* group is created and two activatable RMI services are bound to the registry. Additionally, we bind one
* non activatable service.
*/
public static void init()
public static void init(int activationSystemPort)
{
Logger.increaseIndent();

try {
try
{
Logger.printMixedBlue("Creating", "ActivationSystem", "on port ");
Logger.printlnPlainYellow(String.valueOf(activationSystemPort));
Utils.startActivation(activationSystemPort, null, "/tmp/activation-log", null);
Expand Down Expand Up @@ -90,28 +94,25 @@ public static void init()
Utils.toogleOutput();

ActivationDesc desc = new ActivationDesc(groupID, ActivationService.class.getName(), codebase, null);
remoteObject1 = Activatable.register(desc);
remoteObjectOne = Activatable.register(desc);

ActivationDesc desc2 = new ActivationDesc(groupID, ActivationService2.class.getName(), codebase, null);
remoteObject2 = Activatable.register(desc2);

remoteObject3 = new PlainServer();
IPlainServer stub = (IPlainServer)UnicastRemoteObject.exportObject(remoteObject3, 0);

Utils.bindToRegistry(remoteObject1, LocateRegistry.getRegistry(activationSystemPort), "activation-test");
Utils.bindToRegistry(remoteObject2, LocateRegistry.getRegistry(activationSystemPort), "activation-test2");
Utils.bindToRegistry(stub, LocateRegistry.getRegistry(activationSystemPort), "plain-server");
remoteObjectTwo = Activatable.register(desc2);

Logger.println("");
Logger.decreaseIndent();
remoteObjectThree = new PlainServer();
IPlainServer stub = (IPlainServer)UnicastRemoteObject.exportObject(remoteObjectThree, 0);

Logger.println("Server setup finished.");
Logger.println("Waiting for incoming connections.");
Logger.println("");
Utils.bindToRegistry(remoteObjectOne, LocateRegistry.getRegistry(activationSystemPort), boundNameOne);
Utils.bindToRegistry(remoteObjectTwo, LocateRegistry.getRegistry(activationSystemPort), boundNameTwo);
Utils.bindToRegistry(stub, LocateRegistry.getRegistry(activationSystemPort), boundNameThree);
}

} catch (Exception e) {
catch (Exception e)
{
Logger.eprintln("Unexpected RMI Error:");
e.printStackTrace();
}
}

Logger.println("");
Logger.decreaseIndent(); }
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,4 @@ public String system(String command, String[] args)

return result;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,4 @@ public void updatePreferences(ArrayList<String> preferences) throws RemoteExcept
Logger.printlnMixedBlueYellow("[SecureServer]:", "Processing call for", "void updatePreferences(ArrayList<String> preferences)");
this.preferences = preferences;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ public interface IActivationService extends Remote
{
String execute(String cmd) throws RemoteException;
String system(String cmd, String[] args) throws RemoteException;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ public interface IActivationService2 extends Remote
String login(HashMap<String, String> credentials) throws RemoteException;
void logMessage(int logLevel, Object message) throws RemoteException;
void updatePreferences(ArrayList<String> preferences) throws RemoteException;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package de.qtc.rmg.server.factory;

import java.io.IOException;
import java.io.Serializable;
import java.net.ServerSocket;
import java.net.Socket;
import java.rmi.server.RMISocketFactory;

public class CustomSocketFactory extends RMISocketFactory implements Serializable
{
private static final long serialVersionUID = -1168901302380021730L;
private final transient RMISocketFactory defaultFax;

public CustomSocketFactory()
{
defaultFax = RMISocketFactory.getDefaultSocketFactory();
}

public ServerSocket createServerSocket(int arg0) throws IOException
{
return defaultFax.createServerSocket(arg0);
}

public Socket createSocket(String arg0, int arg1) throws IOException
{
return defaultFax.createSocket(arg0, arg1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package de.qtc.rmg.server.factory;

import java.rmi.AlreadyBoundException;
import java.rmi.NotBoundException;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.RMISocketFactory;
import java.rmi.server.UnicastRemoteObject;

import de.qtc.rmg.server.interfaces.IPlainServer;
import de.qtc.rmg.server.operations.PlainServer;
import de.qtc.rmg.server.utils.Logger;
import de.qtc.rmg.server.utils.Utils;

public class CustomSocketFactoryServer
{
private static Remote remoteObjectOne;;
private static final String boundName = "custom-socks";

public static void startServer(int registryPort)
{
Logger.increaseIndent();

try
{
RMISocketFactory csf = new CustomSocketFactory();

Logger.printMixedBlue("Locating", "RMI-Registry", "on port ");
Logger.printlnPlainYellow(String.valueOf(registryPort));
Registry registry = LocateRegistry.getRegistry(registryPort);
Logger.println("");

Logger.printlnMixedBlue("Creating", "PlainServer", "object.");
remoteObjectOne = new PlainServer();
IPlainServer stub = (IPlainServer)UnicastRemoteObject.exportObject(remoteObjectOne, 0, csf, null);
Utils.bindToRegistry(stub, registry, boundName);

Logger.println("Server setup finished.");
}

catch (RemoteException | AlreadyBoundException | NotBoundException e)
{
Logger.eprintln("Unexpected RMI Error:");
e.printStackTrace();
}

Logger.println("");
Logger.decreaseIndent();
}
}
Loading

0 comments on commit 298f003

Please sign in to comment.