diff --git a/easytier/locales/app.yml b/easytier/locales/app.yml index 120ffc8..fd84007 100644 --- a/easytier/locales/app.yml +++ b/easytier/locales/app.yml @@ -10,6 +10,11 @@ core_clap: 配置服务器地址。允许格式: 完整URL:--config-server udp://127.0.0.1:22020/admin 仅用户名:--config-server admin,将使用官方的服务器 + machine_id: + en: |+ + the machine id to identify this machine, used for config recovery after disconnection, must be unique and fixed. default is from system. + zh-CN: |+ + Web 配置服务器通过 machine id 来识别机器,用于断线重连后的配置恢复,需要保证唯一且固定不变。默认从系统获得。 config_file: en: "path to the config file, NOTE: the options set by cmdline args will override options in config file" zh-CN: "配置文件路径,注意:命令行中的配置的选项会覆盖配置文件中的选项" diff --git a/easytier/src/common/constants.rs b/easytier/src/common/constants.rs index fe561f2..8361442 100644 --- a/easytier/src/common/constants.rs +++ b/easytier/src/common/constants.rs @@ -1,21 +1,21 @@ macro_rules! define_global_var { ($name:ident, $type:ty, $init:expr) => { - pub static $name: once_cell::sync::Lazy> = - once_cell::sync::Lazy::new(|| tokio::sync::Mutex::new($init)); + pub static $name: once_cell::sync::Lazy> = + once_cell::sync::Lazy::new(|| std::sync::Mutex::new($init)); }; } #[macro_export] macro_rules! use_global_var { ($name:ident) => { - crate::common::constants::$name.lock().await.to_owned() + crate::common::constants::$name.lock().unwrap().to_owned() }; } #[macro_export] macro_rules! set_global_var { ($name:ident, $val:expr) => { - *crate::common::constants::$name.lock().await = $val + *crate::common::constants::$name.lock().unwrap() = $val }; } @@ -23,6 +23,8 @@ define_global_var!(MANUAL_CONNECTOR_RECONNECT_INTERVAL_MS, u64, 1000); define_global_var!(OSPF_UPDATE_MY_GLOBAL_FOREIGN_NETWORK_INTERVAL_SEC, u64, 10); +define_global_var!(MACHINE_UID, Option, None); + pub const UDP_HOLE_PUNCH_CONNECTOR_SERVICE_ID: u32 = 2; pub const WIN_SERVICE_WORK_DIR_REG_KEY: &str = "SOFTWARE\\EasyTier\\Service\\WorkDir"; diff --git a/easytier/src/common/mod.rs b/easytier/src/common/mod.rs index e8b46e7..63f9d16 100644 --- a/easytier/src/common/mod.rs +++ b/easytier/src/common/mod.rs @@ -8,6 +8,8 @@ use time::util::refresh_tz; use tokio::{task::JoinSet, time::timeout}; use tracing::Instrument; +use crate::{set_global_var, use_global_var}; + pub mod compressor; pub mod config; pub mod constants; @@ -87,7 +89,17 @@ pub fn join_joinset_background( ); } +pub fn set_default_machine_id(mid: Option) { + set_global_var!(MACHINE_UID, mid); +} + pub fn get_machine_id() -> uuid::Uuid { + if let Some(default_mid) = use_global_var!(MACHINE_UID) { + let mut b = [0u8; 16]; + crate::tunnel::generate_digest_from_str("", &default_mid, &mut b); + return uuid::Uuid::from_bytes(b); + } + // a path same as the binary let machine_id_file = std::env::current_exe() .map(|x| x.with_file_name("et_machine_id")) diff --git a/easytier/src/easytier-core.rs b/easytier/src/easytier-core.rs index 39d53f6..b15b0c8 100644 --- a/easytier/src/easytier-core.rs +++ b/easytier/src/easytier-core.rs @@ -22,6 +22,7 @@ use easytier::{ }, constants::EASYTIER_VERSION, global_ctx::GlobalCtx, + set_default_machine_id, stun::MockStunInfoCollector, }, connector::create_connector_by_url, @@ -99,6 +100,13 @@ struct Cli { )] config_server: Option, + #[arg( + long, + env = "ET_MACHINE_ID", + help = t!("core_clap.machine_id").to_string() + )] + machine_id: Option, + #[arg( short, long, @@ -936,6 +944,7 @@ async fn run_main(cli: Cli) -> anyhow::Result<()> { init_logger(&cli.logging_options, false)?; if cli.config_server.is_some() { + set_default_machine_id(cli.machine_id); let config_server_url_s = cli.config_server.clone().unwrap(); let config_server_url = match url::Url::parse(&config_server_url_s) { Ok(u) => u,