improve jni

This commit is contained in:
CherretGit
2026-01-10 20:13:50 +07:00
parent 962aff21f0
commit 1eb913b436
3 changed files with 55 additions and 14 deletions

43
rust/Cargo.lock generated
View File

@@ -28,6 +28,18 @@ dependencies = [
"log",
]
[[package]]
name = "autocfg"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8"
[[package]]
name = "bitflags"
version = "2.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "812e12b5285cc515a9c72a5c1d3b6d46a19dac5acfef5265968c166106e31dd3"
[[package]]
name = "byedpi"
version = "0.1.0"
@@ -35,9 +47,8 @@ dependencies = [
"android_logger",
"cmake",
"jni",
"libc",
"log",
"once_cell",
"nix",
]
[[package]]
@@ -68,6 +79,12 @@ version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2fd1289c04a9ea8cb22300a459a72a385d7c73d3259e2ed7dcb2af674838cfa9"
[[package]]
name = "cfg_aliases"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724"
[[package]]
name = "cmake"
version = "0.1.54"
@@ -144,10 +161,26 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f52b00d39961fc5b2736ea853c9cc86238e165017a493d1d5c8eac6bdc4cc273"
[[package]]
name = "once_cell"
version = "1.21.3"
name = "memoffset"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d"
checksum = "488016bfae457b036d996092f6cb448677611ce4449e970ceaf42695203f218a"
dependencies = [
"autocfg",
]
[[package]]
name = "nix"
version = "0.30.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "74523f3a35e05aba87a1d978330aef40f67b0304ac79c1c00b294c9830543db6"
dependencies = [
"bitflags",
"cfg-if",
"cfg_aliases",
"libc",
"memoffset",
]
[[package]]
name = "proc-macro2"

View File

@@ -5,10 +5,9 @@ edition = "2024"
[dependencies]
jni = "0.21.1"
libc = "0.2.0"
android_logger = "0.15.1"
log = "0.4"
once_cell = "1.21.3"
nix = { version = "0.30.1", features = ["socket"] }
[lib]
crate-type = ["cdylib"]

View File

@@ -1,11 +1,11 @@
use android_logger::Config;
use core::ffi::{c_char, c_int};
use jni::JNIEnv;
use jni::objects::{JClass, JObjectArray, JString};
use jni::sys::jint;
use libc::{SHUT_RDWR, shutdown};
use log::{LevelFilter, info};
use log::{LevelFilter, error, info};
use nix::sys::socket::{Shutdown, shutdown};
use std::ffi::CString;
use std::os::raw::c_char;
use std::sync::atomic::{AtomicBool, Ordering};
static PROXY_RUNNING: AtomicBool = AtomicBool::new(false);
@@ -15,7 +15,7 @@ unsafe extern "C" {
static mut server_fd: i32;
static mut optind: i32;
static mut optreset: i32;
fn main(argc: libc::c_int, argv: *const *const c_char) -> libc::c_int;
fn main(argc: c_int, argv: *const *const c_char) -> c_int;
fn clear_params();
}
@@ -65,10 +65,19 @@ pub unsafe extern "system" fn Java_com_cherret_zaprett_byedpi_NativeBridge_jniSt
) -> jint {
init_logger();
if !PROXY_RUNNING.load(Ordering::SeqCst) {
info!("failed to stop proxy");
info!("proxy already stopped");
return -1;
}
info!("stopping proxy");
let ret = unsafe { shutdown(server_fd, SHUT_RDWR) };
ret as jint
let ret = unsafe { shutdown(server_fd, Shutdown::Both) };
match ret {
Ok(_) => {
info!("proxy stopped successfully");
0
}
Err(e) => {
error!("failed to stop proxy {}", e);
-1
}
}
}