Skip to content
This repository has been archived by the owner on Oct 1, 2020. It is now read-only.

Commit

Permalink
Merge pull request #3 from trifel/signatures
Browse files Browse the repository at this point in the history
Signatures
  • Loading branch information
trifel authored Jan 2, 2019
2 parents b6a3066 + da3b407 commit 3f34c0f
Show file tree
Hide file tree
Showing 20 changed files with 645 additions and 288 deletions.
20 changes: 3 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,9 @@ reportPort=1338
// "ixuz (ict-0)" or "testIT (ict-0)".
name=
// Insert the IP or domain of your neighbor A.
neighborAHost=
// Insert the additional port of the Report.ixi application of your neighbor A (report port).
// If your neighbor didn't install Report.ixi, ignore this setting.
neighborAPort=1338
// Insert the IP or domain of your neighbor B.
neighborBHost=
// Insert the additional port of the Report.ixi application of your neighbor B (report port).
// If your neighbor didn't install Report.ixi, ignore this setting.
neighborBPort=1338
// Insert the IP or domain of your neighbor C.
neighborCHost=
// Insert the additional port of the Report.ixi application of your neighbor C (report port).
// If your neighbor didn't install Report.ixi, ignore this setting.
neighborCPort=1338
// Add host and port of your neighbors Report.ixi (comma separated, no whitespace).
// Please note that the port numbers should refer to your neighbor's Report.ixi operational port.
neighbors=10.10.10.10:1338,20.20.20.20:1338,public.address.to.neighbor:1338
// A unique identifier that has been generated upon initial start of
// the Report.ixi application. Don't change the uuid by yourself.
Expand Down
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group 'com.ictreport'
version '0.1'
version '0.2'

