From a14f8008b890ea9767d931c762fa4502382170fe Mon Sep 17 00:00:00 2001 From: nazeh Date: Wed, 8 Jan 2025 13:37:37 +0300 Subject: [PATCH] feat: use reserved HTTP_PORT service param for testnet --- Cargo.lock | 6 +++--- pubky-common/src/constants.rs | 9 +++++++++ pubky-common/src/lib.rs | 1 + pubky-homeserver/src/io/mod.rs | 10 +++++----- pubky-homeserver/src/io/pkarr.rs | 7 +++++++ pubky/src/wasm.rs | 2 ++ pubky/src/wasm/http.rs | 14 ++++++++++++-- 7 files changed, 39 insertions(+), 10 deletions(-) create mode 100644 pubky-common/src/constants.rs diff --git a/Cargo.lock b/Cargo.lock index cbfaef8..af098ab 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1593,7 +1593,7 @@ checksum = "234cf4f4a04dc1f57e24b96cc0cd600cf2af460d4161ac5ecdd0af8e1f3b2a38" [[package]] name = "mainline" version = "4.2.0" -source = "git+https://github.com/Pubky/mainline?branch=v5-rc1#f90c68499f26341decad14f9c106687dbe7e440f" +source = "git+https://github.com/Pubky/mainline?branch=v5-rc1#9899b4dd582d53d75f7ada7bde3c345d21fe4f29" dependencies = [ "crc", "document-features", @@ -1937,7 +1937,7 @@ checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" [[package]] name = "pkarr" version = "3.0.0" -source = "git+https://github.com/Pubky/pkarr?branch=v3-rc1#0514b1edc9d4f049adbb407389d79cecd90aa86a" +source = "git+https://github.com/Pubky/pkarr?branch=v3-rc1#116bd22eb2916cff0cb84cd49d840e83094af65b" dependencies = [ "base32", "byteorder", @@ -1974,7 +1974,7 @@ dependencies = [ [[package]] name = "pkarr-relay" version = "0.1.0" -source = "git+https://github.com/Pubky/pkarr?branch=v3-rc1#0514b1edc9d4f049adbb407389d79cecd90aa86a" +source = "git+https://github.com/Pubky/pkarr?branch=v3-rc1#116bd22eb2916cff0cb84cd49d840e83094af65b" dependencies = [ "anyhow", "axum", diff --git a/pubky-common/src/constants.rs b/pubky-common/src/constants.rs new file mode 100644 index 0000000..e7f1210 --- /dev/null +++ b/pubky-common/src/constants.rs @@ -0,0 +1,9 @@ +/// [Reserved param keys](https://www.rfc-editor.org/rfc/rfc9460#name-initial-contents) for HTTPS Resource Records +pub mod reserved_param_keys { + pub const HTTP_PORT: u16 = 65280; +} + +pub mod testnet_ports { + pub const PKARR_RELAY: u16 = 15411; + pub const HTTP_RELAY: u16 = 15412; +} diff --git a/pubky-common/src/lib.rs b/pubky-common/src/lib.rs index b19a187..e71fb7c 100644 --- a/pubky-common/src/lib.rs +++ b/pubky-common/src/lib.rs @@ -1,5 +1,6 @@ pub mod auth; pub mod capabilities; +pub mod constants; pub mod crypto; pub mod namespaces; pub mod recovery_file; diff --git a/pubky-homeserver/src/io/mod.rs b/pubky-homeserver/src/io/mod.rs index 1a5598d..7b00bc6 100644 --- a/pubky-homeserver/src/io/mod.rs +++ b/pubky-homeserver/src/io/mod.rs @@ -98,10 +98,10 @@ impl Homeserver { /// Start a homeserver in a Testnet mode. /// /// - Homeserver address is hardcoded to `8pinxxgqs41n4aididenw5apqp1urfmzdztr8jt4abrkdn435ewo` - /// - Run a pkarr Relay on port `15411` + /// - Run a pkarr Relay on port [15411](pubky_common::constants::testnet_ports::PKARR_RELAY) /// - Use a temporary storage for the both homeserver and relay - /// - Only publish http port (ignore https port or domain configurations) - /// - Run an HTTP relay on port `15412` + /// - Publish http port on a [reserved service parameter key](pubky_common::constants::reserved_param_keys::HTTP_PORT) + /// - Run an HTTP relay on port [15412](pubky_common::constants::testnet_ports::HTTP_RELAY) /// /// # Safety /// See [Self::start] @@ -113,7 +113,7 @@ impl Homeserver { let pkarr_relay = unsafe { let mut config = pkarr_relay::Config { - http_port: 15411, + http_port: pubky_common::constants::testnet_ports::PKARR_RELAY, cache_path: Some(storage.join("pkarr-relay")), rate_limiter: None, ..Default::default() @@ -126,7 +126,7 @@ impl Homeserver { }; let http_relay = http_relay::HttpRelay::builder() - .http_port(15412) + .http_port(pubky_common::constants::testnet_ports::HTTP_RELAY) .build() .await?; diff --git a/pubky-homeserver/src/io/pkarr.rs b/pubky-homeserver/src/io/pkarr.rs index 158c809..296649d 100644 --- a/pubky-homeserver/src/io/pkarr.rs +++ b/pubky-homeserver/src/io/pkarr.rs @@ -51,6 +51,13 @@ pub fn create_signed_packet(config: &Config, port: u16, http_port: u16) -> Resul svcb.priority = 1; svcb.set_port(port); + let http_port_be_bytes = http_port.to_be_bytes(); + + svcb.set_param( + pubky_common::constants::reserved_param_keys::HTTP_PORT, + &http_port_be_bytes, + )?; + let mut signed_packet_builder = SignedPacket::builder().https(".".try_into().unwrap(), svcb.clone(), 60 * 60); diff --git a/pubky/src/wasm.rs b/pubky/src/wasm.rs index 5f364d9..2dfcb22 100644 --- a/pubky/src/wasm.rs +++ b/pubky/src/wasm.rs @@ -28,6 +28,8 @@ impl Client { /// Create a client with with configurations appropriate for local testing: /// - set Pkarr relays to `["http://localhost:15411"]` instead of default relay. + /// - transform `pubky://` to `http:// Self { Self { diff --git a/pubky/src/wasm/http.rs b/pubky/src/wasm/http.rs index 7e5a261..eea495d 100644 --- a/pubky/src/wasm/http.rs +++ b/pubky/src/wasm/http.rs @@ -140,12 +140,22 @@ impl Client { if self.testnet && e.domain() == "localhost" { url.set_scheme("http") .expect("couldn't replace pubky:// with http://"); + + let http_port = e + .get_param(pubky_common::constants::reserved_param_keys::HTTP_PORT) + .and_then(|x| <[u8; 2]>::try_from(x).ok()) + .map(|x| u16::from_be_bytes(x)) + .expect("could not find HTTP_PORT service param"); + + url.set_port(Some(http_port)) + .expect("coultdn't use the resolved endpoint's port"); + } else { + url.set_port(Some(e.port())) + .expect("coultdn't use the resolved endpoint's port"); } 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"); log::debug!("Transformed URL to: {}", url.as_str()); } else {