Skip to content

Commit 0513bea

Browse files
authored
Fix: Print more logs on EasyTier to log.
1 parent e5b3c23 commit 0513bea

File tree

2 files changed

+81
-62
lines changed

2 files changed

+81
-62
lines changed

src/easytier/linkage_impl.rs

Lines changed: 69 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -120,55 +120,71 @@ impl EasytierFactory {
120120
}
121121
}
122122

123-
let instance = toml::to_string(&Value::Table(table.into_inner())).ok()
124-
.and_then(|str| TomlConfigLoader::new_from_str(str.as_str()).ok())
125-
.map(|config| NetworkInstance::new(config));
126-
let instance = if let Some(mut instance) = instance && let Ok((_, server)) = instance.start() {
127-
let tun_fd = instance.get_tun_fd_setting().unwrap();
128-
let service = instance.get_api_service().unwrap();
123+
let Some((mut instance, server)) =
124+
toml::to_string(&Value::Table(table.into_inner()))
125+
.map_err(|e| {
126+
logging!("EasyTier", "Cannot convert configuration to toml string: {:?}", e);
127+
}).ok()
128+
.and_then(|str|
129+
TomlConfigLoader::new_from_str(str.as_str())
130+
.map_err(|e| {
131+
logging!("EasyTier", "Cannot convert toml string to config: {:?}", e);
132+
}).ok()
133+
)
134+
.map(NetworkInstance::new)
135+
.and_then(|mut instance|
136+
instance.start()
137+
.map(|(_, socks5)| (instance, socks5.unwrap()))
138+
.map_err(|e| {
139+
logging!("EasyTier", "Cannot launch EasyTier: {:?}", e);
140+
}).ok()
141+
)
142+
else {
143+
return Easytier { instance: None };
144+
};
129145

130-
tokio::spawn(async move {
131-
let mut p_address = None;
132-
let mut p_proxy_cidrs = vec![];
146+
let tun_fd = instance.get_tun_fd_setting().unwrap();
147+
let service = instance.get_api_service().unwrap();
148+
tokio::spawn(async move {
149+
let mut p_address = None;
150+
let mut p_proxy_cidrs = vec![];
133151

134-
loop {
135-
let address = service.get_peer_manage_service()
136-
.show_node_info(BaseController::default(), ShowNodeInfoRequest::default())
137-
.await.ok()
138-
.and_then(|my_info| my_info.node_info)
139-
.unwrap()
140-
.ipv4_addr
141-
.parse::<cidr::Ipv4Inet>().ok()
142-
.map(|address| { (address.address(), address.network_length()) });
143-
144-
let proxy_cidrs = service.get_peer_manage_service()
145-
.list_route(BaseController::default(), ListRouteRequest::default())
146-
.await.ok()
147-
.unwrap()
148-
.routes
149-
.into_iter()
150-
.flat_map(|route| route.proxy_cidrs).collect::<Vec<_>>();
151-
152-
if p_address != address || p_proxy_cidrs != proxy_cidrs {
153-
if let Some((address, network_length)) = address {
154-
crate::on_vpnservice_change(EasyTierTunRequest {
155-
address, network_length,
156-
cidrs: proxy_cidrs.clone(),
157-
dest: tun_fd.clone(),
158-
});
159-
}
160-
}
152+
loop {
153+
let address = service.get_peer_manage_service()
154+
.show_node_info(BaseController::default(), ShowNodeInfoRequest::default())
155+
.await.ok()
156+
.and_then(|my_info| my_info.node_info)
157+
.unwrap()
158+
.ipv4_addr
159+
.parse::<cidr::Ipv4Inet>().ok()
160+
.map(|address| { (address.address(), address.network_length()) });
161+
162+
let proxy_cidrs = service.get_peer_manage_service()
163+
.list_route(BaseController::default(), ListRouteRequest::default())
164+
.await.ok()
165+
.unwrap()
166+
.routes
167+
.into_iter()
168+
.flat_map(|route| route.proxy_cidrs).collect::<Vec<_>>();
161169

162-
p_address = address;
163-
p_proxy_cidrs = proxy_cidrs;
164-
tokio::time::sleep(Duration::from_millis(100)).await;
170+
if p_address != address || p_proxy_cidrs != proxy_cidrs {
171+
if let Some((address, network_length)) = address {
172+
crate::on_vpnservice_change(EasyTierTunRequest {
173+
address,
174+
network_length,
175+
cidrs: proxy_cidrs.clone(),
176+
dest: tun_fd.clone(),
177+
});
178+
}
165179
}
166-
});
167-
Some((instance, server.unwrap()))
168-
} else {
169-
None
170-
};
171-
Easytier { instance }
180+
181+
p_address = address;
182+
p_proxy_cidrs = proxy_cidrs;
183+
tokio::time::sleep(Duration::from_millis(100)).await;
184+
}
185+
});
186+
187+
Easytier { instance: Some((instance, server)) }
172188
}
173189
}
174190

