Skip to content

Commit

Permalink
Filter out AccountAgeWitness data if it appears to be a dos attack or…
Browse files Browse the repository at this point in the history
… caused from a bug
  • Loading branch information
chimp1984 authored and ripcurlx committed Dec 30, 2020
1 parent a81188e commit 85d0d61
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions p2p/src/main/java/bisq/network/p2p/network/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,13 @@
import java.io.StreamCorruptedException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Queue;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
Expand Down Expand Up @@ -421,11 +425,36 @@ private boolean violatesThrottleLimit(long now, int seconds, int messageCountLim
@Override
public void onMessage(NetworkEnvelope networkEnvelope, Connection connection) {
checkArgument(connection.equals(this));

int accountAgeWitnessEntries = 0;
if (networkEnvelope instanceof BundleOfEnvelopes) {
for (NetworkEnvelope current : ((BundleOfEnvelopes) networkEnvelope).getEnvelopes()) {
UserThread.execute(() -> messageListeners.forEach(e -> e.onMessage(current, connection)));
Map<String, List<NetworkEnvelope>> map = new HashMap<>();
Set<NetworkEnvelope> set = new HashSet<>();

List<NetworkEnvelope> networkEnvelopes = ((BundleOfEnvelopes) networkEnvelope).getEnvelopes();
for (NetworkEnvelope current : networkEnvelopes) {
String simpleName = current.getClass().getSimpleName();
boolean isAccountAgeWitness = false;
if (current instanceof AddPersistableNetworkPayloadMessage) {
PersistableNetworkPayload persistableNetworkPayload = ((AddPersistableNetworkPayloadMessage) current).getPersistableNetworkPayload();
simpleName = "AddPersistableNetworkPayloadMessage." + persistableNetworkPayload.getClass().getSimpleName();
if (simpleName.equals("AddPersistableNetworkPayloadMessage.AccountAgeWitness")) {
accountAgeWitnessEntries++;
isAccountAgeWitness = true;
}
}
map.putIfAbsent(simpleName, new ArrayList<>());
map.get(simpleName).add(current);
if (!isAccountAgeWitness || accountAgeWitnessEntries < 20) {
set.add(current);
}
}
map.forEach((key, value) -> log.info("BundleOfEnvelope with {} items of {}, from {}",
value.size(), key, connection.getPeersNodeAddressOptional()));

log.info("We forward {} items. All received items: {}", set.size(), networkEnvelopes.size());

set.forEach(envelope -> UserThread.execute(() ->
messageListeners.forEach(listener -> listener.onMessage(envelope, connection))));
} else {
UserThread.execute(() -> messageListeners.forEach(e -> e.onMessage(networkEnvelope, connection)));
}
Expand Down Expand Up @@ -718,7 +747,6 @@ public void run() {
lastReadTimeStamp, now, elapsed);
Thread.sleep(20);
}

// Reading the protobuffer message from the inputStream
protobuf.NetworkEnvelope proto = protobuf.NetworkEnvelope.parseDelimitedFrom(protoInputStream);

Expand Down

0 comments on commit 85d0d61

Please sign in to comment.