sourceCompatibility = 1.7
repositories {
Expand All @@ -15,6 +15,7 @@ repositories {
dependencies {
compile 'com.github.iotaledger:ict:master-SNAPSHOT'
compile 'com.google.code.gson:gson:2.8.5'
compile group: 'org.danilopianini', name: 'gson-extras', version: '0.2.1'
compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.11.1'
compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.11.1'
testCompile group: 'junit', name: 'junit', version: '4.12'
Expand Down
41 changes: 27 additions & 14 deletions src/main/java/com/ictreport/ixi/ReportIxi.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.ictreport.ixi;

import com.ictreport.ixi.api.Api;
import com.ictreport.ixi.exchange.*;
import com.ictreport.ixi.model.Neighbor;

import com.ictreport.ixi.utils.Constants;
import com.ictreport.ixi.utils.Cryptography;
import org.iota.ict.ixi.IxiModule;
import org.iota.ict.network.event.GossipFilter;
import org.iota.ict.network.event.GossipReceiveEvent;
Expand All @@ -12,22 +14,25 @@
import org.apache.logging.log4j.Logger;

import java.net.InetSocketAddress;
import java.security.KeyPair;
import java.security.NoSuchAlgorithmException;
import java.util.LinkedList;
import java.util.List;

import com.ictreport.ixi.utils.Properties;

public class ReportIxi extends IxiModule {
public static final String VERSION = "0.1";

public final static Logger LOGGER = LogManager.getLogger(ReportIxi.class);

private Properties properties;
private final List<Neighbor> neighbors = new LinkedList<>();
private static Api api = null;
private KeyPair keyPair = null;

public static void main(String[] args) {

LOGGER.info(String.format("Report.ixi %s started.", Constants.VERSION));

final String propertiesFilePath = (args.length == 0 ? "report.ixi.cfg" : args[0]);
final Properties properties = new Properties(propertiesFilePath);
properties.store(propertiesFilePath);
Expand All @@ -47,12 +52,15 @@ public ReportIxi(Properties properties) {

this.properties = properties;

InetSocketAddress neighborASocketAddress = new InetSocketAddress(properties.getNeighborAHost(), properties.getNeighborAPort());
InetSocketAddress neighborBSocketAddress = new InetSocketAddress(properties.getNeighborBHost(), properties.getNeighborBPort());
InetSocketAddress neighborCSocketAddress = new InetSocketAddress(properties.getNeighborCHost(), properties.getNeighborCPort());
neighbors.add(new Neighbor(neighborASocketAddress.getAddress(), properties.getNeighborAPort()));
neighbors.add(new Neighbor(neighborBSocketAddress.getAddress(), properties.getNeighborBPort()));
neighbors.add(new Neighbor(neighborCSocketAddress.getAddress(), properties.getNeighborCPort()));
try {
keyPair = Cryptography.generateKeyPair(1024);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}

for (InetSocketAddress neighborAddress : properties.getNeighborAddresses()) {
neighbors.add(new Neighbor(neighborAddress.getAddress(), neighborAddress.getPort()));
}

GossipFilter filter = new GossipFilter();
filter.watchTag("REPORT9IXI99999999999999999");
Expand All @@ -79,19 +87,24 @@ public List<Neighbor> getNeighbors() {
return this.neighbors;
}

public KeyPair getKeyPair() {
return keyPair;
}

public Api getApi() {
return api;
}

@Override
public void onTransactionReceived(GossipReceiveEvent event) {
LOGGER.info(String.format("message received '%s'",
event.getTransaction().tag ));
if (api != null) {
api.getSender().reportTransactionReceived(event.getTransaction().decodedSignatureFragments);
Payload payload = Payload.deserialize(event.getTransaction().decodedSignatureFragments);
api.getReceiver().processPayload(null, payload);
}
}

@Override
public void onTransactionSubmitted(GossipSubmitEvent event) {
LOGGER.info(String.format("message submitted '%s'",
event.getTransaction().tag ));

}

Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/ictreport/ixi/api/Api.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.ictreport.ixi.api;

import com.ictreport.ixi.ReportIxi;
import com.ictreport.ixi.utils.Properties;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down
117 changes: 82 additions & 35 deletions src/main/java/com/ictreport/ixi/api/Receiver.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package com.ictreport.ixi.api;

import com.ictreport.ixi.exchange.MetadataPayload;
import com.ictreport.ixi.exchange.Payload;
import com.ictreport.ixi.exchange.PingPayload;
import com.ictreport.ixi.exchange.ReceivedPingPayload;
import com.ictreport.ixi.exchange.SignedPayload;
import com.ictreport.ixi.utils.Constants;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.util.ArrayList;
import java.util.List;

import com.ictreport.ixi.ReportIxi;
import com.ictreport.ixi.model.Neighbor;
Expand All @@ -19,29 +23,10 @@ public class Receiver extends Thread {
private final DatagramSocket socket;
private boolean isReceiving = false;

private List<IIncomingPacketListener> onIncomingPacketListeners = new ArrayList<>();

public Receiver(ReportIxi reportIxi, DatagramSocket socket) {
super("Receiver");
this.reportIxi = reportIxi;
this.socket = socket;

// Default onIncomingPacketListener
addOnIncomingPacketListener(new IIncomingPacketListener() {
@Override
public void OnIncomingPacket(DatagramPacket packet) {
for (final Neighbor neighbor : getReportIxi().getNeighbors()) {
if (packet.getAddress().equals(neighbor.getAddress()) && packet.getPort() == neighbor.getReportPort()) {
String data = new String(packet.getData(), 0, packet.getLength());

if (data.contains("uuid:")) {
neighbor.setUuid(data.substring(5));
}

}
}
}
});
}

@Override
Expand All @@ -51,12 +36,12 @@ public void run() {

while (isReceiving) {

byte[] buf = new byte[256];
byte[] buf = new byte[1024];
DatagramPacket packet = new DatagramPacket(buf, buf.length);

try {
this.socket.receive(packet);
notifyOnIncomingPacketListeners(packet);
socket.receive(packet);
processPacket(packet);
} catch (IOException e) {
if (isReceiving)
e.printStackTrace();
Expand All @@ -68,26 +53,88 @@ public void shutDown() {
isReceiving = false;
}

public void addOnIncomingPacketListener(IIncomingPacketListener onIncomingPacketListener) {
if (!onIncomingPacketListeners.contains(onIncomingPacketListener)) {
onIncomingPacketListeners.add(onIncomingPacketListener);
public void processPacket(final DatagramPacket packet) {

for (final Neighbor neighbor : getReportIxi().getNeighbors()) {
if (packet.getAddress().equals(neighbor.getAddress()) && packet.getPort() == neighbor.getReportPort()) {
String data = new String(packet.getData(), 0, packet.getLength());

try {
final Payload payload = Payload.deserialize(data);
processPayload(neighbor, payload);
} catch (Exception e) {
LOGGER.info(String.format("Received invalid packet from Neighbor[%s:%s]",
neighbor.getAddress(),
neighbor.getReportPort()));
}
}
}
}

public void removeOnIncomingPacketListener(IIncomingPacketListener onIncomingPacketListener) {
if (onIncomingPacketListeners.contains(onIncomingPacketListener)) {
onIncomingPacketListeners.remove(onIncomingPacketListener);
public void processPayload(final Neighbor neighbor, final Payload payload) {

if (payload instanceof MetadataPayload) {
processMetadataPacket(neighbor, (MetadataPayload) payload);
}
if (payload instanceof SignedPayload) {
processSignedPayload((SignedPayload) payload);
}
}

private void notifyOnIncomingPacketListeners(DatagramPacket packet) {
for (IIncomingPacketListener listener : onIncomingPacketListeners) {
listener.OnIncomingPacket(packet);
public void processMetadataPacket(final Neighbor neighbor, final MetadataPayload metadataPayload) {

if (neighbor.getReportIxiVersion() == null ||
!neighbor.getReportIxiVersion().equals(metadataPayload.getReportIxiVersion())) {
neighbor.setReportIxiVersion(metadataPayload.getReportIxiVersion());
LOGGER.info(String.format("Neighbor[%s:%s] operates Report.ixi version: %s",
neighbor.getAddress(),
neighbor.getReportPort(),
neighbor.getReportIxiVersion()));
}

if (neighbor.getUuid() == null ||
!neighbor.getUuid().equals(metadataPayload.getUuid())) {
neighbor.setUuid(metadataPayload.getUuid());
LOGGER.info(String.format("Received new uuid from neighbor[%s:%s]",
neighbor.getAddress(),
neighbor.getReportPort()));
}

if (neighbor.getPublicKey() == null ||
!neighbor.getPublicKey().equals(metadataPayload.getPublicKey())) {
neighbor.setPublicKey(metadataPayload.getPublicKey());
LOGGER.info(String.format("Received new publicKey from neighbor[%s:%s]",
neighbor.getAddress(),
neighbor.getReportPort()));
}
}

public interface IIncomingPacketListener {
void OnIncomingPacket(DatagramPacket packet);
public void processSignedPayload(final SignedPayload signedPayload) {

Neighbor signee = null;
for (Neighbor neighbor : reportIxi.getNeighbors()) {

if (neighbor.getPublicKey() != null && signedPayload.verify(neighbor.getPublicKey())) {

signee = neighbor;
break;
}
}

if (signedPayload.getPayload() instanceof PingPayload) {
PingPayload pingPayload = (PingPayload) signedPayload.getPayload();
ReceivedPingPayload receivedPingPayload;
if (signee != null) {
LOGGER.info(String.format("Received signed ping from neighbor [%s:%s]",
signee.getAddress(),
signee.getReportPort()));

receivedPingPayload = new ReceivedPingPayload(reportIxi.getProperties().getUuid(), pingPayload, true);
} else {
receivedPingPayload = new ReceivedPingPayload(reportIxi.getProperties().getUuid(), pingPayload, false);
}
reportIxi.getApi().getSender().send(receivedPingPayload, Constants.RCS_HOST, Constants.RCS_PORT);
}
}

/**
Expand Down
Loading

0 comments on commit 3f34c0f

Please sign in to comment.