mirror of
https://github.com/tun2proxy/tun2proxy.git
synced 2025-04-19 21:39:09 +00:00
XCFramework build for apple (#93)
This commit is contained in:
parent
01a0d9164d
commit
4ab6f1a9bc
8 changed files with 85 additions and 15 deletions
|
@ -25,6 +25,12 @@ Clone the repository and `cd` into the project folder. Then run the following:
|
||||||
cargo build --release
|
cargo build --release
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Building Framework for Apple Devices
|
||||||
|
To build an XCFramework for macOS and iOS, run the following:
|
||||||
|
```
|
||||||
|
./build-apple.sh
|
||||||
|
```
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
### Install from binary
|
### Install from binary
|
||||||
|
|
|
@ -19,11 +19,11 @@
|
||||||
verbose:(bool)verbose {
|
verbose:(bool)verbose {
|
||||||
ArgDns dns_strategy = dns_over_tcp ? OverTcp : Direct;
|
ArgDns dns_strategy = dns_over_tcp ? OverTcp : Direct;
|
||||||
ArgVerbosity v = verbose ? Trace : Info;
|
ArgVerbosity v = verbose ? Trace : Info;
|
||||||
tun2proxy_run_with_fd(proxy_url.UTF8String, tun_fd, tun_mtu, dns_strategy, v);
|
tun2proxy_with_fd_run(proxy_url.UTF8String, tun_fd, tun_mtu, dns_strategy, v);
|
||||||
}
|
}
|
||||||
|
|
||||||
+ (void)shutdown {
|
+ (void)shutdown {
|
||||||
tun2proxy_stop();
|
tun2proxy_with_fd_stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
44
build-apple.sh
Executable file
44
build-apple.sh
Executable file
|
@ -0,0 +1,44 @@
|
||||||
|
#! /bin/sh
|
||||||
|
|
||||||
|
echo "Setting up the rust environment..."
|
||||||
|
rustup target add aarch64-apple-ios aarch64-apple-ios-sim x86_64-apple-ios x86_64-apple-darwin aarch64-apple-darwin
|
||||||
|
cargo install cbindgen
|
||||||
|
|
||||||
|
echo "Building..."
|
||||||
|
cargo build --release --target x86_64-apple-darwin
|
||||||
|
cargo build --release --target aarch64-apple-darwin
|
||||||
|
cargo build --release --target aarch64-apple-ios
|
||||||
|
cargo build --release --target x86_64-apple-ios
|
||||||
|
cargo build --release --target aarch64-apple-ios-sim
|
||||||
|
|
||||||
|
echo "Generating includes..."
|
||||||
|
mkdir -p target/include/
|
||||||
|
cbindgen --config cbindgen.toml -l C -o target/include/tun2proxy.h
|
||||||
|
cat > target/include/module.modulemap <<EOF
|
||||||
|
framework module Tun2Proxy {
|
||||||
|
umbrella header "tun2proxy.h"
|
||||||
|
|
||||||
|
export *
|
||||||
|
module * { export * }
|
||||||
|
}
|
||||||
|
EOF
|
||||||
|
|
||||||
|
echo "lipo..."
|
||||||
|
echo "Simulator"
|
||||||
|
lipo -create \
|
||||||
|
target/aarch64-apple-ios-sim/release/libtun2proxy.a \
|
||||||
|
target/x86_64-apple-ios/release/libtun2proxy.a \
|
||||||
|
-output ./target/libtun2proxy-ios-sim.a
|
||||||
|
echo "MacOS"
|
||||||
|
lipo -create \
|
||||||
|
target/aarch64-apple-darwin/release/libtun2proxy.a \
|
||||||
|
target/x86_64-apple-darwin/release/libtun2proxy.a \
|
||||||
|
-output ./target/libtun2proxy-macos.a
|
||||||
|
|
||||||
|
echo "Creating XCFramework"
|
||||||
|
rm -rf ./target/Tun2Proxy.xcframework
|
||||||
|
xcodebuild -create-xcframework \
|
||||||
|
-library ./target/aarch64-apple-ios/release/libtun2proxy.a -headers ./target/include/ \
|
||||||
|
-library ./target/libtun2proxy-ios-sim.a -headers ./target/include/ \
|
||||||
|
-library ./target/libtun2proxy-macos.a -headers ./target/include/ \
|
||||||
|
-output ./target/Tun2Proxy.xcframework
|
|
@ -1,8 +1,9 @@
|
||||||
[export]
|
[export]
|
||||||
include = [
|
include = [
|
||||||
"tun2proxy_run_with_fd",
|
"tun2proxy_with_fd_run",
|
||||||
"tun2proxy_run_with_name",
|
"tun2proxy_with_name_run",
|
||||||
"tun2proxy_stop",
|
"tun2proxy_with_name_stop",
|
||||||
|
"tun2proxy_with_fd_stop",
|
||||||
"tun2proxy_set_log_callback",
|
"tun2proxy_set_log_callback",
|
||||||
]
|
]
|
||||||
exclude = [
|
exclude = [
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
#![cfg(target_os = "ios")]
|
#![cfg(any(target_os = "ios", target_os = "macos"))]
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
args::{ArgDns, ArgProxy},
|
args::{ArgDns, ArgProxy},
|
||||||
|
@ -9,8 +9,14 @@ use std::os::raw::{c_char, c_int, c_ushort};
|
||||||
/// # Safety
|
/// # Safety
|
||||||
///
|
///
|
||||||
/// Run the tun2proxy component with some arguments.
|
/// Run the tun2proxy component with some arguments.
|
||||||
|
/// Parameters:
|
||||||
|
/// - proxy_url: the proxy url, e.g. "socks5://127.0.0.1:1080"
|
||||||
|
/// - tun_fd: the tun file descriptor
|
||||||
|
/// - tun_mtu: the tun mtu
|
||||||
|
/// - dns_strategy: the dns strategy, see ArgDns enum
|
||||||
|
/// - verbosity: the verbosity level, see ArgVerbosity enum
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub unsafe extern "C" fn tun2proxy_run_with_fd(
|
pub unsafe extern "C" fn tun2proxy_with_fd_run(
|
||||||
proxy_url: *const c_char,
|
proxy_url: *const c_char,
|
||||||
tun_fd: c_int,
|
tun_fd: c_int,
|
||||||
tun_mtu: c_ushort,
|
tun_mtu: c_ushort,
|
||||||
|
@ -33,6 +39,6 @@ pub unsafe extern "C" fn tun2proxy_run_with_fd(
|
||||||
///
|
///
|
||||||
/// Shutdown the tun2proxy component.
|
/// Shutdown the tun2proxy component.
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub unsafe extern "C" fn tun2proxy_stop() -> c_int {
|
pub unsafe extern "C" fn tun2proxy_with_fd_stop() -> c_int {
|
||||||
crate::mobile_api::mobile_stop()
|
crate::mobile_api::mobile_stop()
|
||||||
}
|
}
|
|
@ -13,8 +13,15 @@ static TUN_QUIT: std::sync::Mutex<Option<tokio_util::sync::CancellationToken>> =
|
||||||
/// # Safety
|
/// # Safety
|
||||||
///
|
///
|
||||||
/// Run the tun2proxy component with some arguments.
|
/// Run the tun2proxy component with some arguments.
|
||||||
|
/// Parameters:
|
||||||
|
/// - proxy_url: the proxy url, e.g. "socks5://127.0.0.1:1080"
|
||||||
|
/// - tun: the tun device name, e.g. "utun5"
|
||||||
|
/// - bypass: the bypass ip, e.g. "123.45.67.89"
|
||||||
|
/// - dns_strategy: the dns strategy, see ArgDns enum
|
||||||
|
/// - root_privilege: whether to run with root privilege
|
||||||
|
/// - verbosity: the verbosity level, see ArgVerbosity enum
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub unsafe extern "C" fn tun2proxy_run_with_name(
|
pub unsafe extern "C" fn tun2proxy_with_name_run(
|
||||||
proxy_url: *const c_char,
|
proxy_url: *const c_char,
|
||||||
tun: *const c_char,
|
tun: *const c_char,
|
||||||
bypass: *const c_char,
|
bypass: *const c_char,
|
||||||
|
@ -142,7 +149,7 @@ pub async fn desktop_run_async(args: Args, shutdown_token: tokio_util::sync::Can
|
||||||
///
|
///
|
||||||
/// Shutdown the tun2proxy component.
|
/// Shutdown the tun2proxy component.
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub unsafe extern "C" fn tun2proxy_stop() -> c_int {
|
pub unsafe extern "C" fn tun2proxy_with_name_stop() -> c_int {
|
||||||
if let Ok(lock) = TUN_QUIT.lock() {
|
if let Ok(lock) = TUN_QUIT.lock() {
|
||||||
if let Some(shutdown_token) = lock.as_ref() {
|
if let Some(shutdown_token) = lock.as_ref() {
|
||||||
shutdown_token.cancel();
|
shutdown_token.cancel();
|
||||||
|
|
|
@ -29,7 +29,11 @@ pub use desktop_api::desktop_run_async;
|
||||||
#[cfg(any(target_os = "ios", target_os = "android"))]
|
#[cfg(any(target_os = "ios", target_os = "android"))]
|
||||||
pub use mobile_api::{desktop_run_async, mobile_run, mobile_stop};
|
pub use mobile_api::{desktop_run_async, mobile_run, mobile_stop};
|
||||||
|
|
||||||
|
#[cfg(target_os = "macos")]
|
||||||
|
pub use mobile_api::{mobile_run, mobile_stop};
|
||||||
|
|
||||||
mod android;
|
mod android;
|
||||||
|
mod apple;
|
||||||
mod args;
|
mod args;
|
||||||
mod desktop_api;
|
mod desktop_api;
|
||||||
mod directions;
|
mod directions;
|
||||||
|
@ -37,7 +41,6 @@ mod dns;
|
||||||
mod dump_logger;
|
mod dump_logger;
|
||||||
mod error;
|
mod error;
|
||||||
mod http;
|
mod http;
|
||||||
mod ios;
|
|
||||||
mod mobile_api;
|
mod mobile_api;
|
||||||
mod proxy_handler;
|
mod proxy_handler;
|
||||||
mod session_info;
|
mod session_info;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
#![cfg(any(target_os = "ios", target_os = "android"))]
|
#![cfg(any(target_os = "ios", target_os = "android", target_os = "macos"))]
|
||||||
|
|
||||||
use crate::Args;
|
use crate::Args;
|
||||||
use std::os::raw::c_int;
|
use std::os::raw::c_int;
|
||||||
|
@ -7,6 +7,7 @@ static TUN_QUIT: std::sync::Mutex<Option<tokio_util::sync::CancellationToken>> =
|
||||||
|
|
||||||
/// Dummy function to make the build pass.
|
/// Dummy function to make the build pass.
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
|
#[cfg(not(target_os = "macos"))]
|
||||||
pub async fn desktop_run_async(_: Args, _: tokio_util::sync::CancellationToken) -> std::io::Result<()> {
|
pub async fn desktop_run_async(_: Args, _: tokio_util::sync::CancellationToken) -> std::io::Result<()> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -28,11 +29,13 @@ pub fn mobile_run(args: Args, tun_mtu: u16) -> c_int {
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
if let Some(fd) = args.tun_fd {
|
if let Some(fd) = args.tun_fd {
|
||||||
config.raw_fd(fd);
|
config.raw_fd(fd);
|
||||||
} else {
|
} else if let Some(ref tun) = args.tun {
|
||||||
config.name(&args.tun);
|
config.tun_name(tun);
|
||||||
}
|
}
|
||||||
#[cfg(windows)]
|
#[cfg(windows)]
|
||||||
config.name(&args.tun);
|
if let Some(ref tun) = args.tun {
|
||||||
|
config.tun_name(tun);
|
||||||
|
}
|
||||||
|
|
||||||
let device = tun2::create_as_async(&config).map_err(std::io::Error::from)?;
|
let device = tun2::create_as_async(&config).map_err(std::io::Error::from)?;
|
||||||
let join_handle = tokio::spawn(crate::run(device, tun_mtu, args, shutdown_token));
|
let join_handle = tokio::spawn(crate::run(device, tun_mtu, args, shutdown_token));
|
||||||
|
|
Loading…
Add table
Reference in a new issue