Skip to content

Commit

Permalink
feat(pubky): use resolve_endpoint from pkarr
Browse files Browse the repository at this point in the history
  • Loading branch information
Nuhvi committed Sep 25, 2024
1 parent 9fd8501 commit 2ae5435
Show file tree
Hide file tree
Showing 15 changed files with 141 additions and 548 deletions.
81 changes: 24 additions & 57 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,3 @@ serde = { version = "^1.0.209", features = ["derive"] }
[profile.release]
lto = true
opt-level = 'z'

[patch.crates-io]
hyper-util = { git = "https://github.com/hyperium/hyper-util.git" }
14 changes: 12 additions & 2 deletions pubky/pkg/test/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,20 @@ const TLD = '8pinxxgqs41n4aididenw5apqp1urfmzdztr8jt4abrkdn435ewo';

// TODO: test HTTPs too somehow.

test.skip("basic fetch", async (t) => {
test("basic fetch", async (t) => {
let client = PubkyClient.testnet();

let response = await client.fetch(`http://${TLD}/`, new Uint8Array([]));
// Normal TLD
{

let response = await client.fetch(`http://relay.pkarr.org/`);

t.equal(response.status, 200);
}


// Pubky
let response = await client.fetch(`http://${TLD}/`);

t.equal(response.status, 200);
})
Expand Down
4 changes: 2 additions & 2 deletions pubky/src/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::PubkyClient;
mod api;
mod internals;

use internals::resolver::PkarrResolver;
use internals::PkarrResolver;

static DEFAULT_USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"),);

Expand Down Expand Up @@ -55,7 +55,7 @@ impl PubkyClientBuilder {
// TODO: convert to Result<PubkyClient>

let pkarr = pkarr::Client::new(self.pkarr_settings).unwrap();
let dns_resolver: PkarrResolver = pkarr.clone().into();
let dns_resolver: PkarrResolver = (&pkarr).into();

PubkyClient {
http: reqwest::Client::builder()
Expand Down
19 changes: 18 additions & 1 deletion pubky/src/native/api/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ mod tests {
use crate::*;

#[tokio::test]
async fn http_get() {
async fn http_get_pubky() {
let testnet = Testnet::new(10);

let homeserver = Homeserver::start_test(&testnet).await.unwrap();
Expand All @@ -45,4 +45,21 @@ mod tests {

assert_eq!(response.status(), 200)
}

#[tokio::test]
async fn http_get_icann() {
let testnet = Testnet::new(10);

let client = PubkyClient::builder().testnet(&testnet).build();

let url = format!("http://example.com/");

let response = client
.request(Default::default(), url)
.send()
.await
.unwrap();

assert_eq!(response.status(), 200);
}
}
2 changes: 0 additions & 2 deletions pubky/src/native/api.rs → pubky/src/native/api/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//! Public API modules
pub mod http;
pub mod recovery_file;

Expand Down
36 changes: 33 additions & 3 deletions pubky/src/native/internals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,43 @@ use url::Url;

use crate::PubkyClient;

mod endpoints;
pub mod resolver;
use std::net::ToSocketAddrs;

use pkarr::{Client, EndpointResolver, PublicKey};
use reqwest::dns::{Addrs, Resolve};

pub struct PkarrResolver(Client);

impl Resolve for PkarrResolver {
fn resolve(&self, name: reqwest::dns::Name) -> reqwest::dns::Resolving {
let client = self.0.clone();

Box::pin(async move {
let name = name.as_str();

if PublicKey::try_from(name).is_ok() {
let endpoint = client.resolve_endpoint(name).await?;

let addrs: Addrs = Box::new(endpoint.to_socket_addrs().into_iter());
return Ok(addrs);
};

Ok(Box::new(format!("{name}:0").to_socket_addrs().unwrap()))
})
}
}

impl From<&pkarr::Client> for PkarrResolver {
fn from(pkarr: &pkarr::Client) -> Self {
PkarrResolver(pkarr.clone())
}
}

impl PubkyClient {
// === HTTP ===

pub(crate) fn inner_request(&self, method: reqwest::Method, url: Url) -> RequestBuilder {
/// A wrapper around [reqwest::Client::request], with the same signature between native and wasm.
pub(crate) async fn inner_request(&self, method: reqwest::Method, url: Url) -> RequestBuilder {
self.http.request(method, url)
}
}
Loading

0 comments on commit 2ae5435

Please sign in to comment.