Skip to content

Commit

Permalink
refactor FFI
Browse files Browse the repository at this point in the history
  • Loading branch information
ssrlive committed Feb 7, 2024
1 parent 38dbd35 commit 8a9c690
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 47 deletions.
2 changes: 1 addition & 1 deletion apple/overtls.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand Down
12 changes: 7 additions & 5 deletions apple/overtls/OverTlsWrapper.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@
#import <Foundation/Foundation.h>

#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
2 changes: 1 addition & 1 deletion apple/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
2 changes: 1 addition & 1 deletion ios-build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

21 changes: 0 additions & 21 deletions readme-cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
11 changes: 6 additions & 5 deletions src/api.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand Down Expand Up @@ -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<unsafe extern "C" fn(c_int, *mut c_void)>,
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::<crate::dump_logger::DumpLogger>::default()).unwrap();

_over_tls_client_run(config_path, callback, ctx)
Expand Down
6 changes: 2 additions & 4 deletions src/main.rs → src/bin/overtls.rs
Original file line number Diff line number Diff line change
@@ -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();

Expand Down
37 changes: 33 additions & 4 deletions src/cmdopt.rs
Original file line number Diff line number Diff line change
@@ -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<ArgVerbosity> 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<log::Level> 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)]
Expand Down
11 changes: 6 additions & 5 deletions src/dump_logger.rs
Original file line number Diff line number Diff line change
@@ -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,
};

Expand All @@ -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<unsafe extern "C" fn(c_int, *const c_char, *mut c_void)>,
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));
}

#[derive(Clone)]
pub struct DumpCallback(Option<unsafe extern "C" fn(c_int, *const c_char, *mut c_void)>, *mut c_void);
pub 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: 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);
}
Expand Down Expand Up @@ -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);
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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};

Expand Down

0 comments on commit 8a9c690

Please sign in to comment.