2020-11-29 18:37:33 +00:00
|
|
|
// False positive with itertools::process_results
|
|
|
|
#![allow (clippy::redundant_closure)]
|
|
|
|
|
2020-11-26 23:30:33 +00:00
|
|
|
use std::{
|
2021-05-02 21:59:04 +00:00
|
|
|
collections::{
|
|
|
|
HashMap,
|
|
|
|
},
|
2020-12-12 17:11:22 +00:00
|
|
|
convert::{TryFrom},
|
2021-04-27 23:48:22 +00:00
|
|
|
net::IpAddr,
|
2020-11-26 23:30:33 +00:00
|
|
|
path::Path,
|
2021-04-27 23:48:22 +00:00
|
|
|
str::FromStr,
|
2020-11-26 23:30:33 +00:00
|
|
|
};
|
|
|
|
|
2020-12-16 14:46:03 +00:00
|
|
|
use crate::{
|
|
|
|
errors::ConfigError,
|
2021-03-21 03:34:47 +00:00
|
|
|
key_validity::{
|
|
|
|
ScraperKey,
|
|
|
|
},
|
2020-12-16 14:46:03 +00:00
|
|
|
};
|
2020-11-26 23:30:33 +00:00
|
|
|
|
2021-04-27 19:31:58 +00:00
|
|
|
/// Machine-editable configs.
|
|
|
|
/// These are stored in the `data` directory and shouldn't be touched by
|
2021-05-02 22:32:24 +00:00
|
|
|
/// humans. `ptth_relay` will re-write them while it's running.
|
2021-04-27 19:31:58 +00:00
|
|
|
|
2021-04-27 22:22:07 +00:00
|
|
|
pub mod machine_editable {
|
|
|
|
use std::{
|
2021-05-02 21:59:04 +00:00
|
|
|
collections::BTreeMap,
|
2021-04-27 22:22:07 +00:00
|
|
|
path::Path,
|
|
|
|
};
|
|
|
|
|
2021-04-27 19:31:58 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
|
|
use super::file::Server;
|
|
|
|
|
2021-05-02 21:59:04 +00:00
|
|
|
#[derive (Deserialize, Serialize)]
|
|
|
|
pub struct ConfigFile {
|
2021-04-27 22:22:07 +00:00
|
|
|
pub servers: Vec <Server>,
|
|
|
|
}
|
|
|
|
|
2021-05-02 21:59:04 +00:00
|
|
|
#[derive (Default)]
|
|
|
|
pub struct Config {
|
|
|
|
pub servers: BTreeMap <String, Server>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ConfigFile {
|
2021-04-29 02:52:43 +00:00
|
|
|
pub fn from_file (path: &Path) -> Result <Self, crate::ConfigError>
|
2021-04-27 22:22:07 +00:00
|
|
|
{
|
2021-04-29 02:52:43 +00:00
|
|
|
let config_s = std::fs::read_to_string (path)?;
|
2021-05-02 21:59:04 +00:00
|
|
|
Ok (toml::from_str (&config_s)?)
|
2021-04-27 22:22:07 +00:00
|
|
|
}
|
2021-04-29 02:52:43 +00:00
|
|
|
|
2021-05-02 21:59:04 +00:00
|
|
|
pub async fn save (&self, path: &Path) -> Result <(), crate::ConfigError>
|
|
|
|
{
|
2021-04-29 02:52:43 +00:00
|
|
|
let s = toml::to_string (self)?;
|
|
|
|
// This is way easier in C++ but also not safe
|
|
|
|
let mut temp_path = path.file_name ().unwrap ().to_os_string ();
|
|
|
|
temp_path.push (".partial");
|
|
|
|
let temp_path = path.with_file_name (temp_path);
|
|
|
|
tokio::fs::write (&temp_path, &s).await?;
|
|
|
|
tokio::fs::rename (&temp_path, path).await?;
|
|
|
|
Ok (())
|
|
|
|
}
|
2021-04-27 19:31:58 +00:00
|
|
|
}
|
2021-05-02 21:59:04 +00:00
|
|
|
|
|
|
|
impl Config {
|
|
|
|
pub fn from_file (path: &Path) -> Result <Self, crate::ConfigError>
|
|
|
|
{
|
|
|
|
let c = ConfigFile::from_file (path)?;
|
|
|
|
|
|
|
|
let servers = c.servers.into_iter ()
|
|
|
|
.map (|s| (s.name.clone (), s))
|
|
|
|
.collect ();
|
|
|
|
|
|
|
|
Ok (Self {
|
|
|
|
servers,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn save (&self, path: &Path) -> Result <(), crate::ConfigError>
|
|
|
|
{
|
|
|
|
let servers = self.servers.values ()
|
|
|
|
.cloned ().into_iter ().collect ();
|
|
|
|
|
|
|
|
let c = ConfigFile {
|
|
|
|
servers,
|
|
|
|
};
|
|
|
|
c.save (path).await?;
|
|
|
|
Ok (())
|
|
|
|
}
|
|
|
|
}
|
2021-04-27 19:31:58 +00:00
|
|
|
}
|
|
|
|
|
2021-04-18 13:51:36 +00:00
|
|
|
/// Config fields as they are loaded from the config file
|
2020-11-26 23:30:33 +00:00
|
|
|
|
|
|
|
pub mod file {
|
2021-04-27 19:31:58 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2020-11-26 23:30:33 +00:00
|
|
|
|
2021-03-21 03:34:47 +00:00
|
|
|
use crate::key_validity::{
|
|
|
|
BlakeHashWrapper,
|
|
|
|
ScraperKey,
|
|
|
|
};
|
2020-12-12 15:35:33 +00:00
|
|
|
|
2021-05-02 21:59:04 +00:00
|
|
|
#[derive (Clone, Debug, Deserialize, Serialize)]
|
2020-11-26 23:30:33 +00:00
|
|
|
pub struct Server {
|
2021-04-18 13:51:36 +00:00
|
|
|
/// This is duplicated in the hashmap, but it's not a problem
|
2020-11-30 15:52:15 +00:00
|
|
|
pub name: String,
|
2021-04-18 13:51:36 +00:00
|
|
|
|
2020-12-12 15:50:38 +00:00
|
|
|
pub tripcode: BlakeHashWrapper,
|
2021-04-18 13:51:36 +00:00
|
|
|
|
|
|
|
/// This allows a relay-side rename of servers
|
2020-11-26 23:30:33 +00:00
|
|
|
pub display_name: Option <String>,
|
|
|
|
}
|
|
|
|
|
2021-04-18 13:51:36 +00:00
|
|
|
/// Empty
|
|
|
|
|
2020-12-12 17:11:22 +00:00
|
|
|
#[derive (Deserialize)]
|
|
|
|
pub struct DevMode {
|
2020-12-16 14:46:03 +00:00
|
|
|
|
2020-12-12 17:11:22 +00:00
|
|
|
}
|
|
|
|
|
2021-04-18 13:51:36 +00:00
|
|
|
/// Config fields that are identical in the file and at runtime
|
|
|
|
|
2020-12-12 01:26:58 +00:00
|
|
|
#[derive (Default, Deserialize)]
|
|
|
|
pub struct Isomorphic {
|
|
|
|
#[serde (default)]
|
2020-12-14 07:07:13 +00:00
|
|
|
pub enable_scraper_api: bool,
|
2020-12-12 15:35:33 +00:00
|
|
|
|
2021-04-18 13:51:36 +00:00
|
|
|
/// If any of the `DevMode` fields are used, we are in dev mode
|
|
|
|
/// and have to show extra warnings, since auth may be weakened
|
2020-12-12 17:11:22 +00:00
|
|
|
pub dev_mode: Option <DevMode>,
|
2020-12-12 01:26:58 +00:00
|
|
|
}
|
|
|
|
|
2020-11-26 23:30:33 +00:00
|
|
|
#[derive (Deserialize)]
|
|
|
|
pub struct Config {
|
2020-12-12 01:26:58 +00:00
|
|
|
#[serde (flatten)]
|
|
|
|
pub iso: Isomorphic,
|
2020-12-16 14:46:03 +00:00
|
|
|
|
2021-04-27 23:48:22 +00:00
|
|
|
pub address: Option <String>,
|
2020-12-16 14:46:03 +00:00
|
|
|
pub port: Option <u16>,
|
2021-03-15 19:11:57 +00:00
|
|
|
pub servers: Option <Vec <Server>>,
|
2020-12-16 14:46:03 +00:00
|
|
|
|
|
|
|
// Adding a DB will take a while, so I'm moving these out of dev mode.
|
2021-08-27 23:34:38 +00:00
|
|
|
pub scraper_keys: Option <Vec <ScraperKey>>,
|
2021-04-27 19:10:11 +00:00
|
|
|
|
|
|
|
pub news_url: Option <String>,
|
2020-11-26 23:30:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-18 13:51:36 +00:00
|
|
|
/// Config fields as they are used at runtime
|
2020-11-26 23:30:33 +00:00
|
|
|
|
|
|
|
pub struct Config {
|
2020-12-16 14:46:03 +00:00
|
|
|
pub iso: file::Isomorphic,
|
|
|
|
|
2021-04-27 23:48:22 +00:00
|
|
|
pub address: IpAddr,
|
2020-12-13 01:54:54 +00:00
|
|
|
pub port: Option <u16>,
|
2020-12-12 15:57:22 +00:00
|
|
|
pub servers: HashMap <String, file::Server>,
|
2021-08-27 23:34:38 +00:00
|
|
|
pub scraper_keys: HashMap <String, ScraperKey>,
|
2021-04-27 19:10:11 +00:00
|
|
|
pub news_url: Option <String>,
|
2020-11-26 23:30:33 +00:00
|
|
|
}
|
|
|
|
|
2021-04-27 23:48:22 +00:00
|
|
|
impl Default for Config {
|
|
|
|
fn default () -> Self {
|
|
|
|
Self {
|
|
|
|
iso: Default::default (),
|
|
|
|
address: IpAddr::from ([0, 0, 0, 0]),
|
|
|
|
port: None,
|
|
|
|
servers: Default::default (),
|
|
|
|
scraper_keys: Default::default (),
|
|
|
|
news_url: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-26 23:30:33 +00:00
|
|
|
impl TryFrom <file::Config> for Config {
|
|
|
|
type Error = ConfigError;
|
|
|
|
|
|
|
|
fn try_from (f: file::Config) -> Result <Self, Self::Error> {
|
2021-03-15 19:11:57 +00:00
|
|
|
let servers = f.servers.unwrap_or_else (|| vec! []);
|
|
|
|
let servers = servers.into_iter ().map (|server| Ok::<_, ConfigError> ((server.name.clone (), server)));
|
2020-11-26 23:30:33 +00:00
|
|
|
|
2021-03-21 03:34:47 +00:00
|
|
|
let servers = itertools::process_results (servers, |i| i.collect ())?;
|
2020-11-26 23:30:33 +00:00
|
|
|
|
2021-03-15 19:11:57 +00:00
|
|
|
let scraper_keys = f.scraper_keys.unwrap_or_else (|| vec! []);
|
2020-12-16 14:46:03 +00:00
|
|
|
let scraper_keys = if f.iso.enable_scraper_api {
|
2021-03-21 03:34:47 +00:00
|
|
|
scraper_keys.into_iter ().map (|key| (key.hash.encode_base64 (), key)).collect ()
|
2020-12-16 14:46:03 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
Default::default ()
|
|
|
|
};
|
|
|
|
|
2020-11-26 23:30:33 +00:00
|
|
|
Ok (Self {
|
2020-12-16 14:46:03 +00:00
|
|
|
iso: f.iso,
|
2021-04-27 23:48:22 +00:00
|
|
|
address: parse_address (f.address.as_ref ().map (|s| &s[..]))?,
|
2020-12-13 01:54:54 +00:00
|
|
|
port: f.port,
|
2020-11-26 23:30:33 +00:00
|
|
|
servers,
|
2020-12-16 14:46:03 +00:00
|
|
|
scraper_keys,
|
2021-05-02 22:32:24 +00:00
|
|
|
news_url: f.news_url,
|
2020-11-26 23:30:33 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-27 23:48:22 +00:00
|
|
|
fn parse_address (s: Option <&str>) -> Result <IpAddr, ConfigError> {
|
|
|
|
Ok (s
|
|
|
|
.map (|s| IpAddr::from_str (s))
|
|
|
|
.transpose ().map_err (|_| ConfigError::BadServerAddress)?
|
2021-05-02 22:32:24 +00:00
|
|
|
.unwrap_or_else (|| IpAddr::from ([0, 0, 0, 0]))
|
2021-04-27 23:48:22 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-11-26 23:30:33 +00:00
|
|
|
impl Config {
|
|
|
|
pub async fn from_file (path: &Path) -> Result <Self, ConfigError> {
|
2021-03-06 21:15:41 +00:00
|
|
|
let config_s = tokio::fs::read_to_string (path).await?;
|
2020-11-26 23:30:33 +00:00
|
|
|
let new_config: file::Config = toml::from_str (&config_s)?;
|
|
|
|
|
|
|
|
Self::try_from (new_config)
|
|
|
|
}
|
|
|
|
}
|
2021-04-27 23:48:22 +00:00
|
|
|
|
|
|
|
#[cfg (test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn ip_address () {
|
|
|
|
for (input, expected) in vec! [
|
|
|
|
(None, Some (IpAddr::from ([0, 0, 0, 0]))),
|
|
|
|
(Some ("bogus"), None),
|
|
|
|
(Some ("0.0.0.0"), Some (IpAddr::from ([0, 0, 0, 0]))),
|
|
|
|
(Some ("0"), None),
|
|
|
|
(Some ("127.0.0.1"), Some (IpAddr::from ([127, 0, 0, 1]))),
|
|
|
|
(Some ("10.0.0.1"), Some (IpAddr::from ([10, 0, 0, 1]))),
|
|
|
|
].into_iter () {
|
|
|
|
let actual = parse_address (input).ok ();
|
|
|
|
assert_eq! (actual, expected);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|