Minor refactoring
I think having the read method in the same place as the write method might help preventing some errors There are some people who are strictly against using static methods. I suppose I'm not experienced enough yet.
This commit is contained in:
parent
2cd857dd36
commit
51c1ecfd41
@ -27,6 +27,7 @@ import org.slf4j.LoggerFactory;
|
|||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by chris on 06.04.15.
|
* Created by chris on 06.04.15.
|
||||||
@ -47,9 +48,9 @@ public class Main {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
|
|
||||||
System.out.print("Press Enter to exit\n");
|
System.out.print("Press Enter to exit\n");
|
||||||
br.readLine();
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
scanner.nextLine();
|
||||||
LOG.info("Shutting down client");
|
LOG.info("Shutting down client");
|
||||||
networkNode.stop();
|
networkNode.stop();
|
||||||
}
|
}
|
||||||
|
@ -19,6 +19,7 @@ package ch.dissem.bitmessage.entity;
|
|||||||
import ch.dissem.bitmessage.Context;
|
import ch.dissem.bitmessage.Context;
|
||||||
import ch.dissem.bitmessage.entity.valueobject.NetworkAddress;
|
import ch.dissem.bitmessage.entity.valueobject.NetworkAddress;
|
||||||
import ch.dissem.bitmessage.utils.Encode;
|
import ch.dissem.bitmessage.utils.Encode;
|
||||||
|
import ch.dissem.bitmessage.utils.UnixTime;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
@ -148,7 +149,7 @@ public class Version implements MessagePayload {
|
|||||||
public Builder defaults() {
|
public Builder defaults() {
|
||||||
version = Context.CURRENT_VERSION;
|
version = Context.CURRENT_VERSION;
|
||||||
services = 1;
|
services = 1;
|
||||||
timestamp = System.currentTimeMillis() / 1000;
|
timestamp = UnixTime.now();
|
||||||
nonce = new Random().nextInt();
|
nonce = new Random().nextInt();
|
||||||
userAgent = "/Jabit:0.0.1/";
|
userAgent = "/Jabit:0.0.1/";
|
||||||
streamNumbers = new long[]{1};
|
streamNumbers = new long[]{1};
|
||||||
|
@ -16,7 +16,10 @@
|
|||||||
|
|
||||||
package ch.dissem.bitmessage.entity.payload;
|
package ch.dissem.bitmessage.entity.payload;
|
||||||
|
|
||||||
|
import ch.dissem.bitmessage.utils.Decode;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -27,11 +30,15 @@ public class GenericPayload implements ObjectPayload {
|
|||||||
private long stream;
|
private long stream;
|
||||||
private byte[] data;
|
private byte[] data;
|
||||||
|
|
||||||
public GenericPayload(long stream, byte[] data) {
|
private GenericPayload(long stream, byte[] data) {
|
||||||
this.stream = stream;
|
this.stream = stream;
|
||||||
this.data = data;
|
this.data = data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static GenericPayload read(InputStream is, long stream, int length) throws IOException {
|
||||||
|
return new GenericPayload(stream, Decode.bytes(is, length));
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long getStream() {
|
public long getStream() {
|
||||||
return stream;
|
return stream;
|
||||||
|
@ -16,7 +16,10 @@
|
|||||||
|
|
||||||
package ch.dissem.bitmessage.entity.payload;
|
package ch.dissem.bitmessage.entity.payload;
|
||||||
|
|
||||||
|
import ch.dissem.bitmessage.utils.Decode;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -27,7 +30,7 @@ public class GetPubkey implements ObjectPayload {
|
|||||||
private byte[] ripe;
|
private byte[] ripe;
|
||||||
private byte[] tag;
|
private byte[] tag;
|
||||||
|
|
||||||
public GetPubkey(long stream, byte[] ripeOrTag) {
|
private GetPubkey(long stream, byte[] ripeOrTag) {
|
||||||
this.stream = stream;
|
this.stream = stream;
|
||||||
switch (ripeOrTag.length) {
|
switch (ripeOrTag.length) {
|
||||||
case 20:
|
case 20:
|
||||||
@ -41,6 +44,10 @@ public class GetPubkey implements ObjectPayload {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static GetPubkey read(InputStream is, long stream, int length) throws IOException {
|
||||||
|
return new GetPubkey(stream, Decode.bytes(is, length));
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long getStream() {
|
public long getStream() {
|
||||||
return stream;
|
return stream;
|
||||||
|
@ -16,7 +16,10 @@
|
|||||||
|
|
||||||
package ch.dissem.bitmessage.entity.payload;
|
package ch.dissem.bitmessage.entity.payload;
|
||||||
|
|
||||||
|
import ch.dissem.bitmessage.utils.Decode;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -27,7 +30,7 @@ public class Msg implements ObjectPayload {
|
|||||||
private byte[] encrypted;
|
private byte[] encrypted;
|
||||||
private UnencryptedMessage unencrypted;
|
private UnencryptedMessage unencrypted;
|
||||||
|
|
||||||
public Msg(long stream, byte[] encrypted) {
|
private Msg(long stream, byte[] encrypted) {
|
||||||
this.stream = stream;
|
this.stream = stream;
|
||||||
this.encrypted = encrypted;
|
this.encrypted = encrypted;
|
||||||
}
|
}
|
||||||
@ -37,6 +40,10 @@ public class Msg implements ObjectPayload {
|
|||||||
this.unencrypted = unencrypted;
|
this.unencrypted = unencrypted;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Msg read(InputStream is, long stream, int length) throws IOException {
|
||||||
|
return new Msg(stream, Decode.bytes(is, length));
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long getStream() {
|
public long getStream() {
|
||||||
return stream;
|
return stream;
|
||||||
|
@ -16,9 +16,11 @@
|
|||||||
|
|
||||||
package ch.dissem.bitmessage.entity.payload;
|
package ch.dissem.bitmessage.entity.payload;
|
||||||
|
|
||||||
|
import ch.dissem.bitmessage.utils.Decode;
|
||||||
import ch.dissem.bitmessage.utils.Encode;
|
import ch.dissem.bitmessage.utils.Encode;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -40,6 +42,15 @@ public class V2Pubkey implements Pubkey {
|
|||||||
publicEncryptionKey = builder.publicEncryptionKey;
|
publicEncryptionKey = builder.publicEncryptionKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static V2Pubkey read(InputStream is, long stream) throws IOException {
|
||||||
|
return new V2Pubkey.Builder()
|
||||||
|
.streamNumber(stream)
|
||||||
|
.behaviorBitfield((int) Decode.uint32(is))
|
||||||
|
.publicSigningKey(Decode.bytes(is, 64))
|
||||||
|
.publicEncryptionKey(Decode.bytes(is, 64))
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long getVersion() {
|
public long getVersion() {
|
||||||
return 2;
|
return 2;
|
||||||
|
@ -16,9 +16,11 @@
|
|||||||
|
|
||||||
package ch.dissem.bitmessage.entity.payload;
|
package ch.dissem.bitmessage.entity.payload;
|
||||||
|
|
||||||
|
import ch.dissem.bitmessage.utils.Decode;
|
||||||
import ch.dissem.bitmessage.utils.Encode;
|
import ch.dissem.bitmessage.utils.Encode;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -40,6 +42,19 @@ public class V3Pubkey extends V2Pubkey {
|
|||||||
signature = builder.signature;
|
signature = builder.signature;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static V3Pubkey read(InputStream is, long stream) throws IOException {
|
||||||
|
V3Pubkey.Builder v3 = new V3Pubkey.Builder()
|
||||||
|
.streamNumber(stream)
|
||||||
|
.behaviorBitfield((int) Decode.uint32(is))
|
||||||
|
.publicSigningKey(Decode.bytes(is, 64))
|
||||||
|
.publicEncryptionKey(Decode.bytes(is, 64))
|
||||||
|
.nonceTrialsPerByte(Decode.varInt(is))
|
||||||
|
.extraBytes(Decode.varInt(is));
|
||||||
|
int sigLength = (int) Decode.varInt(is);
|
||||||
|
v3.signature(Decode.bytes(is, sigLength));
|
||||||
|
return v3.build();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write(OutputStream os) throws IOException {
|
public void write(OutputStream os) throws IOException {
|
||||||
super.write(os);
|
super.write(os);
|
||||||
|
@ -16,7 +16,10 @@
|
|||||||
|
|
||||||
package ch.dissem.bitmessage.entity.payload;
|
package ch.dissem.bitmessage.entity.payload;
|
||||||
|
|
||||||
|
import ch.dissem.bitmessage.utils.Decode;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -28,11 +31,15 @@ public class V4Broadcast implements Broadcast {
|
|||||||
private byte[] encrypted;
|
private byte[] encrypted;
|
||||||
private UnencryptedMessage unencrypted;
|
private UnencryptedMessage unencrypted;
|
||||||
|
|
||||||
public V4Broadcast(long stream, byte[] encrypted) {
|
protected V4Broadcast(long stream, byte[] encrypted) {
|
||||||
this.stream = stream;
|
this.stream = stream;
|
||||||
this.encrypted = encrypted;
|
this.encrypted = encrypted;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static V4Broadcast read(InputStream is, long stream, int length) throws IOException {
|
||||||
|
return new V4Broadcast(stream, Decode.bytes(is, length));
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long getStream() {
|
public long getStream() {
|
||||||
return stream;
|
return stream;
|
||||||
|
@ -16,7 +16,10 @@
|
|||||||
|
|
||||||
package ch.dissem.bitmessage.entity.payload;
|
package ch.dissem.bitmessage.entity.payload;
|
||||||
|
|
||||||
|
import ch.dissem.bitmessage.utils.Decode;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -31,7 +34,7 @@ public class V4Pubkey implements Pubkey {
|
|||||||
private byte[] encrypted;
|
private byte[] encrypted;
|
||||||
private V3Pubkey decrypted;
|
private V3Pubkey decrypted;
|
||||||
|
|
||||||
public V4Pubkey(long stream, byte[] tag, byte[] encrypted) {
|
private V4Pubkey(long stream, byte[] tag, byte[] encrypted) {
|
||||||
this.stream = stream;
|
this.stream = stream;
|
||||||
this.tag = tag;
|
this.tag = tag;
|
||||||
this.encrypted = encrypted;
|
this.encrypted = encrypted;
|
||||||
@ -44,6 +47,10 @@ public class V4Pubkey implements Pubkey {
|
|||||||
// TODO: this.encrypted
|
// TODO: this.encrypted
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static ObjectPayload read(InputStream stream, long streamNumber, int length) throws IOException {
|
||||||
|
return new V4Pubkey(streamNumber, Decode.bytes(stream, 32), Decode.bytes(stream, length - 32));
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write(OutputStream stream) throws IOException {
|
public void write(OutputStream stream) throws IOException {
|
||||||
stream.write(tag);
|
stream.write(tag);
|
||||||
|
@ -16,7 +16,10 @@
|
|||||||
|
|
||||||
package ch.dissem.bitmessage.entity.payload;
|
package ch.dissem.bitmessage.entity.payload;
|
||||||
|
|
||||||
|
import ch.dissem.bitmessage.utils.Decode;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -25,11 +28,15 @@ import java.io.OutputStream;
|
|||||||
public class V5Broadcast extends V4Broadcast {
|
public class V5Broadcast extends V4Broadcast {
|
||||||
private byte[] tag;
|
private byte[] tag;
|
||||||
|
|
||||||
public V5Broadcast(long stream, byte[] tag, byte[] encrypted) {
|
private V5Broadcast(long stream, byte[] tag, byte[] encrypted) {
|
||||||
super(stream, encrypted);
|
super(stream, encrypted);
|
||||||
this.tag = tag;
|
this.tag = tag;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static V5Broadcast read(InputStream is, long stream, int length) throws IOException {
|
||||||
|
return new V5Broadcast(stream, Decode.bytes(is, 32), Decode.bytes(is, length - 32));
|
||||||
|
}
|
||||||
|
|
||||||
public byte[] getTag() {
|
public byte[] getTag() {
|
||||||
return tag;
|
return tag;
|
||||||
}
|
}
|
||||||
|
@ -35,7 +35,7 @@ public class Factory {
|
|||||||
|
|
||||||
public static NetworkMessage getNetworkMessage(int version, InputStream stream) throws SocketTimeoutException {
|
public static NetworkMessage getNetworkMessage(int version, InputStream stream) throws SocketTimeoutException {
|
||||||
try {
|
try {
|
||||||
return new V3MessageFactory().read(stream);
|
return V3MessageFactory.read(stream);
|
||||||
} catch (SocketTimeoutException e) {
|
} catch (SocketTimeoutException e) {
|
||||||
throw e;
|
throw e;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@ -46,7 +46,7 @@ public class Factory {
|
|||||||
|
|
||||||
public static ObjectMessage getObjectMessage(int version, InputStream stream, int length) {
|
public static ObjectMessage getObjectMessage(int version, InputStream stream, int length) {
|
||||||
try {
|
try {
|
||||||
return new V3MessageFactory().readObject(stream, length);
|
return V3MessageFactory.readObject(stream, length);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
LOG.error(e.getMessage(), e);
|
LOG.error(e.getMessage(), e);
|
||||||
return null;
|
return null;
|
||||||
@ -56,13 +56,13 @@ public class Factory {
|
|||||||
static ObjectPayload getObjectPayload(long objectType, long version, long streamNumber, InputStream stream, int length) throws IOException {
|
static ObjectPayload getObjectPayload(long objectType, long version, long streamNumber, InputStream stream, int length) throws IOException {
|
||||||
if (objectType < 4) {
|
if (objectType < 4) {
|
||||||
switch ((int) objectType) {
|
switch ((int) objectType) {
|
||||||
case 0: // getpubkey
|
case 0:
|
||||||
return parseGetPubkey((int) version, streamNumber, stream, length);
|
return parseGetPubkey((int) version, streamNumber, stream, length);
|
||||||
case 1: // pubkey
|
case 1:
|
||||||
return parsePubkey((int) version, streamNumber, stream, length);
|
return parsePubkey((int) version, streamNumber, stream, length);
|
||||||
case 2: // msg
|
case 2:
|
||||||
return parseMsg((int) version, streamNumber, stream, length);
|
return parseMsg((int) version, streamNumber, stream, length);
|
||||||
case 3: // broadcast
|
case 3:
|
||||||
return parseBroadcast((int) version, streamNumber, stream, length);
|
return parseBroadcast((int) version, streamNumber, stream, length);
|
||||||
default:
|
default:
|
||||||
LOG.error("This should not happen, someone broke something in the code!");
|
LOG.error("This should not happen, someone broke something in the code!");
|
||||||
@ -70,53 +70,39 @@ public class Factory {
|
|||||||
}
|
}
|
||||||
// fallback: just store the message - we don't really care what it is
|
// fallback: just store the message - we don't really care what it is
|
||||||
LOG.warn("Unexpected object type: " + objectType);
|
LOG.warn("Unexpected object type: " + objectType);
|
||||||
return new GenericPayload(streamNumber, Decode.bytes(stream, length));
|
return GenericPayload.read(stream, streamNumber, length);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ObjectPayload parseGetPubkey(int version, long streamNumber, InputStream stream, int length) throws IOException {
|
private static ObjectPayload parseGetPubkey(int version, long streamNumber, InputStream stream, int length) throws IOException {
|
||||||
return new GetPubkey(streamNumber, Decode.bytes(stream, length));
|
return GetPubkey.read(stream, streamNumber, length);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ObjectPayload parsePubkey(int version, long streamNumber, InputStream stream, int length) throws IOException {
|
private static ObjectPayload parsePubkey(int version, long streamNumber, InputStream stream, int length) throws IOException {
|
||||||
switch (version) {
|
switch (version) {
|
||||||
case 2:
|
case 2:
|
||||||
return new V2Pubkey.Builder()
|
return V2Pubkey.read(stream, streamNumber);
|
||||||
.streamNumber(streamNumber)
|
|
||||||
.behaviorBitfield((int) Decode.int64(stream))
|
|
||||||
.publicSigningKey(Decode.bytes(stream, 64))
|
|
||||||
.publicEncryptionKey(Decode.bytes(stream, 64))
|
|
||||||
.build();
|
|
||||||
case 3:
|
case 3:
|
||||||
V3Pubkey.Builder v3 = new V3Pubkey.Builder()
|
return V3Pubkey.read(stream, streamNumber);
|
||||||
.streamNumber(streamNumber)
|
|
||||||
.behaviorBitfield((int) Decode.int64(stream))
|
|
||||||
.publicSigningKey(Decode.bytes(stream, 64))
|
|
||||||
.publicEncryptionKey(Decode.bytes(stream, 64))
|
|
||||||
.nonceTrialsPerByte(Decode.varInt(stream))
|
|
||||||
.extraBytes(Decode.varInt(stream));
|
|
||||||
int sigLength = (int) Decode.varInt(stream);
|
|
||||||
v3.signature(Decode.bytes(stream, sigLength));
|
|
||||||
return v3.build();
|
|
||||||
case 4:
|
case 4:
|
||||||
return new V4Pubkey(streamNumber, Decode.bytes(stream, 32), Decode.bytes(stream, length - 32));
|
return V4Pubkey.read(stream, streamNumber, length);
|
||||||
}
|
}
|
||||||
LOG.debug("Unexpected pubkey version " + version + ", handling as generic payload object");
|
LOG.debug("Unexpected pubkey version " + version + ", handling as generic payload object");
|
||||||
return new GenericPayload(streamNumber, Decode.bytes(stream, length));
|
return GenericPayload.read(stream, streamNumber, length);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ObjectPayload parseMsg(int version, long streamNumber, InputStream stream, int length) throws IOException {
|
private static ObjectPayload parseMsg(int version, long streamNumber, InputStream stream, int length) throws IOException {
|
||||||
return new Msg(streamNumber, Decode.bytes(stream, length));
|
return Msg.read(stream, streamNumber, length);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ObjectPayload parseBroadcast(int version, long streamNumber, InputStream stream, int length) throws IOException {
|
private static ObjectPayload parseBroadcast(int version, long streamNumber, InputStream stream, int length) throws IOException {
|
||||||
switch (version) {
|
switch (version) {
|
||||||
case 4:
|
case 4:
|
||||||
return new V4Broadcast(streamNumber, Decode.bytes(stream, length));
|
return V4Broadcast.read(stream, streamNumber, length);
|
||||||
case 5:
|
case 5:
|
||||||
return new V5Broadcast(streamNumber, Decode.bytes(stream, 32), Decode.bytes(stream, length - 32));
|
return V5Broadcast.read(stream, streamNumber, length);
|
||||||
default:
|
default:
|
||||||
LOG.debug("Encountered unknown broadcast version " + version);
|
LOG.debug("Encountered unknown broadcast version " + version);
|
||||||
return new GenericPayload(streamNumber, Decode.bytes(stream, length));
|
return GenericPayload.read(stream, streamNumber, length);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -34,9 +34,9 @@ import java.io.InputStream;
|
|||||||
* Creates protocol v3 network messages from {@link InputStream InputStreams}
|
* Creates protocol v3 network messages from {@link InputStream InputStreams}
|
||||||
*/
|
*/
|
||||||
class V3MessageFactory {
|
class V3MessageFactory {
|
||||||
private Logger LOG = LoggerFactory.getLogger(V3MessageFactory.class);
|
private static Logger LOG = LoggerFactory.getLogger(V3MessageFactory.class);
|
||||||
|
|
||||||
public NetworkMessage read(InputStream stream) throws IOException {
|
public static NetworkMessage read(InputStream stream) throws IOException {
|
||||||
if (testMagic(stream)) {
|
if (testMagic(stream)) {
|
||||||
String command = getCommand(stream);
|
String command = getCommand(stream);
|
||||||
int length = (int) Decode.uint32(stream);
|
int length = (int) Decode.uint32(stream);
|
||||||
@ -56,7 +56,7 @@ class V3MessageFactory {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private MessagePayload getPayload(String command, InputStream stream, int length) throws IOException {
|
private static MessagePayload getPayload(String command, InputStream stream, int length) throws IOException {
|
||||||
switch (command) {
|
switch (command) {
|
||||||
case "version":
|
case "version":
|
||||||
return parseVersion(stream);
|
return parseVersion(stream);
|
||||||
@ -76,7 +76,7 @@ class V3MessageFactory {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public ObjectMessage readObject(InputStream stream, int length) throws IOException {
|
public static ObjectMessage readObject(InputStream stream, int length) throws IOException {
|
||||||
AccessCounter counter = new AccessCounter();
|
AccessCounter counter = new AccessCounter();
|
||||||
byte nonce[] = Decode.bytes(stream, 8, counter);
|
byte nonce[] = Decode.bytes(stream, 8, counter);
|
||||||
long expiresTime = Decode.int64(stream, counter);
|
long expiresTime = Decode.int64(stream, counter);
|
||||||
@ -96,7 +96,7 @@ class V3MessageFactory {
|
|||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
private GetData parseGetData(InputStream stream) throws IOException {
|
private static GetData parseGetData(InputStream stream) throws IOException {
|
||||||
long count = Decode.varInt(stream);
|
long count = Decode.varInt(stream);
|
||||||
GetData.Builder builder = new GetData.Builder();
|
GetData.Builder builder = new GetData.Builder();
|
||||||
for (int i = 0; i < count; i++) {
|
for (int i = 0; i < count; i++) {
|
||||||
@ -105,7 +105,7 @@ class V3MessageFactory {
|
|||||||
return builder.build();
|
return builder.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
private Inv parseInv(InputStream stream) throws IOException {
|
private static Inv parseInv(InputStream stream) throws IOException {
|
||||||
long count = Decode.varInt(stream);
|
long count = Decode.varInt(stream);
|
||||||
Inv.Builder builder = new Inv.Builder();
|
Inv.Builder builder = new Inv.Builder();
|
||||||
for (int i = 0; i < count; i++) {
|
for (int i = 0; i < count; i++) {
|
||||||
@ -114,7 +114,7 @@ class V3MessageFactory {
|
|||||||
return builder.build();
|
return builder.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
private Addr parseAddr(InputStream stream) throws IOException {
|
private static Addr parseAddr(InputStream stream) throws IOException {
|
||||||
long count = Decode.varInt(stream);
|
long count = Decode.varInt(stream);
|
||||||
Addr.Builder builder = new Addr.Builder();
|
Addr.Builder builder = new Addr.Builder();
|
||||||
for (int i = 0; i < count; i++) {
|
for (int i = 0; i < count; i++) {
|
||||||
@ -123,7 +123,7 @@ class V3MessageFactory {
|
|||||||
return builder.build();
|
return builder.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
private Version parseVersion(InputStream stream) throws IOException {
|
private static Version parseVersion(InputStream stream) throws IOException {
|
||||||
int version = Decode.int32(stream);
|
int version = Decode.int32(stream);
|
||||||
long services = Decode.int64(stream);
|
long services = Decode.int64(stream);
|
||||||
long timestamp = Decode.int64(stream);
|
long timestamp = Decode.int64(stream);
|
||||||
@ -143,11 +143,11 @@ class V3MessageFactory {
|
|||||||
.streams(streamNumbers).build();
|
.streams(streamNumbers).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
private InventoryVector parseInventoryVector(InputStream stream) throws IOException {
|
private static InventoryVector parseInventoryVector(InputStream stream) throws IOException {
|
||||||
return new InventoryVector(Decode.bytes(stream, 32));
|
return new InventoryVector(Decode.bytes(stream, 32));
|
||||||
}
|
}
|
||||||
|
|
||||||
private NetworkAddress parseAddress(InputStream stream, boolean light) throws IOException {
|
private static NetworkAddress parseAddress(InputStream stream, boolean light) throws IOException {
|
||||||
long time;
|
long time;
|
||||||
long streamNumber;
|
long streamNumber;
|
||||||
if (!light) {
|
if (!light) {
|
||||||
@ -163,7 +163,7 @@ class V3MessageFactory {
|
|||||||
return new NetworkAddress.Builder().time(time).stream(streamNumber).services(services).ipv6(ipv6).port(port).build();
|
return new NetworkAddress.Builder().time(time).stream(streamNumber).services(services).ipv6(ipv6).port(port).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean testChecksum(byte[] checksum, byte[] payload) {
|
private static boolean testChecksum(byte[] checksum, byte[] payload) {
|
||||||
byte[] payloadChecksum = Security.sha512(payload);
|
byte[] payloadChecksum = Security.sha512(payload);
|
||||||
for (int i = 0; i < checksum.length; i++) {
|
for (int i = 0; i < checksum.length; i++) {
|
||||||
if (checksum[i] != payloadChecksum[i]) {
|
if (checksum[i] != payloadChecksum[i]) {
|
||||||
@ -173,7 +173,7 @@ class V3MessageFactory {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getCommand(InputStream stream) throws IOException {
|
private static String getCommand(InputStream stream) throws IOException {
|
||||||
byte[] bytes = new byte[12];
|
byte[] bytes = new byte[12];
|
||||||
int end = -1;
|
int end = -1;
|
||||||
for (int i = 0; i < bytes.length; i++) {
|
for (int i = 0; i < bytes.length; i++) {
|
||||||
@ -187,7 +187,7 @@ class V3MessageFactory {
|
|||||||
return new String(bytes, 0, end, "ASCII");
|
return new String(bytes, 0, end, "ASCII");
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean testMagic(InputStream stream) throws IOException {
|
private static boolean testMagic(InputStream stream) throws IOException {
|
||||||
for (byte b : NetworkMessage.MAGIC_BYTES) {
|
for (byte b : NetworkMessage.MAGIC_BYTES) {
|
||||||
if (b != (byte) stream.read()) return false;
|
if (b != (byte) stream.read()) return false;
|
||||||
}
|
}
|
||||||
|
@ -92,7 +92,7 @@ public class Security {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static byte[] getProofOfWorkTarget(ObjectMessage object, long nonceTrialsPerByte, long extraBytes) throws IOException {
|
private static byte[] getProofOfWorkTarget(ObjectMessage object, long nonceTrialsPerByte, long extraBytes) throws IOException {
|
||||||
BigInteger TTL = BigInteger.valueOf(object.getExpiresTime() - (System.currentTimeMillis() / 1000));
|
BigInteger TTL = BigInteger.valueOf(object.getExpiresTime() - UnixTime.now());
|
||||||
LOG.debug("TTL: " + TTL + "s");
|
LOG.debug("TTL: " + TTL + "s");
|
||||||
BigInteger numerator = TWO.pow(64);
|
BigInteger numerator = TWO.pow(64);
|
||||||
BigInteger powLength = BigInteger.valueOf(object.getPayloadBytesWithoutNonce().length + extraBytes);
|
BigInteger powLength = BigInteger.valueOf(object.getPayloadBytesWithoutNonce().length + extraBytes);
|
||||||
|
@ -22,6 +22,7 @@ import ch.dissem.bitmessage.ports.MultiThreadedPOWEngine;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import javax.xml.bind.DatatypeConverter;
|
import javax.xml.bind.DatatypeConverter;
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.security.KeyPairGenerator;
|
import java.security.KeyPairGenerator;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
@ -71,8 +72,8 @@ public class SecurityTest {
|
|||||||
public void testProofOfWorkFails() throws IOException {
|
public void testProofOfWorkFails() throws IOException {
|
||||||
ObjectMessage objectMessage = new ObjectMessage.Builder()
|
ObjectMessage objectMessage = new ObjectMessage.Builder()
|
||||||
.nonce(new byte[8])
|
.nonce(new byte[8])
|
||||||
.expiresTime(300 + (System.currentTimeMillis() / 1000)) // 5 minutes
|
.expiresTime(UnixTime.now() + 300) // 5 minutes
|
||||||
.payload(new GenericPayload(1, new byte[0]))
|
.payload(GenericPayload.read(new ByteArrayInputStream(new byte[0]), 1, 0))
|
||||||
.build();
|
.build();
|
||||||
Security.checkProofOfWork(objectMessage, 1000, 1000);
|
Security.checkProofOfWork(objectMessage, 1000, 1000);
|
||||||
}
|
}
|
||||||
@ -84,7 +85,7 @@ public class SecurityTest {
|
|||||||
ObjectMessage objectMessage = new ObjectMessage.Builder()
|
ObjectMessage objectMessage = new ObjectMessage.Builder()
|
||||||
.nonce(new byte[8])
|
.nonce(new byte[8])
|
||||||
.expiresTime(expires.getTimeInMillis() / 1000)
|
.expiresTime(expires.getTimeInMillis() / 1000)
|
||||||
.payload(new GenericPayload(1, new byte[0]))
|
.payload(GenericPayload.read(new ByteArrayInputStream(new byte[0]), 1, 0))
|
||||||
.build();
|
.build();
|
||||||
Security.doProofOfWork(objectMessage, new MultiThreadedPOWEngine(), 1000, 1000);
|
Security.doProofOfWork(objectMessage, new MultiThreadedPOWEngine(), 1000, 1000);
|
||||||
Security.checkProofOfWork(objectMessage, 1000, 1000);
|
Security.checkProofOfWork(objectMessage, 1000, 1000);
|
||||||
|
@ -21,6 +21,7 @@ import ch.dissem.bitmessage.entity.Version;
|
|||||||
import ch.dissem.bitmessage.entity.payload.ObjectPayload;
|
import ch.dissem.bitmessage.entity.payload.ObjectPayload;
|
||||||
import ch.dissem.bitmessage.entity.valueobject.NetworkAddress;
|
import ch.dissem.bitmessage.entity.valueobject.NetworkAddress;
|
||||||
import ch.dissem.bitmessage.ports.NetworkHandler;
|
import ch.dissem.bitmessage.ports.NetworkHandler;
|
||||||
|
import ch.dissem.bitmessage.utils.UnixTime;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -44,7 +45,7 @@ public class NetworkNodeTest {
|
|||||||
new Version.Builder()
|
new Version.Builder()
|
||||||
.version(3)
|
.version(3)
|
||||||
.services(1)
|
.services(1)
|
||||||
.timestamp(System.currentTimeMillis() / 1000)
|
.timestamp(UnixTime.now())
|
||||||
.addrFrom(localhost)
|
.addrFrom(localhost)
|
||||||
.addrRecv(localhost)
|
.addrRecv(localhost)
|
||||||
.nonce(-1)
|
.nonce(-1)
|
||||||
|
Loading…
Reference in New Issue
Block a user