Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only return public addresses in kademlia queries #82

Merged
merged 1 commit into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}