From 628049984ed5ab61d335a7c652098a67c020a905 Mon Sep 17 00:00:00 2001 From: nazeh Date: Tue, 3 Dec 2024 12:02:26 +0300 Subject: [PATCH] feat(pubky): update pkarr and use anyhow --- Cargo.lock | 69 ++++++++++++++++++---------------- pubky-homeserver/src/core.rs | 8 +--- pubky-homeserver/src/pkarr.rs | 42 ++++++--------------- pubky-homeserver/src/server.rs | 5 ++- pubky/Cargo.toml | 1 + pubky/src/error.rs | 66 +------------------------------- pubky/src/lib.rs | 6 +-- pubky/src/native/api/auth.rs | 12 +++--- pubky/src/shared/auth.rs | 9 ++--- pubky/src/shared/pkarr.rs | 67 +++++++++++++++++++++++++-------- 10 files changed, 120 insertions(+), 165 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2b41cc5..39eaba4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -387,9 +387,9 @@ checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" [[package]] name = "bytes" -version = "1.8.0" +version = "1.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ac0150caa2ae65ca5bd83f25c7de183dea78d4d366469f148435e2acfbad0da" +checksum = "325918d6fe32f23b19878fe4b34794ae41fc19ddbe53b10571a4874d44ffd39b" [[package]] name = "cc" @@ -1407,10 +1407,11 @@ checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" [[package]] name = "js-sys" -version = "0.3.72" +version = "0.3.74" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6a88f1bda2bd75b0452a14784937d796722fdebfe50df998aeb3f0b7603019a9" +checksum = "a865e038f7f6ed956f788f0d7d60c541fff74c7bd74272c5d4cf15c63743e705" dependencies = [ + "once_cell", "wasm-bindgen", ] @@ -1422,9 +1423,9 @@ checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" [[package]] name = "libc" -version = "0.2.162" +version = "0.2.167" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "18d287de67fe55fd7e1581fe933d965a5a9477b38e949cfa9f8574ef01506398" +checksum = "09d6582e104315a817dff97f75133544b2e094ee22447d2acf4a74e189ba06fc" [[package]] name = "libredox" @@ -1489,8 +1490,9 @@ checksum = "234cf4f4a04dc1f57e24b96cc0cd600cf2af460d4161ac5ecdd0af8e1f3b2a38" [[package]] name = "mainline" -version = "3.0.1" -source = "git+https://github.com/pubky/mainline#5ff1391ce3e3aeb44752cdfb962e3d7895029b27" +version = "4.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e18c8b0210572062a02c4de8c448865f4ca89824c4ac7da64a0c2669ea2c405" dependencies = [ "bytes", "crc", @@ -1503,7 +1505,7 @@ dependencies = [ "serde_bencode", "serde_bytes", "sha1_smol", - "thiserror 1.0.69", + "thiserror 2.0.3", "tracing", ] @@ -1801,7 +1803,7 @@ checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" [[package]] name = "pkarr" version = "3.0.0" -source = "git+https://github.com/Pubky/pkarr?branch=v3#45e905d273982697e2be19ed738b390fc98a2195" +source = "git+https://github.com/Pubky/pkarr?branch=v3#7b3d13472639699a68416629e126de069836cc33" dependencies = [ "base32", "byteorder", @@ -1829,7 +1831,7 @@ dependencies = [ "serde", "sha1_smol", "simple-dns", - "thiserror 1.0.69", + "thiserror 2.0.3", "tokio", "tracing", "wasm-bindgen-futures", @@ -2539,9 +2541,9 @@ dependencies = [ [[package]] name = "simple-dns" -version = "0.6.2" +version = "0.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "01607fe2e61894468c6dc0b26103abb073fb08b79a3d9e4b6d76a1a341549958" +checksum = "b8f1740a36513fc97c5309eb1b8e8f108b0e95899c66c23fd7259625d4fdb686" dependencies = [ "bitflags", ] @@ -2976,9 +2978,9 @@ checksum = "8df9b6e13f2d32c91b9bd719c00d1958837bc7dec474d94952798cc8e69eeec3" [[package]] name = "tracing" -version = "0.1.40" +version = "0.1.41" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef" +checksum = "784e0ac535deb450455cbfa28a6f0df145ea1bb7ae51b821cf5e7927fdcfbdd0" dependencies = [ "log", "pin-project-lite", @@ -2988,9 +2990,9 @@ dependencies = [ [[package]] name = "tracing-attributes" -version = "0.1.27" +version = "0.1.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" +checksum = "395ae124c09f9e6918a2310af6038fba074bcf474ac352496d5910dd59a2226d" dependencies = [ "proc-macro2", "quote", @@ -2999,9 +3001,9 @@ dependencies = [ [[package]] name = "tracing-core" -version = "0.1.32" +version = "0.1.33" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54" +checksum = "e672c95779cf947c5311f83787af4fa8fffd12fb27e4993211a84bdfd9610f9c" dependencies = [ "once_cell", "valuable", @@ -3134,9 +3136,9 @@ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" [[package]] name = "wasm-bindgen" -version = "0.2.95" +version = "0.2.97" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "128d1e363af62632b8eb57219c8fd7877144af57558fb2ef0368d0087bddeb2e" +checksum = "d15e63b4482863c109d70a7b8706c1e364eb6ea449b201a76c5b89cedcec2d5c" dependencies = [ "cfg-if", "once_cell", @@ -3145,9 +3147,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-backend" -version = "0.2.95" +version = "0.2.97" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cb6dd4d3ca0ddffd1dd1c9c04f94b868c37ff5fac97c30b97cff2d74fce3a358" +checksum = "8d36ef12e3aaca16ddd3f67922bc63e48e953f126de60bd33ccc0101ef9998cd" dependencies = [ "bumpalo", "log", @@ -3160,21 +3162,22 @@ dependencies = [ [[package]] name = "wasm-bindgen-futures" -version = "0.4.45" +version = "0.4.47" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cc7ec4f8827a71586374db3e87abdb5a2bb3a15afed140221307c3ec06b1f63b" +checksum = "9dfaf8f50e5f293737ee323940c7d8b08a66a95a419223d9f41610ca08b0833d" dependencies = [ "cfg-if", "js-sys", + "once_cell", "wasm-bindgen", "web-sys", ] [[package]] name = "wasm-bindgen-macro" -version = "0.2.95" +version = "0.2.97" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e79384be7f8f5a9dd5d7167216f022090cf1f9ec128e6e6a482a2cb5c5422c56" +checksum = "705440e08b42d3e4b36de7d66c944be628d579796b8090bfa3471478a2260051" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -3182,9 +3185,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.95" +version = "0.2.97" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26c6ab57572f7a24a4985830b120de1594465e5d500f24afe89e16b4e833ef68" +checksum = "98c9ae5a76e46f4deecd0f0255cc223cfa18dc9b261213b8aa0c7b36f61b3f1d" dependencies = [ "proc-macro2", "quote", @@ -3195,15 +3198,15 @@ dependencies = [ [[package]] name = "wasm-bindgen-shared" -version = "0.2.95" +version = "0.2.97" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "65fc09f10666a9f147042251e0dda9c18f166ff7de300607007e96bdebc1068d" +checksum = "6ee99da9c5ba11bd675621338ef6fa52296b76b83305e9b6e5c77d4c286d6d49" [[package]] name = "web-sys" -version = "0.3.72" +version = "0.3.74" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f6488b90108c040df0fe62fa815cbdee25124641df01814dd7282749234c6112" +checksum = "a98bc3c33f0fe7e59ad7cd041b89034fa82a7c2d4365ca538dda6cdaf513863c" dependencies = [ "js-sys", "wasm-bindgen", diff --git a/pubky-homeserver/src/core.rs b/pubky-homeserver/src/core.rs index a1c2745..7523fc2 100644 --- a/pubky-homeserver/src/core.rs +++ b/pubky-homeserver/src/core.rs @@ -1,5 +1,3 @@ -use std::net::{SocketAddr, TcpListener}; - use anyhow::Result; use axum::{extract::Request, response::Response, Router}; use pkarr::PublicKey; @@ -50,16 +48,12 @@ impl HomeserverCore { .dht_settings(dht_settings) .build()?; - let listener = TcpListener::bind(SocketAddr::from(([0, 0, 0, 0], config.port())))?; - - let port = listener.local_addr()?.port(); - let state = AppState { verifier: AuthVerifier::default(), db, pkarr_client: pkarr_client.clone(), config: config.clone(), - port, + port: config.port(), }; let router = crate::routes::create_app(state.clone()); diff --git a/pubky-homeserver/src/pkarr.rs b/pubky-homeserver/src/pkarr.rs index 26c6b66..6c777d7 100644 --- a/pubky-homeserver/src/pkarr.rs +++ b/pubky-homeserver/src/pkarr.rs @@ -1,14 +1,6 @@ //! Pkarr related task -use std::net::Ipv4Addr; - -use pkarr::{ - dns::{ - rdata::{RData, A, SVCB}, - Packet, - }, - SignedPacket, -}; +use pkarr::{dns::rdata::SVCB, SignedPacket}; use crate::config::Config; @@ -19,8 +11,6 @@ pub async fn publish_server_packet( ) -> anyhow::Result<()> { // TODO: Try to resolve first before publishing. - let mut packet = Packet::new_reply(0); - let default = ".".to_string(); let target = config.domain().unwrap_or(&default); let mut svcb = SVCB::new(0, target.as_str().try_into()?); @@ -28,36 +18,26 @@ pub async fn publish_server_packet( svcb.priority = 1; svcb.set_port(port); - packet.answers.push(pkarr::dns::ResourceRecord::new( - "@".try_into().unwrap(), - pkarr::dns::CLASS::IN, - 60 * 60, - RData::HTTPS(svcb.clone().into()), - )); + let mut signed_packet_builder = + SignedPacket::builder().https(".".try_into().unwrap(), svcb.clone(), 60 * 60); if config.domain().is_none() { // TODO: remove after remvoing Pubky shared/public // and add local host IP address instead. svcb.target = "localhost".try_into().unwrap(); - packet.answers.push(pkarr::dns::ResourceRecord::new( - "@".try_into().unwrap(), - pkarr::dns::CLASS::IN, - 60 * 60, - RData::HTTPS(svcb.clone().into()), - )); - - packet.answers.push(pkarr::dns::ResourceRecord::new( - "@".try_into().unwrap(), - pkarr::dns::CLASS::IN, - 60 * 60, - RData::A(A::from(Ipv4Addr::from([127, 0, 0, 1]))), - )); + signed_packet_builder = signed_packet_builder + .https(".".try_into().unwrap(), svcb, 60 * 60) + .address( + ".".try_into().unwrap(), + "127.0.0.1".parse().unwrap(), + 60 * 60, + ); } // TODO: announce A/AAAA records as well for TLS connections? - let signed_packet = SignedPacket::from_packet(config.keypair(), &packet)?; + let signed_packet = signed_packet_builder.build(config.keypair())?; pkarr_client.publish(&signed_packet).await?; diff --git a/pubky-homeserver/src/server.rs b/pubky-homeserver/src/server.rs index 68d29e9..2d2d58d 100644 --- a/pubky-homeserver/src/server.rs +++ b/pubky-homeserver/src/server.rs @@ -33,7 +33,10 @@ impl Homeserver { let keypair = config.keypair().clone(); - let core = HomeserverCore::new(&config)?; + let mut core = HomeserverCore::new(&config)?; + + // Update the port. + core.state.port = port; let acceptor = RustlsAcceptor::new(RustlsConfig::from_config(Arc::new( keypair.to_rpk_rustls_server_config(), diff --git a/pubky/Cargo.toml b/pubky/Cargo.toml index dd56d49..91c038b 100644 --- a/pubky/Cargo.toml +++ b/pubky/Cargo.toml @@ -22,6 +22,7 @@ pubky-common = { version = "0.1.0", path = "../pubky-common" } cookie = "0.18.1" tracing = "0.1.40" cookie_store = { version = "0.21.1", default-features = false } +anyhow = "1.0.93" # Native dependencies [target.'cfg(not(target_arch = "wasm32"))'.dependencies] diff --git a/pubky/src/error.rs b/pubky/src/error.rs index 0704065..ec72e51 100644 --- a/pubky/src/error.rs +++ b/pubky/src/error.rs @@ -1,67 +1,5 @@ //! Main Crate Error -use pkarr::dns::SimpleDnsError; - +// TODO: remove anyhow // Alias Result to be the crate Result. -pub type Result = core::result::Result; - -#[derive(thiserror::Error, Debug)] -/// Pubky crate's common Error enum -pub enum Error { - /// For starter, to remove as code matures. - #[error("Generic error: {0}")] - Generic(String), - - #[error("Could not convert the passed type into a Url")] - InvalidUrl, - - // === Transparent === - #[error(transparent)] - Dns(#[from] SimpleDnsError), - - #[error(transparent)] - PublicKeyError(#[from] pkarr::errors::PublicKeyError), - - #[cfg(not(target_arch = "wasm32"))] - #[error(transparent)] - PkarrPublishError(#[from] pkarr::errors::PublishError), - - #[error(transparent)] - SignedPacketError(#[from] pkarr::errors::SignedPacketError), - - #[cfg(not(target_arch = "wasm32"))] - #[error(transparent)] - PkarrClientWasShutdown(#[from] pkarr::errors::ClientWasShutdown), - - #[error(transparent)] - Url(#[from] url::ParseError), - - #[error(transparent)] - Reqwest(#[from] reqwest::Error), - - #[error(transparent)] - Session(#[from] pubky_common::session::Error), - - #[error(transparent)] - Crypto(#[from] pubky_common::crypto::Error), - - #[error(transparent)] - RecoveryFile(#[from] pubky_common::recovery_file::Error), - - #[error(transparent)] - AuthToken(#[from] pubky_common::auth::Error), - - #[error("Could not resolve Endpoint for domain: {0}")] - ResolveEndpoint(String), -} - -#[cfg(target_arch = "wasm32")] -use wasm_bindgen::JsValue; - -#[cfg(target_arch = "wasm32")] -impl From for JsValue { - fn from(error: Error) -> JsValue { - let error_message = error.to_string(); - js_sys::Error::new(&error_message).into() - } -} +pub type Result = anyhow::Result; diff --git a/pubky/src/lib.rs b/pubky/src/lib.rs index 04b1b3c..256c787 100644 --- a/pubky/src/lib.rs +++ b/pubky/src/lib.rs @@ -10,12 +10,10 @@ mod native; #[cfg(target_arch = "wasm32")] mod wasm; -use std::{fmt::Debug, sync::Arc}; +use std::fmt::Debug; use wasm_bindgen::prelude::*; -pub use error::Error; - #[cfg(not(target_arch = "wasm32"))] pub use crate::shared::list_builder::ListBuilder; @@ -27,7 +25,7 @@ pub struct Client { pkarr: pkarr::Client, #[cfg(not(target_arch = "wasm32"))] - cookie_store: Arc, + cookie_store: std::sync::Arc, #[cfg(not(target_arch = "wasm32"))] icann_http: reqwest::Client, } diff --git a/pubky/src/native/api/auth.rs b/pubky/src/native/api/auth.rs index bd74c40..8a9965f 100644 --- a/pubky/src/native/api/auth.rs +++ b/pubky/src/native/api/auth.rs @@ -7,7 +7,7 @@ use pkarr::PublicKey; use pubky_common::capabilities::Capabilities; -use crate::error::{Error, Result}; +use crate::error::Result; use crate::Client; impl Client { @@ -47,7 +47,7 @@ impl Client { ) -> Result<(Url, tokio::sync::oneshot::Receiver)> { let mut relay: Url = relay .try_into() - .map_err(|_| Error::Generic("Invalid relay Url".into()))?; + .map_err(|_| anyhow::anyhow!("Invalid relay Url"))?; let (pubkyauth_url, client_secret) = self.create_auth_request(&mut relay, capabilities)?; @@ -61,9 +61,9 @@ impl Client { .await?; tx.send(to_send) - .map_err(|_| Error::Generic("Failed to send the session after signing in with token, since the receiver is dropped".into()))?; + .map_err(|_| anyhow::anyhow!("Failed to send the session after signing in with token, since the receiver is dropped"))?; - Ok::<(), Error>(()) + Ok::<(), anyhow::Error>(()) }); Ok((pubkyauth_url, rx)) @@ -76,7 +76,9 @@ impl Client { keypair: &Keypair, pubkyauth_url: T, ) -> Result<()> { - let url: Url = pubkyauth_url.try_into().map_err(|_| Error::InvalidUrl)?; + let url: Url = pubkyauth_url + .try_into() + .map_err(|_| anyhow::anyhow!("Invalid Url"))?; self.inner_send_auth_token(keypair, url).await?; diff --git a/pubky/src/shared/auth.rs b/pubky/src/shared/auth.rs index 7b4bfcc..2484729 100644 --- a/pubky/src/shared/auth.rs +++ b/pubky/src/shared/auth.rs @@ -12,10 +12,7 @@ use pubky_common::{ session::Session, }; -use crate::{ - error::{Error, Result}, - Client, -}; +use crate::{error::Result, Client}; impl Client { /// Signup to a homeserver and update Pkarr accordingly. @@ -80,6 +77,7 @@ impl Client { .await? .error_for_status()?; + #[cfg(not(target_arch = "wasm32"))] self.cookie_store.delete_session_after_signout(pubky); Ok(()) @@ -179,7 +177,8 @@ impl Client { let mut segments = relay .path_segments_mut() - .map_err(|_| Error::Generic("Invalid relay".into()))?; + .map_err(|_| anyhow::anyhow!("Invalid relay"))?; + // remove trailing slash if any. segments.pop_if_empty(); let channel_id = &engine.encode(hash(&client_secret).as_bytes()); diff --git a/pubky/src/shared/pkarr.rs b/pubky/src/shared/pkarr.rs index b796597..f3697af 100644 --- a/pubky/src/shared/pkarr.rs +++ b/pubky/src/shared/pkarr.rs @@ -1,7 +1,4 @@ -use pkarr::{ - dns::{rdata::SVCB, Packet}, - Keypair, SignedPacket, -}; +use pkarr::{dns::rdata::SVCB, Keypair, SignedPacket}; use crate::{error::Result, Client}; @@ -12,29 +9,69 @@ impl Client { let existing = self.pkarr.resolve(&keypair.public_key()).await?; - let mut packet = Packet::new_reply(0); + let mut signed_packet_builder = SignedPacket::builder(); if let Some(existing) = existing { - for answer in existing.packet().answers.iter().cloned() { + for answer in existing.resource_records("_pubky") { if !answer.name.to_string().starts_with("_pubky") { - packet.answers.push(answer.into_owned()) + signed_packet_builder = signed_packet_builder.record(answer.to_owned()); } } } let svcb = SVCB::new(0, host.try_into()?); - packet.answers.push(pkarr::dns::ResourceRecord::new( - "_pubky".try_into().unwrap(), - pkarr::dns::CLASS::IN, - 60 * 60, - pkarr::dns::rdata::RData::HTTPS(svcb.into()), - )); - - let signed_packet = SignedPacket::from_packet(keypair, &packet)?; + let signed_packet = SignedPacket::builder() + .https("_pubky".try_into().unwrap(), svcb, 60 * 60) + .sign(keypair)?; self.pkarr.publish(&signed_packet).await?; Ok(()) } + + // pub(crate) resolve_icann_domain() { + // + // let original_url = url.as_str(); + // let mut url = Url::parse(original_url).expect("Invalid url in inner_request"); + // + // if url.scheme() == "pubky" { + // // TODO: use https for anything other than testnet + // url.set_scheme("http") + // .expect("couldn't replace pubky:// with http://"); + // url.set_host(Some(&format!("_pubky.{}", url.host_str().unwrap_or("")))) + // .expect("couldn't map pubk:// to https://_pubky."); + // } + // + // let qname = url.host_str().unwrap_or("").to_string(); + // + // if PublicKey::try_from(original_url).is_ok() { + // let mut stream = self.pkarr.resolve_https_endpoints(&qname); + // + // let mut so_far: Option = None; + // + // while let Some(endpoint) = stream.next().await { + // if let Some(ref e) = so_far { + // if e.domain() == "." && endpoint.domain() != "." { + // so_far = Some(endpoint); + // } + // } else { + // so_far = Some(endpoint) + // } + // } + // + // if let Some(e) = so_far { + // url.set_host(Some(e.domain())) + // .expect("coultdn't use the resolved endpoint's domain"); + // url.set_port(Some(e.port())) + // .expect("coultdn't use the resolved endpoint's port"); + // + // return self.http.request(method, url).fetch_credentials_include(); + // } else { + // // TODO: didn't find any domain, what to do? + // } + // } + // + // self.http.request(method, url).fetch_credentials_include() + // } }