domainlink/src/main.rs
Neshura 20af5172b4
All checks were successful
Run Tests on Code / run-tests (push) Successful in 11s
Refactored User Config loading, closes #3
2024-04-10 18:55:17 +02:00

238 lines
7.8 KiB
Rust

use std::error::Error;
use std::fs;
use std::net::{IpAddr, Ipv6Addr};
use std::path::{Path, PathBuf};
use actix_web::{web, App, HttpResponse, HttpServer, get, Responder, HttpRequest};
use log::{LevelFilter};
use systemd_journal_logger::{connected_to_journal, JournalLog};
use serde::{Deserialize, Serialize};
macro_rules! info {
($msg:tt) => {
match connected_to_journal() {
true => log::info!("[INFO] {}", $msg),
false => println!("[INFO] {}", $msg),
}
};
}
macro_rules! warn {
($msg:tt) => {
match connected_to_journal() {
true => log::warn!("[WARN] {}", $msg),
false => println!("[WARN] {}", $msg),
}
};
}
macro_rules! error {
($msg:tt) => {
match connected_to_journal() {
true => log::error!("[ERROR] {}", $msg),
false => eprintln!("[ERROR] {}", $msg),
}
};
}
#[derive(Clone, Serialize, Deserialize)]
struct DomainLinkConfig {
domains: Vec<String>,
target: String,
}
#[derive(Clone, Serialize, Deserialize, Default)]
struct UserConfig {
domain_configs: Vec<DomainLinkConfig>
}
#[derive(Clone, Serialize, Deserialize)]
struct SystemConfig {
ports: Vec<u16>,
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();
let root_contents = match fs::read_dir("/") {
Ok(contents) => { contents }
Err(e) => {
error!(e);
return Err(Box::new(e));
}
};
root_contents.for_each(|directory| {
let path = directory.expect("Unexpected Error while Unwrapping the listed Dir Entry").path();
if path.is_dir() {
match path.display().to_string().as_str() {
"/root" => {
let mut root_configs = Self::load_user_config_directory(path);
user_config.domain_configs.append(&mut root_configs.domain_configs);
},
"/home" => {
match fs::read_dir(path) {
Ok(home_contents) => {
home_contents.for_each(|home_folder| {
let home_folder_path = home_folder.expect("Unexpected Error while Unwrapping the listed Home Dir Entry").path();
if home_folder_path.is_dir() {
let mut user_folder_configs = Self::load_user_config_directory(home_folder_path);
user_config.domain_configs.append(&mut user_folder_configs.domain_configs);
}
})
},
Err(e) => {
error!(e);
}
}
},
_ => {}
}
}
});
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 {
let cfg_path = path.join("config.toml");
if cfg_path.exists() {
system_path = path;
break;
}
};
let path = format!("{}/config.toml", system_path.display());
match confy::load_path(path.clone()) {
Ok(data) => {
let msg = format!("Using {}", path);
info!(msg);
Ok(Config {
user: user_config,
system: data,
})
},
Err(e) => {
error!(e);
Err(Box::new(e))
}
}
}
fn load_user_config_directory(path: PathBuf) -> UserConfig {
let config_path = format!("{}/.config/{}/domains.toml", path.display(), env!("CARGO_PKG_NAME"));
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);
UserConfig::default()
}
},
_ => {
error!(e);
UserConfig::default()
}
}
}
}
}
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
JournalLog::new().expect("Systemd-Logger crate error").install().expect("Systemd-Logger crate error");
log::set_max_level(LevelFilter::Info);
let config = Config::load().expect("Error while loading or generating the config");
let loaded_redirects_msg = format!("Loaded {} redirects from user config", config.user.domain_configs.len());
info!(loaded_redirects_msg);
let mut server = HttpServer::new(move || {
App::new()
.app_data(web::Data::new(config.user.domain_configs.clone()))
.service(status)
.service(handle)
});
for address in config.system.addresses.iter() {
for port in config.system.ports.iter() {
let msg = if address.is_ipv6() {
format!("Listening on [{address}]:{port}")
}
else {
format!("Listening on {address}:{port}")
};
info!(msg);
server = server.bind((*address, *port))?
}
}
server.run().await
}
#[get("/")]
async fn handle(redirects: web::Data<Vec<DomainLinkConfig>>, request: HttpRequest) -> impl Responder {
if let Some(host_raw) = request.headers().get("host") {
let host = host_raw.to_str().expect("host conversion to string should never fail");
println!("{host}");
for redirect in redirects.iter() {
if redirect.domains.contains(&host.to_owned()) {
return HttpResponse::PermanentRedirect().insert_header(("location", redirect.target.to_string().as_str())).finish();
}
}
let fail_msg = format!("No Redirect for {host} found");
return HttpResponse::NotFound().body(fail_msg)
}
HttpResponse::NotFound().body("Host not specified")
}
#[get("/status")]
async fn status(redirects: web::Data<Vec<DomainLinkConfig>>) -> impl Responder {
let mut body_msg = format!("Redirects Loaded: {}", redirects.len());
for redirect in redirects.iter() {
body_msg += "\n[";
for (idx, domain) in redirect.domains.iter().enumerate() {
body_msg += domain;
if idx != (redirect.domains.len() - 1) {
body_msg += ", ";
}
}
body_msg += format!("] => '{}'", redirect.target).as_str();
}
HttpResponse::Ok().body(body_msg)
}