Skip to content

Commit

Permalink
Bump version 0.2.20
Browse files Browse the repository at this point in the history
  • Loading branch information
ssrlive committed Apr 3, 2024
1 parent 618031f commit 0c23da1
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 23 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "overtls"
version = "0.2.19"
version = "0.2.20"
edition = "2021"
license = "MIT"
description = "A simple proxy tunnel, minimalist tool for bypassing the GFW."
Expand Down Expand Up @@ -40,6 +40,7 @@ rustls-pemfile = "2.1"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
socks5-impl = "0.5"
socket2 = "0.5"
thiserror = "1.0"
tokio = { version = "1", features = ["full"] }
tokio-rustls = { version = "0.26", default-features = false, features = [
Expand Down
12 changes: 8 additions & 4 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ pub unsafe extern "C" fn over_tls_client_run(
ctx: *mut c_void,
) -> c_int {
log::set_max_level(verbosity.into());
if let Err(err) = log::set_boxed_logger(Box::<crate::dump_logger::DumpLogger>::default()) {
log::info!("failed to set logger, error={:?}", err);
if !crate::dump_logger::check_logger() {
if let Err(err) = log::set_boxed_logger(Box::<crate::dump_logger::DumpLogger>::default()) {
log::warn!("failed to set logger, error={:?}", err);
}
}
let config_path = std::ffi::CStr::from_ptr(config_path).to_str();
if let Err(err) = config_path {
Expand Down Expand Up @@ -82,8 +84,10 @@ pub unsafe extern "C" fn over_tls_client_run_with_ssr_url(
ctx: *mut c_void,
) -> c_int {
log::set_max_level(verbosity.into());
if let Err(err) = log::set_boxed_logger(Box::<crate::dump_logger::DumpLogger>::default()) {
log::info!("failed to set logger, error={:?}", err);
if !crate::dump_logger::check_logger() {
if let Err(err) = log::set_boxed_logger(Box::<crate::dump_logger::DumpLogger>::default()) {
log::warn!("failed to set logger, error={:?}", err);
}
}
let url = std::ffi::CStr::from_ptr(url).to_str();
if let Err(err) = url {
Expand Down
2 changes: 1 addition & 1 deletion src/bin/overtls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn main() -> Result<()> {
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 { overtls::overtls_set_log_callback(true, 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);
Expand Down
9 changes: 6 additions & 3 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@ where
F: FnOnce(SocketAddr) + Send + Sync + 'static,
{
log::info!("starting {} {} client...", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"));
log::trace!("with following settings:");
log::trace!("{}", serde_json::to_string_pretty(config)?);
#[cfg(not(target_os = "ios"))]
{
log::trace!("with following settings:");
log::trace!("{}", serde_json::to_string_pretty(config)?);
}

let client = config.client.as_ref().ok_or("client")?;

Expand Down Expand Up @@ -232,7 +235,7 @@ pub(crate) async fn create_plaintext_ws_stream(
config: &Config,
udp_tunnel: Option<bool>,
) -> Result<WebSocketStream<TcpStream>> {
let stream = crate::tcp_stream::create(server_addr).await?;
let stream = crate::tcp_stream::tokio_create(server_addr).await?;
let ws_stream = create_ws_stream(dst_addr, config, udp_tunnel, stream).await?;
Ok(ws_stream)
}
Expand Down
3 changes: 1 addition & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,10 +303,9 @@ impl Config {
if !self.is_server {
let mut addr = (server_host, client.server_port).to_socket_addrs()?;
let addr = addr.next().ok_or("address not available")?;
#[cfg(not(target_os = "android"))]
{
let timeout = std::time::Duration::from_secs(self.test_timeout_secs);
std::net::TcpStream::connect_timeout(&addr, timeout)?;
crate::tcp_stream::std_create(addr, Some(timeout))?;
}
if client.listen_host.is_empty() {
client.listen_host = if addr.is_ipv4() {
Expand Down
34 changes: 31 additions & 3 deletions src/dump_logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,29 @@ use std::{
};

static DUMP_CALLBACK: Mutex<Option<DumpCallback>> = Mutex::new(None);
static LOGGER_SETTED: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);

pub(crate) fn check_logger() -> bool {
LOGGER_SETTED.load(std::sync::atomic::Ordering::SeqCst)
}

/// # Safety
///
/// set dump log info callback.
#[no_mangle]
pub unsafe extern "C" fn overtls_set_log_callback(
set_logger: bool,
callback: Option<unsafe extern "C" fn(ArgVerbosity, *const c_char, *mut c_void)>,
ctx: *mut c_void,
) {
if set_logger {
LOGGER_SETTED.store(true, std::sync::atomic::Ordering::Relaxed);
log::set_max_level(log::LevelFilter::Trace);
if let Err(err) = log::set_boxed_logger(Box::<DumpLogger>::default()) {
log::warn!("failed to set logger, error={:?}", err);
}
}

if let Ok(mut cb) = DUMP_CALLBACK.lock() {
*cb = Some(DumpCallback(callback, ctx));
} else {
Expand Down Expand Up @@ -44,29 +58,43 @@ impl log::Log for DumpLogger {
}

fn log(&self, record: &log::Record) {
#[cfg(not(target_os = "ios"))]
if self.enabled(record.metadata()) {
let current_crate_name = env!("CARGO_CRATE_NAME");
if record.module_path().unwrap_or("").starts_with(current_crate_name) {
if let Err(err) = self.do_dump_log(record) {
log::error!("failed to dump log, error={:?}", err);
eprint!("failed to dump log, error={:?}", err);
}
}
}
#[cfg(target_os = "ios")]
if self.enabled(record.metadata()) {
let module = record.module_path().unwrap_or("");
if module.starts_with("rustls") || module.starts_with("tungstenite") || module.starts_with("tokio_tungstenite") {
return;
}
if let Err(err) = self.do_dump_log(record) {
eprint!("failed to dump log, error={:?}", err);
}
}
}

fn flush(&self) {}
}

impl DumpLogger {
fn do_dump_log(&self, record: &log::Record) -> Result<(), BoxError> {
let timestamp: chrono::DateTime<chrono::Local> = chrono::Local::now();
let _timestamp: chrono::DateTime<chrono::Local> = chrono::Local::now();
#[cfg(not(target_os = "ios"))]
let msg = format!(
"[{} {:<5} {}] - {}",
timestamp.format("%Y-%m-%d %H:%M:%S"),
_timestamp.format("%Y-%m-%d %H:%M:%S"),
record.level(),
record.module_path().unwrap_or(""),
record.args()
);
#[cfg(target_os = "ios")]
let msg = format!("[{:<5} {}] - {}", record.level(), record.module_path().unwrap_or(""), record.args());
let c_msg = std::ffi::CString::new(msg)?;
let ptr = c_msg.as_ptr();
if let Ok(cb) = DUMP_CALLBACK.lock() {
Expand Down
4 changes: 2 additions & 2 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ where
let to_stream = create_tls_client_stream(cert_store, forward_addr, host).await?;
forward_traffic(stream, to_stream, data).await
} else {
let to_stream = crate::tcp_stream::create(forward_addr).await?;
let to_stream = crate::tcp_stream::tokio_create(forward_addr).await?;
forward_traffic(stream, to_stream, data).await
}
}
Expand Down Expand Up @@ -319,7 +319,7 @@ async fn normal_tunnel<S: AsyncRead + AsyncWrite + Unpin>(
client_id: &Option<String>,
dst_addr: SocketAddr,
) -> Result<()> {
let mut outgoing = crate::tcp_stream::create(dst_addr).await?;
let mut outgoing = crate::tcp_stream::tokio_create(dst_addr).await?;
let mut buffer = [0; crate::STREAM_BUFFER_SIZE];
loop {
tokio::select! {
Expand Down
38 changes: 32 additions & 6 deletions src/tcp_stream.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
use crate::error::Result;
use std::net::SocketAddr;
use tokio::net::TcpStream;

pub(crate) async fn create(addr: SocketAddr) -> Result<TcpStream> {
pub(crate) async fn tokio_create(addr: std::net::SocketAddr) -> std::io::Result<tokio::net::TcpStream> {
#[cfg(target_os = "android")]
{
let socket = if addr.is_ipv4() {
Expand All @@ -20,5 +16,35 @@ pub(crate) async fn create(addr: SocketAddr) -> Result<TcpStream> {
}

#[cfg(not(target_os = "android"))]
Ok(TcpStream::connect(addr).await?)
tokio::net::TcpStream::connect(addr).await
}

pub(crate) fn std_create(addr: std::net::SocketAddr, timeout: Option<std::time::Duration>) -> std::io::Result<std::net::TcpStream> {
use socket2::{Domain, SockAddr, Socket, Type};
let domain = if addr.is_ipv4() { Domain::IPV4 } else { Domain::IPV6 };
let socket = Socket::new(domain, Type::STREAM, None)?;

#[cfg(target_os = "android")]
{
use std::os::unix::io::AsRawFd;
crate::android::tun_callbacks::on_socket_created(socket.as_raw_fd());
}

if let Some(timeout) = timeout {
socket.connect_timeout(&SockAddr::from(addr), timeout)?;
} else {
socket.connect(&SockAddr::from(addr))?;
}

#[cfg(unix)]
{
use std::os::unix::io::{FromRawFd, IntoRawFd};
Ok(unsafe { std::net::TcpStream::from_raw_fd(socket.into_raw_fd()) })
}

#[cfg(windows)]
{
use std::os::windows::io::{FromRawSocket, IntoRawSocket};
Ok(unsafe { std::net::TcpStream::from_raw_socket(socket.into_raw_socket()) })
}
}
2 changes: 1 addition & 1 deletion src/tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub(crate) async fn create_tls_client_stream(
.with_no_client_auth();
let connector = TlsConnector::from(std::sync::Arc::new(config));

let stream = crate::tcp_stream::create(addr).await?;
let stream = crate::tcp_stream::tokio_create(addr).await?;

let domain = ServerName::try_from(domain)?.to_owned();

Expand Down

0 comments on commit 0c23da1

Please sign in to comment.