From 9cfa1cf950fb17251a33aaf6765bd6878b0176fe Mon Sep 17 00:00:00 2001 From: Ivan Bushchik Date: Sat, 23 Dec 2023 07:04:36 +0300 Subject: [PATCH] 0.6.0: change alias.json path logic Signed-off-by: Ivan Bushchik --- .gitignore | 2 ++ Cargo.toml | 3 ++- README.md | 8 ++++++++ src/main.rs | 39 ++++++++++++++++++++++++++++----------- src/structs.rs | 4 ++-- 5 files changed, 42 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index ea8c4bf..78fe391 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ /target +.idea/ +*.DS_Store \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index 0e503c0..cfd813e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "urouter" -version = "0.5.3" +version = "0.6.0" edition = "2021" license = "MIT" repository = "https://github.com/ivabus/urouter" @@ -16,3 +16,4 @@ smurf = "0.3.0" clap = { version = "4.4.11", features = ["derive"] } regex = "1.10.2" ureq = { version = "2.9.1", features = ["brotli", "native-certs"] } +users = "0.11.0" \ No newline at end of file diff --git a/README.md b/README.md index b34d8b3..10749d4 100644 --- a/README.md +++ b/README.md @@ -85,6 +85,14 @@ Each set contains 2 necessary elements and 1 optional. Agent matching made for `curl https://url | sh` like scripts. +## `alias.json` location + +- Passed with `--alias_file`, will look up to this path, if file doesn't exist (or no access to it) will panic +- If urouter started with privileges (EUID = 0), file would be `/etc/urouter/alias.json` +- Otherwise if `XDG_CONFIG_HOME` is set, file would be `$XDG_CONFIG_HOME/urouter/alias.json` +- Otherwise if `$HOME` is set, file would be `$HOME/.config/urouter/alias.json` +- If not matched any, will panic and exit + ## License The project is licensed under the terms of the [MIT license](./LICENSE). diff --git a/src/main.rs b/src/main.rs index aee7fb6..ae66225 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,24 +7,23 @@ Global comments: */ mod structs; - use structs::*; + #[macro_use] extern crate rocket; use rocket::http::{ContentType, Status}; -use std::cell::OnceCell; -use std::collections::HashMap; -use std::hint::unreachable_unchecked; -use std::path::PathBuf; -use std::time::Instant; +use std::{ + cell::OnceCell, collections::HashMap, hint::unreachable_unchecked, path::PathBuf, time::Instant, +}; -use rocket::response::content::RawText; -use rocket::response::Redirect; +use rocket::{ + figment::Figment, + response::{content::RawText, Redirect}, +}; use clap::Parser; use regex::Regex; -use rocket::figment::Figment; static mut ALIAS: OnceCell> = OnceCell::new(); static mut COMPILED_REGEXES: OnceCell> = OnceCell::new(); @@ -98,8 +97,26 @@ async fn index(user_agent: UserAgent) -> Response { #[rocket::main] async fn main() -> Result<(), rocket::Error> { - let args = Args::parse(); - let file = std::fs::File::open(args.alias_file).unwrap(); + let mut args = Args::parse(); + args.alias_file = match args.alias_file { + Some(f) => Some(f), + None => Some( + match users::get_effective_uid() { + 0 => "/etc/urouter/alias.json".to_string(), + _ => match std::env::var("XDG_CONFIG_HOME") { + Ok(config_home) => format!("{}/urouter/alias.json", config_home), + Err(_) => match std::env::var("HOME") { + Ok(home) => format!("{}/.config/urouter/alias.json", home), + Err(_) => { + panic!("Could not get config location, see README") + } + }, + }, + } + .into(), + ), + }; + let file = std::fs::File::open(args.alias_file.unwrap()).unwrap(); let alias: Vec = if args.alias_file_is_set_not_a_list { serde_json::from_reader::(file).unwrap().alias } else { diff --git a/src/structs.rs b/src/structs.rs index 5f7989b..33f1319 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -14,8 +14,8 @@ use std::path::PathBuf; #[derive(Parser, Debug)] #[command(about, author)] pub struct Args { - #[arg(long, default_value = "./alias.json")] - pub alias_file: PathBuf, + #[arg(long)] + pub alias_file: Option, /// For internal usage #[arg(long, default_value = "false")]