Skip to content

Commit

Permalink
overtls_generate_url && overtls_free_string
Browse files Browse the repository at this point in the history
  • Loading branch information
ssrlive committed May 22, 2024
1 parent 638f2ee commit 48fac41
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
2 changes: 2 additions & 0 deletions cbindgen.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ include = [
"over_tls_client_run_with_ssr_url",
"over_tls_client_stop",
"overtls_set_log_callback",
"overtls_generate_url",
"overtls_free_string",
]
exclude = [
"Java_com_github_shadowsocks_bg_OverTlsWrapper_runClient",
Expand Down
32 changes: 32 additions & 0 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,35 @@ pub unsafe extern "C" fn over_tls_client_stop() -> c_int {
}
0
}

/// # Safety
///
/// Create a SSR URL from the config file.
#[no_mangle]
pub unsafe extern "C" fn overtls_generate_url(cfg_path: *const c_char, include_ca_file: bool) -> *mut c_char {
let cfg_path = std::ffi::CStr::from_ptr(cfg_path);
let cfg_path = match cfg_path.to_str() {
Ok(s) => s,
Err(_) => return std::ptr::null_mut(),
};
let url = match crate::config::generate_ssr_url(cfg_path, include_ca_file) {
Ok(s) => s,
Err(_) => return std::ptr::null_mut(),
};
let url = match std::ffi::CString::new(url) {
Ok(s) => s,
Err(_) => return std::ptr::null_mut(),
};
url.into_raw()
}

/// # Safety
///
/// Free the string returned by `overtls_generate_url`.
#[no_mangle]
pub unsafe extern "C" fn overtls_free_string(s: *mut c_char) {
if s.is_null() {
return;
}
drop(std::ffi::CString::from_raw(s));
}
11 changes: 11 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,17 @@ impl Config {
}
}

pub(crate) fn generate_ssr_url<P>(path: P, include_ca_file: bool) -> Result<String>
where
P: AsRef<std::path::Path>,
{
let config = Config::from_config_file(path)?;
if config.certificate_content().is_some() && !include_ca_file {
log::warn!("Certificate content discarded");
}
config.generate_ssr_url(include_ca_file)
}

#[test]
fn test_config() {
let mut config = Config::new();
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub(crate) mod udprelay;
pub(crate) mod webapi;
pub(crate) mod weirduri;

pub use api::{over_tls_client_run, over_tls_client_run_with_ssr_url, over_tls_client_stop};
pub use api::{over_tls_client_run, over_tls_client_run_with_ssr_url, over_tls_client_stop, overtls_free_string, overtls_generate_url};
use base64_wrapper::{base64_decode, base64_encode, Base64Engine};
use bytes::BytesMut;
pub use client::run_client;
Expand Down

0 comments on commit 48fac41

Please sign in to comment.