Skip to content

Commit

Permalink
refine code
Browse files Browse the repository at this point in the history
  • Loading branch information
ssrlive committed Feb 18, 2024
1 parent 6de34d9 commit a01ccdd
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 14 deletions.
1 change: 1 addition & 0 deletions apple/overtls/OverTlsWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 6 additions & 1 deletion apple/overtls/OverTlsWrapper.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 1 addition & 2 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,12 @@ impl Config {
}

pub fn listen_addr(&self) -> Result<SocketAddr> {
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())
}
}
Expand Down
25 changes: 17 additions & 8 deletions src/dump_logger.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::ArgVerbosity;
use crate::{ArgVerbosity, BoxError};
use std::{
os::raw::{c_char, c_void},
sync::Mutex,
Expand All @@ -16,7 +16,11 @@ pub unsafe extern "C" fn overtls_set_log_callback(
callback: Option<unsafe extern "C" fn(ArgVerbosity, *const c_char, *mut c_void)>,
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)]
Expand Down Expand Up @@ -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);
}
}
}
}
Expand All @@ -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> = chrono::Local::now();
let msg = format!(
"[{} {:<5} {}] - {}",
Expand All @@ -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(())
}
}
2 changes: 2 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,6 @@ impl From<&String> for Error {
}
}

pub type BoxError = Box<dyn std::error::Error + Send + Sync + 'static>;

pub type Result<T, E = Error> = std::result::Result<T, E>;
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit a01ccdd

Please sign in to comment.