Skip to content

Commit

Permalink
make "password" optional for none/plain method (#838)
Browse files Browse the repository at this point in the history
  • Loading branch information
zonyitoo committed May 16, 2022
1 parent 76f8b02 commit 77d343e
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 14 deletions.
49 changes: 43 additions & 6 deletions crates/shadowsocks-service/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,8 @@ struct SSServerExtConfig {
#[serde(alias = "port")]
server_port: u16,

password: String,
#[serde(skip_serializing_if = "Option::is_none")]
password: Option<String>,
method: String,

#[serde(skip_serializing_if = "Option::is_none")]
Expand Down Expand Up @@ -1461,7 +1462,7 @@ impl Config {
// Standard config
// Server
match (config.server, config.server_port, config.password, &config.method) {
(Some(address), Some(port), Some(pwd), Some(m)) => {
(Some(address), Some(port), pwd_opt, Some(m)) => {
let addr = match address.parse::<Ipv4Addr>() {
Ok(v4) => ServerAddr::SocketAddr(SocketAddr::V4(SocketAddrV4::new(v4, port))),
Err(..) => match address.parse::<Ipv6Addr>() {
Expand All @@ -1483,7 +1484,21 @@ impl Config {
};

// Only "password" support getting from environment variable.
let password = read_variable_field_value(&pwd);
let password = match pwd_opt {
Some(ref pwd) => read_variable_field_value(pwd),
None => {
if method.is_none() {
String::new().into()
} else {
let err = Error::new(
ErrorKind::MissingField,
"`password` is required",
Some(format!("`password` is required for method {}", method)),
);
return Err(err);
}
}
};

let mut nsvr = ServerConfig::new(addr, password, method);
nsvr.set_mode(global_mode);
Expand Down Expand Up @@ -1553,7 +1568,21 @@ impl Config {
};

// Only "password" support getting from environment variable.
let password = read_variable_field_value(&svr.password);
let password = match svr.password {
Some(ref pwd) => read_variable_field_value(pwd),
None => {
if method.is_none() {
String::new().into()
} else {
let err = Error::new(
ErrorKind::MissingField,
"`password` is required",
Some(format!("`password` is required for method {}", method)),
);
return Err(err);
}
}
};

let mut nsvr = ServerConfig::new(addr, password, method);

Expand Down Expand Up @@ -2161,7 +2190,11 @@ impl fmt::Display for Config {
ServerAddr::DomainName(.., port) => port,
});
jconf.method = Some(svr.method().to_string());
jconf.password = Some(svr.password().to_string());
jconf.password = if svr.method().is_none() {
None
} else {
Some(svr.password().to_string())
};
jconf.plugin = svr.plugin().map(|p| p.plugin.to_string());
jconf.plugin_opts = svr.plugin().and_then(|p| p.plugin_opts.clone());
jconf.plugin_args = svr.plugin().and_then(|p| {
Expand All @@ -2188,7 +2221,11 @@ impl fmt::Display for Config {
ServerAddr::SocketAddr(ref sa) => sa.port(),
ServerAddr::DomainName(.., port) => port,
},
password: svr.password().to_string(),
password: if svr.method().is_none() {
None
} else {
Some(svr.password().to_string())
},
method: svr.method().to_string(),
disabled: None,
plugin: svr.plugin().map(|p| p.plugin.to_string()),
Expand Down
14 changes: 10 additions & 4 deletions src/service/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,18 +416,24 @@ pub fn main(matches: &ArgMatches) {
};

if let Some(svr_addr) = matches.value_of("SERVER_ADDR") {
let method = matches.value_of_t_or_exit::<CipherKind>("ENCRYPT_METHOD");

let password = match matches.value_of_t::<String>("PASSWORD") {
Ok(pwd) => read_variable_field_value(&pwd).into(),
Err(err) => {
// NOTE: svr_addr should have been checked by crate::validator
match crate::password::read_server_password(svr_addr) {
Ok(pwd) => pwd,
Err(..) => err.exit(),
if method.is_none() {
// If method doesn't need a key (none, plain), then we can leave it empty
String::new()
} else {
match crate::password::read_server_password(svr_addr) {
Ok(pwd) => pwd,
Err(..) => err.exit(),
}
}
}
};

let method = matches.value_of_t_or_exit::<CipherKind>("ENCRYPT_METHOD");
let svr_addr = svr_addr.parse::<ServerAddr>().expect("server-addr");

let timeout = match matches.value_of_t::<u64>("TIMEOUT") {
Expand Down
14 changes: 10 additions & 4 deletions src/service/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,18 +273,24 @@ pub fn main(matches: &ArgMatches) {
};

if let Some(svr_addr) = matches.value_of("SERVER_ADDR") {
let method = matches.value_of_t_or_exit::<CipherKind>("ENCRYPT_METHOD");

let password = match matches.value_of_t::<String>("PASSWORD") {
Ok(pwd) => read_variable_field_value(&pwd).into(),
Err(err) => {
// NOTE: svr_addr should have been checked by crate::validator
match crate::password::read_server_password(svr_addr) {
Ok(pwd) => pwd,
Err(..) => err.exit(),
if method.is_none() {
// If method doesn't need a key (none, plain), then we can leave it empty
String::new()
} else {
match crate::password::read_server_password(svr_addr) {
Ok(pwd) => pwd,
Err(..) => err.exit(),
}
}
}
};

let method = matches.value_of_t_or_exit::<CipherKind>("ENCRYPT_METHOD");
let svr_addr = svr_addr.parse::<ServerAddr>().expect("server-addr");
let timeout = match matches.value_of_t::<u64>("TIMEOUT") {
Ok(t) => Some(Duration::from_secs(t)),
Expand Down

0 comments on commit 77d343e

Please sign in to comment.