From efa814c70a5602d546bb2abce6c3198153307e1a Mon Sep 17 00:00:00 2001 From: alterdekim Date: Mon, 7 Oct 2024 19:02:24 +0300 Subject: [PATCH] modified: src/client.rs modified: src/main.rs modified: src/mobile.rs modified: src/simple_log.rs --- src/client.rs | 9 +++++---- src/main.rs | 14 +++----------- src/mobile.rs | 4 ++-- src/simple_log.rs | 23 ++++++++--------------- 4 files changed, 18 insertions(+), 32 deletions(-) diff --git a/src/client.rs b/src/client.rs index 1e9c599..3bab10e 100644 --- a/src/client.rs +++ b/src/client.rs @@ -22,10 +22,11 @@ use crate::udp::{UDPVpnPacket, UDPVpnHandshake, UDPSerializable}; use network_interface::NetworkInterface; use network_interface::NetworkInterfaceConfig; -pub async fn client_mode(client_config: ClientConfiguration, fd: i32) { - if let Err(err) = log::set_boxed_logger(Box::::default()) { - warn!("set logger error: {}", err); - } +static MY_LOGGER: SimpleLogger = SimpleLogger::new(); + +pub async fn client_mode(client_config: ClientConfiguration, fd: i32, env: &JNIEnv) { + MY_LOGGER.set_env(env); + log::set_logger(&MY_LOGGER).unwrap(); info!("Starting client..."); diff --git a/src/main.rs b/src/main.rs index 55e9132..83ef179 100644 --- a/src/main.rs +++ b/src/main.rs @@ -75,13 +75,13 @@ mod jni { close_fd_on_drop: bool) -> JniResult { android_logger::init_once(Config::default().with_tag("RustFrida")); - FridaLib::traceFromNative(env, "Hello world".to_string()); + //FridaLib::traceFromNative(env, "Hello world".to_string()); - Ok(-2) + Ok(mobile::mobile_run(config_b32, close_fd_on_drop, tun_fd, env)) } pub extern "jni" fn stop(self, env: &JNIEnv) -> JniResult { - Ok(-1) + Ok(mobile::mobile_stop()) } pub extern "java" fn traceFromNative( @@ -90,14 +90,6 @@ mod jni { ) -> JniResult<()> { } - - /*pub extern "java" fn threadTestNoClass(env: &JNIEnv, s: String) -> JniResult {} - pub extern "java" fn threadTestWithClass( - env: &JNIEnv, - class_ref: &GlobalRef, - s: String, - ) -> JniResult { - }*/ } } diff --git a/src/mobile.rs b/src/mobile.rs index ee7959d..6761172 100644 --- a/src/mobile.rs +++ b/src/mobile.rs @@ -6,9 +6,9 @@ use crate::client; use jni::sys::jint; use tokio::runtime::Runtime; -pub fn mobile_run(cfg_raw: String, close_fd_on_drop: bool, tun_fd: jint) -> c_int { +pub fn mobile_run(cfg_raw: String, close_fd_on_drop: bool, tun_fd: i32, env: &JNIEnv) -> c_int { let config: ClientConfiguration = serde_yaml::from_slice(RFC4648.decode(cfg_raw.as_bytes()).unwrap().as_slice()).expect("Bad client config file structure"); - Runtime::new().unwrap().block_on(client::client_mode(config, tun_fd)); + Runtime::new().unwrap().block_on(client::client_mode(config, tun_fd, env)); 0 /*let block = async move { let mut config = tun2::Configuration::default(); diff --git a/src/simple_log.rs b/src/simple_log.rs index 94233c9..350d697 100644 --- a/src/simple_log.rs +++ b/src/simple_log.rs @@ -2,21 +2,10 @@ use crossbeam_channel::unbounded; use crossbeam_channel::{ Sender, Receiver }; use std::sync::LazyLock; -static bnd: LazyLock<(Sender>, Receiver>)> = LazyLock::new(|| unbounded::>()); - -pub fn fetch_logs() -> Vec { - if let Ok(bytes) = bnd.1.recv() { - return bytes; - } - Vec::new() -} - -pub fn push_log(data: Vec) { - bnd.0.send(data); -} - #[derive(Debug, Clone, PartialEq, Eq, Default)] -pub struct SimpleLogger {} +pub struct SimpleLogger { + env: Option<&JNIEnv> +} impl log::Log for SimpleLogger { fn enabled(&self, metadata: &log::Metadata) -> bool { @@ -45,6 +34,10 @@ impl SimpleLogger { record.module_path().unwrap_or(""), record.args() ); - push_log(msg.as_bytes().to_vec()); + FridaLib::traceFromNative(self.env, msg); + } + + fn set_env(&self, env: &JNIEnv) { + self.env = Some(env); } } \ No newline at end of file