mirror of
https://github.com/astro/buzzrelay.git
synced 2024-11-21 19:51:00 +00:00
main: resolve webfinger with https uri
This commit is contained in:
parent
9cfcf60903
commit
f9a103de9a
2 changed files with 51 additions and 19 deletions
24
src/actor.rs
24
src/actor.rs
|
@ -26,6 +26,30 @@ pub struct Actor {
|
|||
}
|
||||
|
||||
impl Actor {
|
||||
pub fn from_uri(mut uri: &str) -> Option<Self> {
|
||||
if ! uri.starts_with("https://") {
|
||||
return None;
|
||||
}
|
||||
uri = &uri[8..];
|
||||
|
||||
let parts = uri.split("/").collect::<Vec<_>>();
|
||||
if parts.len() != 3 {
|
||||
return None;
|
||||
}
|
||||
|
||||
let Ok(topic) = urlencoding::decode(parts[2]) else { return None; };
|
||||
let kind = match parts[1] {
|
||||
"tag" =>
|
||||
ActorKind::TagRelay(topic.to_string()),
|
||||
"instance" =>
|
||||
ActorKind::InstanceRelay(topic.to_string()),
|
||||
_ =>
|
||||
return None,
|
||||
};
|
||||
let host = Arc::new(parts[0].to_string());
|
||||
Some(Actor { host, kind })
|
||||
}
|
||||
|
||||
pub fn uri(&self) -> String {
|
||||
match &self.kind {
|
||||
ActorKind::TagRelay(tag) =>
|
||||
|
|
46
src/main.rs
46
src/main.rs
|
@ -28,6 +28,7 @@ mod activitypub;
|
|||
mod actor_cache;
|
||||
mod endpoint;
|
||||
|
||||
use actor::Actor;
|
||||
use state::State;
|
||||
|
||||
|
||||
|
@ -46,26 +47,33 @@ async fn webfinger(
|
|||
return StatusCode::NOT_FOUND.into_response();
|
||||
},
|
||||
};
|
||||
let (target_kind, target_host) =
|
||||
if resource.starts_with("acct:tag-") {
|
||||
let off = "acct:tag-".len();
|
||||
let at = resource.find('@');
|
||||
(actor::ActorKind::TagRelay(resource[off..at.unwrap_or(resource.len())].to_string()),
|
||||
at.map_or_else(|| state.hostname.clone(), |at| Arc::new(resource[at + 1..].to_string())))
|
||||
} else if resource.starts_with("acct:instance-") {
|
||||
let off = "acct:instance-".len();
|
||||
let at = resource.find('@');
|
||||
(actor::ActorKind::InstanceRelay(resource[off..at.unwrap_or(resource.len())].to_string()),
|
||||
at.map_or_else(|| state.hostname.clone(), |at| Arc::new(resource[at + 1..].to_string())))
|
||||
} else {
|
||||
track_request("GET", "webfinger", "not_found");
|
||||
return StatusCode::NOT_FOUND.into_response();
|
||||
};
|
||||
track_request("GET", "webfinger", "found");
|
||||
let target = actor::Actor {
|
||||
host: target_host,
|
||||
kind: target_kind,
|
||||
let target = if resource.starts_with("acct:") {
|
||||
let (target_kind, target_host) =
|
||||
if resource.starts_with("acct:tag-") {
|
||||
let off = "acct:tag-".len();
|
||||
let at = resource.find('@');
|
||||
(actor::ActorKind::TagRelay(resource[off..at.unwrap_or(resource.len())].to_string()),
|
||||
at.map_or_else(|| state.hostname.clone(), |at| Arc::new(resource[at + 1..].to_string())))
|
||||
} else if resource.starts_with("acct:instance-") {
|
||||
let off = "acct:instance-".len();
|
||||
let at = resource.find('@');
|
||||
(actor::ActorKind::InstanceRelay(resource[off..at.unwrap_or(resource.len())].to_string()),
|
||||
at.map_or_else(|| state.hostname.clone(), |at| Arc::new(resource[at + 1..].to_string())))
|
||||
} else {
|
||||
track_request("GET", "webfinger", "not_found");
|
||||
return StatusCode::NOT_FOUND.into_response();
|
||||
};
|
||||
actor::Actor {
|
||||
host: target_host,
|
||||
kind: target_kind,
|
||||
}
|
||||
} else if let Some(target) = Actor::from_uri(resource) {
|
||||
target
|
||||
} else {
|
||||
track_request("GET", "webfinger", "not_found");
|
||||
return StatusCode::NOT_FOUND.into_response();
|
||||
};
|
||||
track_request("GET", "webfinger", "found");
|
||||
Json(json!({
|
||||
"subject": &resource,
|
||||
"aliases": &[
|
||||
|
|
Loading…
Reference in a new issue