feat: use reserved HTTP_PORT service param for testnet

This commit is contained in:
nazeh
2025-01-08 13:37:37 +03:00
parent dc01377d09
commit a14f8008b8
7 changed files with 39 additions and 10 deletions

6
Cargo.lock generated
View File

@@ -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",

View File

@@ -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;
}

View File

@@ -1,5 +1,6 @@
pub mod auth;
pub mod capabilities;
pub mod constants;
pub mod crypto;
pub mod namespaces;
pub mod recovery_file;

View File

@@ -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?;

View File

@@ -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);

View File

@@ -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://<pkarr public key>` to `http://<pkarr public key` instead of `https:`
/// and read the homeserver HTTP port from the [reserved service parameter key](pubky_common::constants::reserved_param_keys::HTTP_PORT)
#[wasm_bindgen]
pub fn testnet() -> Self {
Self {

View File

@@ -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 {