Skip to content

Commit

Permalink
fix(pubky): recursive resolution of homeserver
Browse files Browse the repository at this point in the history
  • Loading branch information
Nuhvi committed Jul 30, 2024
1 parent 7e451a5 commit e9bb104
Showing 1 changed file with 30 additions and 19 deletions.
49 changes: 30 additions & 19 deletions pubky/src/shared/pkarr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,39 +65,50 @@ impl PubkyClient {
// TODO: cache the result of this function?

let mut target = target.to_string();

let mut homeserver_public_key = None;
let mut host = target.clone();

let mut step = 0;

// PublicKey is very good at extracting the Pkarr TLD from a string.
while let Ok(public_key) = PublicKey::try_from(target.clone()) {
if step >= MAX_RECURSIVE_PUBKY_HOMESERVER_RESOLUTION {
break;
};

step += 1;

let response = self
if let Some(signed_packet) = self
.pkarr_resolve(&public_key)
.await
.map_err(|_| Error::ResolveEndpoint(original_target.into()))?;

let mut prior = None;

if let Some(signed_packet) = response {
for answer in signed_packet.resource_records(&target) {
if let pkarr::dns::rdata::RData::SVCB(svcb) = &answer.rdata {
if svcb.priority == 0 {
prior = Some(svcb)
} else if let Some(sofar) = prior {
if svcb.priority >= sofar.priority {
prior = Some(svcb)
.map_err(|_| Error::ResolveEndpoint(original_target.into()))?
{
// Choose most prior SVCB record
let svcb = signed_packet.resource_records(&target).fold(
None,
|prev: Option<SVCB>, answer| {
if let pkarr::dns::rdata::RData::SVCB(curr) = &answer.rdata {
let curr = curr.clone();

if curr.priority == 0 {
return Some(curr);
}
if let Some(prev) = &prev {
// TODO return random if priority is the same
if curr.priority >= prev.priority {
return Some(curr);
}
} else {
return Some(curr);
}
// TODO return random if priority is the same
} else {
prior = Some(svcb)
}
}
}

if let Some(svcb) = prior {
prev
},
);

if let Some(svcb) = svcb {
homeserver_public_key = Some(public_key.clone());
target = svcb.target.to_string();

Expand Down

0 comments on commit e9bb104

Please sign in to comment.