From 18f21dc247bbdf47f918513fb163b6b096ac00a4 Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Mon, 3 Jun 2019 21:47:31 +0100 Subject: [PATCH] update server --- settings.gradle | 1 + update-server/build.gradle | 2 + .../com/muwire/update/UpdateServer.groovy | 102 ++++++++++++++++++ 3 files changed, 105 insertions(+) create mode 100644 update-server/build.gradle create mode 100644 update-server/src/main/groovy/com/muwire/update/UpdateServer.groovy diff --git a/settings.gradle b/settings.gradle index 7e4e2a59..0e85015b 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,4 +1,5 @@ include 'pinger' include 'host-cache' +include 'update-server' include 'core' include 'gui' diff --git a/update-server/build.gradle b/update-server/build.gradle new file mode 100644 index 00000000..13ec9d5a --- /dev/null +++ b/update-server/build.gradle @@ -0,0 +1,2 @@ +apply plugin : 'application' +mainClassName = 'com.muwire.update.UpdateServer' diff --git a/update-server/src/main/groovy/com/muwire/update/UpdateServer.groovy b/update-server/src/main/groovy/com/muwire/update/UpdateServer.groovy new file mode 100644 index 00000000..35f0b430 --- /dev/null +++ b/update-server/src/main/groovy/com/muwire/update/UpdateServer.groovy @@ -0,0 +1,102 @@ +package com.muwire.update + +import java.util.logging.Level + +import groovy.util.logging.Log +import net.i2p.client.I2PClientFactory +import net.i2p.client.I2PSession +import net.i2p.client.I2PSessionMuxedListener +import net.i2p.client.datagram.I2PDatagramDissector +import net.i2p.client.datagram.I2PDatagramMaker + + +@Log +class UpdateServer { + public static void main(String[] args) { + def home = System.getProperty("user.hom") + "/.MuWireUpdateServer" + home = new File(home) + if (!home.exists()) + home.mkdirs() + + def keyFile = new File(home, "key.dat") + + def i2pClientFactory = new I2PClientFactory() + def i2pClient = i2pClientFactory.createClient() + + def myDest + def session + if (!keyFile.exists()) { + def os = new FileOutputStream(keyFile); + myDest = i2pClient.createDestination(os) + os.close() + log.info "No key.dat file was found, so creating a new destination." + log.info "This is the destination you want to give out for your new UpdateServer" + log.info myDest.toBase64() + } + + def update = new File(home, "update.json") + if (!update.exists()) { + log.warning("update file doesn't exist, exiting") + System.exit(1) + } + + def props = System.getProperties().clone() + props.putAt("inbound.nickname", "MuWire UpdateServer") + session = i2pClient.createSession(new FileInputStream(keyFile), props) + myDest = session.getMyDestination() + + session.addMuxedSessionListener(new Listener(update), I2PSession.PROTO_DATAGRAM, I2PSession.PORT_ANY) + session.connect() + log.info("Connected, going to sleep") + Thread.sleep(Integer.MAX_VALUE) + + } + + static class Listener implements I2PSessionMuxedListener { + + private final File json + + Listener(File json) { + this.json = json + } + + @Override + public void messageAvailable(I2PSession session, int msgId, long size) { + } + + @Override + public void messageAvailable(I2PSession session, int msgId, long size, int proto, int fromport, int toport) { + if (proto != I2PSession.PROTO_DATAGRAM) { + log.warning("received uknown protocol $proto") + return + } + + def payload = session.receiveMessage(msgId) + def dissector = new I2PDatagramDissector() + try { + dissector.loadI2PDatagram(payload) + def sender = dissector.getSender() + // I don't think we care about the payload at this point + def maker = new I2PDatagramMaker(session) + } catch (Exception e) { + log.log(Level.WARNING, "exception responding to update request",e) + } + } + + @Override + public void reportAbuse(I2PSession session, int severity) { + } + + @Override + public void disconnected(I2PSession session) { + Log.severe("Disconnected from I2P router") + System.exit(1) + } + + @Override + public void errorOccurred(I2PSession session, String message, Throwable error) { + log.log(Level.SEVERE, message, error) + } + + } +}