feat(local): online_config handled only in binary

This commit is contained in:
zonyitoo
2024-05-13 23:28:42 +08:00
parent 62867b3f3e
commit 60b1ecd4e9
3 changed files with 41 additions and 59 deletions

View File

@@ -120,14 +120,6 @@ struct SSBalancerConfig {
check_best_interval: Option<u64>,
}
#[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<u64>,
}
#[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<String>,
#[cfg(feature = "local-online-config")]
#[serde(skip_serializing_if = "Option::is_none")]
online_config: Option<SSOnlineConfig>,
#[cfg(feature = "local-online-config")]
#[serde(skip_serializing_if = "Option::is_none")]
version: Option<u32>,
@@ -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<Duration>,
}
/// 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<PathBuf>,
/// Online Configuration Delivery (SIP008)
#[cfg(feature = "local-online-config")]
pub online_config: Option<OnlineConfig>,
#[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())
}
}

View File

@@ -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<OnlineConfig>,
}
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<Duration>,
}
#[derive(Deserialize)]
struct SSConfig {
#[cfg(feature = "logging")]
log: Option<SSLogConfig>,
runtime: Option<SSRuntimeConfig>,
#[cfg(feature = "local-online-config")]
online_config: Option<SSOnlineConfig>,
}
#[cfg(feature = "logging")]
@@ -285,3 +312,10 @@ struct SSRuntimeConfig {
worker_count: Option<usize>,
mode: Option<String>,
}
#[cfg(feature = "local-online-config")]
#[derive(Deserialize, Debug, Default)]
struct SSOnlineConfig {
config_url: String,
update_interval: Option<u64>,
}

View File

@@ -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<Output = ExitCode>), 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::<PathBuf>("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<Output = Exi
#[cfg(feature = "local-online-config")]
if let Some(online_config_url) = matches.get_one::<String>("ONLINE_CONFIG_URL") {
use shadowsocks_service::config::OnlineConfig;
use crate::config::OnlineConfig;
let online_config_update_interval = matches.get_one::<u64>("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<Output = Exi
let runtime = builder.enable_all().build().expect("create tokio Runtime");
(config, runtime)
(config, service_config, runtime)
};
let main_fut = async move {
@@ -1003,7 +1003,7 @@ pub fn create(matches: &ArgMatches) -> Result<(Runtime, impl Future<Output = Exi
// Fetch servers from remote for the first time
#[cfg(feature = "local-online-config")]
if let Some(ref online_config) = config.online_config {
if let Some(ref online_config) = service_config.online_config {
if let Ok(mut servers) = get_online_config_servers(&online_config.config_url).await {
config.server.append(&mut servers);
}
@@ -1015,12 +1015,6 @@ pub fn create(matches: &ArgMatches) -> Result<(Runtime, impl Future<Output = Exi
return crate::EXIT_CODE_LOAD_CONFIG_FAILURE.into();
}
#[cfg(feature = "local-online-config")]
let (online_config_url, online_config_update_interval) = match config.online_config.clone() {
Some(o) => (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<Output = Exi
balancer: instance.server_balancer().clone(),
static_servers,
#[cfg(feature = "local-online-config")]
online_config_url,
online_config_url: service_config.online_config.as_ref().map(|c| c.config_url.clone()),
#[cfg(feature = "local-online-config")]
online_config_update_interval,
online_config_update_interval: service_config.online_config.as_ref().and_then(|c| c.update_interval),
}
.launch_reload_server_task();