mirror of
https://github.com/tun2proxy/tun2proxy.git
synced 2025-04-22 06:49:08 +00:00
rename desktop_run_async to general_run_async
This commit is contained in:
parent
e933e5d4c0
commit
6034870264
7 changed files with 37 additions and 126 deletions
|
@ -52,7 +52,7 @@ pub unsafe extern "C" fn Java_com_github_shadowsocks_bg_Tun2proxy_run(
|
||||||
.close_fd_on_drop(close_fd_on_drop)
|
.close_fd_on_drop(close_fd_on_drop)
|
||||||
.dns(dns)
|
.dns(dns)
|
||||||
.verbosity(verbosity);
|
.verbosity(verbosity);
|
||||||
crate::mobile_api::mobile_run(args, tun_mtu, false)
|
crate::general_api::general_run_for_api(args, tun_mtu, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// # Safety
|
/// # Safety
|
||||||
|
@ -60,7 +60,7 @@ pub unsafe extern "C" fn Java_com_github_shadowsocks_bg_Tun2proxy_run(
|
||||||
/// Shutdown tun2proxy
|
/// Shutdown tun2proxy
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub unsafe extern "C" fn Java_com_github_shadowsocks_bg_Tun2proxy_stop(_env: JNIEnv, _: JClass) -> jint {
|
pub unsafe extern "C" fn Java_com_github_shadowsocks_bg_Tun2proxy_stop(_env: JNIEnv, _: JClass) -> jint {
|
||||||
crate::mobile_api::mobile_stop()
|
crate::general_api::tun2proxy_stop_internal()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_java_string(env: &mut JNIEnv, string: &JString) -> Result<String, Error> {
|
fn get_java_string(env: &mut JNIEnv, string: &JString) -> Result<String, Error> {
|
||||||
|
|
|
@ -50,7 +50,7 @@ async fn main_async(args: Args) -> Result<(), BoxError> {
|
||||||
}
|
}
|
||||||
unsafe { tun2proxy::tun2proxy_set_traffic_status_callback(1, Some(traffic_cb), std::ptr::null_mut()) };
|
unsafe { tun2proxy::tun2proxy_set_traffic_status_callback(1, Some(traffic_cb), std::ptr::null_mut()) };
|
||||||
|
|
||||||
if let Err(err) = tun2proxy::desktop_run_async(args, MTU, false, shutdown_token).await {
|
if let Err(err) = tun2proxy::general_run_async(args, MTU, false, shutdown_token).await {
|
||||||
log::error!("main loop error: {}", err);
|
log::error!("main loop error: {}", err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,9 @@
|
||||||
#![cfg(any(target_os = "windows", target_os = "macos", target_os = "linux"))]
|
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
args::{ArgDns, ArgProxy},
|
args::{ArgDns, ArgProxy},
|
||||||
ArgVerbosity, Args,
|
ArgVerbosity, Args,
|
||||||
};
|
};
|
||||||
use std::os::raw::{c_char, c_int};
|
use std::os::raw::{c_char, c_int};
|
||||||
use tun::{AbstractDevice, DEFAULT_MTU as MTU};
|
use tun::DEFAULT_MTU as MTU;
|
||||||
|
|
||||||
static TUN_QUIT: std::sync::Mutex<Option<tokio_util::sync::CancellationToken>> = std::sync::Mutex::new(None);
|
static TUN_QUIT: std::sync::Mutex<Option<tokio_util::sync::CancellationToken>> = std::sync::Mutex::new(None);
|
||||||
|
|
||||||
|
@ -41,7 +39,7 @@ pub unsafe extern "C" fn tun2proxy_with_name_run(
|
||||||
#[cfg(target_os = "linux")]
|
#[cfg(target_os = "linux")]
|
||||||
args.setup(_root_privilege);
|
args.setup(_root_privilege);
|
||||||
|
|
||||||
desktop_run(args)
|
general_run_for_api(args, MTU, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// # Safety
|
/// # Safety
|
||||||
|
@ -55,45 +53,48 @@ pub unsafe extern "C" fn tun2proxy_run_with_cli_args(cli_args: *const c_char) ->
|
||||||
return -5;
|
return -5;
|
||||||
};
|
};
|
||||||
let args = <Args as ::clap::Parser>::parse_from(cli_args.split_whitespace());
|
let args = <Args as ::clap::Parser>::parse_from(cli_args.split_whitespace());
|
||||||
desktop_run(args)
|
general_run_for_api(args, MTU, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn desktop_run(args: Args) -> c_int {
|
pub fn general_run_for_api(args: Args, tun_mtu: u16, packet_information: bool) -> c_int {
|
||||||
log::set_max_level(args.verbosity.into());
|
log::set_max_level(args.verbosity.into());
|
||||||
if let Err(err) = log::set_boxed_logger(Box::<crate::dump_logger::DumpLogger>::default()) {
|
if let Err(err) = log::set_boxed_logger(Box::<crate::dump_logger::DumpLogger>::default()) {
|
||||||
log::warn!("set logger error: {}", err);
|
log::debug!("set logger error: {}", err);
|
||||||
}
|
}
|
||||||
|
|
||||||
let shutdown_token = tokio_util::sync::CancellationToken::new();
|
let shutdown_token = tokio_util::sync::CancellationToken::new();
|
||||||
{
|
|
||||||
if let Ok(mut lock) = TUN_QUIT.lock() {
|
if let Ok(mut lock) = TUN_QUIT.lock() {
|
||||||
if lock.is_some() {
|
if lock.is_some() {
|
||||||
|
log::error!("tun2proxy already started");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
*lock = Some(shutdown_token.clone());
|
*lock = Some(shutdown_token.clone());
|
||||||
} else {
|
} else {
|
||||||
|
log::error!("failed to lock tun2proxy quit token");
|
||||||
return -2;
|
return -2;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
let Ok(rt) = tokio::runtime::Builder::new_multi_thread().enable_all().build() else {
|
let Ok(rt) = tokio::runtime::Builder::new_multi_thread().enable_all().build() else {
|
||||||
|
log::error!("failed to create tokio runtime with");
|
||||||
return -3;
|
return -3;
|
||||||
};
|
};
|
||||||
let res = rt.block_on(async move {
|
match rt.block_on(async move {
|
||||||
if let Err(err) = desktop_run_async(args, MTU, false, shutdown_token).await {
|
if let Err(err) = general_run_async(args, tun_mtu, packet_information, shutdown_token).await {
|
||||||
log::error!("main loop error: {}", err);
|
log::error!("main loop error: {}", err);
|
||||||
return Err(err);
|
return Err(err);
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
});
|
}) {
|
||||||
match res {
|
|
||||||
Ok(_) => 0,
|
Ok(_) => 0,
|
||||||
Err(_) => -4,
|
Err(e) => {
|
||||||
|
log::error!("failed to run tun2proxy with error: {:?}", e);
|
||||||
|
-4
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Run the tun2proxy component with some arguments.
|
/// Run the tun2proxy component with some arguments.
|
||||||
pub async fn desktop_run_async(
|
pub async fn general_run_async(
|
||||||
args: Args,
|
args: Args,
|
||||||
tun_mtu: u16,
|
tun_mtu: u16,
|
||||||
_packet_information: bool,
|
_packet_information: bool,
|
||||||
|
@ -153,7 +154,8 @@ pub async fn desktop_run_async(
|
||||||
let device = tun::create_as_async(&tun_config)?;
|
let device = tun::create_as_async(&tun_config)?;
|
||||||
|
|
||||||
#[cfg(any(target_os = "linux", target_os = "windows", target_os = "macos"))]
|
#[cfg(any(target_os = "linux", target_os = "windows", target_os = "macos"))]
|
||||||
if let Ok(tun_name) = device.tun_name() {
|
if let Ok(tun_name) = tun::AbstractDevice::tun_name(&*device) {
|
||||||
|
// Above line is equivalent to: `use tun::AbstractDevice; if let Ok(tun_name) = device.tun_name() {`
|
||||||
tproxy_args = tproxy_args.tun_name(&tun_name);
|
tproxy_args = tproxy_args.tun_name(&tun_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -205,6 +207,10 @@ pub async fn desktop_run_async(
|
||||||
/// Shutdown the tun2proxy component.
|
/// Shutdown the tun2proxy component.
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub unsafe extern "C" fn tun2proxy_with_name_stop() -> c_int {
|
pub unsafe extern "C" fn tun2proxy_with_name_stop() -> c_int {
|
||||||
|
tun2proxy_stop_internal()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn tun2proxy_stop_internal() -> c_int {
|
||||||
if let Ok(mut lock) = TUN_QUIT.lock() {
|
if let Ok(mut lock) = TUN_QUIT.lock() {
|
||||||
if let Some(shutdown_token) = lock.take() {
|
if let Some(shutdown_token) = lock.take() {
|
||||||
shutdown_token.cancel();
|
shutdown_token.cancel();
|
14
src/lib.rs
14
src/lib.rs
|
@ -40,25 +40,17 @@ pub use {
|
||||||
#[global_allocator]
|
#[global_allocator]
|
||||||
static ALLOC: mimalloc::MiMalloc = mimalloc::MiMalloc;
|
static ALLOC: mimalloc::MiMalloc = mimalloc::MiMalloc;
|
||||||
|
|
||||||
#[cfg(any(target_os = "windows", target_os = "macos", target_os = "linux"))]
|
pub use general_api::general_run_async;
|
||||||
pub use desktop_api::desktop_run_async;
|
|
||||||
|
|
||||||
#[cfg(any(target_os = "ios", target_os = "android"))]
|
|
||||||
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 directions;
|
mod directions;
|
||||||
mod dns;
|
mod dns;
|
||||||
mod dump_logger;
|
mod dump_logger;
|
||||||
mod error;
|
mod error;
|
||||||
|
mod general_api;
|
||||||
mod http;
|
mod http;
|
||||||
mod mobile_api;
|
mod mobile;
|
||||||
mod no_proxy;
|
mod no_proxy;
|
||||||
mod proxy_handler;
|
mod proxy_handler;
|
||||||
mod session_info;
|
mod session_info;
|
||||||
|
|
|
@ -27,11 +27,6 @@ pub unsafe extern "C" fn tun2proxy_with_fd_run(
|
||||||
dns_strategy: ArgDns,
|
dns_strategy: ArgDns,
|
||||||
verbosity: ArgVerbosity,
|
verbosity: ArgVerbosity,
|
||||||
) -> c_int {
|
) -> c_int {
|
||||||
log::set_max_level(verbosity.into());
|
|
||||||
if let Err(err) = log::set_boxed_logger(Box::<crate::dump_logger::DumpLogger>::default()) {
|
|
||||||
log::warn!("failed to set logger: {:?}", err);
|
|
||||||
}
|
|
||||||
|
|
||||||
let proxy_url = std::ffi::CStr::from_ptr(proxy_url).to_str().unwrap();
|
let proxy_url = std::ffi::CStr::from_ptr(proxy_url).to_str().unwrap();
|
||||||
let proxy = ArgProxy::try_from(proxy_url).unwrap();
|
let proxy = ArgProxy::try_from(proxy_url).unwrap();
|
||||||
|
|
||||||
|
@ -42,7 +37,7 @@ pub unsafe extern "C" fn tun2proxy_with_fd_run(
|
||||||
.dns(dns_strategy)
|
.dns(dns_strategy)
|
||||||
.verbosity(verbosity);
|
.verbosity(verbosity);
|
||||||
|
|
||||||
crate::mobile_api::mobile_run(args, tun_mtu, packet_information)
|
crate::general_api::general_run_for_api(args, tun_mtu, packet_information)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// # Safety
|
/// # Safety
|
||||||
|
@ -50,5 +45,5 @@ pub unsafe extern "C" fn tun2proxy_with_fd_run(
|
||||||
/// Shutdown the tun2proxy component.
|
/// Shutdown the tun2proxy component.
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub unsafe extern "C" fn tun2proxy_with_fd_stop() -> c_int {
|
pub unsafe extern "C" fn tun2proxy_with_fd_stop() -> c_int {
|
||||||
crate::mobile_api::mobile_stop()
|
crate::general_api::tun2proxy_stop_internal()
|
||||||
}
|
}
|
|
@ -1,82 +0,0 @@
|
||||||
#![cfg(any(target_os = "ios", target_os = "android", target_os = "macos"))]
|
|
||||||
|
|
||||||
use crate::Args;
|
|
||||||
use std::os::raw::c_int;
|
|
||||||
|
|
||||||
static TUN_QUIT: std::sync::Mutex<Option<tokio_util::sync::CancellationToken>> = std::sync::Mutex::new(None);
|
|
||||||
|
|
||||||
/// Dummy function to make the build pass.
|
|
||||||
#[doc(hidden)]
|
|
||||||
#[cfg(not(target_os = "macos"))]
|
|
||||||
pub async fn desktop_run_async(_: Args, _: u16, _: bool, _: tokio_util::sync::CancellationToken) -> std::io::Result<()> {
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn mobile_run_async(
|
|
||||||
args: Args,
|
|
||||||
tun_mtu: u16,
|
|
||||||
_packet_information: bool,
|
|
||||||
shutdown_token: tokio_util::sync::CancellationToken,
|
|
||||||
) -> std::io::Result<()> {
|
|
||||||
let mut config = tun::Configuration::default();
|
|
||||||
|
|
||||||
#[cfg(unix)]
|
|
||||||
if let Some(fd) = args.tun_fd {
|
|
||||||
config.raw_fd(fd);
|
|
||||||
if let Some(v) = args.close_fd_on_drop {
|
|
||||||
config.close_fd_on_drop(v);
|
|
||||||
};
|
|
||||||
} else if let Some(ref tun) = args.tun {
|
|
||||||
config.tun_name(tun);
|
|
||||||
}
|
|
||||||
#[cfg(windows)]
|
|
||||||
if let Some(ref tun) = args.tun {
|
|
||||||
config.tun_name(tun);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(any(target_os = "ios", target_os = "macos"))]
|
|
||||||
config.platform_config(|config| {
|
|
||||||
config.packet_information(_packet_information);
|
|
||||||
});
|
|
||||||
|
|
||||||
let device = tun::create_as_async(&config).map_err(std::io::Error::from)?;
|
|
||||||
let join_handle = tokio::spawn(crate::run(device, tun_mtu, args, shutdown_token));
|
|
||||||
|
|
||||||
Ok(join_handle.await.map_err(std::io::Error::from)??)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn mobile_run(args: Args, tun_mtu: u16, _packet_information: bool) -> c_int {
|
|
||||||
let shutdown_token = tokio_util::sync::CancellationToken::new();
|
|
||||||
if let Ok(mut lock) = TUN_QUIT.lock() {
|
|
||||||
if lock.is_some() {
|
|
||||||
log::error!("tun2proxy already started");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
*lock = Some(shutdown_token.clone());
|
|
||||||
} else {
|
|
||||||
log::error!("failed to lock tun2proxy quit token");
|
|
||||||
return -2;
|
|
||||||
}
|
|
||||||
|
|
||||||
let Ok(rt) = tokio::runtime::Builder::new_multi_thread().enable_all().build() else {
|
|
||||||
log::error!("failed to create tokio runtime with");
|
|
||||||
return -1;
|
|
||||||
};
|
|
||||||
match rt.block_on(mobile_run_async(args, tun_mtu, _packet_information, shutdown_token)) {
|
|
||||||
Ok(_) => 0,
|
|
||||||
Err(e) => {
|
|
||||||
log::error!("failed to run tun2proxy with error: {:?}", e);
|
|
||||||
-2
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn mobile_stop() -> c_int {
|
|
||||||
if let Ok(mut lock) = TUN_QUIT.lock() {
|
|
||||||
if let Some(shutdown_token) = lock.take() {
|
|
||||||
shutdown_token.cancel();
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-1
|
|
||||||
}
|
|
|
@ -78,7 +78,7 @@ fn run_service(_arguments: Vec<std::ffi::OsString>) -> Result<(), crate::BoxErro
|
||||||
}
|
}
|
||||||
unsafe { crate::tun2proxy_set_traffic_status_callback(1, Some(traffic_cb), std::ptr::null_mut()) };
|
unsafe { crate::tun2proxy_set_traffic_status_callback(1, Some(traffic_cb), std::ptr::null_mut()) };
|
||||||
|
|
||||||
if let Err(err) = crate::desktop_run_async(args, tun::DEFAULT_MTU, false, shutdown_token).await {
|
if let Err(err) = crate::general_run_async(args, tun::DEFAULT_MTU, false, shutdown_token).await {
|
||||||
log::error!("main loop error: {}", err);
|
log::error!("main loop error: {}", err);
|
||||||
}
|
}
|
||||||
Ok::<(), crate::Error>(())
|
Ok::<(), crate::Error>(())
|
||||||
|
|
Loading…
Add table
Reference in a new issue