Skip to content

Commit 67232c3

Browse files
authored
Added configs for snap daemons (shadowsocks#1204)
1 parent c2877c1 commit 67232c3

File tree

4 files changed

+32
-20
lines changed

4 files changed

+32
-20
lines changed

src/config.rs

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,24 @@ use directories::ProjectDirs;
1414
use serde::Deserialize;
1515

1616
/// Default configuration file path
17-
pub fn get_default_config_path() -> Option<PathBuf> {
17+
pub fn get_default_config_path(config_file: &str) -> Option<PathBuf> {
1818
// config.json in the current working directory ($PWD)
19+
let config_files = vec![config_file, "config.json"];
1920
if let Ok(mut path) = env::current_dir() {
20-
path.push("config.json");
21-
22-
if path.exists() {
23-
return Some(path);
21+
for filename in &config_files {
22+
path.push(filename);
23+
if path.exists() {
24+
return Some(path);
25+
}
26+
path.pop();
2427
}
2528
} else {
2629
// config.json in the current working directory (relative path)
27-
let relative_path = PathBuf::from("config.json");
28-
if relative_path.exists() {
29-
return Some(relative_path);
30+
for filename in &config_files {
31+
let relative_path = PathBuf::from(filename);
32+
if relative_path.exists() {
33+
return Some(relative_path);
34+
}
3035
}
3136
}
3237

@@ -38,10 +43,12 @@ pub fn get_default_config_path() -> Option<PathBuf> {
3843
// Windows: {FOLDERID_RoamingAppData}/shadowsocks/shadowsocks-rust/config/config.json
3944

4045
let mut config_path = project_dirs.config_dir().to_path_buf();
41-
config_path.push("config.json");
42-
43-
if config_path.exists() {
44-
return Some(config_path);
46+
for filename in &config_files {
47+
config_path.push(filename);
48+
if config_path.exists() {
49+
return Some(config_path);
50+
}
51+
config_path.pop();
4552
}
4653
}
4754

@@ -51,17 +58,22 @@ pub fn get_default_config_path() -> Option<PathBuf> {
5158
if let Ok(base_directories) = xdg::BaseDirectories::with_prefix("shadowsocks-rust") {
5259
// $XDG_CONFIG_HOME/shadowsocks-rust/config.json
5360
// for dir in $XDG_CONFIG_DIRS; $dir/shadowsocks-rust/config.json
54-
if let Some(config_path) = base_directories.find_config_file("config.json") {
55-
return Some(config_path);
61+
for filename in &config_files {
62+
if let Some(config_path) = base_directories.find_config_file(filename) {
63+
return Some(config_path);
64+
}
5665
}
5766
}
5867

5968
// UNIX global configuration file
6069
#[cfg(unix)]
6170
{
62-
let global_config_path = Path::new("/etc/shadowsocks-rust/config.json");
63-
if global_config_path.exists() {
64-
return Some(global_config_path.to_path_buf());
71+
for filename in &config_files {
72+
let path_str = "/etc/shadowsocks-rust/".to_owned() + filename;
73+
let global_config_path = Path::new(&path_str);
74+
if global_config_path.exists() {
75+
return Some(global_config_path.to_path_buf());
76+
}
6577
}
6678
}
6779

src/service/local.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ pub fn main(matches: &ArgMatches) -> ExitCode {
500500
let (config, runtime) = {
501501
let config_path_opt = matches.get_one::<PathBuf>("CONFIG").cloned().or_else(|| {
502502
if !matches.contains_id("SERVER_CONFIG") {
503-
match crate::config::get_default_config_path() {
503+
match crate::config::get_default_config_path("local.json") {
504504
None => None,
505505
Some(p) => {
506506
println!("loading default config {p:?}");

src/service/manager.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ pub fn main(matches: &ArgMatches) -> ExitCode {
260260
let (config, runtime) = {
261261
let config_path_opt = matches.get_one::<PathBuf>("CONFIG").cloned().or_else(|| {
262262
if !matches.contains_id("SERVER_CONFIG") {
263-
match crate::config::get_default_config_path() {
263+
match crate::config::get_default_config_path("manager.json") {
264264
None => None,
265265
Some(p) => {
266266
println!("loading default config {p:?}");

src/service/server.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ pub fn main(matches: &ArgMatches) -> ExitCode {
272272
let (config, runtime) = {
273273
let config_path_opt = matches.get_one::<PathBuf>("CONFIG").cloned().or_else(|| {
274274
if !matches.contains_id("SERVER_CONFIG") {
275-
match crate::config::get_default_config_path() {
275+
match crate::config::get_default_config_path("server.json") {
276276
None => None,
277277
Some(p) => {
278278
println!("loading default config {p:?}");

0 commit comments

Comments
 (0)