From 60b1ecd4e984d06db5490df1afcb2149519895ea Mon Sep 17 00:00:00 2001 From: zonyitoo Date: Mon, 13 May 2024 23:28:42 +0800 Subject: [PATCH] feat(local): online_config handled only in binary --- crates/shadowsocks-service/src/config.rs | 46 ------------------------ src/config.rs | 34 ++++++++++++++++++ src/service/local.rs | 20 ++++------- 3 files changed, 41 insertions(+), 59 deletions(-) diff --git a/crates/shadowsocks-service/src/config.rs b/crates/shadowsocks-service/src/config.rs index 18488828..bb51d632 100644 --- a/crates/shadowsocks-service/src/config.rs +++ b/crates/shadowsocks-service/src/config.rs @@ -120,14 +120,6 @@ struct SSBalancerConfig { check_best_interval: Option, } -#[cfg(feature = "local-online-config")] -#[derive(Serialize, Deserialize, Debug, Default)] -struct SSOnlineConfig { - config_url: String, - #[serde(skip_serializing_if = "Option::is_none")] - update_interval: Option, -} - #[derive(Serialize, Deserialize, Debug, Default)] struct SSConfig { #[serde(skip_serializing_if = "Option::is_none")] @@ -239,10 +231,6 @@ struct SSConfig { #[serde(skip_serializing_if = "Option::is_none")] acl: Option, - #[cfg(feature = "local-online-config")] - #[serde(skip_serializing_if = "Option::is_none")] - online_config: Option, - #[cfg(feature = "local-online-config")] #[serde(skip_serializing_if = "Option::is_none")] version: Option, @@ -1249,17 +1237,6 @@ impl LocalInstanceConfig { } } -/// OnlineConfiguration (SIP008) -/// https://shadowsocks.org/doc/sip008.html -#[cfg(feature = "local-online-config")] -#[derive(Debug, Clone)] -pub struct OnlineConfig { - /// SIP008 URL - pub config_url: String, - /// Update interval, 3600s by default - pub update_interval: Option, -} - /// Configuration #[derive(Clone, Debug)] pub struct Config { @@ -1360,10 +1337,6 @@ pub struct Config { /// This is normally for auto-reloading if implementation supports. pub config_path: Option, - /// Online Configuration Delivery (SIP008) - #[cfg(feature = "local-online-config")] - pub online_config: Option, - #[doc(hidden)] /// Workers in runtime /// It should be replaced with metrics APIs: https://github.com/tokio-rs/tokio/issues/4073 @@ -1487,8 +1460,6 @@ impl Config { balancer: BalancerConfig::default(), config_path: None, - #[cfg(feature = "local-online-config")] - online_config: None, worker_count: 1, } @@ -2381,14 +2352,6 @@ impl Config { nconfig.acl = Some(acl); } - #[cfg(feature = "local-online-config")] - if let Some(online_config) = config.online_config { - nconfig.online_config = Some(OnlineConfig { - config_url: online_config.config_url, - update_interval: online_config.update_interval.map(Duration::from_secs), - }); - } - Ok(nconfig) } @@ -3127,15 +3090,6 @@ impl fmt::Display for Config { jconf.acl = Some(acl.file_path().to_str().unwrap().to_owned()); } - // OnlineConfig - #[cfg(feature = "local-online-config")] - if let Some(ref online_config) = self.online_config { - jconf.online_config = Some(SSOnlineConfig { - config_url: online_config.config_url.clone(), - update_interval: online_config.update_interval.map(|d| d.as_secs()), - }); - } - write!(f, "{}", json5::to_string(&jconf).unwrap()) } } diff --git a/src/config.rs b/src/config.rs index fe1d4fd8..77a1bae7 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,5 +1,7 @@ //! Common configuration utilities +#[cfg(feature = "local-online-config")] +use std::time::Duration; use std::{ env, fs::OpenOptions, @@ -102,6 +104,10 @@ pub struct Config { /// Runtime configuration pub runtime: RuntimeConfig, + + /// Online Configuration Delivery (SIP008) + #[cfg(feature = "local-online-config")] + pub online_config: Option, } impl Config { @@ -165,6 +171,14 @@ impl Config { config.runtime = nruntime; } + #[cfg(feature = "local-online-config")] + if let Some(online_config) = ssconfig.online_config { + config.online_config = Some(OnlineConfig { + config_url: online_config.config_url, + update_interval: online_config.update_interval.map(Duration::from_secs), + }); + } + Ok(config) } @@ -258,11 +272,24 @@ pub struct RuntimeConfig { pub mode: RuntimeMode, } +/// OnlineConfiguration (SIP008) +/// https://shadowsocks.org/doc/sip008.html +#[cfg(feature = "local-online-config")] +#[derive(Debug, Clone)] +pub struct OnlineConfig { + /// SIP008 URL + pub config_url: String, + /// Update interval, 3600s by default + pub update_interval: Option, +} + #[derive(Deserialize)] struct SSConfig { #[cfg(feature = "logging")] log: Option, runtime: Option, + #[cfg(feature = "local-online-config")] + online_config: Option, } #[cfg(feature = "logging")] @@ -285,3 +312,10 @@ struct SSRuntimeConfig { worker_count: Option, mode: Option, } + +#[cfg(feature = "local-online-config")] +#[derive(Deserialize, Debug, Default)] +struct SSOnlineConfig { + config_url: String, + update_interval: Option, +} diff --git a/src/service/local.rs b/src/service/local.rs index a3063ec9..b72e94cb 100644 --- a/src/service/local.rs +++ b/src/service/local.rs @@ -576,7 +576,7 @@ pub fn define_command_line_options(mut app: Command) -> Command { /// Create `Runtime` and `main` entry pub fn create(matches: &ArgMatches) -> Result<(Runtime, impl Future), ExitCode> { #[cfg_attr(not(feature = "local-online-config"), allow(unused_mut))] - let (mut config, runtime) = { + let (mut config, service_config, runtime) = { let config_path_opt = matches.get_one::("CONFIG").cloned().or_else(|| { if !matches.contains_id("SERVER_CONFIG") { match crate::config::get_default_config_path("local.json") { @@ -930,10 +930,10 @@ pub fn create(matches: &ArgMatches) -> Result<(Runtime, impl Future("ONLINE_CONFIG_URL") { - use shadowsocks_service::config::OnlineConfig; + use crate::config::OnlineConfig; let online_config_update_interval = matches.get_one::("ONLINE_CONFIG_UPDATE_INTERVAL").cloned(); - config.online_config = Some(OnlineConfig { + service_config.online_config = Some(OnlineConfig { config_url: online_config_url.clone(), update_interval: online_config_update_interval.map(Duration::from_secs), }); @@ -985,7 +985,7 @@ pub fn create(matches: &ArgMatches) -> Result<(Runtime, impl Future Result<(Runtime, impl Future Result<(Runtime, impl Future (Some(o.config_url), o.update_interval), - None => (None, None), - }; - let instance = Server::new(config).await.expect("create local"); let reload_task = ServerReloader { @@ -1028,9 +1022,9 @@ pub fn create(matches: &ArgMatches) -> Result<(Runtime, impl Future