Skip to content

Commit

Permalink
Add spring remoting server
Browse files Browse the repository at this point in the history
Added a spring remoting server.
  • Loading branch information
qtc-de committed Oct 8, 2023
1 parent aa9637a commit 5bb6761
Show file tree
Hide file tree
Showing 6 changed files with 247 additions and 0 deletions.
35 changes: 35 additions & 0 deletions docker/spring-remoting/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
###########################################
### Build Stage 1 ###
###########################################
FROM maven:3.8.6-openjdk-8-slim AS maven-builder
COPY ./resources/server /usr/src/app
WORKDIR /usr/src/app
RUN mvn clean package

###########################################
### Build Stage 2 ###
###########################################
FROM alpine:latest AS jdk-builder
RUN set -ex \
&& apk add --no-cache openjdk11 \
&& /usr/lib/jvm/java-11-openjdk/bin/jlink --add-modules java.rmi,java.management.rmi,jdk.unsupported --verbose --strip-debug --compress 2 \
--no-header-files --no-man-pages --output /jdk

###########################################
### Container Stage ###
###########################################
FROM alpine:latest

COPY ./resources/scripts/start.sh /opt/start.sh
COPY --from=maven-builder /usr/src/app/target/rmg-spring-remoting-server-*-jar-with-dependencies.jar /opt/remoting-server.jar
COPY --from=jdk-builder /jdk /usr/lib/jvm/java-11-openjdk

RUN set -ex \
&& ln -s /usr/lib/jvm/java-11-openjdk/bin/java /usr/bin/java \
&& chmod +x /opt/start.sh

ENV _JAVA_OPTIONS -Djava.rmi.server.hostname=iinsecure.example

EXPOSE 1099/tcp

CMD ["/opt/start.sh"]
14 changes: 14 additions & 0 deletions docker/spring-remoting/resources/scripts/start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/ash

IP=$(ip a | grep inet | grep -v 127.0.0.1 | grep -o "\([0-9]\{1,3\}\.\?\)\{4\}" | head -n 1)
echo "[+] IP address of the container: ${IP}"

echo "[+] Adding gateway address to /etc/hosts file..."
GATEWAY=$(ip r | grep "default via" | cut -d" " -f 3)
echo "$GATEWAY prevent.reverse.dns" >> /etc/hosts

echo "[+] Adding RMI hostname to /etc/hosts file..."
echo "127.0.0.1 iinsecure.example" >> /etc/hosts

echo "[+] Starting rmi server..."
exec /usr/bin/java -jar /opt/remoting-server.jar
55 changes: 55 additions & 0 deletions docker/spring-remoting/resources/server/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.qtc.rmg</groupId>
<artifactId>spring-remoting</artifactId>
<version>1.0.0</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
<version>2.7.16</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<finalName>rmg-spring-remoting-server-${project.version}</finalName>
<archive>
<manifest>
<mainClass>de.qtc.rmg.springremoting.Starter</mainClass>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package de.qtc.rmg.springremoting;

public interface ServerOperations
{
String notRelevant();
String execute(String cmd);
String system(String cmd, String[] args);
String upload(int size, int id, byte[] content);
int math(int num1, int num2);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package de.qtc.rmg.springremoting;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.remoting.rmi.RmiServiceExporter;

@SuppressWarnings("deprecation")
public class SpringRemotingService implements ServerOperations
{
Logger logger = LoggerFactory.getLogger(SpringRemotingService.class);

SpringRemotingService springRemotingService()
{
return new SpringRemotingService();
}

@Bean
RmiServiceExporter exporter(SpringRemotingService impl)
{
Class<ServerOperations> serverInterface = ServerOperations.class;
RmiServiceExporter exporter = new RmiServiceExporter();

exporter.setServiceInterface(serverInterface);
exporter.setService(impl);
exporter.setServiceName("spring-remoting");
exporter.setRegistryPort(1099);

return exporter;
}

public String notRelevant()
{
logger.info("Method", "notRelevant", "was called.");
return "Hello World :D";
}

public String execute(String command)
{
logger.info("Processing call for", "String execute(String command)");
String result = "";

try
{
Process p = java.lang.Runtime.getRuntime().exec(command);
p.waitFor();
result = readFromProcess(p);
}

catch (IOException | InterruptedException e)
{
result = "Exception: " + e.getMessage();
}

return result;
}

public String system(String command, String[] args)
{
logger.info("Processing call for", "String system(String command, String[] args)");
String result = "";

String[] commandArray = new String[args.length + 1];
commandArray[0] = command;
System.arraycopy(args, 0, commandArray, 1, args.length);

try
{
Process p = java.lang.Runtime.getRuntime().exec(commandArray);
p.waitFor();
result = readFromProcess(p);
}

catch (IOException | InterruptedException e)
{
result = "Exception: " + e.getMessage();
}

return result;
}

public String upload(int size, int id, byte[] content)
{
logger.info("Processing call for", "String upload(int size, int id, byte[] content)");
return "Upload of size " + size + " was saved as user_uploads_" + id + ".";
}

public int math(int num1, int num2)
{
logger.info("Processing call for", "int math(int num1, int num2)");
return num1 / num2;
}

private static String readFromProcess(Process p) throws IOException
{
StringBuilder result = new StringBuilder();

BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = reader.readLine();

while (line != null)
{
result.append(line);
line = reader.readLine();
}

reader = new BufferedReader(new InputStreamReader(p.getErrorStream()));
line = reader.readLine();

while (line != null)
{
result.append(line);
line = reader.readLine();
}

return result.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package de.qtc.rmg.springremoting;

import org.springframework.boot.SpringApplication;

public class Starter
{
public static void main(String[] args)
{
SpringApplication.run(SpringRemotingService.class, args);
}
}

0 comments on commit 5bb6761

Please sign in to comment.