Compare commits
No commits in common. "main" and "0.3.0-rc.1" have entirely different histories.
main
...
0.3.0-rc.1
5 changed files with 82 additions and 380 deletions
|
@ -137,7 +137,7 @@ jobs:
|
|||
run: rm release_blobs/build.env
|
||||
-
|
||||
name: Release New Version
|
||||
uses: actions/forgejo-release@v2
|
||||
uses: actions/forgejo-release@v1
|
||||
with:
|
||||
direction: upload
|
||||
url: https://forgejo.neshweb.net
|
||||
|
|
148
Cargo.lock
generated
148
Cargo.lock
generated
|
@ -380,21 +380,6 @@ dependencies = [
|
|||
"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]]
|
||||
name = "crypto-common"
|
||||
version = "0.1.6"
|
||||
|
@ -460,12 +445,11 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "domainlink"
|
||||
version = "1.0.3"
|
||||
version = "0.3.0-rc.1"
|
||||
dependencies = [
|
||||
"actix-web",
|
||||
"confy",
|
||||
"log",
|
||||
"notify",
|
||||
"serde",
|
||||
"systemd-journal-logger",
|
||||
]
|
||||
|
@ -495,18 +479,6 @@ dependencies = [
|
|||
"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]]
|
||||
name = "flate2"
|
||||
version = "1.0.28"
|
||||
|
@ -532,15 +504,6 @@ dependencies = [
|
|||
"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]]
|
||||
name = "futures-core"
|
||||
version = "0.3.30"
|
||||
|
@ -666,26 +629,6 @@ dependencies = [
|
|||
"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]]
|
||||
name = "itoa"
|
||||
version = "1.0.11"
|
||||
|
@ -701,26 +644,6 @@ dependencies = [
|
|||
"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]]
|
||||
name = "language-tags"
|
||||
version = "0.3.2"
|
||||
|
@ -818,25 +741,6 @@ dependencies = [
|
|||
"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]]
|
||||
name = "num-conv"
|
||||
version = "0.1.0"
|
||||
|
@ -1060,15 +964,6 @@ version = "1.0.17"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
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]]
|
||||
name = "scopeguard"
|
||||
version = "1.2.0"
|
||||
|
@ -1411,53 +1306,12 @@ version = "0.9.4"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
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]]
|
||||
name = "wasi"
|
||||
version = "0.11.0+wasi-snapshot-preview1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
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]]
|
||||
name = "windows-sys"
|
||||
version = "0.48.0"
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
[package]
|
||||
authors = ["Neshura"]
|
||||
name = "domainlink"
|
||||
version = "1.0.3"
|
||||
version = "0.3.0-rc.1"
|
||||
edition = "2021"
|
||||
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"
|
||||
|
@ -31,6 +31,5 @@ systemd-units = { enable = false }
|
|||
actix-web = "4"
|
||||
confy = "0.6"
|
||||
log = "0.4"
|
||||
notify = "6"
|
||||
systemd-journal-logger = "2"
|
||||
serde = { version = "1.0.197", features = ["derive"] }
|
19
README.md
19
README.md
|
@ -1,20 +1,3 @@
|
|||
# DomainLink
|
||||
|
||||
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.
|
||||
Lightweight tool for handling (sub-)domain to URL redirects instead of having to deal with copy and pasting proxy rules.
|
290
src/main.rs
290
src/main.rs
|
@ -1,14 +1,10 @@
|
|||
use std::error::Error;
|
||||
use std::{fs, io};
|
||||
use std::fmt::{Display, format, Formatter};
|
||||
use std::fs;
|
||||
use std::net::{IpAddr, Ipv6Addr};
|
||||
use std::os::unix;
|
||||
use std::os::unix::fs::MetadataExt;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::RwLock;
|
||||
use std::path::Path;
|
||||
use actix_web::{web, App, HttpResponse, HttpServer, get, Responder, HttpRequest};
|
||||
use log::{LevelFilter};
|
||||
use notify::{Event, EventKind, RecursiveMode, Watcher};
|
||||
use notify::event::{AccessKind, AccessMode};
|
||||
use systemd_journal_logger::{connected_to_journal, JournalLog};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
|
@ -40,9 +36,25 @@ macro_rules! error {
|
|||
};
|
||||
}
|
||||
|
||||
#[derive(Clone, Serialize, Deserialize)]
|
||||
enum Protocol {
|
||||
Http,
|
||||
Https
|
||||
}
|
||||
|
||||
impl Display for Protocol {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Protocol::Http => write!(f, "http://"),
|
||||
Protocol::Https => write!(f, "https://")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Serialize, Deserialize)]
|
||||
struct DomainLinkConfig {
|
||||
domains: Vec<String>,
|
||||
domain: String,
|
||||
protocol: Protocol,
|
||||
target: String,
|
||||
}
|
||||
|
||||
|
@ -73,54 +85,41 @@ struct Config {
|
|||
}
|
||||
|
||||
impl Config {
|
||||
pub fn load() -> Result<(Self, Vec<PathBuf>), Box<dyn Error>> {
|
||||
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 directories: Vec<PathBuf> = vec![];
|
||||
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();
|
||||
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() {
|
||||
match path.display().to_string().as_str() {
|
||||
"/root" => {
|
||||
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);
|
||||
},
|
||||
"/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, 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);
|
||||
}
|
||||
})
|
||||
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));
|
||||
}
|
||||
},
|
||||
Err(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"));
|
||||
|
@ -133,24 +132,18 @@ impl Config {
|
|||
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() {
|
||||
if path.exists() {
|
||||
system_path = path;
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
let path = format!("{}/config.toml", system_path.display());
|
||||
match confy::load_path(path.clone()) {
|
||||
let path = format!("{}/config.toml", system_path.display().to_string().as_str());
|
||||
match confy::load_path(path) {
|
||||
Ok(data) => {
|
||||
let msg = format!("Using {}", path);
|
||||
directories.push(system_path.to_path_buf());
|
||||
info!(msg);
|
||||
Self::check_for_duplicate_domains(&user_config.domain_configs);
|
||||
Ok((Config {
|
||||
Ok(Config {
|
||||
user: user_config,
|
||||
system: data,
|
||||
}, directories))
|
||||
})
|
||||
},
|
||||
Err(e) => {
|
||||
error!(e);
|
||||
|
@ -158,169 +151,43 @@ 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]
|
||||
async fn main() -> notify::Result<()> {
|
||||
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, directories) = Config::load().expect("Error while loading or generating the config");
|
||||
|
||||
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 app_data = web::Data::new(RwLock::new(config));
|
||||
let app_data_clone = web::Data::clone(&app_data);
|
||||
|
||||
let mut server = HttpServer::new(move || {
|
||||
App::new()
|
||||
.app_data(web::Data::clone(&app_data_clone))
|
||||
.service(status)
|
||||
.service(do_redirect)
|
||||
.app_data(web::Data::new(config.user.domain_configs.clone()))
|
||||
.service(handle)
|
||||
.service(dry_handle)
|
||||
});
|
||||
|
||||
for address in app_data.read().expect("Read Lock Failed").system.addresses.iter() {
|
||||
let ports = app_data.read().expect("Read Lock Failed").system.ports.clone();
|
||||
for port in ports.iter() {
|
||||
let msg = if address.is_ipv6() {
|
||||
format!("Listening on [{address}]:{port}")
|
||||
}
|
||||
else {
|
||||
format!("Listening on {address}:{port}")
|
||||
};
|
||||
|
||||
for address in config.system.addresses.iter() {
|
||||
for port in config.system.ports.iter() {
|
||||
let msg = format!("Listening on {address}:{port}");
|
||||
info!(msg);
|
||||
server = server.bind((*address, *port))?
|
||||
}
|
||||
}
|
||||
|
||||
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(())
|
||||
server.run().await
|
||||
}
|
||||
|
||||
#[get("/")]
|
||||
async fn do_redirect(data: web::Data<RwLock<Config>>, request: HttpRequest) -> impl Responder {
|
||||
let redirects = &data.read().expect("Read Lock Failed").user.domain_configs;
|
||||
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();
|
||||
if redirect.domain == host {
|
||||
return HttpResponse::PermanentRedirect().insert_header(("location", format!("{}{}", redirect.protocol, redirect.target).as_str())).finish();
|
||||
}
|
||||
}
|
||||
let fail_msg = format!("No Redirect for {host} found");
|
||||
|
@ -329,20 +196,19 @@ async fn do_redirect(data: web::Data<RwLock<Config>>, request: HttpRequest) -> i
|
|||
HttpResponse::NotFound().body("Host not specified")
|
||||
}
|
||||
|
||||
#[get("/status")]
|
||||
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());
|
||||
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 += ", ";
|
||||
#[get("/dry")]
|
||||
async fn dry_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.domain == host {
|
||||
let body = format!("Redirecting: {} -> {}{}", host, redirect.protocol, redirect.target);
|
||||
return HttpResponse::Ok().body(body);
|
||||
}
|
||||
}
|
||||
body_msg += format!("] => '{}'", redirect.target).as_str();
|
||||
let fail_msg = format!("No Redirect for {host} found");
|
||||
return HttpResponse::NotFound().body(fail_msg)
|
||||
}
|
||||
|
||||
HttpResponse::Ok().body(body_msg)
|
||||
}
|
||||
HttpResponse::NotFound().body("Host not specified")
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue