From 512f6c8b8f844d39efc35abdeed667a219cd116b Mon Sep 17 00:00:00 2001 From: alterdekim Date: Sat, 12 Oct 2024 04:11:28 +0300 Subject: [PATCH] Changes to be committed: modified: Cargo.lock modified: Cargo.toml modified: src/client.rs modified: src/main.rs modified: src/mobile.rs --- Cargo.lock | 7 ---- Cargo.toml | 3 +- src/client.rs | 13 ++++--- src/main.rs | 103 +++++++++++++++++++------------------------------- src/mobile.rs | 28 +------------- 5 files changed, 49 insertions(+), 105 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f98345c..486cc3b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -641,12 +641,6 @@ dependencies = [ "pin-project-lite", ] -[[package]] -name = "fast32" -version = "1.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "27ea9bdb2356e5a92403cf23ac493f9b43bd71e4ffd0f800862b841dd723994c" - [[package]] name = "fastrand" version = "2.1.1" @@ -688,7 +682,6 @@ dependencies = [ "clap", "console-subscriber", "crossbeam-channel", - "fast32", "futures", "generic-array", "hex", diff --git a/Cargo.toml b/Cargo.toml index 83d62c2..1510356 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -40,5 +40,4 @@ network-interface = "2.0.0" jni = "^0.20" robusta_jni = "0.2.2" android_logger = "0.14.1" -nonblock = "0.2.0" -fast32 = "1.0.2" \ No newline at end of file +nonblock = "0.2.0" \ No newline at end of file diff --git a/src/client.rs b/src/client.rs index ec858d5..cf05960 100644 --- a/src/client.rs +++ b/src/client.rs @@ -23,7 +23,7 @@ use network_interface::NetworkInterfaceConfig; use robusta_jni::jni::JNIEnv; use crate::jni::FridaLib; -pub async fn client_mode(client_config: ClientConfiguration, fd: i32, env: &JNIEnv<'_>) { +pub async fn client_mode(client_config: ClientConfiguration, fd: i32, env: &JNIEnv<'_>, close_token: CancellationToken) { let (ltx, lrx) = unbounded::>(); ltx.send("Starting client...".as_bytes().to_vec()); @@ -46,7 +46,7 @@ pub async fn client_mode(client_config: ClientConfiguration, fd: i32, env: &JNIE let cipher_shared: Arc>> = Arc::new(Mutex::new(None)); let llltx = ltx.clone(); - tokio::spawn(async move { + let dev_read_task = tokio::spawn(async move { let mut buf = vec![0; 1400]; // mtu loop { if let Ok(n) = dev_reader.read(&mut buf).await { @@ -60,7 +60,7 @@ pub async fn client_mode(client_config: ClientConfiguration, fd: i32, env: &JNIE let cipher_shared_clone = cipher_shared.clone(); let sr_ltx = ltx.clone(); - tokio::spawn(async move { + let sock_read_task = tokio::spawn(async move { let mut buf = vec![0; 4096]; loop { @@ -118,6 +118,11 @@ pub async fn client_mode(client_config: ClientConfiguration, fd: i32, env: &JNIE let s_cipher = cipher_shared.clone(); loop { + if close_token.is_cancelled() { + sock_read_task.abort(); + dev_read_task.abort(); + return; + } if let Ok(bytes) = rx.try_recv() { ltx.send("Write to tun.".as_bytes().to_vec()); if let Err(e) = dev_writer.write_all(&bytes).await { @@ -152,6 +157,4 @@ pub async fn client_mode(client_config: ClientConfiguration, fd: i32, env: &JNIE } //ltx.send("We've got there!".as_bytes().to_vec()); } - - } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 82b6733..0d89b21 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,32 +23,7 @@ mod client; mod udp; mod mobile; -/* -#[no_mangle] -pub unsafe extern "C" fn Java_com_alterdekim_frida_FridaLib_run( - mut env: JNIEnv, - _clazz: JClass, - config_b32: JString, - tun_fd: jint, - close_fd_on_drop: jboolean, -) -> jint { - let config = env.get_string(config_b32).unwrap().into(); - let close_fd_on_drop = close_fd_on_drop != 0; - mobile::mobile_run(config, close_fd_on_drop, tun_fd) -} - -#[no_mangle] -pub unsafe extern "C" fn Java_com_alterdekim_frida_FridaLib_fetchLogs( - mut env: JNIEnv, - _clazz: JClass -) -> jstring { - **env.new_string(String::from_utf8_lossy(&simple_log::fetch_logs()[..]).to_string()).unwrap() -} - -#[no_mangle] -pub unsafe extern "C" fn Java_com_alterdekim_frida_FridaLib_stop(_env: JNIEnv, _: JClass) -> jint { - mobile::mobile_stop() -}*/ +static TUN_QUIT: tokio::sync::Mutex> = tokio::sync::Mutex::new(None); #[bridge] mod jni { @@ -73,15 +48,48 @@ mod jni { pub extern "jni" fn start(self, env: &JNIEnv, config_b32: String, tun_fd: i32, close_fd_on_drop: bool) -> JniResult { - android_logger::init_once(Config::default().with_tag("RustFrida")); + + let close_token = tokio_util::sync::CancellationToken::new(); + if let Ok(mut l) = TUN_QUIT.lock() { + if l.is_some() { + return Ok(-1); + } + *l = Some(close_token.clone()); + } else { + return Ok(-2); + } + + let main_loop = async move { + if let Err(err) = mobile::mobile_run(config_b32, close_fd_on_drop, tun_fd, env, close_token).await { + FridaLib::traceFromNative(env, format!("main loop error: {}", err)); + return Err(err); + } + Ok(()) + }; + + let exit_code = match tokio::runtime::Builder::new_multi_thread().enable_all().build() { + Err(_e) => -3, + Ok(rt) => match rt.block_on(main_loop) { + Ok(_) => 0, + Err(_e) => -4, + }, + }; + + Ok(exit_code) //FridaLib::traceFromNative(env, "Hello world".to_string()); - Ok(mobile::mobile_run(config_b32, close_fd_on_drop, tun_fd, env)) + //Ok(mobile::mobile_run(config_b32, close_fd_on_drop, tun_fd, env)) } pub extern "jni" fn stop(self, env: &JNIEnv) -> JniResult { - Ok(mobile::mobile_stop()) + if let Ok(mut l) = TUN_QUIT.lock() { + if let Some(close_token) = l.take() { + close_token.cancel(); + return Ok(0); + } + } + Ok(-1) } pub extern "java" fn traceFromNative( @@ -91,39 +99,4 @@ mod jni { } } -} - - - /* -#[tokio::main] -async fn main() { - // Initialize the logger with 'info' as the default level - Builder::new() - .filter(None, LevelFilter::Info) - .init(); - - let matches = App::new("Frida") - .version("0.1.2") - .author("alterwain") - .about("VPN software (android port)") - .arg(Arg::with_name("config") - .long("config") - .required(true) - .value_name("B32_RAW") - .help("Configuration file data (base32 encoded)") - .takes_value(true)) - .arg(Arg::with_name("fd") - .long("fd") - .required(true) - .value_name("INT") - .help("File descriptor int") - .takes_value(true)) - .get_matches(); - - let cfg_raw = matches.value_of("config").unwrap(); - - let config: ClientConfiguration = serde_yaml::from_slice(RFC4648.decode(cfg_raw.as_bytes()).unwrap().as_slice()).expect("Bad client config file structure"); - - client::client_mode(config, matches.value_of("fd").unwrap().parse().unwrap()).await; -} - */ \ No newline at end of file +} \ No newline at end of file diff --git a/src/mobile.rs b/src/mobile.rs index 60cedf3..a43574f 100644 --- a/src/mobile.rs +++ b/src/mobile.rs @@ -1,4 +1,3 @@ -use std::os::raw::c_int; use log::{error, info, warn}; use crate::config::{ ServerConfiguration, ClientConfiguration, ObfsProtocol, ServerPeer }; use fast32::base32::RFC4648; @@ -7,30 +6,7 @@ use jni::sys::jint; use tokio::runtime::Runtime; use robusta_jni::jni::JNIEnv; -pub fn mobile_run(cfg_raw: String, close_fd_on_drop: bool, tun_fd: i32, env: &JNIEnv) -> c_int { - +pub async fn mobile_run(cfg_raw: String, close_fd_on_drop: bool, tun_fd: i32, env: &JNIEnv, close_token: tokio_util::sync::CancellationToken) { let config: ClientConfiguration = serde_yaml::from_slice(hex::decode(cfg_raw).unwrap().as_slice()).expect("Bad client config file structure"); - Runtime::new().unwrap().block_on(client::client_mode(config, tun_fd, env)); - 0 - /*let block = async move { - let mut config = tun2::Configuration::default(); - - config.raw_fd(tun_fd); - config.close_fd_on_drop(close_fd_on_drop); - - let device = tun2::create_as_async(&config).map_err(std::io::Error::from)?; - let join_handle = tokio::spawn(run(device, shutdown_token)); - - join_handle.await.map_err(std::io::Error::from)? - };*/ -} - -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 + client::client_mode(config, tun_fd, env).await; } \ No newline at end of file