From 995ff9b059be5f9be032e750403a56c95280414d Mon Sep 17 00:00:00 2001 From: Iron-Yang Date: Wed, 3 Jul 2024 22:46:59 +0800 Subject: [PATCH] Try to execute 'dmidecode' command to get the system identifier first --- .gitignore | 5 ++++- common/src/identifier.rs | 33 ++++++++++++++------------------- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/.gitignore b/.gitignore index 3548505..a4867ea 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ target/* vnt/src/proto/* -common/src/generated_serial_number.rs \ No newline at end of file +common/src/generated_serial_number.rs + +# RustRover +.idea \ No newline at end of file diff --git a/common/src/identifier.rs b/common/src/identifier.rs index 4d3590b..884810d 100644 --- a/common/src/identifier.rs +++ b/common/src/identifier.rs @@ -52,6 +52,18 @@ pub fn get_unique_identifier() -> Option { pub fn get_unique_identifier() -> Option { use std::process::Command; + // Try to execute 'dmidecode' command to get the system identifier first. + if let Ok(output) = Command::new("dmidecode") + .arg("-s") + .arg("system-uuid") + .output() { + let identifier = String::from_utf8_lossy(&output.stdout).trim().to_owned(); + if !identifier.is_empty() { + return Some(identifier.to_string()); + } + } + + // Try to read file /etc/machine-id if 'dmidecode' command cannot be executed or get nothing. // 对 linux 或 wsl 来说,读取 /etc/machine-id 即可获取当前操作系统的 // 唯一标识,而且某些环境没有预装`dmidecode`命令 if let Ok(identifier) = std::fs::read_to_string("/etc/machine-id") { @@ -61,22 +73,5 @@ pub fn get_unique_identifier() -> Option { } } - let output = match Command::new("dmidecode") - .arg("-s") - .arg("system-uuid") - .output() - { - Ok(output) => output, - Err(_) => { - return None; - } - }; - - let result = String::from_utf8_lossy(&output.stdout); - let identifier = result.trim(); - if identifier.is_empty() { - None - } else { - Some(identifier.to_string()) - } -} + None +} \ No newline at end of file