Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 29 additions & 17 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,24 @@ use directories::ProjectDirs;
use serde::Deserialize;

/// Default configuration file path
pub fn get_default_config_path() -> Option<PathBuf> {
pub fn get_default_config_path(config_file: &str) -> Option<PathBuf> {
// 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);
}
}
}

Expand All @@ -38,10 +43,12 @@ pub fn get_default_config_path() -> Option<PathBuf> {
// 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();
}
}

Expand All @@ -51,17 +58,22 @@ pub fn get_default_config_path() -> Option<PathBuf> {
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());
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/service/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ pub fn main(matches: &ArgMatches) -> ExitCode {
let (config, runtime) = {
let config_path_opt = matches.get_one::<PathBuf>("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:?}");
Expand Down
2 changes: 1 addition & 1 deletion src/service/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ pub fn main(matches: &ArgMatches) -> ExitCode {
let (config, runtime) = {
let config_path_opt = matches.get_one::<PathBuf>("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:?}");
Expand Down
2 changes: 1 addition & 1 deletion src/service/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ pub fn main(matches: &ArgMatches) -> ExitCode {
let (config, runtime) = {
let config_path_opt = matches.get_one::<PathBuf>("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:?}");
Expand Down