From 53a3cb2f027159c2df7137291d6f1f83a50bda74 Mon Sep 17 00:00:00 2001 From: alterdekim Date: Sat, 5 Oct 2024 02:45:34 +0300 Subject: [PATCH] modified: Cargo.toml modified: src/main.rs new file: src/mobile.rs --- Cargo.toml | 3 +++ src/main.rs | 33 ++++++++++++++++++++++++++++++++- src/mobile.rs | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 src/mobile.rs diff --git a/Cargo.toml b/Cargo.toml index 86b17cc..157a414 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,9 @@ keywords = ["tun", "network", "tunnel", "vpn"] categories = ["network-programming", "asynchronous"] readme = "README.md" +[lib] +crate-type = ["cdylib"] + [dependencies] clap = "2.33" aes-gcm = "0.10.3" diff --git a/src/main.rs b/src/main.rs index 64e5403..32a1dde 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,10 @@ +#![cfg(target_os = "android")] + +use jni::{ + objects::{JClass, JString}, + sys::{jboolean, jchar, jint}, + JNIEnv, +}; use std::{fs, net::{Ipv4Addr}, str}; use clap::{App, Arg, ArgMatches}; use env_logger::Builder; @@ -8,7 +15,31 @@ use fast32::base32::RFC4648; mod config; 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 = get_java_string(&mut env, &config_b32).unwrap(); + 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_stop(_env: JNIEnv, _: JClass) -> jint { + mobile::mobile_stop() +} + +fn get_java_string(env: &mut JNIEnv, string: &JString) -> Result { + Ok(env.get_string(string)?.into()) +} + +/* #[tokio::main] async fn main() { // Initialize the logger with 'info' as the default level @@ -39,4 +70,4 @@ async fn main() { 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 new file mode 100644 index 0000000..836802b --- /dev/null +++ b/src/mobile.rs @@ -0,0 +1,32 @@ +use std::os::raw::c_int; +use log::{error, info, warn}; +use crate::config::{ ServerConfiguration, ClientConfiguration, ObfsProtocol, ServerPeer }; +use fast32::base32::RFC4648; + + +pub fn mobile_run(cfg_raw: String, close_fd_on_drop: bool, tun_fd: jint) -> c_int { + let config: ClientConfiguration = serde_yaml::from_slice(RFC4648.decode(cfg_raw.as_bytes()).unwrap().as_slice()).expect("Bad client config file structure"); + + let block = client::client_mode(config, tun_fd); + /*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 +} \ No newline at end of file