mirror of
https://github.com/EasyTier/EasyTier.git
synced 2026-09-03 09:35:41 +00:00
[OHOS] fix: 修复内存泄露问题,并重构日志管理,预防性修复数据库初始化异常问题 (#2328)
* fix: leak memory feat: new log manager * fix: fail to init db * fix: fail to init db * fix: cargo format
This commit is contained in:
@@ -3,6 +3,7 @@ use napi_derive_ohos::napi;
|
||||
use serde::Serialize;
|
||||
use std::collections::HashSet;
|
||||
use std::sync::Mutex;
|
||||
use url::Url;
|
||||
|
||||
static ATTACHED_TUN_INSTANCE_IDS: once_cell::sync::Lazy<Mutex<HashSet<String>>> =
|
||||
once_cell::sync::Lazy::new(|| Mutex::new(HashSet::new()));
|
||||
@@ -158,6 +159,136 @@ fn stringify_uuid(value: Option<common::Uuid>) -> Option<String> {
|
||||
value.map(|v| v.to_string())
|
||||
}
|
||||
|
||||
fn non_empty_string(value: Option<String>) -> Option<String> {
|
||||
value.and_then(|raw| {
|
||||
let trimmed = raw.trim();
|
||||
if trimmed.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(trimmed.to_string())
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn config_virtual_ipv4_cidr(config: &api::manage::NetworkConfig) -> Option<String> {
|
||||
non_empty_string(config.virtual_ipv4.clone())
|
||||
.map(|ipv4| format!("{}/{}", ipv4, config.network_length.unwrap_or(24)))
|
||||
}
|
||||
|
||||
fn config_endpoint_urls(config: &api::manage::NetworkConfig) -> Vec<String> {
|
||||
let mut urls = Vec::new();
|
||||
let mut seen = HashSet::new();
|
||||
if let Some(url) = non_empty_string(config.public_server_url.clone())
|
||||
&& seen.insert(url.clone())
|
||||
{
|
||||
urls.push(url);
|
||||
}
|
||||
for raw in &config.peer_urls {
|
||||
let trimmed = raw.trim();
|
||||
if trimmed.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let value = trimmed.to_string();
|
||||
if seen.insert(value.clone()) {
|
||||
urls.push(value);
|
||||
}
|
||||
}
|
||||
urls
|
||||
}
|
||||
|
||||
fn endpoint_url(url: &str) -> Option<Url> {
|
||||
Url::parse(url).ok()
|
||||
}
|
||||
|
||||
fn endpoint_scheme(url: &str) -> Option<String> {
|
||||
endpoint_url(url)
|
||||
.map(|parsed| parsed.scheme().to_string())
|
||||
.or_else(|| {
|
||||
let scheme = url.split("://").next().unwrap_or("").trim();
|
||||
(!scheme.is_empty()).then_some(scheme.to_string())
|
||||
})
|
||||
}
|
||||
|
||||
fn endpoint_label(url: &str) -> String {
|
||||
if let Some(parsed) = endpoint_url(url)
|
||||
&& let Some(host) = parsed.host_str()
|
||||
{
|
||||
return format!("[Config] {}", host);
|
||||
}
|
||||
format!("[Config] {}", url)
|
||||
}
|
||||
|
||||
fn endpoint_remote_display(url: &str) -> String {
|
||||
if let Some(parsed) = endpoint_url(url)
|
||||
&& let Some(host) = parsed.host_str()
|
||||
{
|
||||
return parsed
|
||||
.port()
|
||||
.map(|port| format!("{}:{}", host, port))
|
||||
.unwrap_or_else(|| host.to_string());
|
||||
}
|
||||
url.to_string()
|
||||
}
|
||||
|
||||
fn configured_peer_id(index: usize) -> i64 {
|
||||
9_000_000 + index as i64
|
||||
}
|
||||
|
||||
fn configured_route_views(endpoints: &[String], public_server_url: Option<&str>) -> Vec<RouteView> {
|
||||
endpoints
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(index, endpoint)| RouteView {
|
||||
peer_id: configured_peer_id(index),
|
||||
hostname: Some(endpoint_label(endpoint)),
|
||||
ipv4: Some(endpoint_remote_display(endpoint)),
|
||||
ipv4_cidr: None,
|
||||
ipv6_cidr: None,
|
||||
proxy_cidrs: Vec::new(),
|
||||
next_hop_peer_id: None,
|
||||
cost: Some(0),
|
||||
path_latency: None,
|
||||
udp_nat_type: None,
|
||||
tcp_nat_type: None,
|
||||
inst_id: None,
|
||||
version: None,
|
||||
is_public_server: public_server_url.map(|url| url == endpoint),
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn configured_peer_views(endpoints: &[String]) -> Vec<PeerInfo> {
|
||||
endpoints
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(index, endpoint)| {
|
||||
let conn_id = format!("configured-peer-{}", index);
|
||||
PeerInfo {
|
||||
peer_id: configured_peer_id(index),
|
||||
default_conn_id: Some(conn_id.clone()),
|
||||
directly_connected_conns: vec![conn_id.clone()],
|
||||
conns: vec![PeerConnInfo {
|
||||
conn_id,
|
||||
my_peer_id: 0,
|
||||
peer_id: configured_peer_id(index),
|
||||
features: Vec::new(),
|
||||
tunnel_type: endpoint_scheme(endpoint),
|
||||
local_addr: None,
|
||||
remote_addr: Some(endpoint.clone()),
|
||||
resolved_remote_addr: Some(endpoint_remote_display(endpoint)),
|
||||
stats: None,
|
||||
loss_rate: None,
|
||||
is_client: true,
|
||||
network_name: None,
|
||||
is_closed: false,
|
||||
secure_auth_level: None,
|
||||
peer_identity_type: None,
|
||||
}],
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn optional_u32_to_i64(value: Option<u32>) -> Option<i64> {
|
||||
value.map(|v| v as i64)
|
||||
}
|
||||
@@ -291,3 +422,43 @@ pub fn runtime_instance_from_running_info(
|
||||
peers: info.peers.into_iter().map(peer_to_view).collect(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn runtime_instance_from_config_snapshot(
|
||||
config_id: String,
|
||||
display_name: String,
|
||||
config: api::manage::NetworkConfig,
|
||||
running: bool,
|
||||
) -> RuntimeInstanceState {
|
||||
let tun_attached = running && is_tun_attached(&config_id);
|
||||
let tun_required =
|
||||
running && (config.dev_name.as_deref().unwrap_or("") != "no_tun" || tun_attached);
|
||||
let endpoint_urls = config_endpoint_urls(&config);
|
||||
let public_server_url = non_empty_string(config.public_server_url.clone());
|
||||
let my_node_info = MyNodeInfo {
|
||||
virtual_ipv4: non_empty_string(config.virtual_ipv4.clone()),
|
||||
virtual_ipv4_cidr: config_virtual_ipv4_cidr(&config),
|
||||
hostname: non_empty_string(config.hostname.clone()),
|
||||
version: None,
|
||||
peer_id: None,
|
||||
listeners: config.listener_urls.clone(),
|
||||
vpn_portal_cfg: None,
|
||||
udp_nat_type: None,
|
||||
tcp_nat_type: None,
|
||||
};
|
||||
|
||||
RuntimeInstanceState {
|
||||
config_id: config_id.clone(),
|
||||
instance_id: config_id,
|
||||
display_name,
|
||||
running,
|
||||
tun_required,
|
||||
tun_attached,
|
||||
magic_dns_enabled: config.enable_magic_dns.unwrap_or(false),
|
||||
need_exit_node: !config.exit_nodes.is_empty(),
|
||||
error_message: None,
|
||||
my_node_info: Some(my_node_info),
|
||||
events: Vec::new(),
|
||||
routes: configured_route_views(&endpoint_urls, public_server_url.as_deref()),
|
||||
peers: configured_peer_views(&endpoint_urls),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user