relay/src/config.rs

181 lines
4.8 KiB
Rust
Raw Normal View History

use crate::{
data::{ActorCache, State},
error::MyError,
middleware::MyVerify,
requests::Requests,
};
2020-09-07 21:51:02 +00:00
use activitystreams::{uri, url::Url};
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};
use uuid::Uuid;
#[derive(Clone, Debug, serde::Deserialize)]
2021-02-10 04:17:20 +00:00
pub(crate) struct ParsedConfig {
hostname: String,
addr: IpAddr,
port: u16,
debug: bool,
2021-02-10 04:05:06 +00:00
restricted_mode: bool,
validate_signatures: bool,
https: bool,
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,
}
#[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,
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,
}
pub enum UrlKind {
Activity,
Actor,
Followers,
Following,
Inbox,
Index,
MainKey,
Media(Uuid),
NodeInfo,
Outbox,
}
impl Config {
2021-02-10 04:17:20 +00:00
pub(crate) 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)?
2021-02-10 04:05:06 +00:00
.set_default("restricted_mode", false)?
.set_default("validate_signatures", false)?
.set_default("https", false)?
.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")?
.merge(Environment::new())?;
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));
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,
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-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 {
self.pretty_log
}
2021-02-10 04:17:20 +00:00
pub(crate) fn validate_signatures(&self) -> bool {
self.validate_signatures
}
2021-02-10 04:17:20 +00:00
pub(crate) fn digest_middleware(&self) -> VerifyDigest<Sha256> {
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,
state: State,
2020-03-23 22:17:53 +00:00
) -> VerifySignature<MyVerify> {
if self.validate_signatures {
VerifySignature::new(MyVerify(requests, actors, state), Default::default())
} else {
VerifySignature::new(MyVerify(requests, actors, state), Default::default()).optional()
}
}
2021-02-10 04:17:20 +00:00
pub(crate) fn bind_address(&self) -> (IpAddr, u16) {
(self.addr, self.port)
}
2021-02-10 04:17:20 +00:00
pub(crate) fn debug(&self) -> bool {
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
}
2021-02-10 04:17:20 +00:00
pub(crate) fn hostname(&self) -> &str {
&self.hostname
}
2021-02-10 04:17:20 +00:00
pub(crate) fn generate_resource(&self) -> String {
format!("relay@{}", self.hostname)
}
2021-02-10 04:17:20 +00:00
pub(crate) fn software_name(&self) -> String {
"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()
}
2021-02-10 04:17:20 +00:00
pub(crate) fn source_code(&self) -> String {
2020-03-20 04:06:16 +00:00
"https://git.asonix.dog/asonix/ap-relay".to_owned()
}
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();
match kind {
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"));
}
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
}
}