From fc40cef2c3ec140db75fa015a8dbf635ac97c565 Mon Sep 17 00:00:00 2001 From: zonyitoo Date: Tue, 17 Aug 2021 17:35:36 +0800 Subject: [PATCH] ssmanager support default --plugin and --plugin-opts - fixes #421 --- bin/ssmanager.rs | 12 ++++++ crates/shadowsocks-service/src/config.rs | 41 +++++++++++++++++-- .../shadowsocks-service/src/manager/server.rs | 2 + 3 files changed, 51 insertions(+), 4 deletions(-) diff --git a/bin/ssmanager.rs b/bin/ssmanager.rs index fb7ea441..a3657061 100644 --- a/bin/ssmanager.rs +++ b/bin/ssmanager.rs @@ -21,6 +21,7 @@ use shadowsocks_service::{ shadowsocks::{ config::{ManagerAddr, Mode}, crypto::v1::{available_ciphers, CipherKind}, + plugin::PluginConfig, }, }; @@ -55,6 +56,9 @@ fn main() { (@arg ENCRYPT_METHOD: -m --("encrypt-method") +takes_value possible_values(available_ciphers()) "Default encryption method") (@arg TIMEOUT: --timeout +takes_value {validator::validate_u64} "Default timeout seconds for TCP relay") + (@arg PLUGIN: --plugin +takes_value requires[SERVER_ADDR] "Default SIP003 (https://shadowsocks.org/en/spec/Plugin.html) plugin") + (@arg PLUGIN_OPT: --("plugin-opts") +takes_value requires[PLUGIN] "Default SIP003 plugin options") + (@arg ACL: --acl +takes_value "Path to ACL (Access Control List)") (@arg DNS: --dns +takes_value "DNS nameservers, formatted like [(tcp|udp)://]host[:port][,host[:port]]..., or unix:///path/to/dns, or predefined keys like \"google\", \"cloudflare\"") @@ -198,6 +202,14 @@ fn main() { if let Some(sh) = matches.value_of("SERVER_HOST") { manager_config.server_host = sh.parse::().unwrap(); } + + if let Some(p) = matches.value_of("PLUGIN") { + manager_config.plugin = Some(PluginConfig { + plugin: p.to_owned(), + plugin_opts: matches.value_of("PLUGIN_OPT").map(ToOwned::to_owned), + plugin_args: Vec::new(), + }); + } } // Overrides diff --git a/crates/shadowsocks-service/src/config.rs b/crates/shadowsocks-service/src/config.rs index 62552ca8..e1818718 100644 --- a/crates/shadowsocks-service/src/config.rs +++ b/crates/shadowsocks-service/src/config.rs @@ -514,6 +514,8 @@ pub struct ManagerConfig { pub addr: ManagerAddr, /// Manager's default method pub method: Option, + /// Manager's default plugin + pub plugin: Option, /// Timeout for TCP connections, setting to manager's created servers pub timeout: Option, /// IP/Host for servers to bind (inbound) @@ -530,6 +532,7 @@ impl ManagerConfig { ManagerConfig { addr, method: None, + plugin: None, timeout: None, server_host: ManagerServerHost::default(), mode: Mode::TcpOnly, @@ -1190,14 +1193,14 @@ impl Config { let mut nsvr = ServerConfig::new(addr, pwd, method); nsvr.set_mode(global_mode); - if let Some(p) = config.plugin { + if let Some(ref p) = config.plugin { // SIP008 allows "plugin" to be an empty string // Empty string implies "no plugin" if !p.is_empty() { let plugin = PluginConfig { - plugin: p, - plugin_opts: config.plugin_opts, - plugin_args: config.plugin_args.unwrap_or_default(), + plugin: p.clone(), + plugin_opts: config.plugin_opts.clone(), + plugin_args: config.plugin_args.clone().unwrap_or_default(), }; nsvr.set_plugin(plugin); } @@ -1362,6 +1365,18 @@ impl Config { } } + if let Some(p) = config.plugin { + // SIP008 allows "plugin" to be an empty string + // Empty string implies "no plugin" + if !p.is_empty() { + manager_config.plugin = Some(PluginConfig { + plugin: p, + plugin_opts: config.plugin_opts, + plugin_args: config.plugin_args.unwrap_or_default(), + }); + } + } + nconfig.manager = Some(manager_config); } @@ -1870,6 +1885,24 @@ impl fmt::Display for Config { if jconf.mode.is_none() { jconf.mode = Some(m.mode.to_string()); } + + if jconf.method.is_none() { + if let Some(ref m) = m.method { + jconf.method = Some(m.to_string()); + } + } + + if jconf.plugin.is_none() { + if let Some(ref p) = m.plugin { + jconf.plugin = Some(p.plugin.clone()); + if let Some(ref o) = p.plugin_opts { + jconf.plugin_opts = Some(o.clone()); + } + if !p.plugin_args.is_empty() { + jconf.plugin_args = Some(p.plugin_args.clone()); + } + } + } } if self.no_delay { diff --git a/crates/shadowsocks-service/src/manager/server.rs b/crates/shadowsocks-service/src/manager/server.rs index db7f12e3..6389e47b 100644 --- a/crates/shadowsocks-service/src/manager/server.rs +++ b/crates/shadowsocks-service/src/manager/server.rs @@ -237,6 +237,8 @@ impl Manager { plugin_args: Vec::new(), }; svr_cfg.set_plugin(p); + } else if let Some(ref plugin) = self.svr_cfg.plugin { + svr_cfg.set_plugin(plugin.clone()); } let mode = match req.mode {