Compare commits
15 commits
0.5.0-rc.1
...
main
Author | SHA1 | Date | |
---|---|---|---|
13e1d73029 | |||
a6c4e25702 | |||
fec128243c | |||
ed4ca5899a | |||
7cb109018d | |||
c2bf7cc4ae | |||
add30dcfdd | |||
55748f30fb | |||
7b7353997d | |||
76a567d1bf | |||
a028d4245b | |||
3850f4eb3e | |||
f21af696c4 | |||
6d084c671a | |||
d0eb4b793c |
5 changed files with 75 additions and 11 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
|
||||
|
|
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -460,7 +460,7 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "domainlink"
|
||||
version = "0.5.0-rc.1"
|
||||
version = "1.0.3"
|
||||
dependencies = [
|
||||
"actix-web",
|
||||
"confy",
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
[package]
|
||||
authors = ["Neshura"]
|
||||
name = "domainlink"
|
||||
version = "0.5.0-rc.1"
|
||||
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"
|
||||
|
|
|
@ -10,12 +10,11 @@ Any changes there will persist updates and supersede the default config. The Sys
|
|||
|
||||
#### User Configuration
|
||||
|
||||
DomainLink currently expects redirect files to be placed in any home directory, specifically `/home/{user}/.config/domainlink/config.toml`.
|
||||
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.
|
||||
Support for `/root/.config/domainlink/redirects` may come in the future.
|
||||
By default, DomainLink does not create any redirect rules, you will have to create these yourself.
|
75
src/main.rs
75
src/main.rs
|
@ -1,6 +1,8 @@
|
|||
use std::error::Error;
|
||||
use std::fs;
|
||||
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};
|
||||
|
@ -144,6 +146,7 @@ impl Config {
|
|||
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,
|
||||
|
@ -158,10 +161,20 @@ 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(config_path.clone() + "/domains.toml") {
|
||||
match confy::load_path::<UserConfig>(config_path.clone() + "/domains.toml") {
|
||||
Ok(data) => {
|
||||
let msg = format!("Using {config_path}/domains.toml");
|
||||
info!(msg);
|
||||
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) => {
|
||||
|
@ -185,6 +198,58 @@ impl Config {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
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]
|
||||
|
@ -238,7 +303,7 @@ async fn main() -> notify::Result<()> {
|
|||
}
|
||||
}
|
||||
})?;
|
||||
|
||||
|
||||
for directory in directories.iter() {
|
||||
watcher.watch(directory, RecursiveMode::NonRecursive)?;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue