Add hot reloading of redirect config files
All checks were successful
Run Tests on Code / run-tests (push) Successful in 47s

This commit is contained in:
Neshura 2024-04-10 22:19:50 +02:00
parent 20af5172b4
commit 028067b62a
Signed by: Neshura
GPG key ID: B6983AAA6B9A7A6C
3 changed files with 215 additions and 23 deletions

146
Cargo.lock generated
View file

@ -380,6 +380,21 @@ dependencies = [
"cfg-if", "cfg-if",
] ]
[[package]]
name = "crossbeam-channel"
version = "0.5.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ab3db02a9c5b5121e1e42fbdb1aeb65f5e02624cc58c43f2884c6ccac0b82f95"
dependencies = [
"crossbeam-utils",
]
[[package]]
name = "crossbeam-utils"
version = "0.8.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "248e3bacc7dc6baa3b21e405ee045c3047101a49145e7e9eca583ab4c2ca5345"
[[package]] [[package]]
name = "crypto-common" name = "crypto-common"
version = "0.1.6" version = "0.1.6"
@ -450,6 +465,7 @@ dependencies = [
"actix-web", "actix-web",
"confy", "confy",
"log", "log",
"notify",
"serde", "serde",
"systemd-journal-logger", "systemd-journal-logger",
] ]
@ -479,6 +495,18 @@ dependencies = [
"windows-sys 0.52.0", "windows-sys 0.52.0",
] ]
[[package]]
name = "filetime"
version = "0.2.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ee447700ac8aa0b2f2bd7bc4462ad686ba06baa6727ac149a2d6277f0d240fd"
dependencies = [
"cfg-if",
"libc",
"redox_syscall",
"windows-sys 0.52.0",
]
[[package]] [[package]]
name = "flate2" name = "flate2"
version = "1.0.28" version = "1.0.28"
@ -504,6 +532,15 @@ dependencies = [
"percent-encoding", "percent-encoding",
] ]
[[package]]
name = "fsevent-sys"
version = "4.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "76ee7a02da4d231650c7cea31349b889be2f45ddb3ef3032d2ec8185f6313fd2"
dependencies = [
"libc",
]
[[package]] [[package]]
name = "futures-core" name = "futures-core"
version = "0.3.30" version = "0.3.30"
@ -629,6 +666,26 @@ dependencies = [
"hashbrown", "hashbrown",
] ]
[[package]]
name = "inotify"
version = "0.9.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8069d3ec154eb856955c1c0fbffefbf5f3c40a104ec912d4797314c1801abff"
dependencies = [
"bitflags 1.3.2",
"inotify-sys",
"libc",
]
[[package]]
name = "inotify-sys"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e05c02b5e89bff3b946cedeca278abc628fe811e604f027c45a8aa3cf793d0eb"
dependencies = [
"libc",
]
[[package]] [[package]]
name = "itoa" name = "itoa"
version = "1.0.11" version = "1.0.11"
@ -644,6 +701,26 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "kqueue"
version = "1.0.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7447f1ca1b7b563588a205fe93dea8df60fd981423a768bc1c0ded35ed147d0c"
dependencies = [
"kqueue-sys",
"libc",
]
[[package]]
name = "kqueue-sys"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed9625ffda8729b85e45cf04090035ac368927b8cebc34898e7c120f52e4838b"
dependencies = [
"bitflags 1.3.2",
"libc",
]
[[package]] [[package]]
name = "language-tags" name = "language-tags"
version = "0.3.2" version = "0.3.2"
@ -741,6 +818,25 @@ dependencies = [
"windows-sys 0.48.0", "windows-sys 0.48.0",
] ]
[[package]]
name = "notify"
version = "6.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6205bd8bb1e454ad2e27422015fb5e4f2bcc7e08fa8f27058670d208324a4d2d"
dependencies = [
"bitflags 2.5.0",
"crossbeam-channel",
"filetime",
"fsevent-sys",
"inotify",
"kqueue",
"libc",
"log",
"mio",
"walkdir",
"windows-sys 0.48.0",
]
[[package]] [[package]]
name = "num-conv" name = "num-conv"
version = "0.1.0" version = "0.1.0"
@ -964,6 +1060,15 @@ version = "1.0.17"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e86697c916019a8588c99b5fac3cead74ec0b4b819707a682fd4d23fa0ce1ba1" checksum = "e86697c916019a8588c99b5fac3cead74ec0b4b819707a682fd4d23fa0ce1ba1"
[[package]]
name = "same-file"
version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502"
dependencies = [
"winapi-util",
]
[[package]] [[package]]
name = "scopeguard" name = "scopeguard"
version = "1.2.0" version = "1.2.0"
@ -1306,12 +1411,53 @@ version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "walkdir"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b"
dependencies = [
"same-file",
"winapi-util",
]
[[package]] [[package]]
name = "wasi" name = "wasi"
version = "0.11.0+wasi-snapshot-preview1" version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596"
dependencies = [
"winapi",
]
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]] [[package]]
name = "windows-sys" name = "windows-sys"
version = "0.48.0" version = "0.48.0"

