diff --git a/leaf/src/config/conf/config.rs b/leaf/src/config/conf/config.rs index 4927861..952b777 100644 --- a/leaf/src/config/conf/config.rs +++ b/leaf/src/config/conf/config.rs @@ -1170,6 +1170,12 @@ pub fn to_internal(conf: &mut Config) -> Result { Ok(config) } +pub fn from_string(s: &str) -> Result { + let lines = s.lines().map(|s| Ok(s.to_string())).collect(); + let mut config = from_lines(lines)?; + to_internal(&mut config) +} + pub fn from_file

(path: P) -> Result where P: AsRef, diff --git a/leaf/src/config/json/config.rs b/leaf/src/config/json/config.rs index cbedb3f..5184f14 100644 --- a/leaf/src/config/json/config.rs +++ b/leaf/src/config/json/config.rs @@ -1008,9 +1008,13 @@ pub fn to_internal(json: &mut Config) -> Result { Ok(config) } -pub fn from_string(config: String) -> Result { - serde_json::from_str(config.as_str()) - .map_err(|e| anyhow!("deserialize json config failed: {}", e)) +pub fn json_from_string(config: &str) -> Result { + serde_json::from_str(config).map_err(|e| anyhow!("deserialize json config failed: {}", e)) +} + +pub fn from_string(s: &str) -> Result { + let mut config = json_from_string(s)?; + to_internal(&mut config) } pub fn from_file

(path: P) -> Result @@ -1018,6 +1022,6 @@ where P: AsRef, { let config = std::fs::read_to_string(path)?; - let mut config = from_string(config)?; + let mut config = json_from_string(&config)?; to_internal(&mut config) } diff --git a/leaf/src/config/json/tests/test_config.rs b/leaf/src/config/json/tests/test_config.rs index 5fef894..11e9c59 100644 --- a/leaf/src/config/json/tests/test_config.rs +++ b/leaf/src/config/json/tests/test_config.rs @@ -88,5 +88,5 @@ fn test_config() { } "#; - assert!(crate::config::json::from_string(json_str.to_string()).is_ok()); + assert!(crate::config::json::json_from_string(json_str).is_ok()); } diff --git a/leaf/src/config/json/tests/test_dns.rs b/leaf/src/config/json/tests/test_dns.rs index 1a63d0a..f1eef0f 100644 --- a/leaf/src/config/json/tests/test_dns.rs +++ b/leaf/src/config/json/tests/test_dns.rs @@ -12,7 +12,7 @@ fn test_dns_hosts() { } } "#; - let mut config = crate::config::json::from_string(json_str.to_string()).unwrap(); + let mut config = crate::config::json::json_from_string(json_str).unwrap(); let hosts = config.dns.as_ref().unwrap().hosts.as_ref().unwrap(); let ips = vec!["192.168.0.1".to_string(), "192.168.0.2".to_string()]; diff --git a/leaf/src/config/mod.rs b/leaf/src/config/mod.rs index b403bbe..f89a2ae 100644 --- a/leaf/src/config/mod.rs +++ b/leaf/src/config/mod.rs @@ -15,6 +15,21 @@ pub mod conf; pub use internal::*; +pub fn from_string(s: &str) -> Result { + #[cfg(feature = "config-json")] + { + if let Ok(c) = json::from_string(s) { + return Ok(c); + } + } + #[cfg(feature = "config-conf")] + { + return conf::from_string(s); + } + #[allow(unreachable_code)] + Err(anyhow!("could not load config from:\n{:?}", s)) +} + pub fn from_file(path: &str) -> Result { if let Some(ext) = Path::new(path).extension() { if let Some(ext) = ext.to_str() { diff --git a/leaf/src/lib.rs b/leaf/src/lib.rs index 2414189..0502a93 100644 --- a/leaf/src/lib.rs +++ b/leaf/src/lib.rs @@ -323,6 +323,7 @@ pub enum RuntimeOption { #[derive(Debug)] pub enum Config { File(String), + Str(String), Internal(config::Config), } @@ -350,6 +351,7 @@ pub fn start(rt_id: RuntimeId, opts: StartOptions) -> Result<(), Error> { let mut config = match opts.config { Config::File(p) => config::from_file(&p).map_err(Error::Config)?, + Config::Str(s) => config::from_string(&s).map_err(Error::Config)?, Config::Internal(c) => c, }; @@ -532,3 +534,127 @@ pub fn start(rt_id: RuntimeId, opts: StartOptions) -> Result<(), Error> { Ok(()) } + +#[cfg(test)] +mod tests { + use super::*; + use std::thread; + + #[test] + fn test_restart() { + let conf = r#" +[General] +logllevel = trace +dns-server = 1.1.1.1 +socks-interface = 127.0.0.1 +socks-port = 1080 + +[Proxy] +Direct = direct +"#; + + thread::spawn(move || { + let opts = StartOptions { + config: Config::Str(conf.to_string()), + #[cfg(feature = "auto-reload")] + auto_reload: false, + runtime_opt: RuntimeOption::SingleThread, + }; + start(0, opts); + }); + thread::sleep(std::time::Duration::from_secs(2)); + shutdown(0); + thread::sleep(std::time::Duration::from_secs(2)); + + thread::spawn(move || { + let opts = StartOptions { + config: Config::Str(conf.to_string()), + #[cfg(feature = "auto-reload")] + auto_reload: false, + runtime_opt: RuntimeOption::SingleThread, + }; + start(0, opts); + }); + thread::sleep(std::time::Duration::from_secs(2)); + shutdown(0); + thread::sleep(std::time::Duration::from_secs(2)); + + thread::spawn(move || { + let opts = StartOptions { + config: Config::Str(conf.to_string()), + #[cfg(feature = "auto-reload")] + auto_reload: false, + runtime_opt: RuntimeOption::SingleThread, + }; + start(0, opts); + }); + thread::sleep(std::time::Duration::from_secs(2)); + shutdown(0); + thread::sleep(std::time::Duration::from_secs(2)); + + thread::spawn(move || { + let opts = StartOptions { + config: Config::Str(conf.to_string()), + #[cfg(feature = "auto-reload")] + auto_reload: false, + runtime_opt: RuntimeOption::SingleThread, + }; + start(0, opts); + }); + thread::sleep(std::time::Duration::from_secs(2)); + shutdown(0); + thread::sleep(std::time::Duration::from_secs(2)); + + thread::spawn(move || { + let opts = StartOptions { + config: Config::Str(conf.to_string()), + #[cfg(feature = "auto-reload")] + auto_reload: false, + runtime_opt: RuntimeOption::SingleThread, + }; + start(0, opts); + }); + thread::sleep(std::time::Duration::from_secs(2)); + shutdown(0); + thread::sleep(std::time::Duration::from_secs(2)); + + thread::spawn(move || { + let opts = StartOptions { + config: Config::Str(conf.to_string()), + #[cfg(feature = "auto-reload")] + auto_reload: false, + runtime_opt: RuntimeOption::SingleThread, + }; + start(0, opts); + }); + thread::sleep(std::time::Duration::from_secs(2)); + shutdown(0); + thread::sleep(std::time::Duration::from_secs(2)); + + thread::spawn(move || { + let opts = StartOptions { + config: Config::Str(conf.to_string()), + #[cfg(feature = "auto-reload")] + auto_reload: false, + runtime_opt: RuntimeOption::SingleThread, + }; + start(0, opts); + }); + thread::sleep(std::time::Duration::from_secs(2)); + shutdown(0); + thread::sleep(std::time::Duration::from_secs(2)); + + thread::spawn(move || { + let opts = StartOptions { + config: Config::Str(conf.to_string()), + #[cfg(feature = "auto-reload")] + auto_reload: false, + runtime_opt: RuntimeOption::SingleThread, + }; + start(0, opts); + }); + thread::sleep(std::time::Duration::from_secs(2)); + shutdown(0); + thread::sleep(std::time::Duration::from_secs(2)); + } +} diff --git a/leaf/tests/common.rs b/leaf/tests/common.rs index da06dbc..d3f23d7 100644 --- a/leaf/tests/common.rs +++ b/leaf/tests/common.rs @@ -51,8 +51,7 @@ pub fn run_leaf_instances( let mut leaf_rt_ids = Vec::new(); let mut rt_id = 0; for config in configs { - let mut config = leaf::config::json::from_string(config).unwrap(); - let config = leaf::config::json::to_internal(&mut config).unwrap(); + let config = leaf::config::json::from_string(&config).unwrap(); let opts = leaf::StartOptions { config: leaf::Config::Internal(config), #[cfg(feature = "auto-reload")]