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};
|
2021-02-10 04:05:06 +00:00
|
|
|
use std::{net::IpAddr, path::PathBuf};
|
2020-03-20 00:55:11 +00:00
|
|
|
use uuid::Uuid;
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, serde::Deserialize)]
|
2021-02-10 04:17:20 +00:00
|
|
|
pub(crate) struct ParsedConfig {
|
2020-03-20 00:55:11 +00:00
|
|
|
hostname: String,
|
|
|
|
addr: IpAddr,
|
|
|
|
port: u16,
|
|
|
|
debug: bool,
|
2021-02-10 04:05:06 +00:00
|
|
|
restricted_mode: bool,
|
2020-03-20 00:55:11 +00:00
|
|
|
validate_signatures: bool,
|
|
|
|
https: bool,
|
2020-03-20 15:09:42 +00:00
|
|
|
pretty_log: bool,
|
2020-03-22 04:50:14 +00:00
|
|
|
publish_blocks: bool,
|
2021-02-10 04:05:06 +00:00
|
|
|
sled_path: PathBuf,
|
2021-02-12 04:11:55 +00:00
|
|
|
source_repo: Url,
|
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,
|
2021-02-10 04:05:06 +00:00
|
|
|
restricted_mode: bool,
|
2020-06-03 17:37:36 +00:00
|
|
|
validate_signatures: bool,
|
|
|
|
pretty_log: bool,
|
|
|
|
publish_blocks: bool,
|
2020-06-20 04:11:02 +00:00
|
|
|
base_uri: Url,
|
2021-02-10 04:05:06 +00:00
|
|
|
sled_path: PathBuf,
|
2021-02-12 04:11:55 +00:00
|
|
|
source_repo: 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 {
|
2021-02-10 04:17:20 +00:00
|
|
|
pub(crate) fn build() -> Result<Self, MyError> {
|
2020-03-20 00:55:11 +00:00
|
|
|
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)?
|
2021-02-10 04:05:06 +00:00
|
|
|
.set_default("restricted_mode", false)?
|
2020-03-20 00:55:11 +00:00
|
|
|
.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)?
|
2021-02-10 04:05:06 +00:00
|
|
|
.set_default("sled_path", "./sled/db-0-34")?
|
2021-02-12 04:11:55 +00:00
|
|
|
.set_default("source_repo", "https://git.asonix.dog/asonix/relay")?
|
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,
|
2021-02-10 04:05:06 +00:00
|
|
|
restricted_mode: config.restricted_mode,
|
2020-06-03 17:37:36 +00:00
|
|
|
validate_signatures: config.validate_signatures,
|
|
|
|
pretty_log: config.pretty_log,
|
|
|
|
publish_blocks: config.publish_blocks,
|
|
|
|
base_uri,
|
2021-02-10 04:05:06 +00:00
|
|
|
sled_path: config.sled_path,
|
2021-02-12 04:11:55 +00:00
|
|
|
source_repo: config.source_repo,
|
2020-06-03 17:37:36 +00:00
|
|
|
})
|
2020-03-20 00:55:11 +00:00
|
|
|
}
|
|
|
|
|
2021-02-10 04:17:20 +00:00
|
|
|
pub(crate) fn sled_path(&self) -> &PathBuf {
|
2021-02-10 04:05:06 +00:00
|
|
|
&self.sled_path
|
|
|
|
}
|
|
|
|
|
2021-02-10 04:17:20 +00:00
|
|
|
pub(crate) fn pretty_log(&self) -> bool {
|
2020-03-20 15:09:42 +00:00
|
|
|
self.pretty_log
|
|
|
|
}
|
|
|
|
|
2021-02-10 04:17:20 +00:00
|
|
|
pub(crate) fn validate_signatures(&self) -> bool {
|
2020-03-20 03:23:10 +00:00
|
|
|
self.validate_signatures
|
|
|
|
}
|
|
|
|
|
2021-02-10 04:17:20 +00:00
|
|
|
pub(crate) fn digest_middleware(&self) -> VerifyDigest<Sha256> {
|
2020-03-20 00:55:11 +00:00
|
|
|
if self.validate_signatures {
|
|
|
|
VerifyDigest::new(Sha256::new())
|
|
|
|
} else {
|
|
|
|
VerifyDigest::new(Sha256::new()).optional()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-10 04:17:20 +00:00
|
|
|
pub(crate) fn signature_middleware(
|
2020-03-23 22:17:53 +00:00
|
|
|
&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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-10 04:17:20 +00:00
|
|
|
pub(crate) fn bind_address(&self) -> (IpAddr, u16) {
|
2020-03-20 00:55:11 +00:00
|
|
|
(self.addr, self.port)
|
|
|
|
}
|
|
|
|
|
2021-02-10 04:17:20 +00:00
|
|
|
pub(crate) fn debug(&self) -> bool {
|
2020-03-20 00:55:11 +00:00
|
|
|
self.debug
|
|
|
|
}
|
|
|
|
|
2021-02-10 04:17:20 +00:00
|
|
|
pub(crate) fn publish_blocks(&self) -> bool {
|
2020-03-22 04:50:14 +00:00
|
|
|
self.publish_blocks
|
|
|
|
}
|
|
|
|
|
2021-02-10 04:17:20 +00:00
|
|
|
pub(crate) fn restricted_mode(&self) -> bool {
|
2021-02-10 04:05:06 +00:00
|
|
|
self.restricted_mode
|
2020-03-20 00:55:11 +00:00
|
|
|
}
|
|
|
|
|
2021-02-10 04:17:20 +00:00
|
|
|
pub(crate) fn hostname(&self) -> &str {
|
2020-03-20 00:55:11 +00:00
|
|
|
&self.hostname
|
|
|
|
}
|
|
|
|
|
2021-02-10 04:17:20 +00:00
|
|
|
pub(crate) fn generate_resource(&self) -> String {
|
2020-03-20 00:55:11 +00:00
|
|
|
format!("relay@{}", self.hostname)
|
|
|
|
}
|
|
|
|
|
2021-02-10 04:17:20 +00:00
|
|
|
pub(crate) fn software_name(&self) -> String {
|
2020-03-20 00:55:11 +00:00
|
|
|
"AodeRelay".to_owned()
|
|
|
|
}
|
|
|
|
|
2021-02-10 04:17:20 +00:00
|
|
|
pub(crate) fn software_version(&self) -> String {
|
2021-02-10 04:05:06 +00:00
|
|
|
"v0.2.0-main".to_owned()
|
2020-03-20 00:55:11 +00:00
|
|
|
}
|
|
|
|
|
2021-02-12 04:11:55 +00:00
|
|
|
pub(crate) fn source_code(&self) -> &Url {
|
|
|
|
&self.source_repo
|
2020-03-20 04:06:16 +00:00
|
|
|
}
|
|
|
|
|
2021-02-10 04:17:20 +00:00
|
|
|
pub(crate) fn generate_url(&self, kind: UrlKind) -> Url {
|
2020-06-20 04:11:02 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|