tun2proxy/src/apple.rs

49 lines
1.5 KiB
Rust
Raw Normal View History

2024-02-29 07:08:44 +03:30
#![cfg(any(target_os = "ios", target_os = "macos"))]
2024-02-01 19:15:32 +08:00
use crate::{
args::{ArgDns, ArgProxy},
ArgVerbosity, Args,
};
use std::os::raw::{c_char, c_int, c_ushort};
2024-02-01 19:15:32 +08:00
/// # Safety
///
/// Run the tun2proxy component with some arguments.
2024-02-29 07:08:44 +03:30
/// Parameters:
/// - proxy_url: the proxy url, e.g. "socks5://127.0.0.1:1080"
2024-03-30 12:30:01 +08:00
/// - tun_fd: the tun file descriptor, it will be owned by tun2proxy
2024-03-28 17:03:36 +08:00
/// - packet_information: whether exists packet information in tun_fd
2024-02-29 07:08:44 +03:30
/// - tun_mtu: the tun mtu
/// - dns_strategy: the dns strategy, see ArgDns enum
/// - verbosity: the verbosity level, see ArgVerbosity enum
2024-02-01 19:15:32 +08:00
#[no_mangle]
2024-02-29 07:08:44 +03:30
pub unsafe extern "C" fn tun2proxy_with_fd_run(
2024-02-01 19:15:32 +08:00
proxy_url: *const c_char,
tun_fd: c_int,
2024-03-28 17:03:36 +08:00
packet_information: bool,
tun_mtu: c_ushort,
2024-02-07 23:32:51 +08:00
dns_strategy: ArgDns,
verbosity: ArgVerbosity,
2024-02-01 19:15:32 +08:00
) -> c_int {
2024-02-07 23:32:51 +08:00
log::set_max_level(verbosity.into());
2024-03-30 12:30:01 +08:00
if let Err(err) = log::set_boxed_logger(Box::<crate::dump_logger::DumpLogger>::default()) {
log::error!("failed to set logger: {:?}", err);
}
2024-02-01 19:15:32 +08:00
let proxy_url = std::ffi::CStr::from_ptr(proxy_url).to_str().unwrap();
let proxy = ArgProxy::from_url(proxy_url).unwrap();
2024-02-12 21:36:18 +08:00
let mut args = Args::default();
args.proxy(proxy).tun_fd(Some(tun_fd)).dns(dns_strategy).verbosity(verbosity);
2024-02-01 19:15:32 +08:00
2024-03-28 17:03:36 +08:00
crate::mobile_api::mobile_run(args, tun_mtu, packet_information)
2024-02-01 19:15:32 +08:00
}
/// # Safety
///
/// Shutdown the tun2proxy component.
#[no_mangle]
2024-02-29 07:08:44 +03:30
pub unsafe extern "C" fn tun2proxy_with_fd_stop() -> c_int {
2024-02-13 10:46:13 +08:00
crate::mobile_api::mobile_stop()
2024-02-01 19:15:32 +08:00
}