feat: proxy url

should be properly activitypub compliant
This commit is contained in:
əlemi 2024-05-20 05:18:27 +02:00
parent b3df8efa29
commit 27073138ae
Signed by: alemi
GPG key ID: A4895B84D311642C
3 changed files with 31 additions and 5 deletions

View file

@ -1,5 +1,5 @@
use apb::{ActorMut, BaseMut, ObjectMut, PublicKeyMut}; use apb::{ActorMut, BaseMut, ObjectMut, PublicKeyMut};
use axum::{extract::{Query, State}, http::HeaderMap, response::{IntoResponse, Redirect, Response}, Json}; use axum::{extract::{Query, State}, http::HeaderMap, response::{IntoResponse, Redirect, Response}, Form, Json};
use reqwest::Method; use reqwest::Method;
use crate::{errors::UpubError, server::{auth::{AuthIdentity, Identity}, fetcher::Fetcher, Context}, url}; use crate::{errors::UpubError, server::{auth::{AuthIdentity, Identity}, fetcher::Fetcher, Context}, url};
@ -44,12 +44,12 @@ pub struct FetchPath {
id: String, id: String,
} }
pub async fn debug( pub async fn proxy_get(
State(ctx): State<Context>, State(ctx): State<Context>,
Query(query): Query<FetchPath>, Query(query): Query<FetchPath>,
AuthIdentity(auth): AuthIdentity, AuthIdentity(auth): AuthIdentity,
) -> crate::Result<Json<serde_json::Value>> { ) -> crate::Result<Json<serde_json::Value>> {
// only local users can request debug fetches // only local users can request fetches
if !ctx.cfg().security.allow_public_debugger && !matches!(auth, Identity::Local(_)) { if !ctx.cfg().security.allow_public_debugger && !matches!(auth, Identity::Local(_)) {
return Err(UpubError::unauthorized()); return Err(UpubError::unauthorized());
} }
@ -60,7 +60,31 @@ pub async fn debug(
None, None,
&ctx.base(), &ctx.base(),
&ctx.app().private_key, &ctx.app().private_key,
&format!("{}|devtools", ctx.domain()), &format!("{}+proxy", ctx.domain()),
)
.await?
.json::<serde_json::Value>()
.await?
))
}
pub async fn proxy_form(
State(ctx): State<Context>,
AuthIdentity(auth): AuthIdentity,
Form(query): Form<FetchPath>,
) -> crate::Result<Json<serde_json::Value>> {
// only local users can request fetches
if !ctx.cfg().security.allow_public_debugger && !matches!(auth, Identity::Local(_)) {
return Err(UpubError::unauthorized());
}
Ok(Json(
Context::request(
Method::GET,
&query.id,
None,
&ctx.base(),
&ctx.app().private_key,
&format!("{}+proxy", ctx.domain()),
) )
.await? .await?
.json::<serde_json::Value>() .json::<serde_json::Value>()

View file

@ -25,7 +25,8 @@ impl ActivityPubRouter for Router<crate::server::Context> {
// core server inbox/outbox, maybe for feeds? TODO do we need these? // core server inbox/outbox, maybe for feeds? TODO do we need these?
.route("/", get(ap::application::view)) .route("/", get(ap::application::view))
// fetch route, to debug and retreive remote objects // fetch route, to debug and retreive remote objects
.route("/dbg", get(ap::application::debug)) .route("/proxy", get(ap::application::proxy_get))
.route("/proxy", post(ap::application::proxy_form))
// TODO shared inboxes and instance stream will come later, just use users *boxes for now // TODO shared inboxes and instance stream will come later, just use users *boxes for now
.route("/inbox", post(ap::inbox::post)) .route("/inbox", post(ap::inbox::post))
.route("/inbox", get(ap::inbox::get)) .route("/inbox", get(ap::inbox::get))

View file

@ -77,6 +77,7 @@ pub async fn view(
.set_endpoints(Node::object( .set_endpoints(Node::object(
serde_json::Value::new_object() serde_json::Value::new_object()
.set_shared_inbox(Some(&url!(ctx, "/inbox"))) .set_shared_inbox(Some(&url!(ctx, "/inbox")))
.set_proxy_url(Some(&url!(ctx, "/proxy")))
)); ));
if !auth.is(&uid) && !cfg.show_followers_count { if !auth.is(&uid) && !cfg.show_followers_count {