@@ -241,10 +257,13 @@ impl Drop for Easytier {
241257
fn drop(&mut self) {
242258
logging!("EasyTier", "Killing EasyTier.");
243259

244-
self.instance.take()
245-
.and_then(|(instance, _)| instance.get_stop_notifier())
246-
.map(|stop| {
247-
Handle::current().block_on(stop.notified());
248-
});
260+
if let Some((instance, _)) = self.instance.take() {
261+
if let Some(msg) = instance.get_latest_error_msg() {
262+
logging!("EasyTier", "EasyTier has encountered an fatal error: {}", msg);
263+
}
264+
if let Some(notifier) = instance.get_stop_notifier() {
265+
Handle::current().block_on(notifier.notified());
266+
}
267+
}
249268
}
250269
}

src/lib.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,17 @@ macro_rules! logging {
2121

2222
use lazy_static::lazy_static;
2323

24+
use crate::controller::Room;
2425
use chrono::{FixedOffset, TimeZone, Utc};
26+
use jni::objects::JClass;
2527
use jni::signature::{Primitive, ReturnType};
2628
use jni::sys::{jclass, jshort, jsize, jvalue, JavaVM};
27-
use jni::{objects::JString, strings::JavaStr, sys::{jboolean, jint, jobject, JNI_FALSE, JNI_TRUE}, JNIEnv};
29+
use jni::{objects::JString, sys::{jboolean, jint, jobject, JNI_FALSE, JNI_TRUE}, JNIEnv};
2830
use libc::{c_char, c_int};
2931
use std::time::Duration;
3032
use std::{
31-
env, ffi::CString, fs, net::{IpAddr, Ipv4Addr, Ipv6Addr}, ptr::null_mut, sync::{Arc, Mutex}, thread,
33+
env, ffi::CString, fs, net::{IpAddr, Ipv4Addr, Ipv6Addr}, sync::{Arc, Mutex}, thread,
3234
};
33-
use jni::objects::JClass;
34-
use crate::controller::Room;
3535

3636
pub mod controller;
3737
mod easytier;
@@ -94,9 +94,11 @@ lazy_static! {
9494

9595
static VPN_SERVICE_CFG: Mutex<Option<crate::easytier::EasyTierTunRequest>> = Mutex::new(None);
9696

97+
// FIXME: Third-party crate 'jni-sys' leaves a dynamic link to JNI_GetCreatedJavaVMs,
98+
// which doesn't exist on Android, so A dummy JNI_GetCreatedJavaVMs is declared as a workaround.
9799
#[unsafe(no_mangle)]
98100
#[allow(non_snake_case)]
99-
extern "system" fn JNI_GetCreatedJavaVMs(vmBuf: *mut *mut JavaVM, bufLen: jsize, nVMs: *mut jsize) -> jint {
101+
extern "system" fn JNI_GetCreatedJavaVMs(_: *mut *mut JavaVM, _: jsize, _: *mut jsize) -> jint {
100102
unreachable!();
101103
}
102104

@@ -178,7 +180,7 @@ extern "system" fn Java_net_burningtnt_terracotta_TerracottaAndroidAPI_start0(en
178180
])
179181
}.unwrap();
180182
if !jenv.exception_check().unwrap() {
181-
cfg.dest.write().unwrap().replace(tun_fd.i().unwrap() as i32);
183+
cfg.dest.write().unwrap().replace(tun_fd.i().unwrap());
182184
}
183185
}
184186
});
@@ -247,14 +249,12 @@ pub(crate) fn on_vpnservice_change(request: crate::easytier::EasyTierTunRequest)
247249
}
248250

249251
fn parse_jstring(env: &JNIEnv<'static>, value: jobject) -> Option<String> {
250-
if value == null_mut() {
252+
if value.is_null() {
251253
None
252254
} else {
253255
// SAFETY: value is a Java String Object
254-
255-
let value = unsafe { JString::from_raw(value) };
256-
Some(unsafe {
257-
env.get_string_unchecked(&value)
258-
}.unwrap().into())
256+
unsafe {
257+
Some(env.get_string_unchecked(&JString::from_raw(value)).unwrap().into())
258+
}
259259
}
260260
}

0 commit comments

Comments
 (0)