feat: update pkarr and mainline and add pubky ClientBuilder

This commit is contained in:
nazeh
2025-02-04 21:56:23 +03:00
parent 7ed43f0fba
commit 8be809c159
14 changed files with 253 additions and 173 deletions

288
Cargo.lock generated
View File

@@ -122,6 +122,19 @@ version = "0.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50"
[[package]]
name = "async-compat"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7bab94bde396a3f7b4962e396fdad640e241ed797d4d8d77fc8c237d14c58fc0"
dependencies = [
"futures-core",
"futures-io",
"once_cell",
"pin-project-lite",
"tokio",
]
[[package]]
name = "async-trait"
version = "0.1.85"
@@ -177,7 +190,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "edca88bc138befd0323b20752846e6587272d3b03b0343c8ea28a6f819e6e71f"
dependencies = [
"async-trait",
"axum-core",
"axum-core 0.4.5",
"axum-macros",
"bytes",
"futures-util",
@@ -187,7 +200,41 @@ dependencies = [
"hyper",
"hyper-util",
"itoa",
"matchit",
"matchit 0.7.3",
"memchr",
"mime",
"percent-encoding",
"pin-project-lite",
"rustversion",
"serde",
"serde_json",
"serde_path_to_error",
"serde_urlencoded",
"sync_wrapper",
"tokio",
"tower 0.5.2",
"tower-layer",
"tower-service",
"tracing",
]
[[package]]
name = "axum"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d6fd624c75e18b3b4c6b9caf42b1afe24437daaee904069137d8bab077be8b8"
dependencies = [
"axum-core 0.5.0",
"bytes",
"form_urlencoded",
"futures-util",
"http",
"http-body",
"http-body-util",
"hyper",
"hyper-util",
"itoa",
"matchit 0.8.4",
"memchr",
"mime",
"percent-encoding",
@@ -226,14 +273,34 @@ dependencies = [
"tracing",
]
[[package]]
name = "axum-core"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df1362f362fd16024ae199c1970ce98f9661bf5ef94b9808fee734bc3698b733"
dependencies = [
"bytes",
"futures-util",
"http",
"http-body",
"http-body-util",
"mime",
"pin-project-lite",
"rustversion",
"sync_wrapper",
"tower-layer",
"tower-service",
"tracing",
]
[[package]]
name = "axum-extra"
version = "0.9.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c794b30c904f0a1c2fb7740f7df7f7972dfaa14ef6f57cb6178dc63e5dca2f04"
dependencies = [
"axum",
"axum-core",
"axum 0.7.9",
"axum-core 0.4.5",
"bytes",
"fastrand",
"futures-util",
@@ -427,9 +494,9 @@ dependencies = [
[[package]]
name = "clap"
version = "4.5.26"
version = "4.5.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a8eb5e908ef3a6efbe1ed62520fb7287959888c88485abe072543190ecc66783"
checksum = "3e77c3243bd94243c03672cb5154667347c457ca271254724f9f393aee1c05ff"
dependencies = [
"clap_builder",
"clap_derive",
@@ -437,9 +504,9 @@ dependencies = [
[[package]]
name = "clap_builder"
version = "4.5.26"
version = "4.5.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "96b01801b5fc6a0a232407abc821660c9c6d25a1cafc0d4f85f29fb8d9afc121"
checksum = "1b26884eb4b57140e4d2d93652abfa49498b938b3c9179f9fc487b0acc3edad7"
dependencies = [
"anstream",
"anstyle",
@@ -449,9 +516,9 @@ dependencies = [
[[package]]
name = "clap_derive"
version = "4.5.24"
version = "4.5.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "54b755194d6389280185988721fffba69495eed5ee9feeee9a599b53db80318c"
checksum = "bf4ced95c6f4a675af3da73304b9ac4ed991640c36374e4b46795c49e17cf1ed"
dependencies = [
"heck",
"proc-macro2",
@@ -529,6 +596,16 @@ dependencies = [
"url",
]
[[package]]
name = "cordyceps"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec10f0a762d93c4498d2e97a333805cb6250d60bead623f71d8034f9a4152ba3"
dependencies = [
"loom",
"tracing",
]
[[package]]
name = "core-foundation"
version = "0.9.4"
@@ -645,11 +722,12 @@ dependencies = [
[[package]]
name = "dashmap"
version = "5.5.3"
version = "6.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "978747c1d849a7d2ee5e8adc0159961c48fb7e5db2f06af6723b80123bb53856"
checksum = "5041cc499144891f3790297212f32a74fb938e5136a14943f338ef9e0ae276cf"
dependencies = [
"cfg-if",
"crossbeam-utils",
"hashbrown 0.14.5",
"lock_api",
"once_cell",
@@ -675,6 +753,12 @@ dependencies = [
"powerfmt",
]
[[package]]
name = "diatomic-waker"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ab03c107fafeb3ee9f5925686dbb7a73bc76e3932abb0d2b365cb64b169cf04c"
[[package]]
name = "digest"
version = "0.10.7"
@@ -870,18 +954,15 @@ dependencies = [
]
[[package]]
name = "futures"
version = "0.3.31"
name = "futures-buffered"
version = "0.2.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "65bc07b1a8bc7c85c5f2e110c476c7389b4554ba72af57d8445ea63a576b0876"
checksum = "34acda8ae8b63fbe0b2195c998b180cff89a8212fb2622a78b572a9f1c6f7684"
dependencies = [
"futures-channel",
"cordyceps",
"diatomic-waker",
"futures-core",
"futures-executor",
"futures-io",
"futures-sink",
"futures-task",
"futures-util",
"pin-project-lite",
]
[[package]]
@@ -891,7 +972,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dff15bf788c671c1934e366d07e30c1814a8ef514e1af724a602e8a2fbe1b10"
dependencies = [
"futures-core",
"futures-sink",
]
[[package]]
@@ -900,17 +980,6 @@ version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e"
[[package]]
name = "futures-executor"
version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e28d1d997f585e54aebc3f97d39e72338912123a67330d723fdbb564d646c9f"
dependencies = [
"futures-core",
"futures-task",
"futures-util",
]
[[package]]
name = "futures-io"
version = "0.3.31"
@@ -965,13 +1034,10 @@ version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81"
dependencies = [
"futures-channel",
"futures-core",
"futures-io",
"futures-macro",
"futures-sink",
"futures-task",
"memchr",
"pin-project-lite",
"pin-utils",
"slab",
@@ -993,6 +1059,19 @@ version = "0.99.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b32dfe1fdfc0bbde1f22a5da25355514b5e450c33a6af6770884c8750aedfbc"
[[package]]
name = "generator"
version = "0.7.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5cc16584ff22b460a382b7feec54b23d2908d858152e5739a120b949293bd74e"
dependencies = [
"cc",
"libc",
"log",
"rustversion",
"windows",
]
[[package]]
name = "generic-array"
version = "0.14.7"
@@ -1025,14 +1104,15 @@ checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f"
[[package]]
name = "governor"
version = "0.6.3"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "68a7f542ee6b35af73b06abc0dad1c1bae89964e4e253bc4b587b91c9637867b"
checksum = "842dc78579ce01e6a1576ad896edc92fca002dd60c9c3746b7fc2bec6fb429d0"
dependencies = [
"cfg-if",
"dashmap",
"futures",
"futures-sink",
"futures-timer",
"futures-util",
"no-std-compat",
"nonzero_ext",
"parking_lot",
@@ -1127,24 +1207,6 @@ version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]]
name = "heed"
version = "0.20.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7d4f449bab7320c56003d37732a917e18798e2f1709d80263face2b4f9436ddb"
dependencies = [
"bitflags",
"byteorder",
"heed-traits",
"heed-types 0.20.1",
"libc",
"lmdb-master-sys",
"once_cell",
"page_size",
"synchronoise",
"url",
]
[[package]]
name = "heed"
version = "0.21.0"
@@ -1154,7 +1216,7 @@ dependencies = [
"bitflags",
"byteorder",
"heed-traits",
"heed-types 0.21.0",
"heed-types",
"libc",
"lmdb-master-sys",
"once_cell",
@@ -1170,16 +1232,6 @@ version = "0.20.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eb3130048d404c57ce5a1ac61a903696e8fcde7e8c2991e9fcfc1f27c3ef74ff"
[[package]]
name = "heed-types"
version = "0.20.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9d3f528b053a6d700b2734eabcd0fd49cb8230647aa72958467527b0b7917114"
dependencies = [
"byteorder",
"heed-traits",
]
[[package]]
name = "heed-types"
version = "0.21.0"
@@ -1238,7 +1290,7 @@ name = "http-relay"
version = "0.1.0"
dependencies = [
"anyhow",
"axum",
"axum 0.7.9",
"axum-server",
"futures-util",
"tokio",
@@ -1585,6 +1637,19 @@ version = "0.4.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "04cbf5b083de1c7e0222a7a51dbfdba1cbe1c6ab0b15e29fff3f6c077fd9cd9f"
[[package]]
name = "loom"
version = "0.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ff50ecb28bb86013e935fb6683ab1f6d3a20016f123c76fd4c27470076ac30f5"
dependencies = [
"cfg-if",
"generator",
"scoped-tls",
"tracing",
"tracing-subscriber",
]
[[package]]
name = "lru"
version = "0.12.5"
@@ -1593,13 +1658,15 @@ checksum = "234cf4f4a04dc1f57e24b96cc0cd600cf2af460d4161ac5ecdd0af8e1f3b2a38"
[[package]]
name = "mainline"
version = "4.2.0"
source = "git+https://github.com/Pubky/mainline?branch=v5-rc1#9899b4dd582d53d75f7ada7bde3c345d21fe4f29"
version = "4.2.1"
source = "git+https://github.com/Pubky/mainline?branch=v5#e9dde7cc32a9f70dd9b9edbc92536d6cb88b5ed0"
dependencies = [
"crc",
"document-features",
"dyn-clone",
"ed25519-dalek",
"flume",
"futures-lite",
"lru",
"rand",
"serde",
@@ -1625,6 +1692,12 @@ version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0e7465ac9959cc2b1404e8e2367b43684a6d13790fe23056cc8c6c5a6b7bcb94"
[[package]]
name = "matchit"
version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "47e1ffaa40ddd1f3ed91f717a33c8c0ee23fff369e3aa8772b9605cc1d22f4c3"
[[package]]
name = "memchr"
version = "2.7.4"
@@ -1938,25 +2011,25 @@ checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
[[package]]
name = "pkarr"
version = "3.0.0"
source = "git+https://github.com/Pubky/pkarr?branch=v3-rc1#116bd22eb2916cff0cb84cd49d840e83094af65b"
source = "git+https://github.com/Pubky/pkarr?branch=v3#8e77df1c30f38d19fc6c5dd123d59f30b788cb75"
dependencies = [
"async-compat",
"base32",
"byteorder",
"bytes",
"document-features",
"dyn-clone",
"ed25519-dalek",
"flume",
"futures",
"futures-buffered",
"futures-lite",
"genawaiter",
"getrandom",
"heed 0.20.5",
"js-sys",
"heed",
"log",
"lru",
"mainline",
"once_cell",
"page_size",
"pubky-timestamp",
"pubky-timestamp 0.3.0",
"rand",
"reqwest",
"rustls",
@@ -1975,10 +2048,10 @@ dependencies = [
[[package]]
name = "pkarr-relay"
version = "0.1.0"
source = "git+https://github.com/Pubky/pkarr?branch=v3-rc1#116bd22eb2916cff0cb84cd49d840e83094af65b"
source = "git+https://github.com/Pubky/pkarr?branch=v3#8e77df1c30f38d19fc6c5dd123d59f30b788cb75"
dependencies = [
"anyhow",
"axum",
"axum 0.8.1",
"axum-server",
"bytes",
"clap",
@@ -1988,7 +2061,7 @@ dependencies = [
"httpdate",
"mainline",
"pkarr",
"pubky-timestamp",
"pubky-timestamp 0.3.0",
"rustls",
"serde",
"thiserror 2.0.11",
@@ -1998,6 +2071,7 @@ dependencies = [
"tower_governor",
"tracing",
"tracing-subscriber",
"url",
]
[[package]]
@@ -2081,7 +2155,7 @@ name = "pubky"
version = "0.3.0"
dependencies = [
"anyhow",
"axum",
"axum 0.7.9",
"axum-server",
"base64 0.22.1",
"bytes",
@@ -2120,7 +2194,7 @@ dependencies = [
"once_cell",
"pkarr",
"postcard",
"pubky-timestamp",
"pubky-timestamp 0.2.0",
"rand",
"serde",
"thiserror 2.0.11",
@@ -2131,7 +2205,7 @@ name = "pubky-homeserver"
version = "0.1.0"
dependencies = [
"anyhow",
"axum",
"axum 0.7.9",
"axum-extra",
"axum-server",
"base32",
@@ -2140,7 +2214,7 @@ dependencies = [
"dirs-next",
"flume",
"futures-util",
"heed 0.21.0",
"heed",
"hex",
"http-relay",
"httpdate",
@@ -2176,6 +2250,21 @@ dependencies = [
"serde",
]
[[package]]
name = "pubky-timestamp"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4869545866057b802cdfe69f7d6e3db0783741b74c46caa11434a82f523f61ca"
dependencies = [
"base32",
"document-features",
"getrandom",
"httpdate",
"js-sys",
"once_cell",
"serde",
]
[[package]]
name = "publicsuffix"
version = "2.3.0"
@@ -2559,6 +2648,12 @@ dependencies = [
"windows-sys 0.59.0",
]
[[package]]
name = "scoped-tls"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e1cf6437eb19a8f4a6cc0f7dca544973b0b78843adbfeb3683d1a94a0024a294"
[[package]]
name = "scopeguard"
version = "1.2.0"
@@ -2745,9 +2840,9 @@ dependencies = [
[[package]]
name = "simple-dns"
version = "0.9.2"
version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "84330be8d9f218c15b4583c74d809643fb82bdcbbd48302a36469ea5b63a1d69"
checksum = "dee851d0e5e7af3721faea1843e8015e820a234f81fda3dea9247e15bac9a86a"
dependencies = [
"bitflags",
]
@@ -3144,7 +3239,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4fd0118512cf0b3768f7fcccf0bef1ae41d68f2b45edc1e77432b36c97c56c6d"
dependencies = [
"async-trait",
"axum-core",
"axum-core 0.4.5",
"cookie",
"futures-util",
"http",
@@ -3184,16 +3279,16 @@ checksum = "8df9b6e13f2d32c91b9bd719c00d1958837bc7dec474d94952798cc8e69eeec3"
[[package]]
name = "tower_governor"
version = "0.4.3"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aea939ea6cfa7c4880f3e7422616624f97a567c16df67b53b11f0d03917a8e46"
checksum = "57a2ccff6830fa835371af7541e561a90e4c07b84f72991ebac4b3cb6790dc0d"
dependencies = [
"axum",
"axum 0.8.1",
"forwarded-header-value",
"governor",
"http",
"pin-project",
"thiserror 1.0.69",
"thiserror 2.0.11",
"tower 0.5.2",
"tracing",
]
@@ -3478,6 +3573,15 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "windows"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e686886bc078bc1b0b600cac0147aadb815089b6e4da64016cbd754b6342700f"
dependencies = [
"windows-targets 0.48.5",
]
[[package]]
name = "windows-registry"
version = "0.2.0"

View File

@@ -12,9 +12,10 @@ members = [
resolver = "2"
[workspace.dependencies]
pkarr = { git = "https://github.com/Pubky/pkarr", branch = "v3-rc1", package = "pkarr" }
pkarr-relay = { git = "https://github.com/Pubky/pkarr", branch = "v3-rc1", package = "pkarr-relay" }
mainline = { git = "https://github.com/Pubky/mainline", branch = "v5-rc1", package = "mainline" }
# Todo move to crates after release.
pkarr = { git = "https://github.com/Pubky/pkarr", branch = "v3", package = "pkarr" }
pkarr-relay = { git = "https://github.com/Pubky/pkarr", branch = "v3", package = "pkarr-relay" }
mainline = { git = "https://github.com/Pubky/mainline", branch = "v5", package = "mainline" }
[profile.release]
lto = true

View File

@@ -24,7 +24,7 @@ async fn main() -> Result<()> {
let homeserver = cli.homeserver;
let client = Client::new()?;
let client = Client::builder().build()?;
println!("Enter your recovery_file's passphrase to signup:");
let passphrase = rpassword::read_password()?;

View File

@@ -78,7 +78,7 @@ async fn main() -> Result<()> {
client
} else {
Client::new()?
Client::builder().build()?
};
println!("Sending AuthToken to the 3rd party app...");

View File

@@ -30,7 +30,7 @@ async fn main() -> Result<()> {
let client = if args.testnet {
Client::testnet()?
} else {
Client::new()?
Client::builder().build()?
};
// Build the request

View File

@@ -17,7 +17,7 @@ heed = "0.21.0"
hex = "0.4.3"
httpdate = "1.0.3"
postcard = { version = "1.1.1", features = ["alloc"] }
pkarr = { workspace = true }
pkarr = { workspace = true, features = ["dht", "lmdb-cache"] }
pubky-common = { version = "0.1.0", path = "../pubky-common" }
serde = { version = "1.0.216", features = ["derive"] }
tokio = { version = "1.42.0", features = ["full"] }

View File

@@ -90,12 +90,6 @@ impl From<pkarr::errors::PublishError> for Error {
}
}
impl From<pkarr::errors::ClientWasShutdown> for Error {
fn from(error: pkarr::errors::ClientWasShutdown) -> Self {
Self::new(StatusCode::BAD_REQUEST, Some(error))
}
}
impl From<pkarr::errors::PublicKeyError> for Error {
fn from(error: pkarr::errors::PublicKeyError) -> Self {
Self::new(StatusCode::BAD_REQUEST, Some(error))

View File

@@ -107,6 +107,7 @@ impl Homeserver {
/// See [Self::start]
pub async unsafe fn start_testnet() -> Result<Self> {
let testnet = mainline::Testnet::new(10)?;
testnet.leak();
let storage =
std::env::temp_dir().join(pubky_common::timestamp::Timestamp::now().to_string());
@@ -119,8 +120,8 @@ impl Homeserver {
..Default::default()
};
config.pkarr_config.dht_config.bootstrap = testnet.bootstrap.clone();
config.pkarr_config.resolvers = Some(vec![].into());
config.pkarr.bootstrap(&testnet.bootstrap);
config.pkarr.no_resolvers();
pkarr_relay::Relay::start(config).await?
};

View File

@@ -12,16 +12,20 @@ pub struct PkarrServer {
impl PkarrServer {
pub fn new(config: &Config, https_port: u16, http_port: u16) -> Result<Self> {
let mut dht_config = mainline::Config::default();
let mut builder = pkarr::Client::builder();
if let Some(bootstrap) = config.bootstrap.clone() {
dht_config.bootstrap = bootstrap;
// TODO: should we enable relays in homeservers for udp restricted environments?
builder.no_relays();
if let Some(bootstrap) = &config.bootstrap {
builder.bootstrap(bootstrap);
}
if let Some(request_timeout) = config.dht_request_timeout {
dht_config.request_timeout = request_timeout;
builder.request_timeout(request_timeout);
}
let client = pkarr::Client::builder().dht_config(dht_config).build()?;
let client = builder.build()?;
let signed_packet = create_signed_packet(config, https_port, http_port)?;
@@ -35,7 +39,7 @@ impl PkarrServer {
// TODO: warn if packet is not most recent, which means the
// user is publishing a Packet from somewhere else.
self.client.publish(&self.signed_packet).await?;
self.client.publish(&self.signed_packet, None).await?;
Ok(())
}

View File

@@ -5,8 +5,6 @@ import { Client, Keypair, PublicKey } from '../index.cjs'
const HOMESERVER_PUBLICKEY = PublicKey.from('8pinxxgqs41n4aididenw5apqp1urfmzdztr8jt4abrkdn435ewo')
const TESTNET_HTTP_RELAY = "http://localhost:15412/link";
// TODO: test multiple users in wasm
test('Auth: basic', async (t) => {
const client = Client.testnet();

View File

@@ -1,6 +1,6 @@
import test from 'tape'
import { Client, Keypair, PublicKey ,setLogLevel} from '../index.cjs'
import { Client, Keypair, PublicKey, setLogLevel } from '../index.cjs'
const HOMESERVER_PUBLICKEY = PublicKey.from('8pinxxgqs41n4aididenw5apqp1urfmzdztr8jt4abrkdn435ewo')
@@ -129,8 +129,6 @@ test("list", async (t) => {
await client.signup(keypair, HOMESERVER_PUBLICKEY)
let urls = [
`pubky://${pubky}/pub/a.wrong/a.txt`,
`pubky://${pubky}/pub/example.com/a.txt`,

View File

@@ -1,8 +1,4 @@
use std::{
net::{SocketAddr, ToSocketAddrs},
sync::Arc,
time::Duration,
};
use std::{sync::Arc, time::Duration};
use mainline::Testnet;
@@ -17,18 +13,11 @@ pub(crate) use cookies::CookieJar;
static DEFAULT_USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"),);
#[derive(Debug, Default)]
pub struct Settings {
pkarr_config: pkarr::Config,
pub struct ClientBuilder {
pkarr: pkarr::ClientBuilder,
}
impl Settings {
/// Set Pkarr client [pkarr::Settings].
pub fn pkarr_config(mut self, settings: pkarr::Config) -> Self {
self.pkarr_config = settings;
self
}
impl ClientBuilder {
/// Sets the following:
/// - Pkarr client's DHT bootstrap nodes = `testnet` bootstrap nodes.
/// - Pkarr client's resolvers = `testnet` bootstrap nodes.
@@ -36,34 +25,19 @@ impl Settings {
pub fn testnet(mut self, testnet: &Testnet) -> Self {
let bootstrap = testnet.bootstrap.clone();
self.pkarr_config.resolvers = Some(
bootstrap
.iter()
.flat_map(|resolver| {
resolver.to_socket_addrs().map(|iter| {
iter.filter_map(|a| match a {
SocketAddr::V4(a) => Some(a),
_ => None,
})
})
})
.flatten()
.collect::<Vec<_>>()
.into(),
);
self.pkarr_config.dht_config.bootstrap = bootstrap;
self.pkarr.resolvers(&bootstrap);
self.pkarr.bootstrap(&bootstrap);
if std::env::var("CI").is_err() {
self.pkarr_config.dht_config.request_timeout = Duration::from_millis(500);
self.pkarr.request_timeout(Duration::from_millis(500));
}
self
}
/// Build [Client]
pub fn build(self) -> Result<Client, std::io::Error> {
let pkarr = pkarr::Client::new(self.pkarr_config)?;
pub fn build(self) -> Result<Client, BuildError> {
let pkarr = self.pkarr.build()?;
let cookie_store = Arc::new(CookieJar::default());
@@ -93,19 +67,14 @@ impl Settings {
}
impl Client {
/// Create a new [Client] with default [Settings]
pub fn new() -> Result<Self, std::io::Error> {
Self::builder().build()
}
/// Returns a builder to edit settings before creating [Client].
pub fn builder() -> Settings {
Settings::default()
pub fn builder() -> ClientBuilder {
ClientBuilder::default()
}
/// Create a client connected to the local network
/// with the bootstrapping node: `localhost:6881`
pub fn testnet() -> Result<Self, std::io::Error> {
pub fn testnet() -> Result<Self, BuildError> {
Self::builder()
.testnet(&Testnet {
bootstrap: vec!["localhost:6881".to_string()],
@@ -120,3 +89,10 @@ impl Client {
Client::builder().testnet(testnet).build().unwrap()
}
}
#[derive(Debug, thiserror::Error)]
pub enum BuildError {
#[error(transparent)]
/// Error building Pkarr client.
PkarrBuildError(#[from] pkarr::errors::BuildError),
}

View File

@@ -9,11 +9,11 @@ impl Client {
pub(crate) async fn publish_homeserver(&self, keypair: &Keypair, host: &str) -> Result<()> {
// TODO: Before making public, consider the effect on other records and other mirrors
let existing = self.pkarr.resolve(&keypair.public_key()).await?;
let existing = self.pkarr.resolve_most_recent(&keypair.public_key()).await;
let mut signed_packet_builder = SignedPacket::builder();
if let Some(existing) = existing {
if let Some(ref existing) = existing {
for answer in existing.resource_records("_pubky") {
if !answer.name.to_string().starts_with("_pubky") {
signed_packet_builder = signed_packet_builder.record(answer.to_owned());
@@ -27,7 +27,9 @@ impl Client {
.https("_pubky".try_into().unwrap(), svcb, 60 * 60)
.sign(keypair)?;
self.pkarr.publish(&signed_packet).await?;
self.pkarr
.publish(&signed_packet, existing.map(|s| s.timestamp()))
.await?;
Ok(())
}

View File

@@ -8,7 +8,7 @@ use reqwest::{IntoUrl, Method, RequestBuilder, Url};
use futures_lite::StreamExt;
use pkarr::extra::endpoints::{Endpoint, EndpointsResolver};
use pkarr::extra::endpoints::Endpoint;
use pkarr::PublicKey;
use crate::Client;
@@ -126,7 +126,7 @@ impl Client {
let mut so_far: Option<Endpoint> = None;
while let Some(endpoint) = stream.next().await {
if endpoint.domain() != "." {
if let Some(domain) = endpoint.domain() {
so_far = Some(endpoint);
// TODO: currently we return the first thing we can see,
@@ -137,7 +137,7 @@ impl Client {
if let Some(e) = so_far {
// TODO: detect loopback IPs and other equivilants to localhost
if self.testnet && e.domain() == "localhost" {
if self.testnet && e.domain() == Some("localhost") {
url.set_scheme("http")
.expect("couldn't replace pubky:// with http://");
@@ -150,20 +150,22 @@ impl Client {
url.set_port(Some(http_port))
.expect("coultdn't use the resolved endpoint's port");
} else {
// TODO: where does port zero come from?
if e.port() != 0 {
url.set_port(Some(e.port()))
if let Some(port) = e.port() {
url.set_port(Some(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");
if let Some(domain) = e.domain() {
url.set_host(Some(domain))
.expect("coultdn't use the resolved endpoint's domain");
}
log::debug!("Transformed URL to: {}", url.as_str());
} else {
// TODO: didn't find any domain, what to do?
log::debug!("Could not resolve Pubky URL to clearnet {}", url.as_str());
// return an error.
log::debug!("Could not resolve host: {}", qname);
}
}
}