Add optional publish-blocks option

This commit is contained in:
asonix 2020-03-21 23:50:14 -05:00
parent 82d837b963
commit d00961ae86
4 changed files with 36 additions and 12 deletions

View file

@ -21,7 +21,8 @@ $ ./relay -w asonix.dog blimps.xyz
$ ./relay -uw asonix.dog blimps.xyz
```
Whitelisted domains are only checked against incoming activities if `WHITELIST_MODE` is enabled
Whitelisted domains are only checked against incoming activities if `WHITELIST_MODE` is enabled.
Blocks can be published in the nodeinfo metadata by settings `PUBLISH_BLOCKS` to true
### Subscribing
Mastodon admins can subscribe to this relay by adding the `/inbox` route to their relay settings.
@ -61,6 +62,7 @@ VALIDATE_SIGNATURES=false
HTTPS=false
DATABASE_URL=
PRETTY_LOG=true
PUBLISH_BLOCKS=false
```
To run this server in production, you'll likely want to set most of them
```env
@ -73,6 +75,7 @@ VALIDATE_SIGNATURES=true
HTTPS=true
DATABASE_URL=postgres://pg_user:pg_pass@pg_host:pg_port/pg_database
PRETTY_LOG=false
PUBLISH_BLOCKS=true
```
### Contributing

View file

@ -16,6 +16,7 @@ pub struct Config {
https: bool,
database_url: String,
pretty_log: bool,
publish_blocks: bool,
}
pub enum UrlKind {
@ -42,6 +43,7 @@ impl Config {
.set_default("validate_signatures", false)?
.set_default("https", false)?
.set_default("pretty_log", true)?
.set_default("publish_blocks", false)?
.merge(Environment::new())?;
Ok(config.try_into()?)
@ -79,6 +81,10 @@ impl Config {
self.debug
}
pub fn publish_blocks(&self) -> bool {
self.publish_blocks
}
pub fn whitelist_mode(&self) -> bool {
self.whitelist_mode
}

View file

@ -4,22 +4,24 @@ use crate::{
};
use actix_web::{web, Responder};
use actix_webfinger::Link;
use serde_json::json;
pub async fn well_known(config: web::Data<Config>) -> impl Responder {
web::Json(json!({
"links": [
Link {
rel: "http://nodeinfo.diaspora.software/ns/schema/2.0".to_owned(),
href: Some(config.generate_url(UrlKind::NodeInfo)),
template: None,
kind: None,
}
]
}))
web::Json(Links {
links: vec![Link {
rel: "http://nodeinfo.diaspora.software/ns/schema/2.0".to_owned(),
href: Some(config.generate_url(UrlKind::NodeInfo)),
template: None,
kind: None,
}],
})
.with_header("Content-Type", "application/jrd+json")
}
#[derive(serde::Serialize)]
struct Links {
links: Vec<Link>,
}
pub async fn route(config: web::Data<Config>, state: web::Data<State>) -> web::Json<NodeInfo> {
web::Json(NodeInfo {
version: NodeInfoVersion,
@ -50,6 +52,11 @@ pub async fn route(config: web::Data<Config>, state: web::Data<State>) -> web::J
.filter_map(|listener| listener.as_url().domain())
.map(|s| s.to_owned())
.collect(),
blocks: if config.publish_blocks() {
Some(state.blocks().await)
} else {
None
},
},
})
}
@ -102,6 +109,9 @@ pub struct Usage {
#[derive(Clone, Debug, Default, serde::Serialize)]
pub struct Metadata {
peers: Vec<String>,
#[serde(skip_serializing_if = "Option::is_none")]
blocks: Option<Vec<String>>,
}
#[derive(Clone, Debug, Default, serde::Serialize)]

View file

@ -65,6 +65,11 @@ impl State {
read_guard.iter().cloned().collect()
}
pub async fn blocks(&self) -> Vec<String> {
let read_guard = self.blocks.read().await;
read_guard.iter().cloned().collect()
}
pub async fn listeners_without(&self, inbox: &XsdAnyUri, domain: &str) -> Vec<XsdAnyUri> {
let read_guard = self.listeners.read().await;