回调增加网络路由

This commit is contained in:
lubeilin
2024-04-19 18:35:37 +08:00
parent cc6cd6dc37
commit 0352982c14
3 changed files with 27 additions and 2 deletions
+2 -2
View File
@@ -68,10 +68,10 @@ pub fn out_ips_parse(ips: &Vec<String>) -> Result<Vec<(u32, u32)>, String> {
pub fn to_ip(mask: &str) -> Result<u32, String> {
if let Ok(m) = mask.parse::<u32>() {
if m >= 32 {
if m > 32 {
return Err("not netmask".to_string());
}
let mut mask = 0 as u32;
let mut mask = 0u32;
for i in 0..m {
mask = mask | (1 << (31 - i));
}
@@ -6,6 +6,12 @@ package top.wherewego.vnt.jni;
* @author https://github.com/lbl8603/vnt
*/
public class IpUtils {
/**
* 将整数的ip地址转成字符串,例如 0 转成 "0.0.0.0"
*
* @param ipAddress
* @return
*/
public static String intToIpAddress(int ipAddress) {
return ((ipAddress & 0xFF000000) >>> 24) + "." +
@@ -13,6 +19,13 @@ public class IpUtils {
((ipAddress & 0x0000FF00) >>> 8) + "." +
(ipAddress & 0x000000FF);
}
/**
* 返回掩码的长度
*
* @param subnetMask
* @return
*/
public static int subnetMaskToPrefixLength(int subnetMask) {
int prefixLength = 0;
int bit = 1 << 31;
+12
View File
@@ -176,6 +176,17 @@ impl CallBack {
fn generate_tun0(&self, info: DeviceConfig) -> jni::errors::Result<u32> {
let mut env = self.jvm.attach_current_thread_as_daemon()?;
let class = unsafe { JClass::from_raw(self.device_config_class.as_raw()) };
let object_array = env.new_object_array(
info.external_route.len() as _,
"java/lang/String",
JObject::null(),
)?;
for (index, (network, mask)) in info.external_route.into_iter().enumerate() {
let param =
env.new_string(format!("{}/{}", network, u32::from(mask).leading_ones()))?;
env.set_object_array_element(&object_array, index as _, &param)?;
}
let param = env.new_object(
class,
"(IIII)V",
@@ -184,6 +195,7 @@ impl CallBack {
JValue::Int(Into::<u32>::into(info.virtual_netmask) as _),
JValue::Int(Into::<u32>::into(info.virtual_gateway) as _),
JValue::Int(Into::<u32>::into(info.virtual_network) as _),
JValue::Object(&object_array),
],
)?;
let rs = env.call_method(