mirror of
https://github.com/EasyTier/EasyTier.git
synced 2026-05-06 17:59:11 +00:00
easytier-core支持多配置文件 (#964)
* 将web和gui允许多网络实例逻辑抽离到NetworkInstanceManager中 * easytier-core支持多配置文件 * FFI复用instance manager * 添加instance manager 单元测试
This commit is contained in:
+42
-23
@@ -1,6 +1,5 @@
|
||||
use std::{
|
||||
collections::VecDeque,
|
||||
net::SocketAddr,
|
||||
sync::{atomic::AtomicBool, Arc, RwLock},
|
||||
};
|
||||
|
||||
@@ -214,24 +213,18 @@ impl EasyTierLauncher {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn check_tcp_available(port: u16) -> bool {
|
||||
let s = format!("0.0.0.0:{}", port).parse::<SocketAddr>().unwrap();
|
||||
std::net::TcpListener::bind(s).is_ok()
|
||||
}
|
||||
|
||||
fn select_proper_rpc_port(cfg: &TomlConfigLoader) {
|
||||
let Some(mut f) = cfg.get_rpc_portal() else {
|
||||
return;
|
||||
};
|
||||
|
||||
if f.port() == 0 {
|
||||
for i in 15888..15900 {
|
||||
if Self::check_tcp_available(i) {
|
||||
f.set_port(i);
|
||||
cfg.set_rpc_portal(f);
|
||||
break;
|
||||
}
|
||||
}
|
||||
let Some(port) = crate::utils::find_free_tcp_port(15888..15900) else {
|
||||
tracing::warn!("No free port found for RPC portal, skipping setting RPC portal");
|
||||
return;
|
||||
};
|
||||
f.set_port(port);
|
||||
cfg.set_rpc_portal(f);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -343,25 +336,40 @@ impl Drop for EasyTierLauncher {
|
||||
|
||||
pub type NetworkInstanceRunningInfo = crate::proto::web::NetworkInstanceRunningInfo;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum ConfigSource {
|
||||
Cli,
|
||||
File,
|
||||
Web,
|
||||
GUI,
|
||||
FFI,
|
||||
}
|
||||
|
||||
pub struct NetworkInstance {
|
||||
config: TomlConfigLoader,
|
||||
launcher: Option<EasyTierLauncher>,
|
||||
|
||||
fetch_node_info: bool,
|
||||
config_source: ConfigSource,
|
||||
}
|
||||
|
||||
impl NetworkInstance {
|
||||
pub fn new(config: TomlConfigLoader) -> Self {
|
||||
pub fn new(config: TomlConfigLoader, source: ConfigSource) -> Self {
|
||||
Self {
|
||||
config,
|
||||
launcher: None,
|
||||
fetch_node_info: true,
|
||||
config_source: source,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_fetch_node_info(mut self, fetch_node_info: bool) -> Self {
|
||||
self.fetch_node_info = fetch_node_info;
|
||||
self
|
||||
fn get_fetch_node_info(&self) -> bool {
|
||||
match self.config_source {
|
||||
ConfigSource::Cli | ConfigSource::File => false,
|
||||
ConfigSource::Web | ConfigSource::GUI | ConfigSource::FFI => true,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_config_source(&self) -> ConfigSource {
|
||||
self.config_source.clone()
|
||||
}
|
||||
|
||||
pub fn is_easytier_running(&self) -> bool {
|
||||
@@ -395,6 +403,10 @@ impl NetworkInstance {
|
||||
})
|
||||
}
|
||||
|
||||
pub fn get_inst_name(&self) -> String {
|
||||
self.config.get_inst_name()
|
||||
}
|
||||
|
||||
pub fn set_tun_fd(&mut self, tun_fd: i32) {
|
||||
if let Some(launcher) = self.launcher.as_ref() {
|
||||
launcher.data.tun_fd.write().unwrap().replace(tun_fd);
|
||||
@@ -406,7 +418,7 @@ impl NetworkInstance {
|
||||
return Ok(self.subscribe_event().unwrap());
|
||||
}
|
||||
|
||||
let launcher = EasyTierLauncher::new(self.fetch_node_info);
|
||||
let launcher = EasyTierLauncher::new(self.get_fetch_node_info());
|
||||
self.launcher = Some(launcher);
|
||||
let ev = self.subscribe_event().unwrap();
|
||||
|
||||
@@ -418,7 +430,7 @@ impl NetworkInstance {
|
||||
Ok(ev)
|
||||
}
|
||||
|
||||
fn subscribe_event(&self) -> Option<broadcast::Receiver<GlobalCtxEvent>> {
|
||||
pub fn subscribe_event(&self) -> Option<broadcast::Receiver<GlobalCtxEvent>> {
|
||||
if let Some(launcher) = self.launcher.as_ref() {
|
||||
Some(launcher.data.event_subscriber.read().unwrap().subscribe())
|
||||
} else {
|
||||
@@ -426,9 +438,16 @@ impl NetworkInstance {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn wait(&self) -> Option<String> {
|
||||
pub fn get_stop_notifier(&self) -> Option<Arc<tokio::sync::Notify>> {
|
||||
if let Some(launcher) = self.launcher.as_ref() {
|
||||
Some(launcher.data.instance_stop_notifier.clone())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_latest_error_msg(&self) -> Option<String> {
|
||||
if let Some(launcher) = self.launcher.as_ref() {
|
||||
launcher.data.instance_stop_notifier.notified().await;
|
||||
launcher.error_msg.read().unwrap().clone()
|
||||
} else {
|
||||
None
|
||||
|
||||
Reference in New Issue
Block a user