iroh_dns_server/http/
pkarr.rs1use axum::{
2 extract::{Path, State},
3 response::IntoResponse,
4};
5use bytes::Bytes;
6use http::{StatusCode, header};
7use iroh_base::PublicKey;
8use iroh_dns::pkarr::SignedPacket;
9use tracing::info;
10
11use super::error::AppError;
12use crate::{state::AppState, store::PacketSource, util::PublicKeyBytes};
13
14pub(super) async fn put(
15 State(state): State<AppState>,
16 Path(key): Path<String>,
17 body: Bytes,
18) -> Result<impl IntoResponse, AppError> {
19 let public_key = PublicKey::from_z32(&key)
20 .map_err(|e| AppError::new(StatusCode::BAD_REQUEST, Some(format!("invalid key: {e}"))))?;
21 let label = key.get(..10).unwrap_or(&key);
22 let signed_packet = SignedPacket::from_relay_payload(&public_key, &body).map_err(|e| {
23 AppError::new(
24 StatusCode::BAD_REQUEST,
25 Some(format!("invalid body payload: {e}")),
26 )
27 })?;
28
29 let updated = state
30 .store
31 .insert(signed_packet, PacketSource::PkarrPublish)
32 .await?;
33 info!(key = %label, ?updated, "pkarr upsert");
34 Ok(StatusCode::NO_CONTENT)
35}
36
37pub(super) async fn get(
38 State(state): State<AppState>,
39 Path(pubkey): Path<String>,
40) -> Result<impl IntoResponse, AppError> {
41 let pubkey = PublicKeyBytes::from_z32(&pubkey)
42 .map_err(|e| AppError::new(StatusCode::BAD_REQUEST, Some(format!("invalid key: {e}"))))?;
43 let signed_packet = state
44 .store
45 .get_signed_packet(&pubkey)
46 .await?
47 .ok_or_else(|| AppError::with_status(StatusCode::NOT_FOUND))?;
48 let body = signed_packet.to_relay_payload();
49 let headers = [(header::CONTENT_TYPE, "application/x-pkarr-signed-packet")];
50 Ok((headers, body))
51}