From ec812064697a62457b332824d5608cf2cce85a8a Mon Sep 17 00:00:00 2001 From: eric Date: Sun, 1 May 2022 09:57:52 +0800 Subject: [PATCH] ffi: run with config string in alternative to file path --- leaf-ffi/src/lib.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/leaf-ffi/src/lib.rs b/leaf-ffi/src/lib.rs index 0839181..b48927c 100644 --- a/leaf-ffi/src/lib.rs +++ b/leaf-ffi/src/lib.rs @@ -106,6 +106,24 @@ pub extern "C" fn leaf_run(rt_id: u16, config_path: *const c_char) -> i32 { } } +#[no_mangle] +pub extern "C" fn leaf_run_with_config_string(rt_id: u16, config: *const c_char) -> i32 { + if let Ok(config) = unsafe { CStr::from_ptr(config).to_str() } { + let opts = leaf::StartOptions { + config: leaf::Config::Str(config.to_string()), + #[cfg(feature = "auto-reload")] + auto_reload: false, + runtime_opt: leaf::RuntimeOption::SingleThread, + }; + if let Err(e) = leaf::start(rt_id, opts) { + return to_errno(e); + } + ERR_OK + } else { + ERR_CONFIG_PATH + } +} + /// Reloads DNS servers, outbounds and routing rules from the config file. /// /// @param rt_id The ID of the leaf instance to reload.