Skip to content

Commit

Permalink
Fix identify call on new connections
Browse files Browse the repository at this point in the history
Make sure to add outbound connections to address book in kademlia so we have bootstrap nodes in there

Switch to TLS before noise
  • Loading branch information
ianopolous committed Jul 12, 2023
1 parent 1b94a80 commit c1cfbea
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/main/java/org/peergos/HostBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ public static Host build(PrivKey privKey,
Host host = BuilderJKt.hostJ(Builder.Defaults.None, b -> {
b.getIdentity().setFactory(() -> privKey);
b.getTransports().add(TcpTransport::new);
b.getSecureChannels().add((k, m) -> new NoiseXXSecureChannel(k, m));
b.getSecureChannels().add((k, m) -> new TlsSecureChannel(k, m));
b.getSecureChannels().add(TlsSecureChannel::new);
b.getSecureChannels().add(NoiseXXSecureChannel::new);

b.getMuxers().addAll(muxers);
RamAddressBook addrs = new RamAddressBook();
Expand Down Expand Up @@ -177,12 +177,16 @@ public static Host build(PrivKey privKey,

// Send an identify req on all new connections
b.getConnectionHandlers().add(connection -> {
IdentifyOuterClass.Identify remoteId = connection.muxerSession()
.createStream(new IdentifyBinding(new IdentifyProtocol())).getController().join().id().join();
addrs.setAddrs(connection.remoteAddress().getPeerId(), 0, remoteId.getListenAddrsList()
.stream()
.map(bytes -> Multiaddr.deserialize(bytes.toByteArray()))
.toArray(Multiaddr[]::new));
if (connection.isInitiator())
return;
StreamPromise<IdentifyController> stream = connection.muxerSession()
.createStream(new IdentifyBinding(new IdentifyProtocol()));
stream.getController()
.thenCompose(IdentifyController::id)
.thenApply(remoteId -> addrs.setAddrs(connection.remoteAddress().getPeerId(), 0, remoteId.getListenAddrsList()
.stream()
.map(bytes -> Multiaddr.deserialize(bytes.toByteArray()))
.toArray(Multiaddr[]::new)));
});

for (String listenAddr : listenAddrs) {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/peergos/protocol/dht/KademliaEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public void setAddressBook(AddressBook addrs) {

public synchronized void addOutgoingConnection(PeerId peer, Multiaddr addr) {
router.touch(Instant.now(), new Node(Id.create(Hash.sha256(peer.getBytes()), 256), peer.toString()));
addressBook.addAddrs(peer, 0, addr);
}

public synchronized void addIncomingConnection(PeerId peer, Multiaddr addr) {
Expand Down

0 comments on commit c1cfbea

Please sign in to comment.