Skip to content

Commit

Permalink
Test suit for C API
Browse files Browse the repository at this point in the history
  • Loading branch information
ssrlive committed Feb 17, 2024
1 parent 30b9dc3 commit 6de34d9
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 11 deletions.
13 changes: 5 additions & 8 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ pub unsafe extern "C" fn over_tls_client_run(
ctx: *mut c_void,
) -> c_int {
log::set_max_level(verbosity.into());
log::set_boxed_logger(Box::<crate::dump_logger::DumpLogger>::default()).unwrap();

if let Err(err) = log::set_boxed_logger(Box::<crate::dump_logger::DumpLogger>::default()) {
log::info!("failed to set logger, error={:?}", err);
}
_over_tls_client_run(config_path, callback, ctx)
}

Expand All @@ -61,12 +62,8 @@ unsafe fn _over_tls_client_run(
let block = || -> Result<()> {
let config_path = std::ffi::CStr::from_ptr(config_path).to_str()?;

let cb = |addr: SocketAddr| {
log::trace!("Listening on {}", addr);
let port = addr.port();
unsafe {
ccb.call(port as c_int);
}
let cb = |addr: SocketAddr| unsafe {
ccb.call(addr.port() as _);
};

let mut config = crate::config::Config::from_config_file(config_path)?;
Expand Down
31 changes: 31 additions & 0 deletions src/bin/overtls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,37 @@ use overtls::{client, config, server, CmdOpt, Error, Result};
fn main() -> Result<()> {
let opt = CmdOpt::parse_cmd();

if opt.c_api {
if opt.is_server() {
return Err(Error::from("C API is not supported for server"));
}

// Test the C API usage
let config_path_str = opt.config.as_path().to_string_lossy().into_owned();
let c_string = std::ffi::CString::new(config_path_str).unwrap();
let config_path: *const std::os::raw::c_char = c_string.as_ptr();

let join = ctrlc2::set_handler(|| {
log::info!("Ctrl-C received, exiting...");
unsafe { overtls::over_tls_client_stop() };
true
})
.expect("Error setting Ctrl-C handler");

unsafe extern "C" fn log_cb(_: overtls::ArgVerbosity, msg: *const std::os::raw::c_char, _ctx: *mut std::os::raw::c_void) {
println!("{:?}", unsafe { std::ffi::CStr::from_ptr(msg).to_str() });
}
unsafe { overtls::overtls_set_log_callback(Some(log_cb), std::ptr::null_mut()) };

unsafe extern "C" fn port_cb(port: i32, _ctx: *mut std::os::raw::c_void) {
log::info!("Listening on {}", port);
}
unsafe { overtls::over_tls_client_run(config_path, opt.verbosity, Some(port_cb), std::ptr::null_mut()) };

join.join().unwrap();
return Ok(());
}

dotenvy::dotenv().ok();

let level = format!("{}={:?}", module_path!(), opt.verbosity);
Expand Down
4 changes: 4 additions & 0 deletions src/cmdopt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ pub struct CmdOpt {
/// Generate QR code for client.
#[arg(short, long)]
pub qrcode: bool,

/// Use C API for client.
#[arg(long)]
pub c_api: bool,
}

impl CmdOpt {
Expand Down
6 changes: 3 additions & 3 deletions src/dump_logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{
};

lazy_static::lazy_static! {
pub static ref DUMP_CALLBACK: Mutex<Option<DumpCallback>> = Mutex::new(None);
static ref DUMP_CALLBACK: Mutex<Option<DumpCallback>> = Mutex::new(None);
}

/// # Safety
Expand All @@ -20,7 +20,7 @@ pub unsafe extern "C" fn overtls_set_log_callback(
}

#[derive(Clone)]
pub struct DumpCallback(Option<unsafe extern "C" fn(ArgVerbosity, *const c_char, *mut c_void)>, *mut c_void);
struct DumpCallback(Option<unsafe extern "C" fn(ArgVerbosity, *const c_char, *mut c_void)>, *mut c_void);

impl DumpCallback {
unsafe fn call(self, dump_level: ArgVerbosity, info: *const c_char) {
Expand All @@ -34,7 +34,7 @@ unsafe impl Send for DumpCallback {}
unsafe impl Sync for DumpCallback {}

#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct DumpLogger {}
pub(crate) struct DumpLogger;

impl log::Log for DumpLogger {
fn enabled(&self, metadata: &log::Metadata) -> bool {
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ pub(crate) mod udprelay;
pub(crate) mod webapi;
pub(crate) mod weirduri;

pub use api::{over_tls_client_run, over_tls_client_stop};
use base64_wrapper::{base64_decode, base64_encode, Base64Engine};
use bytes::BytesMut;
pub use cmdopt::{ArgVerbosity, CmdOpt, Role};
pub use dump_logger::overtls_set_log_callback;
pub use error::{Error, Result};
use socks5_impl::protocol::{Address, StreamOperation};
pub use tokio_util::sync::CancellationToken;
Expand Down

0 comments on commit 6de34d9

Please sign in to comment.