implemented split user/system config with unified service at the top due to port conflicts

This commit is contained in:
Neshura 2024-04-08 22:12:22 +02:00
parent 2f9ef48c3d
commit 483fe1e649
Signed by: Neshura
GPG key ID: B6983AAA6B9A7A6C
2 changed files with 111 additions and 40 deletions

View file

@ -24,5 +24,7 @@ systemd-units = { enable = false }
[dependencies] [dependencies]
actix-web = "4" actix-web = "4"
confy = "0.6"
log = "0.4" log = "0.4"
systemd-journal-logger = "2" systemd-journal-logger = "2"
serde = { version = "1.0.197", features = ["derive"] }

View file

@ -1,8 +1,12 @@
use std::fmt::{Display, Formatter}; use std::error::Error;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; use std::fmt::{Display, format, Formatter};
use std::fs;
use std::net::{IpAddr, Ipv6Addr};
use std::path::Path;
use actix_web::{web, App, HttpResponse, HttpServer, get, Responder, HttpRequest}; use actix_web::{web, App, HttpResponse, HttpServer, get, Responder, HttpRequest};
use log::{LevelFilter}; use log::{LevelFilter};
use systemd_journal_logger::{connected_to_journal, JournalLog}; use systemd_journal_logger::{connected_to_journal, JournalLog};
use serde::{Deserialize, Serialize};
macro_rules! info { macro_rules! info {
@ -32,7 +36,7 @@ macro_rules! error {
}; };
} }
#[derive(Clone)] #[derive(Clone, Serialize, Deserialize)]
enum Protocol { enum Protocol {
Http, Http,
Https Https
@ -47,64 +51,129 @@ impl Display for Protocol {
} }
} }
#[derive(Clone)] #[derive(Clone, Serialize, Deserialize)]
struct DomainLinkConfig { struct DomainLinkConfig {
domain: String, domain: String,
protocol: Protocol, protocol: Protocol,
target: String, target: String,
} }
#[derive(Clone)] #[derive(Clone, Serialize, Deserialize, Default)]
struct Config { struct UserConfig {
redirects: Vec<DomainLinkConfig>, domain_configs: Vec<DomainLinkConfig>
}
#[derive(Clone, Serialize, Deserialize)]
struct SystemConfig {
ports: Vec<u16>, ports: Vec<u16>,
addresses: Vec<IpAddr>, addresses: Vec<IpAddr>,
} }
impl Default for SystemConfig {
fn default() -> Self {
Self {
ports: vec![8000],
addresses: vec![IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0))],
}
}
}
#[derive(Clone)]
struct Config {
user: UserConfig,
system: SystemConfig,
}
impl Config {
pub fn load() -> Result<Self, Box<dyn Error>> {
// get list of home directories
// query every home directory for a config file (just attempt a load, an empty config is perfectly fine)
// merge all configs into one
let mut user_config = UserConfig::default();
for entry in fs::read_dir("/home").expect("home directory is expected") {
let entry = entry.expect("home directory is expected to have at least one directory");
let path = entry.path();
if path.is_dir() {
let config_path = format!("{}/.config/{}/config.toml", path.display().to_string().as_str(), env!("CARGO_PKG_NAME"));
let mut path_config: UserConfig = match confy::load_path(config_path) {
Ok(data) => data,
Err(e) => {
match &e {
confy::ConfyError::GeneralLoadError(os_error) => {
if os_error.raw_os_error() == Some(13) {
let msg = format!("Missing read permissions for {}, skipping", path.display().to_string().as_str());
warn!(msg);
UserConfig::default()
}
else {
error!(e);
return Err(Box::new(e));
}
},
_ => {
error!(e);
return Err(Box::new(e));
}
}
}
};
user_config.domain_configs.append(&mut path_config.domain_configs)
}
}
let etc_path = format!("/etc/{}", env!("CARGO_PKG_NAME"));
let usr_path = format!("/usr/local/share/{}", env!("CARGO_PKG_NAME"));
let system_config_paths = vec![
Path::new(&etc_path),
Path::new(&usr_path),
];
let mut system_path= system_config_paths[1];
for path in system_config_paths {
if path.exists() {
system_path = path;
}
};
let path = format!("{}/config.toml", system_path.display().to_string().as_str());
match confy::load_path(path) {
Ok(data) => {
Ok(Config {
user: user_config,
system: data,
})
},
Err(e) => {
error!(e);
Err(Box::new(e))
}
}
}
}
#[actix_web::main] #[actix_web::main]
async fn main() -> std::io::Result<()> { async fn main() -> std::io::Result<()> {
JournalLog::new().expect("Systemd-Logger crate error").install().expect("Systemd-Logger crate error"); JournalLog::new().expect("Systemd-Logger crate error").install().expect("Systemd-Logger crate error");
log::set_max_level(LevelFilter::Info); log::set_max_level(LevelFilter::Info);
let config = Config { let config = Config::load().expect("Error while loading or generating the config");
redirects: vec![
DomainLinkConfig { let loaded_redirects_msg = format!("Loaded {} redirects from user config", config.user.domain_configs.len());
domain: "neshura.me".to_owned(), info!(loaded_redirects_msg);
protocol: Protocol::Https,
target: "neshweb.net".to_owned(),
},
DomainLinkConfig {
domain: "lemmy.neshura.me".to_owned(),
protocol: Protocol::Https,
target: "bookwormstory.social/u/neshura".to_owned()
},
DomainLinkConfig {
domain: "test2.neshura.me".to_owned(),
protocol: Protocol::Https,
target: "neshweb.net".to_owned()
}
],
ports: vec![8080, 8090],
addresses: vec![
IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)),
IpAddr::V4(Ipv4Addr::new(192, 168, 178, 11))
],
};
let msg = "This Build is not intended for production use!";
info!("Test Info");
warn!(msg);
error!(msg);
let mut server = HttpServer::new(move || { let mut server = HttpServer::new(move || {
App::new() App::new()
.app_data(web::Data::new(config.redirects.clone())) .app_data(web::Data::new(config.user.domain_configs.clone()))
.service(handle) .service(handle)
.service(dry_handle) .service(dry_handle)
}); });
for address in config.addresses.iter() { for address in config.system.addresses.iter() {
for port in config.ports.iter() { for port in config.system.ports.iter() {
let msg = format!("Listening on {address}:{port}");
info!(msg);
server = server.bind((*address, *port))? server = server.bind((*address, *port))?
} }
} }