@@ -14,19 +14,24 @@ use directories::ProjectDirs;
1414use 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
0 commit comments