Compare commits

..

21 commits
0.4.0 ... main

Author SHA1 Message Date
13e1d73029 Merge pull request 'Bump forgejo-release to v2' (#6) from actions-update into main
All checks were successful
Run Tests on Code / run-tests (push) Successful in 13s
Reviewed-on: https://forgejo.neshweb.net///Neshura/domainlink/pulls/6
2024-08-06 12:31:33 +00:00
a6c4e25702 Bump forgejo-release to v2
All checks were successful
Run Tests on Code / run-tests (push) Successful in 48s
Build binary file and bundle packages / test (pull_request) Successful in 47s
Build binary file and bundle packages / build (pull_request) Successful in 51s
2024-08-06 12:27:57 +00:00
fec128243c
Release 1.0.3
All checks were successful
Run Tests on Code / run-tests (push) Successful in 33s
Build and release binary file and packages / test (push) Successful in 12s
Build and release binary file and packages / build (push) Successful in 46s
Build and release binary file and packages / upload-generic-package (push) Successful in 1s
Build and release binary file and packages / upload-debian-package (push) Successful in 1s
Build and release binary file and packages / create-release (push) Successful in 7s
2024-04-10 23:32:57 +02:00
ed4ca5899a
Bugfix for previous Logging change
All checks were successful
Run Tests on Code / run-tests (push) Successful in 13s
2024-04-10 23:32:43 +02:00
7cb109018d
Release 1.0.2
All checks were successful
Run Tests on Code / run-tests (push) Successful in 36s
Build and release binary file and packages / upload-generic-package (push) Successful in 1s
Build and release binary file and packages / create-release (push) Successful in 7s
Build and release binary file and packages / test (push) Successful in 35s
Build and release binary file and packages / build (push) Successful in 47s
Build and release binary file and packages / upload-debian-package (push) Successful in 1s
2024-04-10 23:29:55 +02:00
c2bf7cc4ae
Only Output "Using domains.toml" when config contains any redirects
All checks were successful
Run Tests on Code / run-tests (push) Successful in 12s
2024-04-10 23:29:43 +02:00
add30dcfdd
Updated Readme
All checks were successful
Run Tests on Code / run-tests (push) Successful in 13s
2024-04-10 23:28:23 +02:00
55748f30fb
Release 1.0.1
All checks were successful
Run Tests on Code / run-tests (push) Successful in 31s
Build and release binary file and packages / test (push) Successful in 11s
Build and release binary file and packages / build (push) Successful in 48s
Build and release binary file and packages / upload-generic-package (push) Successful in 1s
Build and release binary file and packages / upload-debian-package (push) Successful in 1s
Build and release binary file and packages / create-release (push) Successful in 7s
2024-04-10 23:26:16 +02:00
7b7353997d
Removed Debug Logging 2024-04-10 23:26:11 +02:00
76a567d1bf
Add duplicate domain check, closes #5
All checks were successful
Run Tests on Code / run-tests (push) Successful in 13s
2024-04-10 23:22:11 +02:00
a028d4245b
Revert previous
All checks were successful
Run Tests on Code / run-tests (push) Successful in 13s
2024-04-10 23:00:33 +02:00
3850f4eb3e
Add concurrent test ci runs
All checks were successful
Run Tests on Code / run-tests (push) Successful in 19s
2024-04-10 22:56:11 +02:00
f21af696c4
Release 1.0.0
All checks were successful
Run Tests on Code / run-tests (push) Successful in 36s
Build and release binary file and packages / test (push) Successful in 36s
Build and release binary file and packages / build (push) Successful in 48s
Build and release binary file and packages / upload-generic-package (push) Successful in 1s
Build and release binary file and packages / upload-debian-package (push) Successful in 1s
Build and release binary file and packages / create-release (push) Successful in 8s
2024-04-10 22:50:39 +02:00
6d084c671a
Fix ownership issues caused by confy when interacting with other users' config directory. Closes #1
All checks were successful
Run Tests on Code / run-tests (push) Successful in 19s
2024-04-10 22:50:25 +02:00
d0eb4b793c
Release 0.5.0
All checks were successful
Run Tests on Code / run-tests (push) Successful in 37s
Build and release binary file and packages / test (push) Successful in 35s
Build and release binary file and packages / build (push) Successful in 47s
Build and release binary file and packages / upload-generic-package (push) Successful in 1s
Build and release binary file and packages / upload-debian-package (push) Successful in 1s
Build and release binary file and packages / create-release (push) Successful in 13s
2024-04-10 22:22:29 +02:00
bf3c0102a1
Release Candidate 0.5.0-rc.1
All checks were successful
Run Tests on Code / run-tests (push) Successful in 46s
Build and release binary file and packages / test (push) Successful in 38s
Build and release binary file and packages / build (push) Successful in 59s
Build and release binary file and packages / upload-generic-package (push) Successful in 1s
Build and release binary file and packages / upload-debian-package (push) Successful in 1s
Build and release binary file and packages / create-release (push) Successful in 19s
2024-04-10 22:20:10 +02:00
028067b62a
Add hot reloading of redirect config files
All checks were successful
Run Tests on Code / run-tests (push) Successful in 47s
2024-04-10 22:19:50 +02:00
20af5172b4
Refactored User Config loading, closes #3
All checks were successful
Run Tests on Code / run-tests (push) Successful in 11s
2024-04-10 18:55:17 +02:00
e1260877b6
Replaced /dry route with a more general /status route
All checks were successful
Run Tests on Code / run-tests (push) Successful in 19s
2024-04-10 18:54:21 +02:00
7b3115f5cf Update README.md
All checks were successful
Run Tests on Code / run-tests (push) Successful in 12s
2024-04-09 11:32:53 +00:00
5fcdddde03
Updated Readme
All checks were successful
Run Tests on Code / run-tests (push) Successful in 12s
2024-04-08 23:20:42 +02:00
5 changed files with 364 additions and 59 deletions

View file

@ -137,7 +137,7 @@ jobs:
run: rm release_blobs/build.env run: rm release_blobs/build.env
- -
name: Release New Version name: Release New Version
uses: actions/forgejo-release@v1 uses: actions/forgejo-release@v2
with: with:
direction: upload direction: upload
url: https://forgejo.neshweb.net url: https://forgejo.neshweb.net

148
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"
@ -445,11 +460,12 @@ dependencies = [
[[package]] [[package]]
name = "domainlink" name = "domainlink"
version = "0.4.0" version = "1.0.3"
dependencies = [ 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

@ -1,7 +1,7 @@
[package] [package]
authors = ["Neshura"] authors = ["Neshura"]
name = "domainlink" name = "domainlink"
version = "0.4.0" version = "1.0.3"
edition = "2021" edition = "2021"
description = "Lightweight tool for handling (sub-)domain to URL redirects instead of having to deal with copy and pasting proxy rules" description = "Lightweight tool for handling (sub-)domain to URL redirects instead of having to deal with copy and pasting proxy rules"
license = "GPL-3.0-or-later" license = "GPL-3.0-or-later"
@ -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

@ -1,3 +1,20 @@
# DomainLink # DomainLink
Lightweight tool for handling (sub-)domain to URL redirects instead of having to deal with copy and pasting proxy rules. Lightweight tool for handling (sub-)domain to URL redirects instead of having to deal with copy and pasting proxy rules.
#### System Configuration
The package provides a default configuration in `/usr/share/local/domainlink/config.toml`.
Modifying this is not recommended as updates may override any changes, instead make a copy at `/etc/domainlink/config.toml`.
Any changes there will persist updates and supersede the default config. The System Config only contains the settings for listen addresses and ports.
#### User Configuration
DomainLink currently expects redirect files to be placed in any home directory, specifically `/home/{user}/.config/domainlink/domains.toml` or `/root/.config/domainlink/domains.toml`.
Redirects are configured in an array, below is an example config.
```toml
[[domain_configs]]
domains = ["sub.domain.tld", "sub2.domain.tld"]
target = "https://sub.domain.tld/query"
```
By default, DomainLink does not create any redirect rules, you will have to create these yourself.

View file

@ -1,10 +1,14 @@
use std::error::Error; use std::error::Error;
use std::fmt::{Display, format, Formatter}; use std::{fs, io};
use std::fs;
use std::net::{IpAddr, Ipv6Addr}; use std::net::{IpAddr, Ipv6Addr};
use std::path::Path; use std::os::unix;
use std::os::unix::fs::MetadataExt;
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};
@ -69,41 +73,54 @@ 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();
for entry in fs::read_dir("/home").expect("home directory is expected") { let root_contents = match fs::read_dir("/") {
let entry = entry.expect("home directory is expected to have at least one directory"); Ok(contents) => { contents }
let path = entry.path(); 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() { if path.is_dir() {
let config_path = format!("{}/.config/{}/config.toml", path.display().to_string().as_str(), env!("CARGO_PKG_NAME")); match path.display().to_string().as_str() {
let mut path_config: UserConfig = match confy::load_path(config_path) { "/root" => {
Ok(data) => data, let (mut root_configs, root_path) = Self::load_user_config_directory(path);
Err(e) => { if let Some(root_directory) = root_path {
match &e { directories.push(Path::new(root_directory.as_str()).to_path_buf());
confy::ConfyError::GeneralLoadError(os_error) => { }
if os_error.raw_os_error() == Some(13) { user_config.domain_configs.append(&mut root_configs.domain_configs);
let msg = format!("Missing read permissions for {}, skipping", path.display().to_string().as_str()); },
warn!(msg); "/home" => {
UserConfig::default() match fs::read_dir(path) {
} Ok(home_contents) => {
else { home_contents.for_each(|home_folder| {
error!(e); let home_folder_path = home_folder.expect("Unexpected Error while Unwrapping the listed Home Dir Entry").path();
return Err(Box::new(e)); if home_folder_path.is_dir() {
} 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);
}
})
}, },
_ => { Err(e) => {
error!(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 etc_path = format!("/etc/{}", env!("CARGO_PKG_NAME"));
let usr_path = format!("/usr/local/share/{}", env!("CARGO_PKG_NAME")); let usr_path = format!("/usr/local/share/{}", env!("CARGO_PKG_NAME"));
@ -123,15 +140,17 @@ impl Config {
} }
}; };
let path = format!("{}/config.toml", system_path.display().to_string().as_str()); let path = format!("{}/config.toml", system_path.display());
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 { Self::check_for_duplicate_domains(&user_config.domain_configs);
Ok((Config {
user: user_config, user: user_config,
system: data, system: data,
}) }, directories))
}, },
Err(e) => { Err(e) => {
error!(e); error!(e);
@ -139,27 +158,123 @@ impl Config {
} }
} }
} }
fn load_user_config_directory(path: PathBuf) -> (UserConfig, Option<String>) {
let config_path = format!("{}/.config/{}", path.display(), env!("CARGO_PKG_NAME"));
match confy::load_path::<UserConfig>(config_path.clone() + "/domains.toml") {
Ok(data) => {
if data.domain_configs.is_empty() {
match Self::fix_path_ownership(path, vec![".config", env!("CARGO_PKG_NAME"), "domains.toml"]) {
Ok(_) => (),
Err(e) => {
error!(e);
}
};
}
else {
let msg = format!("Using {config_path}/domains.toml");
info!(msg);
}
(data, Some(config_path))
},
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(), None)
}
else {
error!(e);
(UserConfig::default(), None)
}
},
_ => {
error!(e);
(UserConfig::default(), None)
}
}
}
}
}
fn fix_path_ownership(root: PathBuf, paths: Vec<&str>) -> io::Result<()> {
let root_metadata = fs::metadata(&root)?;
let uid = root_metadata.uid();
let gid = root_metadata.gid();
match paths.len() {
1 => {
let new_root = root.join(paths[0]);
unix::fs::chown(new_root, Some(uid), Some(gid))
},
_ => {
let new_root = root.join(paths[0]);
let ret = unix::fs::chown(&new_root, Some(uid), Some(gid));
let mut new_paths = paths.clone();
new_paths.remove(0);
match Self::fix_path_ownership(new_root, new_paths) {
Ok(_) => ret,
Err(e) => {
error!(e);
Err(e)
}
}
}
}
}
fn check_for_duplicate_domains(domain_configs: &[DomainLinkConfig]) {
let mut checked_domains: Vec<String> = vec![];
for (cfg_idx, config) in domain_configs.iter().enumerate() {
for (idx, domain) in config.domains.iter().enumerate() {
if !checked_domains.contains(domain) {
if config.domains[idx+1..].contains(domain) {
// Error
let msg = format!("Duplicate Domain use detected for '{domain}");
warn!(msg);
checked_domains.push(domain.clone());
}
else {
for d in domain_configs[cfg_idx+1..].iter() {
if d.domains.contains(domain) {
let msg = format!("Duplicate Domain use detected for '{domain}");
warn!(msg);
checked_domains.push(domain.clone());
break;
}
}
}
}
}
}
}
} }
#[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(handle) .service(status)
.service(dry_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}")
} }
@ -170,14 +285,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();
@ -189,19 +329,20 @@ async fn handle(redirects: web::Data<Vec<DomainLinkConfig>>, request: HttpReques
HttpResponse::NotFound().body("Host not specified") HttpResponse::NotFound().body("Host not specified")
} }
#[get("/dry")] #[get("/status")]
async fn dry_handle(redirects: web::Data<Vec<DomainLinkConfig>>, request: HttpRequest) -> impl Responder { async fn status(data: web::Data<RwLock<Config>>) -> impl Responder {
if let Some(host_raw) = request.headers().get("host") { let redirects = &data.read().expect("Read Lock Failed").user.domain_configs;
let host = host_raw.to_str().expect("host conversion to string should never fail"); let mut body_msg = format!("Redirects Loaded: {}", redirects.len());
println!("{host}"); for redirect in redirects.iter() {
for redirect in redirects.iter() { body_msg += "\n[";
if redirect.domains.contains(&host.to_owned()) { for (idx, domain) in redirect.domains.iter().enumerate() {
let body = format!("Redirecting: {} -> {}", host, redirect.target); body_msg += domain;
return HttpResponse::Ok().body(body); if idx != (redirect.domains.len() - 1) {
body_msg += ", ";
} }
} }
let fail_msg = format!("No Redirect for {host} found"); body_msg += format!("] => '{}'", redirect.target).as_str();
return HttpResponse::NotFound().body(fail_msg)
} }
HttpResponse::NotFound().body("Host not specified")
} HttpResponse::Ok().body(body_msg)
}