relay/src/main.rs

111 lines
3.4 KiB
Rust
Raw Normal View History

2020-03-15 02:05:40 +00:00
#![feature(drain_filter)]
2020-03-15 22:37:53 +00:00
use activitystreams::{actor::apub::Application, context, endpoint::EndpointProperties};
use actix_web::{client::Client, middleware::Logger, web, App, HttpServer, Responder};
2020-03-15 02:05:40 +00:00
use bb8_postgres::tokio_postgres;
2020-03-16 04:15:50 +00:00
use http_signature_normalization_actix::prelude::{VerifyDigest, VerifySignature};
2020-03-16 03:36:46 +00:00
use rsa_pem::KeyExt;
2020-03-16 04:15:50 +00:00
use sha2::{Digest, Sha256};
2020-03-15 02:05:40 +00:00
mod apub;
mod db_actor;
2020-03-16 03:36:46 +00:00
mod error;
2020-03-15 02:05:40 +00:00
mod inbox;
mod label;
mod state;
2020-03-16 04:15:50 +00:00
mod verifier;
2020-03-16 03:36:46 +00:00
mod webfinger;
2020-03-15 02:05:40 +00:00
2020-03-15 22:37:53 +00:00
use self::{
2020-03-16 03:36:46 +00:00
apub::PublicKey,
2020-03-15 22:37:53 +00:00
db_actor::DbActor,
2020-03-16 03:36:46 +00:00
error::MyError,
2020-03-15 22:37:53 +00:00
label::ArbiterLabelFactory,
state::{State, UrlKind},
2020-03-16 04:15:50 +00:00
verifier::MyVerify,
webfinger::RelayResolver,
2020-03-15 22:37:53 +00:00
};
2020-03-15 02:05:40 +00:00
async fn index() -> impl Responder {
"hewwo, mr obama"
}
2020-03-15 22:37:53 +00:00
async fn actor_route(state: web::Data<State>) -> Result<impl Responder, MyError> {
let mut application = Application::default();
let mut endpoint = EndpointProperties::default();
endpoint.set_shared_inbox(format!("https://{}/inbox", "localhost"))?;
application
.object_props
.set_id(state.generate_url(UrlKind::Actor))?
.set_summary_xsd_string("AodeRelay bot")?
.set_name_xsd_string("AodeRelay")?
.set_url_xsd_any_uri(state.generate_url(UrlKind::Actor))?
.set_context_xsd_any_uri(context())?;
application
.ap_actor_props
.set_preferred_username("relay")?
.set_followers(state.generate_url(UrlKind::Followers))?
.set_following(state.generate_url(UrlKind::Following))?
.set_inbox(state.generate_url(UrlKind::Inbox))?
.set_endpoints(endpoint)?;
2020-03-16 03:36:46 +00:00
let public_key = PublicKey {
id: state.generate_url(UrlKind::MainKey).parse()?,
owner: state.generate_url(UrlKind::Actor).parse()?,
public_key_pem: state.settings.public_key.to_pem_pkcs8()?,
};
Ok(inbox::response(public_key.extend(application)))
2020-03-15 22:37:53 +00:00
}
2020-03-15 02:05:40 +00:00
#[actix_rt::main]
async fn main() -> Result<(), anyhow::Error> {
dotenv::dotenv().ok();
std::env::set_var("RUST_LOG", "info");
pretty_env_logger::init();
let pg_config: tokio_postgres::Config = std::env::var("DATABASE_URL")?.parse()?;
2020-03-15 22:37:53 +00:00
let hostname: String = std::env::var("HOSTNAME")?;
let use_whitelist = std::env::var("USE_WHITELIST").is_ok();
let use_https = std::env::var("USE_HTTPS").is_ok();
2020-03-15 02:05:40 +00:00
let arbiter_labeler = ArbiterLabelFactory::new();
let db_actor = DbActor::new(pg_config.clone());
arbiter_labeler.clone().set_label();
let state: State = db_actor
2020-03-15 22:37:53 +00:00
.send(db_actor::DbQuery(move |pool| {
State::hydrate(use_https, use_whitelist, hostname, pool)
}))
2020-03-15 02:05:40 +00:00
.await?
.await??;
HttpServer::new(move || {
let actor = DbActor::new(pg_config.clone());
arbiter_labeler.clone().set_label();
let client = Client::default();
App::new()
2020-03-16 04:15:50 +00:00
.wrap(VerifyDigest::new(Sha256::new()))
.wrap(VerifySignature::new(
MyVerify(state.clone(), client.clone()),
Default::default(),
))
2020-03-15 22:37:53 +00:00
.wrap(Logger::default())
2020-03-15 02:05:40 +00:00
.data(actor)
.data(state.clone())
.data(client)
.service(web::resource("/").route(web::get().to(index)))
.service(web::resource("/inbox").route(web::post().to(inbox::inbox)))
2020-03-15 22:37:53 +00:00
.service(web::resource("/actor").route(web::get().to(actor_route)))
2020-03-16 04:15:50 +00:00
.service(actix_webfinger::resource::<_, RelayResolver>())
2020-03-15 02:05:40 +00:00
})
.bind("127.0.0.1:8080")?
.run()
.await?;
Ok(())
}