This commit is contained in:
Astro 2022-12-12 21:31:42 +01:00
parent 1ea9cac671
commit 6260f4306e
2 changed files with 34 additions and 21 deletions

View file

@ -8,9 +8,11 @@ pub struct Actor {
pub actor_type: String, pub actor_type: String,
pub id: String, pub id: String,
pub inbox: String, pub inbox: String,
pub outbox: String, // pub outbox: String,
#[serde(rename = "publicKey")] #[serde(rename = "publicKey")]
pub public_key: ActorPublicKey, pub public_key: ActorPublicKey,
#[serde(rename = "preferredUsername")]
pub preferredUsername: Option<String>,
} }
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]

View file

@ -7,6 +7,7 @@ use axum::{
Form, Json, RequestExt, Router, Form, Json, RequestExt, Router,
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::json;
use sigh::{PrivateKey, PublicKey, alg::{RsaSha256, Algorithm}, Key}; use sigh::{PrivateKey, PublicKey, alg::{RsaSha256, Algorithm}, Key};
use std::{net::SocketAddr, sync::Arc}; use std::{net::SocketAddr, sync::Arc};
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
@ -18,6 +19,9 @@ pub use send::send;
mod activitypub; mod activitypub;
mod endpoint; mod endpoint;
const ACTOR_ID: &str = "https://relay.fedi.buzz/actor";
const ACTOR_KEY: &str = "https://relay.fedi.buzz/actor#key";
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
struct State { struct State {
client: Arc<reqwest::Client>, client: Arc<reqwest::Client>,
@ -32,22 +36,25 @@ impl FromRef<State> for Arc<reqwest::Client> {
} }
} }
async fn actor(axum::extract::State(state): axum::extract::State<State>) -> impl IntoResponse { async fn actor(axum::extract::State(state): axum::extract::State<State>) -> Response {
let id = "https://relay.fedi.buzz/".to_string(); let id = ACTOR_ID.to_string();
([("content-type", "application/activity+json")],
Json(activitypub::Actor { Json(activitypub::Actor {
jsonld_context: serde_json::Value::String( jsonld_context: json!([
"https://www.w3.org/ns/activitystreams".to_string() "https://www.w3.org/ns/activitystreams",
), "https://w3id.org/security/v1",
actor_type: "Application".to_string(), ]),
actor_type: "Service".to_string(),
id: id.clone(), id: id.clone(),
inbox: id.clone(), inbox: "https://relay.fedi.buzz/inbox".to_string(),
outbox: id.clone(), // outbox: "https://relay.fedi.buzz/outbox".to_string(),
public_key: activitypub::ActorPublicKey { public_key: activitypub::ActorPublicKey {
id: id.clone(), id: ACTOR_KEY.to_string(),
owner: Some(id), owner: Some(id.clone()),
pem: state.public_key.to_pem().unwrap(), pem: state.public_key.to_pem().unwrap(),
}, },
}) preferredUsername: Some("buzzrelay".to_string()),
})).into_response()
} }
async fn handler( async fn handler(
@ -69,21 +76,24 @@ async fn handler(
tokio::spawn(async move { tokio::spawn(async move {
let accept = activitypub::Action { let accept = activitypub::Action {
action_type: "Accept".to_string(), action_type: "Accept".to_string(),
actor: "https://relay.fedi.buzz/".to_string(), actor: ACTOR_ID.to_string(),
to: Some(endpoint.actor.id), to: Some(endpoint.actor.id),
object: Some(endpoint.payload), object: Some(endpoint.payload),
}; };
dbg!(serde_json::to_string_pretty(&accept)); dbg!(serde_json::to_string_pretty(&accept));
send::send( send::send(
client.as_ref(), &endpoint.actor.inbox, client.as_ref(), &endpoint.actor.inbox,
"https://relay.fedi.buzz/", ACTOR_KEY,
&private_key, &private_key,
accept, accept,
).await ).await
.map_err(|e| tracing::error!("post: {}", e)); .map_err(|e| tracing::error!("post: {}", e));
}); });
StatusCode::OK.into_response() (StatusCode::CREATED,
[("content-type", "application/activity+json")],
"{}"
).into_response()
} else { } else {
(StatusCode::BAD_REQUEST, "Not a recognized request").into_response() (StatusCode::BAD_REQUEST, "Not a recognized request").into_response()
} }
@ -103,7 +113,8 @@ async fn main() {
let (private_key, public_key) = RsaSha256.generate_keys().unwrap(); let (private_key, public_key) = RsaSha256.generate_keys().unwrap();
let app = Router::new() let app = Router::new()
.route("/", get(actor).post(handler)) .route("/actor", get(actor))
.route("/relay", post(handler))
.with_state(State { .with_state(State {
client: Arc::new(reqwest::Client::new()), client: Arc::new(reqwest::Client::new()),
private_key, public_key, private_key, public_key,