From a912eb5a1ad3ffb25ee801f0a745a28a18c45070 Mon Sep 17 00:00:00 2001 From: knaccc Date: Mon, 14 Jan 2019 18:01:40 +0000 Subject: [PATCH] First commit --- .gitignore | 4 ++ README | 41 +++++++++++++++ bin/build-launcher.sh | 15 ++++++ bin/convert-jars-to-modules.sh | 21 ++++++++ src/module-info.java | 7 +++ src/org/getmonero/i2p/embedded/Main.java | 64 ++++++++++++++++++++++++ 6 files changed, 152 insertions(+) create mode 100644 .gitignore create mode 100644 README create mode 100755 bin/build-launcher.sh create mode 100755 bin/convert-jars-to-modules.sh create mode 100644 src/module-info.java create mode 100644 src/org/getmonero/i2p/embedded/Main.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0a06796 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +out/ +target/ +.idea/ +*.swp \ No newline at end of file diff --git a/README b/README new file mode 100644 index 0000000..cb83a28 --- /dev/null +++ b/README @@ -0,0 +1,41 @@ +# Embedded I2P Java Router with SAM interface + +This project will build a native launcher. The launcher will include the I2P router, a SAM listener and a minimal JVM. + +## Prerequisites + +This project requires the JAR files and base configuration dir from an existing I2P installation. + +To install I2P on Ubuntu so that these files are available to you, type: + +``` +sudo apt-add-repository ppa:i2p-maintainers/i2p +sudo apt-get update +sudo apt-get install i2p +``` + +Then copy the following 4 JAR files from the I2P installation to the import/lib directory in your clone of this GitHub project: + +`for i in i2p.jar mstreaming.jar router.jar sam.jar streaming.jar; do cp /usr/share/i2p/lib/$i import/lib/; done` + +You will need OpenJDK 11 installed: + +`sudo apt install openjdk-11-jdk-headless` + +## Building the launcher + +`bin/build-launcher.sh` + +This will convert the imported JARs to modules, compile the Java source code in this project, and then use the jlink tool +to build a platform-specific launcher executable. + +## Running the launcher + +`target/router/bin/router` + +## Check that the I2P router is running and that it is listening for SAM connections + +`fuser 7656/tcp` + +## Todo +Need to determine the most minimal base configuration directory contents, and include it as part of this project. diff --git a/bin/build-launcher.sh b/bin/build-launcher.sh new file mode 100755 index 0000000..d4ebd04 --- /dev/null +++ b/bin/build-launcher.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +basedir=`dirname $0`/.. + +# convert the jar files from an existing I2P built into modules suitable for use with jlink +basedir/bin/convert-jars-to-modules.sh + +# compile the Main class that starts the I2P router and SAM listener +javac --module-path lib -d target/classes $(find src -name '*.java') + +# package as a modular jar +jar --create --file target/org.getmonero.i2p.embedded.jar --main-class org.getmonero.i2p.embedded.Main -C target/classes . + +# create an OS specific launcher which will bundle together the code and a minimal JVM +jlink --module-path target/modules:target/org.getmonero.i2p.embedded.jar --add-modules org.getmonero.i2p.embedded --launcher router=org.getmonero.i2p.embedded --output target/router --strip-debug --compress 2 --no-header-files --no-man-pages diff --git a/bin/convert-jars-to-modules.sh b/bin/convert-jars-to-modules.sh new file mode 100755 index 0000000..f09601d --- /dev/null +++ b/bin/convert-jars-to-modules.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +basedir=`dirname $0`/.. + +jarPaths=`find $basedir/import/lib -name '*.jar'` + +mkdir -p $basedir/target/modules +rm -f $basedir/target/modules/* + +for jarPath in $jarPaths; do + moduleName=$(basename "${jarPath%.*}") + echo "*** Determining dependencies for " $moduleName + jdeps --module-path $basedir/import/lib --add-modules=ALL-MODULE-PATH --generate-module-info $basedir/target/module-info $jarPath +done +for jarPath in $jarPaths; do + moduleName=$(basename "${jarPath%.*}") + echo "*** Creating new modular jar for " $moduleName + javac --module-path $basedir/import/lib --patch-module $moduleName=$jarPath $basedir/target/module-info/$moduleName/module-info.java + cp $jarPath $basedir/target/modules/ + jar uf $basedir/target/modules/${moduleName}.jar -C $basedir/target/module-info/$moduleName module-info.class +done diff --git a/src/module-info.java b/src/module-info.java new file mode 100644 index 0000000..1ffde98 --- /dev/null +++ b/src/module-info.java @@ -0,0 +1,7 @@ +module org.getmonero.i2p.embedded { + requires i2p; + requires mstreaming; + requires streaming; + requires router; + requires sam; +} \ No newline at end of file diff --git a/src/org/getmonero/i2p/embedded/Main.java b/src/org/getmonero/i2p/embedded/Main.java new file mode 100644 index 0000000..592b22b --- /dev/null +++ b/src/org/getmonero/i2p/embedded/Main.java @@ -0,0 +1,64 @@ +package org.getmonero.i2p.embedded; + +import java.io.File; +import java.util.Properties; + +import net.i2p.I2PAppContext; +import net.i2p.app.ClientAppManager; +import net.i2p.app.ClientAppManagerImpl; +import net.i2p.router.Router; +import net.i2p.sam.SAMBridge; + +public class Main { + + public static void main(String[] args) { + + Properties p = new Properties(); + // add your configuration settings, directories, etc. + // where to find the I2P installation files + p.put("i2p.dir.base", "/usr/share/i2p"); + // where to find the I2P data files + p.put("i2p.dir.config", System.getProperty("user.home") + File.separator + ".i2p"); + // bandwidth limits in K bytes per second + p.put("i2np.inboundKBytesPerSecond","50"); + p.put("i2np.outboundKBytesPerSecond","50"); + p.put("router.sharePercentage","80"); + + Router r = new Router(p); + + new Thread() { + @Override + public void run() { + // don't call exit() when the router stops + r.setKillVMOnEnd(false); + r.runRouter(); + } + }.start(); + + new Thread() { + @Override + public void run() { + try { + String[] args = new String[]{"sam.keys", "127.0.0.1", "7656", "i2cp.tcp.host=127.0.0.1", "i2cp.tcp.port=7654"}; + I2PAppContext context = r.getContext(); + ClientAppManager mgr = new ClientAppManagerImpl(context); + SAMBridge samBridge = new SAMBridge(context, mgr, args); + samBridge.startup(); + } + catch (Exception e) { + e.printStackTrace(); + } + } + }.start(); + + Runtime.getRuntime().addShutdownHook(new Thread() { + @Override + public void run() { + System.out.println("I2P router will shut down gracefully"); + r.shutdownGracefully(); + } + }); + + } + +} \ No newline at end of file