Skip to content

Commit

Permalink
Merge pull request #82 from Peergos/opt/public-addresses-in-kademlia-…
Browse files Browse the repository at this point in the history
…responses

Only return public addresses in kademlia queries
  • Loading branch information
ianopolous authored Dec 6, 2023
2 parents 6ea7add + bbf6ad6 commit 9cd535d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
11 changes: 11 additions & 0 deletions src/main/java/org/peergos/PeerAddresses.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.function.*;
import java.util.stream.*;

public class PeerAddresses {
Expand Down Expand Up @@ -70,6 +71,16 @@ public Dht.Message.Peer toProtobuf() {
.build();
}

public Dht.Message.Peer toProtobuf(Predicate<Multiaddr> filter) {
return Dht.Message.Peer.newBuilder()
.setId(ByteString.copyFrom(peerId.toBytes()))
.addAllAddrs(addresses.stream()
.filter(filter)
.map(a -> ByteString.copyFrom(a.serialize()))
.collect(Collectors.toList()))
.build();
}

public static PeerAddresses fromHost(Host host) {
Multihash peerId = Multihash.deserialize(host.getPeerId().getBytes());
List<Multiaddr> addrs = host.listenAddresses();
Expand Down
31 changes: 27 additions & 4 deletions src/main/java/org/peergos/protocol/dht/KademliaEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
import io.libp2p.core.*;
import io.libp2p.core.Stream;
import io.libp2p.core.multiformats.*;
import io.libp2p.core.multiformats.Protocol;
import io.prometheus.client.*;
import org.peergos.*;
import org.peergos.blockstore.*;
import org.peergos.protocol.dht.pb.*;
import org.peergos.protocol.ipns.*;

import java.io.*;
import java.net.*;
import java.time.*;
import java.util.*;
import java.util.stream.*;
Expand Down Expand Up @@ -117,7 +120,7 @@ public void receiveRequest(Dht.Message msg, PeerId source, Stream stream) {
.setValue(ByteString.copyFrom(ipnsRecord.get().raw)).build());
builder = builder.addAllCloserPeers(getKClosestPeers(msg.getKey().toByteArray())
.stream()
.map(PeerAddresses::toProtobuf)
.map(p -> p.toProtobuf(a -> isPublic(a)))
.collect(Collectors.toList()));
Dht.Message reply = builder.build();
stream.writeAndFlush(reply);
Expand All @@ -140,14 +143,17 @@ public void receiveRequest(Dht.Message msg, PeerId source, Stream stream) {
if (blocks.hasAny(hash).join()) {
providers = new HashSet<>(providers);
providers.add(new PeerAddresses(ourPeerId,
new ArrayList<>(addressBook.getAddrs(PeerId.fromBase58(ourPeerId.toBase58())).join())).toProtobuf());
addressBook.getAddrs(PeerId.fromBase58(ourPeerId.toBase58())).join()
.stream()
.filter(a -> isPublic(a))
.collect(Collectors.toList())).toProtobuf());
}
Dht.Message.Builder builder = msg.toBuilder();
builder = builder.addAllProviderPeers(providers.stream()
.collect(Collectors.toList()));
builder = builder.addAllCloserPeers(getKClosestPeers(msg.getKey().toByteArray())
.stream()
.map(PeerAddresses::toProtobuf)
.map(p -> p.toProtobuf(a -> isPublic(a)))
.collect(Collectors.toList()));
Dht.Message reply = builder.build();
stream.writeAndFlush(reply);
Expand All @@ -162,7 +168,7 @@ public void receiveRequest(Dht.Message msg, PeerId source, Stream stream) {
builder = builder.addAllCloserPeers(getKClosestPeers(target)
.stream()
.filter(p -> ! p.peerId.equals(sourcePeer)) // don't tell a peer about themselves
.map(PeerAddresses::toProtobuf)
.map(p -> p.toProtobuf(a -> isPublic(a)))
.collect(Collectors.toList()));
Dht.Message reply = builder.build();
stream.writeAndFlush(reply);
Expand All @@ -174,4 +180,21 @@ public void receiveRequest(Dht.Message msg, PeerId source, Stream stream) {
default: throw new IllegalStateException("Unknown message kademlia type: " + msg.getType());
}
}

public static boolean isPublic(Multiaddr addr) {
try {
List<MultiaddrComponent> parts = addr.getComponents();
for (MultiaddrComponent part: parts) {
if (part.getProtocol() == Protocol.IP6ZONE)
return true;
if (part.getProtocol() == Protocol.IP4 || part.getProtocol() == Protocol.IP6) {
InetAddress ip = InetAddress.getByName(part.getStringValue());
if (ip.isLoopbackAddress() || ip.isSiteLocalAddress() || ip.isLinkLocalAddress() || ip.isAnyLocalAddress())
return false;
return true;
}
}
} catch (IOException e) {}
return false;
}
}

0 comments on commit 9cd535d

Please sign in to comment.