-
Notifications
You must be signed in to change notification settings - Fork 185
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
feat(iroh-net)!: Create our own connection struct #2768
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,94 +1,32 @@ | ||
//! Common code used to created quinn connections in the examples | ||
use anyhow::{bail, Context, Result}; | ||
use quinn::crypto::rustls::{QuicClientConfig, QuicServerConfig}; | ||
use std::{path::PathBuf, sync::Arc}; | ||
use tokio::fs; | ||
//! Common code used to created connections in the examples | ||
|
||
pub const EXAMPLE_ALPN: &[u8] = b"n0/iroh/examples/bytes/0"; | ||
|
||
// Path where the tls certificates are saved. This example expects that you have run the `provide-bytes` example first, which generates the certificates. | ||
pub const CERT_PATH: &str = "./certs"; | ||
|
||
// derived from `quinn/examples/client.rs` | ||
// load the certificates from CERT_PATH | ||
// Assumes that you have already run the `provide-bytes` example, that generates the certificates | ||
#[allow(unused)] | ||
pub async fn load_certs() -> Result<rustls::RootCertStore> { | ||
let mut roots = rustls::RootCertStore::empty(); | ||
let path = PathBuf::from(CERT_PATH).join("cert.der"); | ||
match fs::read(path).await { | ||
Ok(cert) => { | ||
roots.add(rustls::pki_types::CertificateDer::from(cert))?; | ||
} | ||
Err(e) => { | ||
bail!("failed to open local server certificate: {}\nYou must run the `provide-bytes` example to create the certificate.\n\tcargo run --example provide-bytes", e); | ||
} | ||
} | ||
Ok(roots) | ||
} | ||
use anyhow::{Context, Result}; | ||
use futures_lite::StreamExt; | ||
use iroh_net::discovery::dns::DnsDiscovery; | ||
use iroh_net::discovery::local_swarm_discovery::LocalSwarmDiscovery; | ||
use iroh_net::discovery::pkarr::PkarrPublisher; | ||
use iroh_net::discovery::ConcurrentDiscovery; | ||
use iroh_net::key::SecretKey; | ||
|
||
// derived from `quinn/examples/server.rs` | ||
// creates a self signed certificate and saves it to "./certs" | ||
#[allow(unused)] | ||
pub async fn make_and_write_certs() -> Result<( | ||
rustls::pki_types::PrivateKeyDer<'static>, | ||
rustls::pki_types::CertificateDer<'static>, | ||
)> { | ||
let path = std::path::PathBuf::from(CERT_PATH); | ||
let cert = rcgen::generate_simple_self_signed(vec!["localhost".into()]).unwrap(); | ||
let key_path = path.join("key.der"); | ||
let cert_path = path.join("cert.der"); | ||
pub const EXAMPLE_ALPN: &[u8] = b"n0/iroh/examples/bytes/0"; | ||
|
||
let key = cert.serialize_private_key_der(); | ||
let cert = cert.serialize_der().unwrap(); | ||
tokio::fs::create_dir_all(path) | ||
.await | ||
.context("failed to create certificate directory")?; | ||
tokio::fs::write(cert_path, &cert) | ||
.await | ||
.context("failed to write certificate")?; | ||
tokio::fs::write(key_path, &key) | ||
pub async fn make_iroh_endpoint() -> Result<iroh_net::Endpoint> { | ||
let secret_key = SecretKey::generate(); | ||
let discovery = ConcurrentDiscovery::from_services(vec![ | ||
Box::new(PkarrPublisher::n0_dns(secret_key.clone())), | ||
Box::new(DnsDiscovery::n0_dns()), | ||
Box::new(LocalSwarmDiscovery::new(secret_key.public())?), | ||
]); | ||
let ep = iroh_net::Endpoint::builder() | ||
.secret_key(secret_key) | ||
.discovery(Box::new(discovery)) | ||
.alpns(vec![EXAMPLE_ALPN.to_vec()]) | ||
.bind() | ||
.await?; | ||
// Wait for full connectivity | ||
ep.direct_addresses() | ||
.next() | ||
.await | ||
.context("failed to write private key")?; | ||
|
||
Ok(( | ||
rustls::pki_types::PrivateKeyDer::try_from(key).unwrap(), | ||
rustls::pki_types::CertificateDer::from(cert), | ||
)) | ||
} | ||
|
||
// derived from `quinn/examples/client.rs` | ||
// Creates a client quinn::Endpoint | ||
#[allow(unused)] | ||
pub fn make_client_endpoint(roots: rustls::RootCertStore) -> Result<quinn::Endpoint> { | ||
let mut client_crypto = rustls::ClientConfig::builder() | ||
.with_root_certificates(roots) | ||
.with_no_client_auth(); | ||
|
||
client_crypto.alpn_protocols = vec![EXAMPLE_ALPN.to_vec()]; | ||
let client_config: QuicClientConfig = client_crypto.try_into()?; | ||
let client_config = quinn::ClientConfig::new(Arc::new(client_config)); | ||
let mut endpoint = quinn::Endpoint::client("[::]:0".parse().unwrap())?; | ||
endpoint.set_default_client_config(client_config); | ||
Ok(endpoint) | ||
} | ||
|
||
// derived from `quinn/examples/server.rs` | ||
// makes a quinn server endpoint | ||
#[allow(unused)] | ||
pub fn make_server_endpoint( | ||
key: rustls::pki_types::PrivateKeyDer<'static>, | ||
cert: rustls::pki_types::CertificateDer<'static>, | ||
) -> Result<quinn::Endpoint> { | ||
let mut server_crypto = rustls::ServerConfig::builder() | ||
.with_no_client_auth() | ||
.with_single_cert(vec![cert], key)?; | ||
server_crypto.alpn_protocols = vec![EXAMPLE_ALPN.to_vec()]; | ||
let server_config: QuicServerConfig = server_crypto.try_into()?; | ||
let mut server_config = quinn::ServerConfig::with_crypto(Arc::new(server_config)); | ||
let transport_config = Arc::get_mut(&mut server_config.transport).unwrap(); | ||
transport_config.max_concurrent_uni_streams(0_u8.into()); | ||
|
||
let endpoint = quinn::Endpoint::server(server_config, "[::1]:4433".parse()?)?; | ||
Ok(endpoint) | ||
.context("no direct addrs")?; | ||
Ok(ep) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ use std::{ | |
time::{Duration, Instant}, | ||
}; | ||
|
||
use iroh_net::{endpoint::get_remote_node_id, key::PublicKey, Endpoint, NodeAddr}; | ||
use iroh_net::{key::PublicKey, Endpoint, NodeAddr}; | ||
use serde::{Deserialize, Serialize}; | ||
use tracing::{debug, error_span, trace, Instrument}; | ||
|
||
|
@@ -116,7 +116,7 @@ where | |
{ | ||
let t_start = Instant::now(); | ||
let connection = connecting.await.map_err(AcceptError::connect)?; | ||
let peer = get_remote_node_id(&connection).map_err(AcceptError::connect)?; | ||
let peer = connection.remote_node_id().map_err(AcceptError::connect)?; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. node_id? |
||
let (mut send_stream, mut recv_stream) = connection | ||
.accept_bi() | ||
.await | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -221,7 +221,7 @@ async fn handle_connection( | |
) -> anyhow::Result<()> { | ||
let alpn = conn.alpn().await?; | ||
let conn = conn.await?; | ||
let peer_id = iroh_net::endpoint::get_remote_node_id(&conn)?; | ||
let peer_id = conn.remote_node_id()?; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Call this node_id while we're at it? |
||
match alpn.as_ref() { | ||
GOSSIP_ALPN => gossip.handle_connection(conn).await.context(format!( | ||
"connection to {peer_id} with ALPN {} failed", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let Ok(remote_node_id)