diff --git a/apple/overtls/OverTlsWrapper.h b/apple/overtls/OverTlsWrapper.h index 1e32970..bb83a42 100644 --- a/apple/overtls/OverTlsWrapper.h +++ b/apple/overtls/OverTlsWrapper.h @@ -12,6 +12,7 @@ + (void) startWithConfig:(NSString*)filePath handler:(void (*)(int port, void *ctx))handler context:(void*)ctx; + (void) shutdown; ++ (void) setLogCallback:(void (*)(int verbosity, const char*, void*))cb context:(void*)ctx; @end diff --git a/apple/overtls/OverTlsWrapper.m b/apple/overtls/OverTlsWrapper.m index f37f3aa..754fdcf 100644 --- a/apple/overtls/OverTlsWrapper.m +++ b/apple/overtls/OverTlsWrapper.m @@ -15,11 +15,16 @@ @implementation OverTlsWrapper + (void)startWithConfig:(NSString *)filePath handler:(void (*)(int port, void *ctx))handler context:(void *)ctx { - over_tls_client_run(filePath.UTF8String, Info, handler, ctx); + over_tls_client_run(filePath.UTF8String, Trace, handler, ctx); } + (void)shutdown { over_tls_client_stop(); } ++ (void) setLogCallback:(void (*)(int verbosity, const char*, void*))cb context:(void*)ctx { + typedef void (*p_cb)(enum ArgVerbosity, const char*, void*); + overtls_set_log_callback((p_cb)cb, ctx); +} + @end diff --git a/src/api.rs b/src/api.rs index bf035ac..479d0d3 100644 --- a/src/api.rs +++ b/src/api.rs @@ -48,8 +48,7 @@ unsafe fn _over_tls_client_run( ctx: *mut c_void, ) -> c_int { let shutdown_token = crate::CancellationToken::new(); - { - let mut lock = EXITING_FLAG.lock().unwrap(); + if let Ok(mut lock) = EXITING_FLAG.lock() { if lock.is_some() { log::error!("tun2proxy already started"); return -1; diff --git a/src/config.rs b/src/config.rs index 8280545..9d979a0 100644 --- a/src/config.rs +++ b/src/config.rs @@ -131,11 +131,12 @@ impl Config { } pub fn listen_addr(&self) -> Result { + let unspec = std::net::IpAddr::from(Ipv4Addr::UNSPECIFIED); if self.is_server { - let f = |s: &Server| SocketAddr::new(s.listen_host.parse().unwrap(), s.listen_port); + let f = |s: &Server| SocketAddr::new(s.listen_host.parse().unwrap_or(unspec), s.listen_port); self.server.as_ref().map(f).ok_or_else(|| "Server listen address is not set".into()) } else { - let f = |c: &Client| SocketAddr::new(c.listen_host.parse().unwrap(), c.listen_port); + let f = |c: &Client| SocketAddr::new(c.listen_host.parse().unwrap_or(unspec), c.listen_port); self.client.as_ref().map(f).ok_or_else(|| "Client listen address is not set".into()) } } diff --git a/src/dump_logger.rs b/src/dump_logger.rs index 2f6a800..a8bb5fc 100644 --- a/src/dump_logger.rs +++ b/src/dump_logger.rs @@ -1,4 +1,4 @@ -use crate::ArgVerbosity; +use crate::{ArgVerbosity, BoxError}; use std::{ os::raw::{c_char, c_void}, sync::Mutex, @@ -16,7 +16,11 @@ pub unsafe extern "C" fn overtls_set_log_callback( callback: Option, ctx: *mut c_void, ) { - *DUMP_CALLBACK.lock().unwrap() = Some(DumpCallback(callback, ctx)); + if let Ok(mut cb) = DUMP_CALLBACK.lock() { + *cb = Some(DumpCallback(callback, ctx)); + } else { + log::error!("set log callback failed"); + } } #[derive(Clone)] @@ -45,7 +49,9 @@ impl log::Log for DumpLogger { if self.enabled(record.metadata()) { let current_crate_name = env!("CARGO_CRATE_NAME"); if record.module_path().unwrap_or("").starts_with(current_crate_name) { - self.do_dump_log(record); + if let Err(err) = self.do_dump_log(record) { + log::error!("failed to dump log, error={:?}", err); + } } } } @@ -54,7 +60,7 @@ impl log::Log for DumpLogger { } impl DumpLogger { - fn do_dump_log(&self, record: &log::Record) { + fn do_dump_log(&self, record: &log::Record) -> Result<(), BoxError> { let timestamp: chrono::DateTime = chrono::Local::now(); let msg = format!( "[{} {:<5} {}] - {}", @@ -63,12 +69,15 @@ impl DumpLogger { record.module_path().unwrap_or(""), record.args() ); - let c_msg = std::ffi::CString::new(msg).unwrap(); + let c_msg = std::ffi::CString::new(msg)?; let ptr = c_msg.as_ptr(); - if let Some(cb) = DUMP_CALLBACK.lock().unwrap().clone() { - unsafe { - cb.call(record.level().into(), ptr); + if let Ok(cb) = DUMP_CALLBACK.lock() { + if let Some(cb) = cb.clone() { + unsafe { + cb.call(record.level().into(), ptr); + } } } + Ok(()) } } diff --git a/src/error.rs b/src/error.rs index 5cd937c..bd73d53 100644 --- a/src/error.rs +++ b/src/error.rs @@ -81,4 +81,6 @@ impl From<&String> for Error { } } +pub type BoxError = Box; + pub type Result = std::result::Result; diff --git a/src/lib.rs b/src/lib.rs index ecc8076..6f77e34 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,7 +20,7 @@ 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}; +pub use error::{BoxError, Error, Result}; use socks5_impl::protocol::{Address, StreamOperation}; pub use tokio_util::sync::CancellationToken;