Skip to content

Commit

Permalink
Merge pull request #9 from itsMatoosh/version-0.4
Browse files Browse the repository at this point in the history
Version 0.4
  • Loading branch information
itsMatoosh authored Feb 26, 2019
2 parents 6c6dda1 + f7b1607 commit b181337
Show file tree
Hide file tree
Showing 47 changed files with 1,011 additions and 436 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@ standalone/random.id
cache
standalone.yaml
network.yaml
*.id
2 changes: 2 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ allprojects {
sourceCompatibility = "1.8"
targetCompatibility = "1.8"
group 'me.matoosh.undernet'
version '0.3.2'
version '0.4'
}
subprojects {
repositories {
Expand Down
2 changes: 2 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@
include ':shared'
include ':api'
include ':standalone'
include ':shine'

rootProject.name = 'UnderNet'

4 changes: 3 additions & 1 deletion shared/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
dependencies {
implementation group: 'io.netty', name: 'netty-all', version: '4.1.22.Final'
implementation project(':shine')
implementation group: 'io.netty', name: 'netty-all', version: '4.1.33.Final'
implementation group: 'com.barchart.udt', name: 'barchart-udt-bundle', version: '2.3.0'
implementation group: 'org.apache.commons', name: 'commons-crypto', version: '1.0.0'
implementation group: 'org.bouncycastle', name: 'bcprov-jdk16', version: '1.45'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public ChannelCreatedEvent(Channel c, boolean isServer) {
@Override
public void onCalled() {
if(isServer) {
Server.logger.info("Channel has been created to: {}", channel.remoteAddress());
Server.logger.info("New connection established with: {}", channel.remoteAddress());
} else {
Client.logger.info("Channel has been created to: {}", channel.remoteAddress());
Client.logger.info("New connection established with: {}", channel.remoteAddress());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
package me.matoosh.undernet.event.channel.message.tunnel;

import me.matoosh.undernet.p2p.router.data.message.NetworkMessage;
import me.matoosh.undernet.p2p.router.data.message.NetworkMessageManager;
import me.matoosh.undernet.p2p.router.data.message.tunnel.MessageTunnel;

public class MessageTunnelEstablishedEvent extends MessageTunnelEvent {
/**
* The direction in which the tunnel was established.
*/
public NetworkMessage.MessageDirection establishDirection;

public MessageTunnelEstablishedEvent(MessageTunnel messageTunnel, NetworkMessage.MessageDirection establishDirection) {
public MessageTunnelEstablishedEvent(MessageTunnel messageTunnel) {
super(messageTunnel);
this.establishDirection = establishDirection;
}

@Override
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.ArrayList;

Expand All @@ -28,47 +30,22 @@ public class EntryNodeCache {
public static Logger logger = LoggerFactory.getLogger(EntryNodeCache.class);

/**
* Returns a specific number of the most reliable nodes.
* Returns a specific number of random cached nodes.
* @param amount
* @return
*/
public static ArrayList<Node> getMostReliable(int amount, Node... exclude) {
public static ArrayList<Node> getRandom(int amount) {
//Skipping if there are no cached nodes.
if(cachedNodes == null || cachedNodes.size() == 0) {
return new ArrayList<>();
}

ArrayList<Node> resultList = (ArrayList<Node>) cachedNodes.clone();

//Excluding nodes.
if(exclude != null) {
for (Node n :
exclude) {
resultList.remove(n);
}
}

//Adjusting the amount.
if(amount > resultList.size()) {
amount = resultList.size();
if(amount >= cachedNodes.size()) {
return cachedNodes;
} else {
int i = UnderNet.secureRandom.nextInt(cachedNodes.size() - amount);
return (ArrayList<Node>) cachedNodes.subList(i, i + amount);
}

//Removing nodes with lowest reliability until reached the amount.
for(int i = 0; i < amount; i++) {
if(amount == resultList.size()) break;

//Getting the least reliable node in the remaining set.
Node lowestRel = resultList.get(0);
/*for(Node node : resultList) {
if(node.reliability < lowestRel.reliability) {
lowestRel = node;
}
}*/

resultList.remove(lowestRel);
}

return resultList;
}

/**
Expand Down Expand Up @@ -96,7 +73,9 @@ public static Node addNode(String host) {
if(node.getAddress() == null || node.getAddress().getAddress() == null) return null;

//Adding the node to the cache.
EntryNodeCache.addNode(node);
if(!Node.isLocalAddress(node.getAddress())) {
EntryNodeCache.addNode(node);
}

return node;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,22 @@ public interface NetworkConfig {
* @return
*/
Integer maxReconnectCount();

/**
* Whether UnderNet should use SHINE.
* @return
*/
Boolean useShine();

/**
* The used SHINE address.
* @return
*/
String shineAddress();

/**
* The used SHINE port.
* @return
*/
Integer shinePort();
}
37 changes: 15 additions & 22 deletions shared/src/main/java/me/matoosh/undernet/p2p/crypto/KeyTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public class KeyTools {
*/
public static final String ECC_CURVE_NAME = "secp256k1";

/**
* The logger of the class.
*/
public static Logger logger = LoggerFactory.getLogger(KeyTools.class);

/**
* Represents the ECC curve used.
*/
Expand All @@ -48,12 +53,6 @@ public class KeyTools {
e.printStackTrace();
}
}
private static final byte UNCOMPRESSED_POINT_INDICATOR = 0x04;

/**
* The logger of the class.
*/
static final Logger logger = LoggerFactory.getLogger(KeyTools.class);

/**
* Generates a random keypair.
Expand Down Expand Up @@ -82,7 +81,7 @@ public static KeyPair generateKeypair() {
}

/**
* Gets public key from byte representation.
* Converts byte array with compressed EC public key data into an EC public key instance.
* @param uncompressedPoint
* @return
* @throws Exception
Expand All @@ -92,17 +91,12 @@ public static ECPublicKey fromUncompressedPoint(
throws Exception {

int offset = 0;
if (uncompressedPoint[offset++] != UNCOMPRESSED_POINT_INDICATOR) {
throw new IllegalArgumentException(
"Invalid uncompressedPoint encoding, no uncompressed point indicator");
}

int keySizeBytes = (ECC_CURVE.getOrder().bitLength() + Byte.SIZE - 1)
/ Byte.SIZE;

if (uncompressedPoint.length != 1 + 2 * keySizeBytes) {
throw new IllegalArgumentException(
"Invalid uncompressedPoint encoding, not the correct size");
if (uncompressedPoint.length != 2 * keySizeBytes) {
logger.error("Couldn't deserialize EC Public Key, invalid data size!");
return null;
}

final BigInteger x = new BigInteger(1, Arrays.copyOfRange(
Expand All @@ -117,18 +111,15 @@ public static ECPublicKey fromUncompressedPoint(
}

/**
* Gets byte representation of a public key.
* Converts EC public key data into a byte array.
* @param publicKey
* @return
*/
public static byte[] toUncompressedPoint(final ECPublicKey publicKey) {

int keySizeBytes = (publicKey.getParams().getOrder().bitLength() + Byte.SIZE - 1)
/ Byte.SIZE;

final byte[] uncompressedPoint = new byte[1 + 2 * keySizeBytes];
final byte[] uncompressedPoint = new byte[2 * keySizeBytes];
int offset = 0;
uncompressedPoint[offset++] = 0x04;

final byte[] x = publicKey.getW().getAffineX().toByteArray();
if (x.length <= keySizeBytes) {
Expand All @@ -137,7 +128,8 @@ public static byte[] toUncompressedPoint(final ECPublicKey publicKey) {
} else if (x.length == keySizeBytes + 1 && x[0] == 0) {
System.arraycopy(x, 1, uncompressedPoint, offset, keySizeBytes);
} else {
throw new IllegalStateException("x value is too large");
logger.error("Couldn't serialize EC public key {}, X value too large!", publicKey);
return null;
}
offset += keySizeBytes;

Expand All @@ -148,7 +140,8 @@ public static byte[] toUncompressedPoint(final ECPublicKey publicKey) {
} else if (y.length == keySizeBytes + 1 && y[0] == 0) {
System.arraycopy(y, 1, uncompressedPoint, offset, keySizeBytes);
} else {
throw new IllegalStateException("y value is too large");
logger.error("Couldn't serialize EC public key {}, Y value too large!", publicKey);
return null;
}

return uncompressedPoint;
Expand Down
Loading

0 comments on commit b181337

Please sign in to comment.