From 67232c301030b0ff8bcf04a45e6e6a341a3e5d81 Mon Sep 17 00:00:00 2001 From: Kirill Fertikov Date: Wed, 14 Jun 2023 17:45:10 +0500 Subject: [PATCH] Added configs for snap daemons (#1204) --- src/config.rs | 46 ++++++++++++++++++++++++++---------------- src/service/local.rs | 2 +- src/service/manager.rs | 2 +- src/service/server.rs | 2 +- 4 files changed, 32 insertions(+), 20 deletions(-) diff --git a/src/config.rs b/src/config.rs index d3498939..c0d942e1 100644 --- a/src/config.rs +++ b/src/config.rs @@ -14,19 +14,24 @@ use directories::ProjectDirs; use serde::Deserialize; /// Default configuration file path -pub fn get_default_config_path() -> Option { +pub fn get_default_config_path(config_file: &str) -> Option { // config.json in the current working directory ($PWD) + let config_files = vec![config_file, "config.json"]; if let Ok(mut path) = env::current_dir() { - path.push("config.json"); - - if path.exists() { - return Some(path); + for filename in &config_files { + path.push(filename); + if path.exists() { + return Some(path); + } + path.pop(); } } else { // config.json in the current working directory (relative path) - let relative_path = PathBuf::from("config.json"); - if relative_path.exists() { - return Some(relative_path); + for filename in &config_files { + let relative_path = PathBuf::from(filename); + if relative_path.exists() { + return Some(relative_path); + } } } @@ -38,10 +43,12 @@ pub fn get_default_config_path() -> Option { // Windows: {FOLDERID_RoamingAppData}/shadowsocks/shadowsocks-rust/config/config.json let mut config_path = project_dirs.config_dir().to_path_buf(); - config_path.push("config.json"); - - if config_path.exists() { - return Some(config_path); + for filename in &config_files { + config_path.push(filename); + if config_path.exists() { + return Some(config_path); + } + config_path.pop(); } } @@ -51,17 +58,22 @@ pub fn get_default_config_path() -> Option { if let Ok(base_directories) = xdg::BaseDirectories::with_prefix("shadowsocks-rust") { // $XDG_CONFIG_HOME/shadowsocks-rust/config.json // for dir in $XDG_CONFIG_DIRS; $dir/shadowsocks-rust/config.json - if let Some(config_path) = base_directories.find_config_file("config.json") { - return Some(config_path); + for filename in &config_files { + if let Some(config_path) = base_directories.find_config_file(filename) { + return Some(config_path); + } } } // UNIX global configuration file #[cfg(unix)] { - let global_config_path = Path::new("/etc/shadowsocks-rust/config.json"); - if global_config_path.exists() { - return Some(global_config_path.to_path_buf()); + for filename in &config_files { + let path_str = "/etc/shadowsocks-rust/".to_owned() + filename; + let global_config_path = Path::new(&path_str); + if global_config_path.exists() { + return Some(global_config_path.to_path_buf()); + } } } diff --git a/src/service/local.rs b/src/service/local.rs index e1bc6693..d46a1aa9 100644 --- a/src/service/local.rs +++ b/src/service/local.rs @@ -500,7 +500,7 @@ pub fn main(matches: &ArgMatches) -> ExitCode { let (config, runtime) = { let config_path_opt = matches.get_one::("CONFIG").cloned().or_else(|| { if !matches.contains_id("SERVER_CONFIG") { - match crate::config::get_default_config_path() { + match crate::config::get_default_config_path("local.json") { None => None, Some(p) => { println!("loading default config {p:?}"); diff --git a/src/service/manager.rs b/src/service/manager.rs index 89e634d9..47b723a8 100644 --- a/src/service/manager.rs +++ b/src/service/manager.rs @@ -260,7 +260,7 @@ pub fn main(matches: &ArgMatches) -> ExitCode { let (config, runtime) = { let config_path_opt = matches.get_one::("CONFIG").cloned().or_else(|| { if !matches.contains_id("SERVER_CONFIG") { - match crate::config::get_default_config_path() { + match crate::config::get_default_config_path("manager.json") { None => None, Some(p) => { println!("loading default config {p:?}"); diff --git a/src/service/server.rs b/src/service/server.rs index 4072cc06..8e114500 100644 --- a/src/service/server.rs +++ b/src/service/server.rs @@ -272,7 +272,7 @@ pub fn main(matches: &ArgMatches) -> ExitCode { let (config, runtime) = { let config_path_opt = matches.get_one::("CONFIG").cloned().or_else(|| { if !matches.contains_id("SERVER_CONFIG") { - match crate::config::get_default_config_path() { + match crate::config::get_default_config_path("server.json") { None => None, Some(p) => { println!("loading default config {p:?}");