Skip to content

Commit

Permalink
fix: make client and server config ad-hoc in secure_outbound method
Browse files Browse the repository at this point in the history
Introduce the `with_remote_peer_id` constructor in `Config`.
  • Loading branch information
denis2glez committed Nov 24, 2023
1 parent d8440e7 commit c2bf72d
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions transports/tls/src/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ impl Config {
client: crate::make_client_config(identity, None)?,
})
}

pub(crate) fn with_remote_peer_id(
remote_peer_id: Option<PeerId>,
) -> Result<Self, certificate::GenError> {
let identity = libp2p_identity::Keypair::generate_ed25519();

Ok(Self {
server: crate::make_server_config(&identity)?,
client: crate::make_client_config(&identity, remote_peer_id)?,
})
}
}

impl UpgradeInfo for Config {
Expand Down Expand Up @@ -117,9 +128,9 @@ where
.await
.map_err(UpgradeError::ServerUpgrade)?;

let expected = extract_single_certificate(stream.get_ref().1)?.peer_id();
let peer_id = extract_single_certificate(stream.get_ref().1)?.peer_id();

Ok((expected, stream.into()))
Ok((peer_id, stream.into()))
}
.boxed()
}
Expand Down Expand Up @@ -153,15 +164,9 @@ where

let peer_id = extract_single_certificate(stream.get_ref().1)?.peer_id();

match remote_peer_id {
Some(remote_peer_id) if remote_peer_id != peer_id => {
Err(UpgradeError::PeerIdMismatch {
peer_id,
remote_peer_id,
})
}
_ => Ok((peer_id, stream.into())),
}
Self::with_remote_peer_id(remote_peer_id)?;

Ok((peer_id, stream.into()))
}
.boxed()
}
Expand Down

0 comments on commit c2bf72d

Please sign in to comment.