2020-12-23 18:30:19 +00:00
|
|
|
use crate::{
|
|
|
|
data::{ActorCache, State},
|
|
|
|
error::MyError,
|
|
|
|
middleware::MyVerify,
|
|
|
|
requests::Requests,
|
|
|
|
};
|
2020-09-07 21:51:02 +00:00
|
|
|
use activitystreams::{uri, url::Url};
|
2020-03-20 00:55:11 +00:00
|
|
|
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)]
|
2020-06-03 17:37:36 +00:00
|
|
|
pub struct ParsedConfig {
|
2020-03-20 00:55:11 +00:00
|
|
|
hostname: String,
|
|
|
|
addr: IpAddr,
|
|
|
|
port: u16,
|
|
|
|
debug: bool,
|
|
|
|
whitelist_mode: bool,
|
|
|
|
validate_signatures: bool,
|
|
|
|
https: bool,
|
|
|
|
database_url: String,
|
2020-03-20 15:09:42 +00:00
|
|
|
pretty_log: bool,
|
2020-03-22 04:50:14 +00:00
|
|
|
publish_blocks: bool,
|
2020-04-21 17:07:39 +00:00
|
|
|
max_connections: usize,
|
2020-03-20 00:55:11 +00:00
|
|
|
}
|
|
|
|
|
2020-06-03 17:37:36 +00:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct Config {
|
|
|
|
hostname: String,
|
|
|
|
addr: IpAddr,
|
|
|
|
port: u16,
|
|
|
|
debug: bool,
|
|
|
|
whitelist_mode: bool,
|
|
|
|
validate_signatures: bool,
|
|
|
|
database_url: String,
|
|
|
|
pretty_log: bool,
|
|
|
|
publish_blocks: bool,
|
|
|
|
max_connections: usize,
|
2020-06-20 04:11:02 +00:00
|
|
|
base_uri: Url,
|
2020-06-03 17:37:36 +00:00
|
|
|
}
|
|
|
|
|
2020-03-20 00:55:11 +00:00
|
|
|
pub enum UrlKind {
|
|
|
|
Activity,
|
|
|
|
Actor,
|
|
|
|
Followers,
|
|
|
|
Following,
|
|
|
|
Inbox,
|
2020-03-21 21:01:54 +00:00
|
|
|
Index,
|
2020-03-20 00:55:11 +00:00
|
|
|
MainKey,
|
2020-03-26 03:26:45 +00:00
|
|
|
Media(Uuid),
|
2020-03-20 00:55:11 +00:00
|
|
|
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)?
|
2020-03-20 15:09:42 +00:00
|
|
|
.set_default("pretty_log", true)?
|
2020-03-22 04:50:14 +00:00
|
|
|
.set_default("publish_blocks", false)?
|
2020-04-21 17:07:39 +00:00
|
|
|
.set_default("max_connections", 2)?
|
2020-03-20 00:55:11 +00:00
|
|
|
.merge(Environment::new())?;
|
|
|
|
|
2020-06-03 17:37:36 +00:00
|
|
|
let config: ParsedConfig = config.try_into()?;
|
|
|
|
|
|
|
|
let scheme = if config.https { "https" } else { "http" };
|
2020-06-03 21:01:28 +00:00
|
|
|
let base_uri = uri!(format!("{}://{}", scheme, config.hostname));
|
2020-06-03 17:37:36 +00:00
|
|
|
|
|
|
|
Ok(Config {
|
|
|
|
hostname: config.hostname,
|
|
|
|
addr: config.addr,
|
|
|
|
port: config.port,
|
|
|
|
debug: config.debug,
|
|
|
|
whitelist_mode: config.whitelist_mode,
|
|
|
|
validate_signatures: config.validate_signatures,
|
|
|
|
database_url: config.database_url,
|
|
|
|
pretty_log: config.pretty_log,
|
|
|
|
publish_blocks: config.publish_blocks,
|
|
|
|
max_connections: config.max_connections,
|
|
|
|
base_uri,
|
|
|
|
})
|
2020-03-20 00:55:11 +00:00
|
|
|
}
|
|
|
|
|
2020-03-20 15:09:42 +00:00
|
|
|
pub fn pretty_log(&self) -> bool {
|
|
|
|
self.pretty_log
|
|
|
|
}
|
|
|
|
|
2020-04-21 17:07:39 +00:00
|
|
|
pub fn max_connections(&self) -> usize {
|
|
|
|
self.max_connections
|
2020-03-22 23:21:40 +00:00
|
|
|
}
|
|
|
|
|
2020-03-20 03:23:10 +00:00
|
|
|
pub fn validate_signatures(&self) -> bool {
|
|
|
|
self.validate_signatures
|
|
|
|
}
|
|
|
|
|
2020-03-20 00:55:11 +00:00
|
|
|
pub fn digest_middleware(&self) -> VerifyDigest<Sha256> {
|
|
|
|
if self.validate_signatures {
|
|
|
|
VerifyDigest::new(Sha256::new())
|
|
|
|
} else {
|
|
|
|
VerifyDigest::new(Sha256::new()).optional()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-23 22:17:53 +00:00
|
|
|
pub fn signature_middleware(
|
|
|
|
&self,
|
|
|
|
requests: Requests,
|
|
|
|
actors: ActorCache,
|
2020-12-23 18:30:19 +00:00
|
|
|
state: State,
|
2020-03-23 22:17:53 +00:00
|
|
|
) -> VerifySignature<MyVerify> {
|
2020-03-20 00:55:11 +00:00
|
|
|
if self.validate_signatures {
|
2020-12-23 18:30:19 +00:00
|
|
|
VerifySignature::new(MyVerify(requests, actors, state), Default::default())
|
2020-03-20 00:55:11 +00:00
|
|
|
} else {
|
2020-12-23 18:30:19 +00:00
|
|
|
VerifySignature::new(MyVerify(requests, actors, state), Default::default()).optional()
|
2020-03-20 00:55:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn bind_address(&self) -> (IpAddr, u16) {
|
|
|
|
(self.addr, self.port)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn debug(&self) -> bool {
|
|
|
|
self.debug
|
|
|
|
}
|
|
|
|
|
2020-03-22 04:50:14 +00:00
|
|
|
pub fn publish_blocks(&self) -> bool {
|
|
|
|
self.publish_blocks
|
|
|
|
}
|
|
|
|
|
2020-03-20 00:55:11 +00:00
|
|
|
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 {
|
2020-07-25 22:33:11 +00:00
|
|
|
"v0.1.0-main".to_owned()
|
2020-03-20 00:55:11 +00:00
|
|
|
}
|
|
|
|
|
2020-03-20 04:06:16 +00:00
|
|
|
pub fn source_code(&self) -> String {
|
|
|
|
"https://git.asonix.dog/asonix/ap-relay".to_owned()
|
|
|
|
}
|
|
|
|
|
2020-06-20 04:11:02 +00:00
|
|
|
pub fn generate_url(&self, kind: UrlKind) -> Url {
|
|
|
|
let mut url = self.base_uri.clone();
|
2020-03-20 00:55:11 +00:00
|
|
|
|
|
|
|
match kind {
|
2020-06-03 17:37:36 +00:00
|
|
|
UrlKind::Activity => url.set_path(&format!("activity/{}", Uuid::new_v4())),
|
|
|
|
UrlKind::Actor => url.set_path("actor"),
|
|
|
|
UrlKind::Followers => url.set_path("followers"),
|
|
|
|
UrlKind::Following => url.set_path("following"),
|
|
|
|
UrlKind::Inbox => url.set_path("inbox"),
|
|
|
|
UrlKind::Index => (),
|
|
|
|
UrlKind::MainKey => {
|
|
|
|
url.set_path("actor");
|
|
|
|
url.set_fragment(Some("main-key"));
|
2020-03-20 00:55:11 +00:00
|
|
|
}
|
2020-06-03 17:37:36 +00:00
|
|
|
UrlKind::Media(uuid) => url.set_path(&format!("media/{}", uuid)),
|
|
|
|
UrlKind::NodeInfo => url.set_path("nodeinfo/2.0.json"),
|
|
|
|
UrlKind::Outbox => url.set_path("outbox"),
|
|
|
|
};
|
|
|
|
|
2020-06-20 04:11:02 +00:00
|
|
|
url
|
2020-03-20 00:55:11 +00:00
|
|
|
}
|
|
|
|
}
|