allow set machine uid with command line (#1009)

This commit is contained in:
Sijie.Sun
2025-06-18 11:02:29 +08:00
committed by GitHub
parent 34ba0bc95b
commit 8c2f96d1aa
4 changed files with 32 additions and 4 deletions

View File

@@ -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: "配置文件路径,注意:命令行中的配置的选项会覆盖配置文件中的选项"

View File

@@ -1,21 +1,21 @@
macro_rules! define_global_var {
($name:ident, $type:ty, $init:expr) => {
pub static $name: once_cell::sync::Lazy<tokio::sync::Mutex<$type>> =
once_cell::sync::Lazy::new(|| tokio::sync::Mutex::new($init));
pub static $name: once_cell::sync::Lazy<std::sync::Mutex<$type>> =
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<String>, None);
pub const UDP_HOLE_PUNCH_CONNECTOR_SERVICE_ID: u32 = 2;
pub const WIN_SERVICE_WORK_DIR_REG_KEY: &str = "SOFTWARE\\EasyTier\\Service\\WorkDir";

View File

@@ -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<T: Debug + Send + Sync + 'static>(
);
}
pub fn set_default_machine_id(mid: Option<String>) {
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"))

View File

@@ -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<String>,
#[arg(
long,
env = "ET_MACHINE_ID",
help = t!("core_clap.machine_id").to_string()
)]
machine_id: Option<String>,
#[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,