View file

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

View file

@ -2,8 +2,11 @@ use std::error::Error;
use std::fs; use std::fs;
use std::net::{IpAddr, Ipv6Addr}; use std::net::{IpAddr, Ipv6Addr};
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::sync::RwLock;
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 notify::{Event, EventKind, RecursiveMode, Watcher};
use notify::event::{AccessKind, AccessMode};
use systemd_journal_logger::{connected_to_journal, JournalLog}; use systemd_journal_logger::{connected_to_journal, JournalLog};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -68,10 +71,11 @@ struct Config {
} }
impl Config { impl Config {
pub fn load() -> Result<Self, Box<dyn Error>> { pub fn load() -> Result<(Self, Vec<PathBuf>), Box<dyn Error>> {
// get list of home directories // get list of home directories
// query every home directory for a config file (just attempt a load, an empty config is perfectly fine) // query every home directory for a config file (just attempt a load, an empty config is perfectly fine)
// merge all configs into one // merge all configs into one
let mut directories: Vec<PathBuf> = vec![];
let mut user_config = UserConfig::default(); let mut user_config = UserConfig::default();
let root_contents = match fs::read_dir("/") { let root_contents = match fs::read_dir("/") {
Ok(contents) => { contents } Ok(contents) => { contents }
@ -86,7 +90,10 @@ impl Config {
if path.is_dir() { if path.is_dir() {
match path.display().to_string().as_str() { match path.display().to_string().as_str() {
"/root" => { "/root" => {
let mut root_configs = Self::load_user_config_directory(path); let (mut root_configs, root_path) = Self::load_user_config_directory(path);
if let Some(root_directory) = root_path {
directories.push(Path::new(root_directory.as_str()).to_path_buf());
}
user_config.domain_configs.append(&mut root_configs.domain_configs); user_config.domain_configs.append(&mut root_configs.domain_configs);
}, },
"/home" => { "/home" => {
@ -95,7 +102,10 @@ impl Config {
home_contents.for_each(|home_folder| { home_contents.for_each(|home_folder| {
let home_folder_path = home_folder.expect("Unexpected Error while Unwrapping the listed Home Dir Entry").path(); let home_folder_path = home_folder.expect("Unexpected Error while Unwrapping the listed Home Dir Entry").path();
if home_folder_path.is_dir() { if home_folder_path.is_dir() {
let mut user_folder_configs = Self::load_user_config_directory(home_folder_path); let (mut user_folder_configs, user_folder_path) = Self::load_user_config_directory(home_folder_path);
if let Some(user_folder_directory) = user_folder_path {
directories.push(Path::new(user_folder_directory.as_str()).to_path_buf());
}
user_config.domain_configs.append(&mut user_folder_configs.domain_configs); user_config.domain_configs.append(&mut user_folder_configs.domain_configs);
} }
}) })
@ -132,11 +142,12 @@ impl Config {
match confy::load_path(path.clone()) { match confy::load_path(path.clone()) {
Ok(data) => { Ok(data) => {
let msg = format!("Using {}", path); let msg = format!("Using {}", path);
directories.push(system_path.to_path_buf());
info!(msg); info!(msg);
Ok(Config { Ok((Config {
user: user_config, user: user_config,
system: data, system: data,
}) }, directories))
}, },
Err(e) => { Err(e) => {
error!(e); error!(e);
@ -145,26 +156,30 @@ impl Config {
} }
} }
fn load_user_config_directory(path: PathBuf) -> UserConfig { fn load_user_config_directory(path: PathBuf) -> (UserConfig, Option<String>) {
let config_path = format!("{}/.config/{}/domains.toml", path.display(), env!("CARGO_PKG_NAME")); let config_path = format!("{}/.config/{}", path.display(), env!("CARGO_PKG_NAME"));
match confy::load_path(config_path) { match confy::load_path(config_path.clone() + "/domains.toml") {
Ok(data) => data, Ok(data) => {
let msg = format!("Using {config_path}/domains.toml");
info!(msg);
(data, Some(config_path))
},
Err(e) => { Err(e) => {
match &e { match &e {
confy::ConfyError::GeneralLoadError(os_error) => { confy::ConfyError::GeneralLoadError(os_error) => {
if os_error.raw_os_error() == Some(13) { if os_error.raw_os_error() == Some(13) {
let msg = format!("Missing read permissions for {}, skipping", path.display().to_string().as_str()); let msg = format!("Missing read permissions for {}, skipping", path.display().to_string().as_str());
warn!(msg); warn!(msg);
UserConfig::default() (UserConfig::default(), None)
} }
else { else {
error!(e); error!(e);
UserConfig::default() (UserConfig::default(), None)
} }
}, },
_ => { _ => {
error!(e); error!(e);
UserConfig::default() (UserConfig::default(), None)
} }
} }
} }
@ -173,24 +188,28 @@ impl Config {
} }
#[actix_web::main] #[actix_web::main]
async fn main() -> std::io::Result<()> { async fn main() -> notify::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::load().expect("Error while loading or generating the config"); let (config, directories) = 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()); let loaded_redirects_msg = format!("Loaded {} redirects from user config", config.user.domain_configs.len());
info!(loaded_redirects_msg); info!(loaded_redirects_msg);
let app_data = web::Data::new(RwLock::new(config));
let app_data_clone = web::Data::clone(&app_data);
let mut server = HttpServer::new(move || { let mut server = HttpServer::new(move || {
App::new() App::new()
.app_data(web::Data::new(config.user.domain_configs.clone())) .app_data(web::Data::clone(&app_data_clone))
.service(status) .service(status)
.service(handle) .service(do_redirect)
}); });
for address in config.system.addresses.iter() { for address in app_data.read().expect("Read Lock Failed").system.addresses.iter() {
for port in config.system.ports.iter() { let ports = app_data.read().expect("Read Lock Failed").system.ports.clone();
for port in ports.iter() {
let msg = if address.is_ipv6() { let msg = if address.is_ipv6() {
format!("Listening on [{address}]:{port}") format!("Listening on [{address}]:{port}")
} }
@ -201,14 +220,39 @@ async fn main() -> std::io::Result<()> {
server = server.bind((*address, *port))? server = server.bind((*address, *port))?
} }
} }
server.run().await
let mut watcher = notify::recommended_watcher(move |res: Result<Event, notify::Error>| {
match res {
Ok(event) => {
if event.kind == EventKind::Access(AccessKind::Close(AccessMode::Write)) {
let (config, _) = Config::load().expect("Error while loading or generating the config");
let mut tmp_app_data = app_data.write().expect("Write Lock Failed");
tmp_app_data.system = config.system;
tmp_app_data.user = config.user;
info!("Reloading Configuration");
}
},
Err(e) => {
let msg = format!("Error watching files: {e}");
error!(msg);
}
}
})?;
for directory in directories.iter() {
watcher.watch(directory, RecursiveMode::NonRecursive)?;
}
let _ = server.run().await;
Ok(())
} }
#[get("/")] #[get("/")]
async fn handle(redirects: web::Data<Vec<DomainLinkConfig>>, request: HttpRequest) -> impl Responder { async fn do_redirect(data: web::Data<RwLock<Config>>, request: HttpRequest) -> impl Responder {
let redirects = &data.read().expect("Read Lock Failed").user.domain_configs;
if let Some(host_raw) = request.headers().get("host") { if let Some(host_raw) = request.headers().get("host") {
let host = host_raw.to_str().expect("host conversion to string should never fail"); let host = host_raw.to_str().expect("host conversion to string should never fail");
println!("{host}");
for redirect in redirects.iter() { for redirect in redirects.iter() {
if redirect.domains.contains(&host.to_owned()) { if redirect.domains.contains(&host.to_owned()) {
return HttpResponse::PermanentRedirect().insert_header(("location", redirect.target.to_string().as_str())).finish(); return HttpResponse::PermanentRedirect().insert_header(("location", redirect.target.to_string().as_str())).finish();
@ -221,7 +265,8 @@ async fn handle(redirects: web::Data<Vec<DomainLinkConfig>>, request: HttpReques
} }
#[get("/status")] #[get("/status")]
async fn status(redirects: web::Data<Vec<DomainLinkConfig>>) -> impl Responder { async fn status(data: web::Data<RwLock<Config>>) -> impl Responder {
let redirects = &data.read().expect("Read Lock Failed").user.domain_configs;
let mut body_msg = format!("Redirects Loaded: {}", redirects.len()); let mut body_msg = format!("Redirects Loaded: {}", redirects.len());
for redirect in redirects.iter() { for redirect in redirects.iter() {
body_msg += "\n["; body_msg += "\n[";