Add commandline blocking and whitelisting
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
use crate::{
|
||||
apub::PublicKey,
|
||||
config::{Config, UrlKind},
|
||||
error::MyError,
|
||||
responses::ok,
|
||||
state::State,
|
||||
};
|
||||
use activitystreams::{
|
||||
actor::Application, context, endpoint::EndpointProperties, ext::Extensible,
|
||||
object::properties::ObjectProperties, security,
|
||||
};
|
||||
use actix_web::{web, Responder};
|
||||
use rsa_pem::KeyExt;
|
||||
|
||||
pub async fn route(
|
||||
state: web::Data<State>,
|
||||
config: web::Data<Config>,
|
||||
) -> Result<impl Responder, MyError> {
|
||||
let mut application = Application::full();
|
||||
let mut endpoint = EndpointProperties::default();
|
||||
|
||||
endpoint.set_shared_inbox(config.generate_url(UrlKind::Inbox))?;
|
||||
|
||||
let props: &mut ObjectProperties = application.as_mut();
|
||||
props
|
||||
.set_id(config.generate_url(UrlKind::Actor))?
|
||||
.set_summary_xsd_string("AodeRelay bot")?
|
||||
.set_name_xsd_string("AodeRelay")?
|
||||
.set_url_xsd_any_uri(config.generate_url(UrlKind::Actor))?
|
||||
.set_many_context_xsd_any_uris(vec![context(), security()])?;
|
||||
|
||||
application
|
||||
.extension
|
||||
.set_preferred_username("relay")?
|
||||
.set_followers(config.generate_url(UrlKind::Followers))?
|
||||
.set_following(config.generate_url(UrlKind::Following))?
|
||||
.set_inbox(config.generate_url(UrlKind::Inbox))?
|
||||
.set_outbox(config.generate_url(UrlKind::Outbox))?
|
||||
.set_endpoints(endpoint)?;
|
||||
|
||||
let public_key = PublicKey {
|
||||
id: config.generate_url(UrlKind::MainKey).parse()?,
|
||||
owner: config.generate_url(UrlKind::Actor).parse()?,
|
||||
public_key_pem: state.public_key.to_pem_pkcs8()?,
|
||||
};
|
||||
|
||||
Ok(ok(application.extend(public_key.to_ext())))
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
use structopt::StructOpt;
|
||||
|
||||
#[derive(Debug, StructOpt)]
|
||||
#[structopt(name = "relay", about = "An activitypub relay")]
|
||||
pub struct Args {
|
||||
#[structopt(short, help = "A list of domains that should be blocked")]
|
||||
blocks: Vec<String>,
|
||||
|
||||
#[structopt(short, help = "A list of domains that should be whitelisted")]
|
||||
whitelists: Vec<String>,
|
||||
|
||||
#[structopt(short, help = "Undo whitelisting or blocking these domains")]
|
||||
undo: bool,
|
||||
}
|
||||
|
||||
impl Args {
|
||||
pub fn new() -> Self {
|
||||
Self::from_args()
|
||||
}
|
||||
|
||||
pub fn blocks(&self) -> &[String] {
|
||||
&self.blocks
|
||||
}
|
||||
|
||||
pub fn whitelists(&self) -> &[String] {
|
||||
&self.whitelists
|
||||
}
|
||||
|
||||
pub fn undo(&self) -> bool {
|
||||
self.undo
|
||||
}
|
||||
}
|
||||
+111
@@ -0,0 +1,111 @@
|
||||
use crate::{error::MyError, requests::Requests, verifier::MyVerify};
|
||||
use config::Environment;
|
||||
use http_signature_normalization_actix::prelude::{VerifyDigest, VerifySignature};
|
||||
use sha2::{Digest, Sha256};
|
||||
use std::net::IpAddr;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Clone, Debug, serde::Deserialize)]
|
||||
pub struct Config {
|
||||
hostname: String,
|
||||
addr: IpAddr,
|
||||
port: u16,
|
||||
debug: bool,
|
||||
whitelist_mode: bool,
|
||||
validate_signatures: bool,
|
||||
https: bool,
|
||||
database_url: String,
|
||||
}
|
||||
|
||||
pub enum UrlKind {
|
||||
Activity,
|
||||
Actor,
|
||||
Followers,
|
||||
Following,
|
||||
Inbox,
|
||||
MainKey,
|
||||
NodeInfo,
|
||||
Outbox,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub fn build() -> Result<Self, MyError> {
|
||||
let mut config = config::Config::new();
|
||||
config
|
||||
.set_default("hostname", "localhost:8080")?
|
||||
.set_default("addr", "127.0.0.1")?
|
||||
.set_default("port", 8080)?
|
||||
.set_default("debug", true)?
|
||||
.set_default("whitelist_mode", false)?
|
||||
.set_default("validate_signatures", false)?
|
||||
.set_default("https", false)?
|
||||
.merge(Environment::new())?;
|
||||
|
||||
Ok(config.try_into()?)
|
||||
}
|
||||
|
||||
pub fn digest_middleware(&self) -> VerifyDigest<Sha256> {
|
||||
if self.validate_signatures {
|
||||
VerifyDigest::new(Sha256::new())
|
||||
} else {
|
||||
VerifyDigest::new(Sha256::new()).optional()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn signature_middleware(&self, requests: Requests) -> VerifySignature<MyVerify> {
|
||||
if self.validate_signatures {
|
||||
VerifySignature::new(MyVerify(requests), Default::default())
|
||||
} else {
|
||||
VerifySignature::new(MyVerify(requests), Default::default()).optional()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn bind_address(&self) -> (IpAddr, u16) {
|
||||
(self.addr, self.port)
|
||||
}
|
||||
|
||||
pub fn debug(&self) -> bool {
|
||||
self.debug
|
||||
}
|
||||
|
||||
pub fn whitelist_mode(&self) -> bool {
|
||||
self.whitelist_mode
|
||||
}
|
||||
|
||||
pub fn database_url(&self) -> &str {
|
||||
&self.database_url
|
||||
}
|
||||
|
||||
pub fn hostname(&self) -> &str {
|
||||
&self.hostname
|
||||
}
|
||||
|
||||
pub fn generate_resource(&self) -> String {
|
||||
format!("relay@{}", self.hostname)
|
||||
}
|
||||
|
||||
pub fn software_name(&self) -> String {
|
||||
"AodeRelay".to_owned()
|
||||
}
|
||||
|
||||
pub fn software_version(&self) -> String {
|
||||
"v0.1.0-master".to_owned()
|
||||
}
|
||||
|
||||
pub fn generate_url(&self, kind: UrlKind) -> String {
|
||||
let scheme = if self.https { "https" } else { "http" };
|
||||
|
||||
match kind {
|
||||
UrlKind::Activity => {
|
||||
format!("{}://{}/activity/{}", scheme, self.hostname, Uuid::new_v4())
|
||||
}
|
||||
UrlKind::Actor => format!("{}://{}/actor", scheme, self.hostname),
|
||||
UrlKind::Followers => format!("{}://{}/followers", scheme, self.hostname),
|
||||
UrlKind::Following => format!("{}://{}/following", scheme, self.hostname),
|
||||
UrlKind::Inbox => format!("{}://{}/inbox", scheme, self.hostname),
|
||||
UrlKind::MainKey => format!("{}://{}/actor#main-key", scheme, self.hostname),
|
||||
UrlKind::NodeInfo => format!("{}://{}/nodeinfo/2.0", scheme, self.hostname),
|
||||
UrlKind::Outbox => format!("{}://{}/outbox", scheme, self.hostname),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,11 @@ use crate::error::MyError;
|
||||
use activitystreams::primitives::XsdAnyUri;
|
||||
use bb8_postgres::{
|
||||
bb8,
|
||||
tokio_postgres::{row::Row, Client, Config, NoTls},
|
||||
tokio_postgres::{
|
||||
error::{Error, SqlState},
|
||||
row::Row,
|
||||
Client, Config, NoTls,
|
||||
},
|
||||
PostgresConnectionManager,
|
||||
};
|
||||
use log::{info, warn};
|
||||
@@ -43,6 +47,48 @@ impl Db {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn add_blocks(&self, domains: &[String]) -> Result<(), MyError> {
|
||||
let conn = self.pool.get().await?;
|
||||
for domain in domains {
|
||||
match add_block(&conn, domain.as_str()).await {
|
||||
Err(e) if e.code() != Some(&SqlState::UNIQUE_VIOLATION) => {
|
||||
Err(e)?;
|
||||
}
|
||||
_ => (),
|
||||
};
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn remove_blocks(&self, domains: &[String]) -> Result<(), MyError> {
|
||||
let conn = self.pool.get().await?;
|
||||
for domain in domains {
|
||||
remove_block(&conn, domain.as_str()).await?
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn add_whitelists(&self, domains: &[String]) -> Result<(), MyError> {
|
||||
let conn = self.pool.get().await?;
|
||||
for domain in domains {
|
||||
match add_whitelist(&conn, domain.as_str()).await {
|
||||
Err(e) if e.code() != Some(&SqlState::UNIQUE_VIOLATION) => {
|
||||
Err(e)?;
|
||||
}
|
||||
_ => (),
|
||||
};
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn remove_whitelists(&self, domains: &[String]) -> Result<(), MyError> {
|
||||
let conn = self.pool.get().await?;
|
||||
for domain in domains {
|
||||
remove_whitelist(&conn, domain.as_str()).await?
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn hydrate_blocks(&self) -> Result<HashSet<String>, MyError> {
|
||||
let conn = self.pool.get().await?;
|
||||
|
||||
@@ -74,7 +120,7 @@ impl Db {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn listen(client: &Client) -> Result<(), MyError> {
|
||||
pub async fn listen(client: &Client) -> Result<(), Error> {
|
||||
info!("LISTEN new_blocks;");
|
||||
info!("LISTEN new_whitelists;");
|
||||
info!("LISTEN new_listeners;");
|
||||
@@ -117,49 +163,67 @@ async fn update_private_key(client: &Client, key: &RSAPrivateKey) -> Result<(),
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn add_block(client: &Client, block: &XsdAnyUri) -> Result<(), MyError> {
|
||||
let host = if let Some(host) = block.as_url().host() {
|
||||
host
|
||||
} else {
|
||||
return Err(MyError::Host(block.to_string()));
|
||||
};
|
||||
|
||||
async fn add_block(client: &Client, domain: &str) -> Result<(), Error> {
|
||||
info!(
|
||||
"INSERT INTO blocks (domain_name, created_at) VALUES ($1::TEXT, 'now'); [{}]",
|
||||
host.to_string()
|
||||
domain,
|
||||
);
|
||||
client
|
||||
.execute(
|
||||
"INSERT INTO blocks (domain_name, created_at) VALUES ($1::TEXT, 'now');",
|
||||
&[&host.to_string()],
|
||||
&[&domain],
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn add_whitelist(client: &Client, whitelist: &XsdAnyUri) -> Result<(), MyError> {
|
||||
let host = if let Some(host) = whitelist.as_url().host() {
|
||||
host
|
||||
} else {
|
||||
return Err(MyError::Host(whitelist.to_string()));
|
||||
};
|
||||
async fn remove_block(client: &Client, domain: &str) -> Result<(), Error> {
|
||||
info!(
|
||||
"DELETE FROM blocks WHERE domain_name = $1::TEXT; [{}]",
|
||||
domain,
|
||||
);
|
||||
client
|
||||
.execute(
|
||||
"DELETE FROM blocks WHERE domain_name = $1::TEXT;",
|
||||
&[&domain],
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn add_whitelist(client: &Client, domain: &str) -> Result<(), Error> {
|
||||
info!(
|
||||
"INSERT INTO whitelists (domain_name, created_at) VALUES ($1::TEXT, 'now'); [{}]",
|
||||
host.to_string()
|
||||
domain,
|
||||
);
|
||||
client
|
||||
.execute(
|
||||
"INSERT INTO whitelists (domain_name, created_at) VALUES ($1::TEXT, 'now');",
|
||||
&[&host.to_string()],
|
||||
&[&domain],
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn remove_listener(client: &Client, listener: &XsdAnyUri) -> Result<(), MyError> {
|
||||
async fn remove_whitelist(client: &Client, domain: &str) -> Result<(), Error> {
|
||||
info!(
|
||||
"DELETE FROM whitelists WHERE domain_name = $1::TEXT; [{}]",
|
||||
domain,
|
||||
);
|
||||
client
|
||||
.execute(
|
||||
"DELETE FROM whitelists WHERE domain_name = $1::TEXT;",
|
||||
&[&domain],
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn remove_listener(client: &Client, listener: &XsdAnyUri) -> Result<(), Error> {
|
||||
info!(
|
||||
"DELETE FROM listeners WHERE actor_id = {};",
|
||||
listener.as_str()
|
||||
@@ -174,7 +238,7 @@ async fn remove_listener(client: &Client, listener: &XsdAnyUri) -> Result<(), My
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn add_listener(client: &Client, listener: &XsdAnyUri) -> Result<(), MyError> {
|
||||
async fn add_listener(client: &Client, listener: &XsdAnyUri) -> Result<(), Error> {
|
||||
info!(
|
||||
"INSERT INTO listeners (actor_id, created_at) VALUES ($1::TEXT, 'now'); [{}]",
|
||||
listener.as_str(),
|
||||
@@ -189,14 +253,14 @@ async fn add_listener(client: &Client, listener: &XsdAnyUri) -> Result<(), MyErr
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn hydrate_blocks(client: &Client) -> Result<HashSet<String>, MyError> {
|
||||
async fn hydrate_blocks(client: &Client) -> Result<HashSet<String>, Error> {
|
||||
info!("SELECT domain_name FROM blocks");
|
||||
let rows = client.query("SELECT domain_name FROM blocks", &[]).await?;
|
||||
|
||||
parse_rows(rows)
|
||||
}
|
||||
|
||||
async fn hydrate_whitelists(client: &Client) -> Result<HashSet<String>, MyError> {
|
||||
async fn hydrate_whitelists(client: &Client) -> Result<HashSet<String>, Error> {
|
||||
info!("SELECT domain_name FROM whitelists");
|
||||
let rows = client
|
||||
.query("SELECT domain_name FROM whitelists", &[])
|
||||
@@ -205,14 +269,14 @@ async fn hydrate_whitelists(client: &Client) -> Result<HashSet<String>, MyError>
|
||||
parse_rows(rows)
|
||||
}
|
||||
|
||||
async fn hydrate_listeners(client: &Client) -> Result<HashSet<XsdAnyUri>, MyError> {
|
||||
async fn hydrate_listeners(client: &Client) -> Result<HashSet<XsdAnyUri>, Error> {
|
||||
info!("SELECT actor_id FROM listeners");
|
||||
let rows = client.query("SELECT actor_id FROM listeners", &[]).await?;
|
||||
|
||||
parse_rows(rows)
|
||||
}
|
||||
|
||||
fn parse_rows<T, E>(rows: Vec<Row>) -> Result<HashSet<T>, MyError>
|
||||
fn parse_rows<T, E>(rows: Vec<Row>) -> Result<HashSet<T>, Error>
|
||||
where
|
||||
T: std::str::FromStr<Err = E> + Eq + std::hash::Hash,
|
||||
E: std::fmt::Display,
|
||||
|
||||
+24
-5
@@ -1,11 +1,18 @@
|
||||
use activitystreams::primitives::XsdAnyUriError;
|
||||
use actix_web::{error::ResponseError, http::StatusCode, HttpResponse};
|
||||
use actix_web::{
|
||||
error::{BlockingError, ResponseError},
|
||||
http::StatusCode,
|
||||
HttpResponse,
|
||||
};
|
||||
use log::error;
|
||||
use rsa_pem::KeyError;
|
||||
use std::{convert::Infallible, io::Error};
|
||||
use std::{convert::Infallible, fmt::Debug, io::Error};
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum MyError {
|
||||
#[error("Error in configuration, {0}")]
|
||||
Config(#[from] config::ConfigError),
|
||||
|
||||
#[error("Error in db, {0}")]
|
||||
DbError(#[from] bb8_postgres::tokio_postgres::error::Error),
|
||||
|
||||
@@ -51,9 +58,6 @@ pub enum MyError {
|
||||
#[error("Wrong ActivityPub kind, {0}")]
|
||||
Kind(String),
|
||||
|
||||
#[error("No host present in URI, {0}")]
|
||||
Host(String),
|
||||
|
||||
#[error("Too many CPUs, {0}")]
|
||||
CpuCount(#[from] std::num::TryFromIntError),
|
||||
|
||||
@@ -77,6 +81,9 @@ pub enum MyError {
|
||||
|
||||
#[error("URI is missing domain field")]
|
||||
Domain,
|
||||
|
||||
#[error("Blocking operation was canceled")]
|
||||
Canceled,
|
||||
}
|
||||
|
||||
impl ResponseError for MyError {
|
||||
@@ -102,6 +109,18 @@ impl ResponseError for MyError {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> From<BlockingError<T>> for MyError
|
||||
where
|
||||
T: Into<MyError> + Debug,
|
||||
{
|
||||
fn from(e: BlockingError<T>) -> Self {
|
||||
match e {
|
||||
BlockingError::Error(e) => e.into(),
|
||||
BlockingError::Canceled => MyError::Canceled,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> From<bb8_postgres::bb8::RunError<T>> for MyError
|
||||
where
|
||||
T: Into<MyError>,
|
||||
|
||||
+27
-23
@@ -1,10 +1,11 @@
|
||||
use crate::{
|
||||
accepted,
|
||||
apub::{AcceptedActors, AcceptedObjects, ValidTypes},
|
||||
config::{Config, UrlKind},
|
||||
db::Db,
|
||||
error::MyError,
|
||||
requests::Requests,
|
||||
state::{State, UrlKind},
|
||||
responses::accepted,
|
||||
state::State,
|
||||
};
|
||||
use activitystreams::{
|
||||
activity::{Accept, Announce, Follow, Undo},
|
||||
@@ -22,6 +23,7 @@ use std::convert::TryInto;
|
||||
pub async fn inbox(
|
||||
db: web::Data<Db>,
|
||||
state: web::Data<State>,
|
||||
config: web::Data<Config>,
|
||||
client: web::Data<Requests>,
|
||||
input: web::Json<AcceptedObjects>,
|
||||
verified: SignatureVerified,
|
||||
@@ -58,19 +60,20 @@ pub async fn inbox(
|
||||
|
||||
match input.kind {
|
||||
ValidTypes::Announce | ValidTypes::Create => {
|
||||
handle_announce(&state, &client, input, actor).await
|
||||
handle_announce(&state, &config, &client, input, actor).await
|
||||
}
|
||||
ValidTypes::Follow => handle_follow(&db, &state, &client, input, actor, is_listener).await,
|
||||
ValidTypes::Follow => handle_follow(&db, &config, &client, input, actor, is_listener).await,
|
||||
ValidTypes::Delete | ValidTypes::Update => {
|
||||
handle_forward(&state, &client, input, actor).await
|
||||
}
|
||||
ValidTypes::Undo => handle_undo(&db, &state, &client, input, actor).await,
|
||||
ValidTypes::Undo => handle_undo(&db, &state, &config, &client, input, actor).await,
|
||||
}
|
||||
}
|
||||
|
||||
async fn handle_undo(
|
||||
db: &Db,
|
||||
state: &State,
|
||||
config: &Config,
|
||||
client: &Requests,
|
||||
input: AcceptedObjects,
|
||||
actor: AcceptedActors,
|
||||
@@ -88,7 +91,7 @@ async fn handle_undo(
|
||||
return handle_forward(state, client, input, actor).await;
|
||||
}
|
||||
|
||||
let my_id: XsdAnyUri = state.generate_url(UrlKind::Actor).parse()?;
|
||||
let my_id: XsdAnyUri = config.generate_url(UrlKind::Actor).parse()?;
|
||||
|
||||
if !input.object.child_object_is(&my_id) && !input.object.child_object_is(&public()) {
|
||||
return Err(MyError::WrongActor(input.object.id().to_string()));
|
||||
@@ -97,7 +100,7 @@ async fn handle_undo(
|
||||
let inbox = actor.inbox().to_owned();
|
||||
db.remove_listener(inbox).await?;
|
||||
|
||||
let undo = generate_undo_follow(state, &actor.id, &my_id)?;
|
||||
let undo = generate_undo_follow(config, &actor.id, &my_id)?;
|
||||
|
||||
let client2 = client.clone();
|
||||
let inbox = actor.inbox().clone();
|
||||
@@ -125,6 +128,7 @@ async fn handle_forward(
|
||||
|
||||
async fn handle_announce(
|
||||
state: &State,
|
||||
config: &Config,
|
||||
client: &Requests,
|
||||
input: AcceptedObjects,
|
||||
actor: AcceptedActors,
|
||||
@@ -135,9 +139,9 @@ async fn handle_announce(
|
||||
return Err(MyError::Duplicate);
|
||||
}
|
||||
|
||||
let activity_id: XsdAnyUri = state.generate_url(UrlKind::Activity).parse()?;
|
||||
let activity_id: XsdAnyUri = config.generate_url(UrlKind::Activity).parse()?;
|
||||
|
||||
let announce = generate_announce(state, &activity_id, object_id)?;
|
||||
let announce = generate_announce(config, &activity_id, object_id)?;
|
||||
let inboxes = get_inboxes(state, &actor, &object_id).await?;
|
||||
client.deliver_many(inboxes, announce.clone());
|
||||
|
||||
@@ -148,13 +152,13 @@ async fn handle_announce(
|
||||
|
||||
async fn handle_follow(
|
||||
db: &Db,
|
||||
state: &State,
|
||||
config: &Config,
|
||||
client: &Requests,
|
||||
input: AcceptedObjects,
|
||||
actor: AcceptedActors,
|
||||
is_listener: bool,
|
||||
) -> Result<HttpResponse, MyError> {
|
||||
let my_id: XsdAnyUri = state.generate_url(UrlKind::Actor).parse()?;
|
||||
let my_id: XsdAnyUri = config.generate_url(UrlKind::Actor).parse()?;
|
||||
|
||||
if !input.object.is(&my_id) && !input.object.is(&public()) {
|
||||
return Err(MyError::WrongActor(input.object.id().to_string()));
|
||||
@@ -166,7 +170,7 @@ async fn handle_follow(
|
||||
|
||||
// if following relay directly, not just following 'public', followback
|
||||
if input.object.is(&my_id) {
|
||||
let follow = generate_follow(state, &actor.id, &my_id)?;
|
||||
let follow = generate_follow(config, &actor.id, &my_id)?;
|
||||
let client2 = client.clone();
|
||||
let inbox = actor.inbox().clone();
|
||||
let follow2 = follow.clone();
|
||||
@@ -176,7 +180,7 @@ async fn handle_follow(
|
||||
}
|
||||
}
|
||||
|
||||
let accept = generate_accept_follow(state, &actor.id, &input.id, &my_id)?;
|
||||
let accept = generate_accept_follow(config, &actor.id, &input.id, &my_id)?;
|
||||
|
||||
let client2 = client.clone();
|
||||
let inbox = actor.inbox().clone();
|
||||
@@ -190,7 +194,7 @@ async fn handle_follow(
|
||||
|
||||
// Generate a type that says "I want to stop following you"
|
||||
fn generate_undo_follow(
|
||||
state: &State,
|
||||
config: &Config,
|
||||
actor_id: &XsdAnyUri,
|
||||
my_id: &XsdAnyUri,
|
||||
) -> Result<Undo, MyError> {
|
||||
@@ -203,7 +207,7 @@ fn generate_undo_follow(
|
||||
|
||||
follow
|
||||
.object_props
|
||||
.set_id(state.generate_url(UrlKind::Activity))?;
|
||||
.set_id(config.generate_url(UrlKind::Activity))?;
|
||||
follow
|
||||
.follow_props
|
||||
.set_actor_xsd_any_uri(actor_id.clone())?
|
||||
@@ -212,12 +216,12 @@ fn generate_undo_follow(
|
||||
follow
|
||||
})?;
|
||||
|
||||
prepare_activity(undo, state.generate_url(UrlKind::Actor), actor_id.clone())
|
||||
prepare_activity(undo, config.generate_url(UrlKind::Actor), actor_id.clone())
|
||||
}
|
||||
|
||||
// Generate a type that says "Look at this object"
|
||||
fn generate_announce(
|
||||
state: &State,
|
||||
config: &Config,
|
||||
activity_id: &XsdAnyUri,
|
||||
object_id: &XsdAnyUri,
|
||||
) -> Result<Announce, MyError> {
|
||||
@@ -226,18 +230,18 @@ fn generate_announce(
|
||||
announce
|
||||
.announce_props
|
||||
.set_object_xsd_any_uri(object_id.clone())?
|
||||
.set_actor_xsd_any_uri(state.generate_url(UrlKind::Actor))?;
|
||||
.set_actor_xsd_any_uri(config.generate_url(UrlKind::Actor))?;
|
||||
|
||||
prepare_activity(
|
||||
announce,
|
||||
activity_id.clone(),
|
||||
state.generate_url(UrlKind::Followers),
|
||||
config.generate_url(UrlKind::Followers),
|
||||
)
|
||||
}
|
||||
|
||||
// Generate a type that says "I want to follow you"
|
||||
fn generate_follow(
|
||||
state: &State,
|
||||
config: &Config,
|
||||
actor_id: &XsdAnyUri,
|
||||
my_id: &XsdAnyUri,
|
||||
) -> Result<Follow, MyError> {
|
||||
@@ -250,14 +254,14 @@ fn generate_follow(
|
||||
|
||||
prepare_activity(
|
||||
follow,
|
||||
state.generate_url(UrlKind::Activity),
|
||||
config.generate_url(UrlKind::Activity),
|
||||
actor_id.clone(),
|
||||
)
|
||||
}
|
||||
|
||||
// Generate a type that says "I accept your follow request"
|
||||
fn generate_accept_follow(
|
||||
state: &State,
|
||||
config: &Config,
|
||||
actor_id: &XsdAnyUri,
|
||||
input_id: &XsdAnyUri,
|
||||
my_id: &XsdAnyUri,
|
||||
@@ -281,7 +285,7 @@ fn generate_accept_follow(
|
||||
|
||||
prepare_activity(
|
||||
accept,
|
||||
state.generate_url(UrlKind::Activity),
|
||||
config.generate_url(UrlKind::Activity),
|
||||
actor_id.clone(),
|
||||
)
|
||||
}
|
||||
|
||||
+36
-81
@@ -1,122 +1,77 @@
|
||||
use activitystreams::{
|
||||
actor::Application, context, endpoint::EndpointProperties, ext::Extensible,
|
||||
object::properties::ObjectProperties, security,
|
||||
};
|
||||
use actix_web::{middleware::Logger, web, App, HttpResponse, HttpServer, Responder};
|
||||
use actix_web::{middleware::Logger, web, App, HttpServer, Responder};
|
||||
use bb8_postgres::tokio_postgres;
|
||||
use http_signature_normalization_actix::prelude::{VerifyDigest, VerifySignature};
|
||||
use rsa_pem::KeyExt;
|
||||
use sha2::{Digest, Sha256};
|
||||
|
||||
mod actor;
|
||||
mod apub;
|
||||
mod args;
|
||||
mod config;
|
||||
mod db;
|
||||
mod error;
|
||||
mod inbox;
|
||||
mod nodeinfo;
|
||||
mod notify;
|
||||
mod requests;
|
||||
mod responses;
|
||||
mod state;
|
||||
mod verifier;
|
||||
mod webfinger;
|
||||
|
||||
use self::{
|
||||
apub::PublicKey,
|
||||
db::Db,
|
||||
error::MyError,
|
||||
state::{State, UrlKind},
|
||||
verifier::MyVerify,
|
||||
webfinger::RelayResolver,
|
||||
};
|
||||
|
||||
pub fn ok<T>(item: T) -> HttpResponse
|
||||
where
|
||||
T: serde::ser::Serialize,
|
||||
{
|
||||
HttpResponse::Ok()
|
||||
.content_type("application/activity+json")
|
||||
.json(item)
|
||||
}
|
||||
|
||||
pub fn accepted<T>(item: T) -> HttpResponse
|
||||
where
|
||||
T: serde::ser::Serialize,
|
||||
{
|
||||
HttpResponse::Accepted()
|
||||
.content_type("application/activity+json")
|
||||
.json(item)
|
||||
}
|
||||
use self::{args::Args, config::Config, db::Db, state::State, webfinger::RelayResolver};
|
||||
|
||||
async fn index() -> impl Responder {
|
||||
"hewwo, mr obama"
|
||||
}
|
||||
|
||||
async fn actor_route(state: web::Data<State>) -> Result<impl Responder, MyError> {
|
||||
let mut application = Application::full();
|
||||
let mut endpoint = EndpointProperties::default();
|
||||
|
||||
endpoint.set_shared_inbox(state.generate_url(UrlKind::Inbox))?;
|
||||
|
||||
let props: &mut ObjectProperties = application.as_mut();
|
||||
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_many_context_xsd_any_uris(vec![context(), security()])?;
|
||||
|
||||
application
|
||||
.extension
|
||||
.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_outbox(state.generate_url(UrlKind::Outbox))?
|
||||
.set_endpoints(endpoint)?;
|
||||
|
||||
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(ok(application.extend(public_key.to_ext())))
|
||||
}
|
||||
|
||||
#[actix_rt::main]
|
||||
async fn main() -> Result<(), anyhow::Error> {
|
||||
dotenv::dotenv().ok();
|
||||
|
||||
let config = Config::build()?;
|
||||
|
||||
if config.debug() {
|
||||
std::env::set_var("RUST_LOG", "debug")
|
||||
} else {
|
||||
std::env::set_var("RUST_LOG", "info")
|
||||
}
|
||||
|
||||
pretty_env_logger::init();
|
||||
|
||||
let pg_config: tokio_postgres::Config = std::env::var("DATABASE_URL")?.parse()?;
|
||||
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();
|
||||
|
||||
let pg_config: tokio_postgres::Config = config.database_url().parse()?;
|
||||
let db = Db::build(pg_config.clone()).await?;
|
||||
|
||||
let state = State::hydrate(use_https, use_whitelist, hostname, &db).await?;
|
||||
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?;
|
||||
|
||||
let _ = notify::NotifyHandler::start_handler(state.clone(), pg_config.clone());
|
||||
|
||||
let bind_address = config.bind_address();
|
||||
HttpServer::new(move || {
|
||||
let state = state.clone();
|
||||
|
||||
App::new()
|
||||
.wrap(Logger::default())
|
||||
.data(db.clone())
|
||||
.data(state.clone())
|
||||
.data(state.requests())
|
||||
.data(config.clone())
|
||||
.service(web::resource("/").route(web::get().to(index)))
|
||||
.service(
|
||||
web::resource("/inbox")
|
||||
.wrap(VerifyDigest::new(Sha256::new()))
|
||||
.wrap(VerifySignature::new(
|
||||
MyVerify(state.requests()),
|
||||
Default::default(),
|
||||
))
|
||||
.wrap(config.digest_middleware())
|
||||
.wrap(config.signature_middleware(state.requests()))
|
||||
.route(web::post().to(inbox::inbox)),
|
||||
)
|
||||
.service(web::resource("/actor").route(web::get().to(actor_route)))
|
||||
.service(web::resource("/actor").route(web::get().to(actor::route)))
|
||||
.service(web::resource("/nodeinfo/2.0").route(web::get().to(nodeinfo::route)))
|
||||
.service(
|
||||
web::scope("/.well-known")
|
||||
@@ -124,7 +79,7 @@ async fn main() -> Result<(), anyhow::Error> {
|
||||
.service(web::resource("/nodeinfo").route(web::get().to(nodeinfo::well_known))),
|
||||
)
|
||||
})
|
||||
.bind("0.0.0.0:8080")?
|
||||
.bind(bind_address)?
|
||||
.run()
|
||||
.await?;
|
||||
Ok(())
|
||||
|
||||
+6
-6
@@ -1,24 +1,24 @@
|
||||
use crate::state::{State, UrlKind};
|
||||
use crate::config::{Config, UrlKind};
|
||||
use actix_web::{web, Responder};
|
||||
use actix_webfinger::Link;
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub async fn well_known(state: web::Data<State>) -> impl Responder {
|
||||
pub async fn well_known(config: web::Data<Config>) -> impl Responder {
|
||||
web::Json(Link {
|
||||
rel: "http://nodeinfo.diaspora.software/ns/schema/2.0".to_owned(),
|
||||
href: Some(state.generate_url(UrlKind::NodeInfo)),
|
||||
href: Some(config.generate_url(UrlKind::NodeInfo)),
|
||||
template: None,
|
||||
kind: None,
|
||||
})
|
||||
.with_header("Content-Type", "application/jrd+json")
|
||||
}
|
||||
|
||||
pub async fn route(state: web::Data<State>) -> web::Json<NodeInfo> {
|
||||
pub async fn route(config: web::Data<Config>) -> web::Json<NodeInfo> {
|
||||
web::Json(NodeInfo {
|
||||
version: NodeInfoVersion,
|
||||
software: Software {
|
||||
name: state.software_name(),
|
||||
version: state.software_version(),
|
||||
name: config.software_name(),
|
||||
version: config.software_version(),
|
||||
},
|
||||
protocols: vec![Protocol::ActivityPub],
|
||||
services: vec![],
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
use actix_web::HttpResponse;
|
||||
use serde::ser::Serialize;
|
||||
|
||||
static CONTENT_TYPE: &str = "application/activity+json";
|
||||
|
||||
pub fn ok<T>(item: T) -> HttpResponse
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
HttpResponse::Ok().content_type(CONTENT_TYPE).json(item)
|
||||
}
|
||||
|
||||
pub fn accepted<T>(item: T) -> HttpResponse
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
HttpResponse::Accepted()
|
||||
.content_type(CONTENT_TYPE)
|
||||
.json(item)
|
||||
}
|
||||
+43
-112
@@ -1,5 +1,12 @@
|
||||
use crate::{apub::AcceptedActors, db::Db, error::MyError, requests::Requests};
|
||||
use crate::{
|
||||
apub::AcceptedActors,
|
||||
config::{Config, UrlKind},
|
||||
db::Db,
|
||||
error::MyError,
|
||||
requests::Requests,
|
||||
};
|
||||
use activitystreams::primitives::XsdAnyUri;
|
||||
use actix_web::web;
|
||||
use futures::try_join;
|
||||
use log::info;
|
||||
use lru::LruCache;
|
||||
@@ -8,13 +15,14 @@ use rsa::{RSAPrivateKey, RSAPublicKey};
|
||||
use std::{collections::HashSet, sync::Arc};
|
||||
use tokio::sync::RwLock;
|
||||
use ttl_cache::TtlCache;
|
||||
use uuid::Uuid;
|
||||
|
||||
pub type ActorCache = Arc<RwLock<TtlCache<XsdAnyUri, AcceptedActors>>>;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct State {
|
||||
pub settings: Settings,
|
||||
pub public_key: RSAPublicKey,
|
||||
private_key: RSAPrivateKey,
|
||||
config: Config,
|
||||
actor_cache: ActorCache,
|
||||
actor_id_cache: Arc<RwLock<LruCache<XsdAnyUri, XsdAnyUri>>>,
|
||||
blocks: Arc<RwLock<HashSet<String>>>,
|
||||
@@ -22,112 +30,20 @@ pub struct State {
|
||||
listeners: Arc<RwLock<HashSet<XsdAnyUri>>>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Settings {
|
||||
pub use_https: bool,
|
||||
pub whitelist_enabled: bool,
|
||||
pub hostname: String,
|
||||
pub public_key: RSAPublicKey,
|
||||
private_key: RSAPrivateKey,
|
||||
}
|
||||
|
||||
pub enum UrlKind {
|
||||
Activity,
|
||||
Actor,
|
||||
Followers,
|
||||
Following,
|
||||
Inbox,
|
||||
MainKey,
|
||||
NodeInfo,
|
||||
Outbox,
|
||||
}
|
||||
|
||||
impl Settings {
|
||||
async fn hydrate(
|
||||
db: &Db,
|
||||
use_https: bool,
|
||||
whitelist_enabled: bool,
|
||||
hostname: String,
|
||||
) -> Result<Self, MyError> {
|
||||
let private_key = if let Some(key) = db.hydrate_private_key().await? {
|
||||
key
|
||||
} else {
|
||||
info!("Generating new keys");
|
||||
let mut rng = thread_rng();
|
||||
let key = RSAPrivateKey::new(&mut rng, 4096)?;
|
||||
|
||||
db.update_private_key(&key).await?;
|
||||
|
||||
key
|
||||
};
|
||||
|
||||
let public_key = private_key.to_public_key();
|
||||
|
||||
Ok(Settings {
|
||||
use_https,
|
||||
whitelist_enabled,
|
||||
hostname,
|
||||
private_key,
|
||||
public_key,
|
||||
})
|
||||
}
|
||||
|
||||
fn generate_url(&self, kind: UrlKind) -> String {
|
||||
let scheme = if self.use_https { "https" } else { "http" };
|
||||
|
||||
match kind {
|
||||
UrlKind::Activity => {
|
||||
format!("{}://{}/activity/{}", scheme, self.hostname, Uuid::new_v4())
|
||||
}
|
||||
UrlKind::Actor => format!("{}://{}/actor", scheme, self.hostname),
|
||||
UrlKind::Followers => format!("{}://{}/followers", scheme, self.hostname),
|
||||
UrlKind::Following => format!("{}://{}/following", scheme, self.hostname),
|
||||
UrlKind::Inbox => format!("{}://{}/inbox", scheme, self.hostname),
|
||||
UrlKind::MainKey => format!("{}://{}/actor#main-key", scheme, self.hostname),
|
||||
UrlKind::NodeInfo => format!("{}://{}/nodeinfo/2.0", scheme, self.hostname),
|
||||
UrlKind::Outbox => format!("{}://{}/outbox", scheme, self.hostname),
|
||||
}
|
||||
}
|
||||
|
||||
fn generate_resource(&self) -> String {
|
||||
format!("relay@{}", self.hostname)
|
||||
}
|
||||
|
||||
fn software_name(&self) -> String {
|
||||
"AodeRelay".to_owned()
|
||||
}
|
||||
|
||||
fn software_version(&self) -> String {
|
||||
"v0.1.0-master".to_owned()
|
||||
}
|
||||
}
|
||||
|
||||
impl State {
|
||||
pub fn software_name(&self) -> String {
|
||||
self.settings.software_name()
|
||||
}
|
||||
|
||||
pub fn software_version(&self) -> String {
|
||||
self.settings.software_version()
|
||||
}
|
||||
|
||||
pub fn requests(&self) -> Requests {
|
||||
Requests::new(
|
||||
self.generate_url(UrlKind::MainKey),
|
||||
self.settings.private_key.clone(),
|
||||
self.config.generate_url(UrlKind::MainKey),
|
||||
self.private_key.clone(),
|
||||
self.actor_cache.clone(),
|
||||
format!("{} {}", self.software_name(), self.software_version()),
|
||||
format!(
|
||||
"{} {}",
|
||||
self.config.software_name(),
|
||||
self.config.software_version()
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn generate_url(&self, kind: UrlKind) -> String {
|
||||
self.settings.generate_url(kind)
|
||||
}
|
||||
|
||||
pub fn generate_resource(&self) -> String {
|
||||
self.settings.generate_resource()
|
||||
}
|
||||
|
||||
pub async fn bust_whitelist(&self, whitelist: &str) {
|
||||
let hs = self.whitelists.clone();
|
||||
|
||||
@@ -169,7 +85,7 @@ impl State {
|
||||
}
|
||||
|
||||
pub async fn is_whitelisted(&self, actor_id: &XsdAnyUri) -> bool {
|
||||
if !self.settings.whitelist_enabled {
|
||||
if !self.config.whitelist_mode() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -236,21 +152,36 @@ impl State {
|
||||
write_guard.insert(listener);
|
||||
}
|
||||
|
||||
pub async fn hydrate(
|
||||
use_https: bool,
|
||||
whitelist_enabled: bool,
|
||||
hostname: String,
|
||||
db: &Db,
|
||||
) -> Result<Self, MyError> {
|
||||
pub async fn hydrate(config: Config, db: &Db) -> Result<Self, MyError> {
|
||||
let f1 = db.hydrate_blocks();
|
||||
let f2 = db.hydrate_whitelists();
|
||||
let f3 = db.hydrate_listeners();
|
||||
let f4 = Settings::hydrate(db, use_https, whitelist_enabled, hostname);
|
||||
|
||||
let (blocks, whitelists, listeners, settings) = try_join!(f1, f2, f3, f4)?;
|
||||
let f4 = async move {
|
||||
if let Some(key) = db.hydrate_private_key().await? {
|
||||
Ok(key)
|
||||
} else {
|
||||
info!("Generating new keys");
|
||||
let key = web::block(move || {
|
||||
let mut rng = thread_rng();
|
||||
RSAPrivateKey::new(&mut rng, 4096)
|
||||
})
|
||||
.await?;
|
||||
|
||||
db.update_private_key(&key).await?;
|
||||
|
||||
Ok(key)
|
||||
}
|
||||
};
|
||||
|
||||
let (blocks, whitelists, listeners, private_key) = try_join!(f1, f2, f3, f4)?;
|
||||
|
||||
let public_key = private_key.to_public_key();
|
||||
|
||||
Ok(State {
|
||||
settings,
|
||||
public_key,
|
||||
private_key,
|
||||
config,
|
||||
actor_cache: Arc::new(RwLock::new(TtlCache::new(1024 * 8))),
|
||||
actor_id_cache: Arc::new(RwLock::new(LruCache::new(1024 * 8))),
|
||||
blocks: Arc::new(RwLock::new(blocks)),
|
||||
|
||||
+12
-9
@@ -1,4 +1,7 @@
|
||||
use crate::state::{State, UrlKind};
|
||||
use crate::{
|
||||
config::{Config, UrlKind},
|
||||
state::State,
|
||||
};
|
||||
use activitystreams::context;
|
||||
use actix_web::web::Data;
|
||||
use actix_webfinger::{Link, Resolver, Webfinger};
|
||||
@@ -11,19 +14,19 @@ pub struct RelayResolver;
|
||||
#[error("Error resolving webfinger data")]
|
||||
pub struct RelayError;
|
||||
|
||||
impl Resolver<Data<State>> for RelayResolver {
|
||||
impl Resolver<(Data<State>, Data<Config>)> for RelayResolver {
|
||||
type Error = RelayError;
|
||||
|
||||
fn find(
|
||||
account: &str,
|
||||
domain: &str,
|
||||
state: Data<State>,
|
||||
(state, config): (Data<State>, Data<Config>),
|
||||
) -> Pin<Box<dyn Future<Output = Result<Option<Webfinger>, Self::Error>>>> {
|
||||
let domain = domain.to_owned();
|
||||
let account = account.to_owned();
|
||||
|
||||
let fut = async move {
|
||||
if domain != state.settings.hostname {
|
||||
if domain != config.hostname() {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
@@ -31,13 +34,13 @@ impl Resolver<Data<State>> for RelayResolver {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
let mut wf = Webfinger::new(&state.generate_resource());
|
||||
wf.add_alias(&state.generate_url(UrlKind::Actor))
|
||||
.add_activitypub(&state.generate_url(UrlKind::Actor))
|
||||
.add_magic_public_key(&state.settings.public_key.as_magic_public_key())
|
||||
let mut wf = Webfinger::new(&config.generate_resource());
|
||||
wf.add_alias(&config.generate_url(UrlKind::Actor))
|
||||
.add_activitypub(&config.generate_url(UrlKind::Actor))
|
||||
.add_magic_public_key(&state.public_key.as_magic_public_key())
|
||||
.add_link(Link {
|
||||
rel: "self".to_owned(),
|
||||
href: Some(state.generate_url(UrlKind::Actor)),
|
||||
href: Some(config.generate_url(UrlKind::Actor)),
|
||||
template: None,
|
||||
kind: Some(format!("application/ld+json; profile=\"{}\"", context())),
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user