Compare commits
31 commits
0.2.0-rc.2
...
main
Author | SHA1 | Date | |
---|---|---|---|
13e1d73029 | |||
a6c4e25702 | |||
fec128243c | |||
ed4ca5899a | |||
7cb109018d | |||
c2bf7cc4ae | |||
add30dcfdd | |||
55748f30fb | |||
7b7353997d | |||
76a567d1bf | |||
a028d4245b | |||
3850f4eb3e | |||
f21af696c4 | |||
6d084c671a | |||
d0eb4b793c | |||
bf3c0102a1 | |||
028067b62a | |||
20af5172b4 | |||
e1260877b6 | |||
7b3115f5cf | |||
5fcdddde03 | |||
5a46d3722b | |||
8e077609e9 | |||
0b1ada9221 | |||
c5f2f49c16 | |||
9e7977b4cd | |||
38cfbdcfb8 | |||
bd7942430c | |||
9cb4dba537 | |||
abfa4237ed | |||
483fe1e649 |
7 changed files with 591 additions and 82 deletions
|
@ -137,7 +137,7 @@ jobs:
|
|||
run: rm release_blobs/build.env
|
||||
-
|
||||
name: Release New Version
|
||||
uses: actions/forgejo-release@v1
|
||||
uses: actions/forgejo-release@v2
|
||||
with:
|
||||
direction: upload
|
||||
url: https://forgejo.neshweb.net
|
||||
|
|
282
Cargo.lock
generated
282
Cargo.lock
generated
|
@ -333,6 +333,18 @@ version = "1.0.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "confy"
|
||||
version = "0.6.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "45b1f4c00870f07dc34adcac82bb6a72cc5aabca8536ba1797e01df51d2ce9a0"
|
||||
dependencies = [
|
||||
"directories",
|
||||
"serde",
|
||||
"thiserror",
|
||||
"toml",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "convert_case"
|
||||
version = "0.4.0"
|
||||
|
@ -368,6 +380,21 @@ 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"
|
||||
|
@ -410,12 +437,36 @@ dependencies = [
|
|||
"crypto-common",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "directories"
|
||||
version = "5.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9a49173b84e034382284f27f1af4dcbbd231ffa358c0fe316541a7337f376a35"
|
||||
dependencies = [
|
||||
"dirs-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "dirs-sys"
|
||||
version = "0.4.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "520f05a5cbd335fae5a99ff7a6ab8627577660ee5cfd6a94a6a929b52ff0321c"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"option-ext",
|
||||
"redox_users",
|
||||
"windows-sys 0.48.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "domainlink"
|
||||
version = "0.2.0-rc.2"
|
||||
version = "1.0.3"
|
||||
dependencies = [
|
||||
"actix-web",
|
||||
"confy",
|
||||
"log",
|
||||
"notify",
|
||||
"serde",
|
||||
"systemd-journal-logger",
|
||||
]
|
||||
|
||||
|
@ -444,6 +495,18 @@ 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"
|
||||
|
@ -469,6 +532,15 @@ 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"
|
||||
|
@ -594,6 +666,26 @@ 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"
|
||||
|
@ -609,6 +701,26 @@ 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"
|
||||
|
@ -621,6 +733,16 @@ version = "0.2.153"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd"
|
||||
|
||||
[[package]]
|
||||
name = "libredox"
|
||||
version = "0.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c0ff37bd590ca25063e35af745c343cb7a0271906fb7b37e4813e8f79f00268d"
|
||||
dependencies = [
|
||||
"bitflags 2.5.0",
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "linux-raw-sys"
|
||||
version = "0.4.13"
|
||||
|
@ -696,6 +818,25 @@ 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"
|
||||
|
@ -717,6 +858,12 @@ version = "1.19.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"
|
||||
|
||||
[[package]]
|
||||
name = "option-ext"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d"
|
||||
|
||||
[[package]]
|
||||
name = "parking_lot"
|
||||
version = "0.12.1"
|
||||
|
@ -839,6 +986,17 @@ dependencies = [
|
|||
"bitflags 1.3.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "redox_users"
|
||||
version = "0.4.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bd283d9651eeda4b2a83a43c1c91b266c40fd76ecd39a50a8c630ae69dc72891"
|
||||
dependencies = [
|
||||
"getrandom",
|
||||
"libredox",
|
||||
"thiserror",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex"
|
||||
version = "1.10.4"
|
||||
|
@ -902,6 +1060,15 @@ 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"
|
||||
|
@ -945,6 +1112,15 @@ dependencies = [
|
|||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_spanned"
|
||||
version = "0.6.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "eb3622f419d1296904700073ea6cc23ad690adbd66f13ea683df73298736f0c1"
|
||||
dependencies = [
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_urlencoded"
|
||||
version = "0.7.1"
|
||||
|
@ -1034,6 +1210,26 @@ dependencies = [
|
|||
"rustix",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thiserror"
|
||||
version = "1.0.58"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "03468839009160513471e86a034bb2c5c0e4baae3b43f79ffc55c4a5427b3297"
|
||||
dependencies = [
|
||||
"thiserror-impl",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thiserror-impl"
|
||||
version = "1.0.58"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c61f3ba182994efc43764a46c018c347bc492c79f024e705f46567b418f6d4f7"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.58",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "time"
|
||||
version = "0.3.34"
|
||||
|
@ -1111,6 +1307,40 @@ dependencies = [
|
|||
"tracing",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "toml"
|
||||
version = "0.8.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e9dd1545e8208b4a5af1aa9bbd0b4cf7e9ea08fabc5d0a5c67fcaafa17433aa3"
|
||||
dependencies = [
|
||||
"serde",
|
||||
"serde_spanned",
|
||||
"toml_datetime",
|
||||
"toml_edit",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "toml_datetime"
|
||||
version = "0.6.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3550f4e9685620ac18a50ed434eb3aec30db8ba93b0287467bca5826ea25baf1"
|
||||
dependencies = [
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "toml_edit"
|
||||
version = "0.22.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8e40bb779c5187258fd7aad0eb68cb8706a0a81fa712fbea808ab43c4b8374c4"
|
||||
dependencies = [
|
||||
"indexmap",
|
||||
"serde",
|
||||
"serde_spanned",
|
||||
"toml_datetime",
|
||||
"winnow",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tracing"
|
||||
version = "0.1.40"
|
||||
|
@ -1181,12 +1411,53 @@ 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"
|
||||
|
@ -1319,6 +1590,15 @@ version = "0.52.4"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "32b752e52a2da0ddfbdbcc6fceadfeede4c939ed16d13e648833a61dfb611ed8"
|
||||
|
||||
[[package]]
|
||||
name = "winnow"
|
||||
version = "0.6.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dffa400e67ed5a4dd237983829e66475f0a4a26938c4b04c21baede6262215b8"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zerocopy"
|
||||
version = "0.7.32"
|
||||
|
|
10
Cargo.toml
10
Cargo.toml
|
@ -1,7 +1,7 @@
|
|||
[package]
|
||||
authors = ["Neshura"]
|
||||
name = "domainlink"
|
||||
version = "0.2.0-rc.2"
|
||||
version = "1.0.3"
|
||||
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"
|
||||
|
@ -16,6 +16,11 @@ assets = [
|
|||
"target/release/domainlink",
|
||||
"/usr/local/bin/domainlink",
|
||||
"755",
|
||||
],
|
||||
[
|
||||
"debian/app_config.toml",
|
||||
"/usr/local/share/domainlink/config.toml",
|
||||
"755",
|
||||
]
|
||||
]
|
||||
systemd-units = { enable = false }
|
||||
|
@ -24,5 +29,8 @@ systemd-units = { enable = false }
|
|||
|
||||
[dependencies]
|
||||
actix-web = "4"
|
||||
confy = "0.6"
|
||||
log = "0.4"
|
||||
notify = "6"
|
||||
systemd-journal-logger = "2"
|
||||
serde = { version = "1.0.197", features = ["derive"] }
|
17
README.md
17
README.md
|
@ -1,3 +1,20 @@
|
|||
# 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.
|
2
debian/app_config.toml
vendored
Normal file
2
debian/app_config.toml
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
ports = [80]
|
||||
addresses = ["::"]
|
1
debian/domainlink.service
vendored
1
debian/domainlink.service
vendored
|
@ -5,7 +5,6 @@ After=network-online.target
|
|||
|
||||
[Service]
|
||||
Type=simple
|
||||
User=%i
|
||||
ExecStart=/usr/local/bin/domainlink
|
||||
Restart=always
|
||||
RestartSec=3
|
||||
|
|
351
src/main.rs
351
src/main.rs
|
@ -1,8 +1,16 @@
|
|||
use std::fmt::{Display, Formatter};
|
||||
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
|
||||
use std::error::Error;
|
||||
use std::{fs, io};
|
||||
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 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};
|
||||
|
||||
|
||||
macro_rules! info {
|
||||
|
@ -32,93 +40,287 @@ macro_rules! error {
|
|||
};
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
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)]
|
||||
#[derive(Clone, Serialize, Deserialize)]
|
||||
struct DomainLinkConfig {
|
||||
domain: String,
|
||||
protocol: Protocol,
|
||||
domains: Vec<String>,
|
||||
target: String,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct Config {
|
||||
redirects: Vec<DomainLinkConfig>,
|
||||
#[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, Vec<PathBuf>), 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();
|
||||
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);
|
||||
}
|
||||
})
|
||||
},
|
||||
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);
|
||||
directories.push(system_path.to_path_buf());
|
||||
info!(msg);
|
||||
Self::check_for_duplicate_domains(&user_config.domain_configs);
|
||||
Ok((Config {
|
||||
user: user_config,
|
||||
system: data,
|
||||
}, directories))
|
||||
},
|
||||
Err(e) => {
|
||||
error!(e);
|
||||
Err(Box::new(e))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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() -> std::io::Result<()> {
|
||||
async fn main() -> notify::Result<()> {
|
||||
JournalLog::new().expect("Systemd-Logger crate error").install().expect("Systemd-Logger crate error");
|
||||
log::set_max_level(LevelFilter::Info);
|
||||
|
||||
let config = Config {
|
||||
redirects: vec![
|
||||
DomainLinkConfig {
|
||||
domain: "neshura.me".to_owned(),
|
||||
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 (config, directories) = Config::load().expect("Error while loading or generating the config");
|
||||
|
||||
let msg = "This Build is not intended for production use!";
|
||||
info!("Test Info");
|
||||
warn!(msg);
|
||||
error!(msg);
|
||||
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::new(config.redirects.clone()))
|
||||
.service(handle)
|
||||
.service(dry_handle)
|
||||
.app_data(web::Data::clone(&app_data_clone))
|
||||
.service(status)
|
||||
.service(do_redirect)
|
||||
});
|
||||
|
||||
for address in config.addresses.iter() {
|
||||
for port in config.ports.iter() {
|
||||
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}")
|
||||
};
|
||||
info!(msg);
|
||||
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("/")]
|
||||
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") {
|
||||
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 {
|
||||
return HttpResponse::PermanentRedirect().insert_header(("location", format!("{}{}", redirect.protocol, redirect.target).as_str())).finish();
|
||||
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");
|
||||
|
@ -127,19 +329,20 @@ async fn handle(redirects: web::Data<Vec<DomainLinkConfig>>, request: HttpReques
|
|||
HttpResponse::NotFound().body("Host not specified")
|
||||
}
|
||||
|
||||
#[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);
|
||||
#[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 += ", ";
|
||||
}
|
||||
}
|
||||
let fail_msg = format!("No Redirect for {host} found");
|
||||
return HttpResponse::NotFound().body(fail_msg)
|
||||
body_msg += format!("] => '{}'", redirect.target).as_str();
|
||||
}
|
||||
HttpResponse::NotFound().body("Host not specified")
|
||||
|
||||
HttpResponse::Ok().body(body_msg)
|
||||
}
|
Loading…
Reference in a new issue