2020-03-20 00:55:11 +00:00
|
|
|
use actix_web::{middleware::Logger, web, App, HttpServer, Responder};
|
2020-03-15 02:05:40 +00:00
|
|
|
use bb8_postgres::tokio_postgres;
|
|
|
|
|
2020-03-20 00:55:11 +00:00
|
|
|
mod actor;
|
2020-03-15 02:05:40 +00:00
|
|
|
mod apub;
|
2020-03-20 00:55:11 +00:00
|
|
|
mod args;
|
|
|
|
mod config;
|
2020-03-16 17:56:26 +00:00
|
|
|
mod db;
|
2020-03-16 03:36:46 +00:00
|
|
|
mod error;
|
2020-03-15 02:05:40 +00:00
|
|
|
mod inbox;
|
2020-03-19 19:05:16 +00:00
|
|
|
mod nodeinfo;
|
2020-03-16 17:56:26 +00:00
|
|
|
mod notify;
|
2020-03-17 17:15:16 +00:00
|
|
|
mod requests;
|
2020-03-20 00:55:11 +00:00
|
|
|
mod responses;
|
2020-03-15 02:05:40 +00:00
|
|
|
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-20 00:55:11 +00:00
|
|
|
use self::{args::Args, config::Config, db::Db, state::State, webfinger::RelayResolver};
|
2020-03-18 05:01:14 +00:00
|
|
|
|
2020-03-20 04:06:16 +00:00
|
|
|
async fn index(state: web::Data<State>, config: web::Data<Config>) -> impl Responder {
|
|
|
|
let mut s = String::new();
|
|
|
|
s.push_str(&format!("Welcome to the relay on {}\n", config.hostname()));
|
|
|
|
|
|
|
|
let listeners = state.listeners().await;
|
|
|
|
if listeners.is_empty() {
|
|
|
|
s.push_str("There are no currently connected servers\n");
|
|
|
|
} else {
|
|
|
|
s.push_str("Here are the currently connected servers:\n");
|
|
|
|
s.push_str("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
for listener in listeners {
|
|
|
|
if let Some(domain) = listener.as_url().domain() {
|
|
|
|
s.push_str(&format!("{}\n", domain));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.push_str("\n");
|
|
|
|
s.push_str(&format!(
|
|
|
|
"The source code for this project can be found at {}\n",
|
|
|
|
config.source_code()
|
|
|
|
));
|
|
|
|
|
|
|
|
s
|
2020-03-15 02:05:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::main]
|
|
|
|
async fn main() -> Result<(), anyhow::Error> {
|
|
|
|
dotenv::dotenv().ok();
|
|
|
|
|
2020-03-20 00:55:11 +00:00
|
|
|
let config = Config::build()?;
|
2020-03-15 22:37:53 +00:00
|
|
|
|
2020-03-20 00:55:11 +00:00
|
|
|
if config.debug() {
|
|
|
|
std::env::set_var("RUST_LOG", "debug")
|
|
|
|
} else {
|
|
|
|
std::env::set_var("RUST_LOG", "info")
|
|
|
|
}
|
|
|
|
|
2020-03-20 15:09:42 +00:00
|
|
|
if config.pretty_log() {
|
|
|
|
pretty_env_logger::init();
|
|
|
|
} else {
|
|
|
|
env_logger::init();
|
|
|
|
}
|
2020-03-20 00:55:11 +00:00
|
|
|
|
|
|
|
let pg_config: tokio_postgres::Config = config.database_url().parse()?;
|
2020-03-19 22:19:05 +00:00
|
|
|
let db = Db::build(pg_config.clone()).await?;
|
2020-03-15 02:05:40 +00:00
|
|
|
|
2020-03-20 00:55:11 +00:00
|
|
|
let args = Args::new();
|
|
|
|
|
|
|
|
if !args.blocks().is_empty() || !args.whitelists().is_empty() {
|
|
|
|
if args.undo() {
|
|
|
|
db.remove_blocks(args.blocks()).await?;
|
|
|
|
db.remove_whitelists(args.whitelists()).await?;
|
|
|
|
} else {
|
|
|
|
db.add_blocks(args.blocks()).await?;
|
|
|
|
db.add_whitelists(args.whitelists()).await?;
|
|
|
|
}
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
let state = State::hydrate(config.clone(), &db).await?;
|
2020-03-15 02:05:40 +00:00
|
|
|
|
2020-03-16 17:56:26 +00:00
|
|
|
let _ = notify::NotifyHandler::start_handler(state.clone(), pg_config.clone());
|
|
|
|
|
2020-03-20 00:55:11 +00:00
|
|
|
let bind_address = config.bind_address();
|
2020-03-15 02:05:40 +00:00
|
|
|
HttpServer::new(move || {
|
|
|
|
App::new()
|
2020-03-15 22:37:53 +00:00
|
|
|
.wrap(Logger::default())
|
2020-03-19 22:19:05 +00:00
|
|
|
.data(db.clone())
|
2020-03-15 02:05:40 +00:00
|
|
|
.data(state.clone())
|
2020-03-18 04:35:20 +00:00
|
|
|
.data(state.requests())
|
2020-03-20 00:55:11 +00:00
|
|
|
.data(config.clone())
|
2020-03-15 02:05:40 +00:00
|
|
|
.service(web::resource("/").route(web::get().to(index)))
|
2020-03-16 04:26:31 +00:00
|
|
|
.service(
|
|
|
|
web::resource("/inbox")
|
2020-03-20 00:55:11 +00:00
|
|
|
.wrap(config.digest_middleware())
|
|
|
|
.wrap(config.signature_middleware(state.requests()))
|
2020-03-16 04:26:31 +00:00
|
|
|
.route(web::post().to(inbox::inbox)),
|
|
|
|
)
|
2020-03-20 00:55:11 +00:00
|
|
|
.service(web::resource("/actor").route(web::get().to(actor::route)))
|
2020-03-19 19:05:16 +00:00
|
|
|
.service(web::resource("/nodeinfo/2.0").route(web::get().to(nodeinfo::route)))
|
|
|
|
.service(
|
|
|
|
web::scope("/.well-known")
|
|
|
|
.service(actix_webfinger::scoped::<_, RelayResolver>())
|
|
|
|
.service(web::resource("/nodeinfo").route(web::get().to(nodeinfo::well_known))),
|
|
|
|
)
|
2020-03-15 02:05:40 +00:00
|
|
|
})
|
2020-03-20 00:55:11 +00:00
|
|
|
.bind(bind_address)?
|
2020-03-15 02:05:40 +00:00
|
|
|
.run()
|
|
|
|
.await?;
|
|
|
|
Ok(())
|
|
|
|
}
|