From 8a9c6908dce547b56d2e04c36c5d4c9db3113674 Mon Sep 17 00:00:00 2001 From: ssrlive <30760636+ssrlive@users.noreply.github.com> Date: Thu, 8 Feb 2024 00:34:43 +0800 Subject: [PATCH] refactor FFI --- apple/overtls.xcodeproj/project.pbxproj | 2 +- apple/overtls/OverTlsWrapper.m | 12 ++++---- apple/readme.md | 2 +- ios-build.sh | 2 +- readme-cn.md | 21 -------------- src/api.rs | 11 ++++---- src/{main.rs => bin/overtls.rs} | 6 ++-- src/cmdopt.rs | 37 ++++++++++++++++++++++--- src/dump_logger.rs | 11 ++++---- src/lib.rs | 2 ++ 10 files changed, 59 insertions(+), 47 deletions(-) rename src/{main.rs => bin/overtls.rs} (95%) diff --git a/apple/overtls.xcodeproj/project.pbxproj b/apple/overtls.xcodeproj/project.pbxproj index 662676c..3f8a556 100644 --- a/apple/overtls.xcodeproj/project.pbxproj +++ b/apple/overtls.xcodeproj/project.pbxproj @@ -161,7 +161,7 @@ ); runOnlyForDeploymentPostprocessing = 0; shellPath = /bin/bash; - shellScript = "set -e\nPATH=\"$PATH:${HOME}/.cargo/bin\"\nRUST_PROJ=${PROJECT_DIR}/..\ncd \"${RUST_PROJ}\"\ncargo build --release --target aarch64-apple-ios\ncargo build --release --target x86_64-apple-ios\nlipo -create target/aarch64-apple-ios/release/libovertls.a target/x86_64-apple-ios/release/libovertls.a -output target/libovertls.a\ncbindgen --config cbindgen.toml -l C -o target/overtls-ios.h\n"; + shellScript = "set -e\nPATH=\"$PATH:${HOME}/.cargo/bin\"\nRUST_PROJ=${PROJECT_DIR}/..\ncd \"${RUST_PROJ}\"\ncargo build --release --target aarch64-apple-ios\ncargo build --release --target x86_64-apple-ios\nlipo -create target/aarch64-apple-ios/release/libovertls.a target/x86_64-apple-ios/release/libovertls.a -output target/libovertls.a\ncbindgen --config cbindgen.toml -l C -o target/overtls-ffi.h\n"; }; /* End PBXShellScriptBuildPhase section */ diff --git a/apple/overtls/OverTlsWrapper.m b/apple/overtls/OverTlsWrapper.m index 3226aa1..f37f3aa 100644 --- a/apple/overtls/OverTlsWrapper.m +++ b/apple/overtls/OverTlsWrapper.m @@ -8,16 +8,18 @@ #import #import "OverTlsWrapper.h" -#include "overtls-ios.h" +#include "overtls-ffi.h" @implementation OverTlsWrapper -+ (void) startWithConfig:(NSString*)filePath handler:(void (*)(int port, void *ctx))handler context:(void*)ctx { - over_tls_client_run(filePath.UTF8String, 1, handler, ctx); ++ (void)startWithConfig:(NSString *)filePath + handler:(void (*)(int port, void *ctx))handler + context:(void *)ctx { + over_tls_client_run(filePath.UTF8String, Info, handler, ctx); } -+ (void) shutdown { - over_tls_client_stop(); ++ (void)shutdown { + over_tls_client_stop(); } @end diff --git a/apple/readme.md b/apple/readme.md index 58db657..12391b9 100644 --- a/apple/readme.md +++ b/apple/readme.md @@ -15,5 +15,5 @@ cd overtls cargo build --release --target aarch64-apple-ios cargo build --release --target x86_64-apple-ios lipo -create target/aarch64-apple-ios/release/libovertls.a target/x86_64-apple-ios/release/libovertls.a -output target/libovertls.a -cbindgen --config cbindgen.toml -l C -o target/overtls-ios.h +cbindgen --config cbindgen.toml -l C -o target/overtls-ffi.h ``` diff --git a/ios-build.sh b/ios-build.sh index 967bffc..7c9915f 100755 --- a/ios-build.sh +++ b/ios-build.sh @@ -3,5 +3,5 @@ cargo build --release --target aarch64-apple-ios cargo build --release --target x86_64-apple-ios lipo -create target/aarch64-apple-ios/release/libovertls.a target/x86_64-apple-ios/release/libovertls.a -output target/libovertls.a -cbindgen --config cbindgen.toml -l C -o target/overtls-ios.h +cbindgen --config cbindgen.toml -l C -o target/overtls-ffi.h diff --git a/readme-cn.md b/readme-cn.md index 75514a6..6090ca4 100644 --- a/readme-cn.md +++ b/readme-cn.md @@ -111,24 +111,3 @@ overtls -r client -c config.json > 爲方便測試,提供了 `disable_tls` 選項以具備停用 `TLS` 的能力;就是說,若該項存在且爲 `true` 時,本軟件將 `明文(plain text)` 傳輸流量;出於安全考慮,正式場合請勿使用。 本示例展示的是最少條目的配置文件,完整的配置文件可以參考 [config.json](config.json)。 - - -## 构建 iOS framework - -### 安装 **Rust** 构建工具 -- 安装 Xcode 命令行工具: `xcode-select --install` -- 安装 Rust 编程语言: `curl https://sh.rustup.rs -sSf | sh` -- 安装 iOS 编译目标支持: `rustup target add aarch64-apple-ios aarch64-apple-ios-sim x86_64-apple-ios` -- 安装 `cbindgen` 工具: `cargo install cbindgen` - -### 构建 iOS framework -由于目前某种未知的原因, 从 Xcode 这个 IDE 环境里编译 Rust 代码会失败, 因此你得手工从命令行编译它. -请在 zsh (或 bash) 终端运行如下这些命令完成编译: -``` -cd overtls - -cargo build --release --target aarch64-apple-ios -cargo build --release --target x86_64-apple-ios -lipo -create target/aarch64-apple-ios/release/libovertls.a target/x86_64-apple-ios/release/libovertls.a -output target/libovertls.a -cbindgen --config cbindgen.toml -l C -o target/overtls-ios.h -``` diff --git a/src/api.rs b/src/api.rs index bcd52bc..7dccac3 100644 --- a/src/api.rs +++ b/src/api.rs @@ -1,6 +1,9 @@ #![cfg(not(target_os = "android"))] -use crate::error::{Error, Result}; +use crate::{ + error::{Error, Result}, + ArgVerbosity, +}; use std::{ net::{Ipv4Addr, Ipv6Addr, SocketAddr}, os::raw::{c_char, c_int, c_void}, @@ -35,13 +38,11 @@ lazy_static::lazy_static! { #[no_mangle] pub unsafe extern "C" fn over_tls_client_run( config_path: *const c_char, - verbose: c_char, + verbosity: ArgVerbosity, callback: Option, ctx: *mut c_void, ) -> c_int { - use log::LevelFilter; - let log_level = if verbose != 0 { LevelFilter::Trace } else { LevelFilter::Info }; - log::set_max_level(log_level); + log::set_max_level(verbosity.into()); log::set_boxed_logger(Box::::default()).unwrap(); _over_tls_client_run(config_path, callback, ctx) diff --git a/src/main.rs b/src/bin/overtls.rs similarity index 95% rename from src/main.rs rename to src/bin/overtls.rs index de263dc..b74a6d1 100644 --- a/src/main.rs +++ b/src/bin/overtls.rs @@ -1,13 +1,11 @@ -use overtls::{client, config, server, Error, Result}; +use overtls::{client, config, server, CmdOpt, Error, Result}; use std::{ net::{Ipv4Addr, Ipv6Addr, SocketAddr}, sync::{atomic::AtomicBool, Arc}, }; -mod cmdopt; - fn main() -> Result<()> { - let opt = cmdopt::CmdOpt::parse_cmd(); + let opt = CmdOpt::parse_cmd(); dotenvy::dotenv().ok(); diff --git a/src/cmdopt.rs b/src/cmdopt.rs index d3e07a1..d32bcc8 100644 --- a/src/cmdopt.rs +++ b/src/cmdopt.rs @@ -1,19 +1,48 @@ -#[derive(clap::ValueEnum, Debug, Clone, Copy, PartialEq, Eq, Hash)] +#[repr(C)] +#[derive(clap::ValueEnum, Default, Debug, Clone, Copy, PartialEq, Eq, Hash)] pub enum Role { - Server, + Server = 0, + #[default] Client, } -#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, clap::ValueEnum)] +#[repr(C)] +#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, clap::ValueEnum)] pub enum ArgVerbosity { - Off, + Off = 0, Error, Warn, + #[default] Info, Debug, Trace, } +impl From for log::LevelFilter { + fn from(verbosity: ArgVerbosity) -> Self { + match verbosity { + ArgVerbosity::Off => log::LevelFilter::Off, + ArgVerbosity::Error => log::LevelFilter::Error, + ArgVerbosity::Warn => log::LevelFilter::Warn, + ArgVerbosity::Info => log::LevelFilter::Info, + ArgVerbosity::Debug => log::LevelFilter::Debug, + ArgVerbosity::Trace => log::LevelFilter::Trace, + } + } +} + +impl From for ArgVerbosity { + fn from(level: log::Level) -> Self { + match level { + log::Level::Error => ArgVerbosity::Error, + log::Level::Warn => ArgVerbosity::Warn, + log::Level::Info => ArgVerbosity::Info, + log::Level::Debug => ArgVerbosity::Debug, + log::Level::Trace => ArgVerbosity::Trace, + } + } +} + /// Proxy tunnel over tls #[derive(clap::Parser, Debug, Clone, PartialEq, Eq)] #[command(author, version, about = "Proxy tunnel over tls.", long_about = None)] diff --git a/src/dump_logger.rs b/src/dump_logger.rs index 46cf3c5..ec1500c 100644 --- a/src/dump_logger.rs +++ b/src/dump_logger.rs @@ -1,5 +1,6 @@ +use crate::ArgVerbosity; use std::{ - os::raw::{c_char, c_int, c_void}, + os::raw::{c_char, c_void}, sync::Mutex, }; @@ -12,17 +13,17 @@ lazy_static::lazy_static! { /// set dump log info callback. #[no_mangle] pub unsafe extern "C" fn overtls_set_log_callback( - callback: Option, + callback: Option, ctx: *mut c_void, ) { *DUMP_CALLBACK.lock().unwrap() = Some(DumpCallback(callback, ctx)); } #[derive(Clone)] -pub struct DumpCallback(Option, *mut c_void); +pub struct DumpCallback(Option, *mut c_void); impl DumpCallback { - unsafe fn call(self, dump_level: c_int, info: *const c_char) { + unsafe fn call(self, dump_level: ArgVerbosity, info: *const c_char) { if let Some(cb) = self.0 { cb(dump_level, info, self.1); } @@ -66,7 +67,7 @@ impl DumpLogger { let ptr = c_msg.as_ptr(); if let Some(cb) = DUMP_CALLBACK.lock().unwrap().clone() { unsafe { - cb.call(record.level() as c_int, ptr); + cb.call(record.level().into(), ptr); } } } diff --git a/src/lib.rs b/src/lib.rs index 0dbe79e..bc1aa28 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,7 @@ pub(crate) mod android; pub(crate) mod api; pub(crate) mod base64_wrapper; pub mod client; +pub(crate) mod cmdopt; pub mod config; pub(crate) mod dns; pub mod dump_logger; @@ -16,6 +17,7 @@ pub(crate) mod weirduri; use base64_wrapper::{base64_decode, base64_encode, Base64Engine}; use bytes::BytesMut; +pub use cmdopt::{ArgVerbosity, CmdOpt, Role}; pub use error::{Error, Result}; use socks5_impl::protocol::{Address, StreamOperation};