From 9c1faf29080f439f78c74558a6aaf200a6cb0138 Mon Sep 17 00:00:00 2001 From: Erik McClure Date: Fri, 9 Jan 2026 00:34:09 -0800 Subject: [PATCH] Fix machine ids --- Cargo.lock | 67 +++++++++++++++++++++++++++-------- Cargo.toml | 3 +- core/Cargo.toml | 2 ++ core/src/posix.rs | 5 --- core/src/sqlite.rs | 2 ++ core/src/util.rs | 87 ++++++++++++++++++---------------------------- 6 files changed, 90 insertions(+), 76 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7514533..f48d39f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -473,7 +473,7 @@ dependencies = [ "android-tzdata", "iana-time-zone", "num-traits", - "windows-link", + "windows-link 0.1.3", ] [[package]] @@ -1434,7 +1434,7 @@ dependencies = [ "tokio", "tower-service", "tracing", - "windows-registry", + "windows-registry 0.5.3", ] [[package]] @@ -1770,8 +1770,10 @@ dependencies = [ "tracing-subscriber", "tui-textarea", "url", + "uuid", "wasmparser", "wax", + "windows-registry 0.6.0", "windows-service", "windows-sys 0.60.2", ] @@ -3613,7 +3615,7 @@ dependencies = [ "windows-collections", "windows-core", "windows-future", - "windows-link", + "windows-link 0.1.3", "windows-numerics", ] @@ -3634,9 +3636,9 @@ checksum = "c0fdd3ddb90610c7638aa2b3a3ab2904fb9e5cdbecc643ddb3647212781c4ae3" dependencies = [ "windows-implement", "windows-interface", - "windows-link", - "windows-result", - "windows-strings", + "windows-link 0.1.3", + "windows-result 0.3.4", + "windows-strings 0.4.2", ] [[package]] @@ -3646,7 +3648,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fc6a41e98427b19fe4b73c550f060b59fa592d7d686537eebf9385621bfbad8e" dependencies = [ "windows-core", - "windows-link", + "windows-link 0.1.3", "windows-threading", ] @@ -3678,6 +3680,12 @@ version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5e6ad25900d524eaabdbbb96d20b4311e1e7ae1699af4fb28c17ae66c80d798a" +[[package]] +name = "windows-link" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "45e46c0661abb7180e7b9c281db115305d49ca1709ab8242adf09666d2173c65" + [[package]] name = "windows-numerics" version = "0.2.0" @@ -3685,7 +3693,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9150af68066c4c5c07ddc0ce30421554771e528bde427614c61038bc2c92c2b1" dependencies = [ "windows-core", - "windows-link", + "windows-link 0.1.3", ] [[package]] @@ -3694,9 +3702,20 @@ version = "0.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5b8a9ed28765efc97bbc954883f4e6796c33a06546ebafacbabee9696967499e" dependencies = [ - "windows-link", - "windows-result", - "windows-strings", + "windows-link 0.1.3", + "windows-result 0.3.4", + "windows-strings 0.4.2", +] + +[[package]] +name = "windows-registry" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f91f87ce112ffb7275000ea98eb1940912c21c1567c9312fde20261f3eadd29" +dependencies = [ + "windows-link 0.2.0", + "windows-result 0.4.0", + "windows-strings 0.5.0", ] [[package]] @@ -3705,7 +3724,16 @@ version = "0.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "56f42bd332cc6c8eac5af113fc0c1fd6a8fd2aa08a0119358686e5160d0586c6" dependencies = [ - "windows-link", + "windows-link 0.1.3", +] + +[[package]] +name = "windows-result" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7084dcc306f89883455a206237404d3eaf961e5bd7e0f312f7c91f57eb44167f" +dependencies = [ + "windows-link 0.2.0", ] [[package]] @@ -3725,7 +3753,16 @@ version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "56e6c93f3a0c3b36176cb1327a4958a0353d5d166c2a35cb268ace15e91d3b57" dependencies = [ - "windows-link", + "windows-link 0.1.3", +] + +[[package]] +name = "windows-strings" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7218c655a553b0bed4426cf54b20d7ba363ef543b52d515b3e48d7fd55318dda" +dependencies = [ + "windows-link 0.2.0", ] [[package]] @@ -3768,7 +3805,7 @@ version = "0.53.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d5fe6031c4041849d7c496a8ded650796e7b6ecc19df1a431c1a363342e5dc91" dependencies = [ - "windows-link", + "windows-link 0.1.3", "windows_aarch64_gnullvm 0.53.0", "windows_aarch64_msvc 0.53.0", "windows_i686_gnu 0.53.0", @@ -3785,7 +3822,7 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b66463ad2e0ea3bbf808b7f1d371311c80e115c0b71d60efc142cafbcfb057a6" dependencies = [ - "windows-link", + "windows-link 0.1.3", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 7ef8782..7690e3c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -45,7 +45,6 @@ futures-util = "0.3" capstone-macros = { git = "https://github.com/Fundament-Software/capstone-rs" } capstone-gen = { git = "https://github.com/Fundament-Software/capstone-rs" } async-byte-channel = { git = "https://github.com/Fundament-Software/capstone-rs" } -uuid = "1.*" rusqlite = { version = "0.37", features = [ "bundled", "modern_sqlite", @@ -67,4 +66,4 @@ chrono = { version = "0.4", default-features = false, features = [ "alloc", "clock", ] } -atomic-take = "1.1.0" \ No newline at end of file +atomic-take = "1.1.0" diff --git a/core/Cargo.toml b/core/Cargo.toml index 4fdd6d0..d12c805 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -83,6 +83,7 @@ libc = "0.2" parking_lot = "0.12" derive_more = { version = "2.0.1", features = ["try_from"] } crsql_bundle = { git = "https://github.com/Fundament-Software/cr-sqlite.git" } +uuid = "1.*" [target.'cfg(target_os = "linux")'.dependencies] libc = "0.2" @@ -90,6 +91,7 @@ sshkeys = "0.3" [target.'cfg(target_os = "windows")'.dependencies] windows-service = "0.8.0" +windows-registry = "0.6" [target.'cfg(target_os = "windows")'.dependencies.windows-sys] version = "0.60" diff --git a/core/src/posix.rs b/core/src/posix.rs index 3fe5045..7212697 100644 --- a/core/src/posix.rs +++ b/core/src/posix.rs @@ -165,9 +165,4 @@ pub fn get_file_hash(source: &cap_std::fs::File) -> std::io::Result { Ok(i128_hash(caplog::murmur3::murmur3_unaligned(bytes, 273849))) } -// Adopted from khash 64 -> 32 int hash -#[inline] -fn i128_hash(key: u128) -> u64 { - ((key) >> 66 ^ (key) ^ (key) << 22) as u64 -} */ diff --git a/core/src/sqlite.rs b/core/src/sqlite.rs index 5ca08c4..118aa37 100644 --- a/core/src/sqlite.rs +++ b/core/src/sqlite.rs @@ -145,6 +145,8 @@ impl SqliteDatabase { } pub fn new_connection(conn: Connection) -> capnp::Result { + let cr_result = crsql_bundle::init_cr_sqlite_ext(); + assert_eq!(cr_result, 0); let column_set = RefCell::new(create_column_set(&conn)?); Ok(Self { connection: conn, diff --git a/core/src/util.rs b/core/src/util.rs index 8298f71..0dfc316 100644 --- a/core/src/util.rs +++ b/core/src/util.rs @@ -1,36 +1,3 @@ -#[cfg(target_os = "windows")] -unsafe fn get_mac() -> Option { - use std::mem::MaybeUninit; - use windows_sys::Win32::{Foundation::ERROR_SUCCESS, NetworkManagement::IpHelper}; - - let mut info: [MaybeUninit; 32] = - [const { MaybeUninit::uninit() }; 32]; - let mut len: u32 = size_of_val(&info) as u32; - - let status = unsafe { IpHelper::GetAdaptersInfo(info[0].as_mut_ptr(), &mut len) }; - if status != ERROR_SUCCESS { - return None; - } - - let mut all = Vec::new(); - - unsafe { - let mut padapter = info[0].assume_init_mut() as *mut IpHelper::IP_ADAPTER_INFO; - let mut i = 0; - while !padapter.is_null() { - (&mut (*padapter).Address)[(*padapter).AddressLength as usize..].fill(0); - all.push(u64::from_le_bytes((*padapter).Address)); - padapter = (*padapter).Next; - info[i].assume_init_drop(); - i += 1; - } - } - - // We only need one MAC address but we have to be absolutely sure it's the same one every time. - all.sort(); - all.first().copied() -} - fn get_cpuid() -> u128 { let r = unsafe { std::arch::x86_64::__cpuid(0) }; r.eax as u128 | (r.ebx as u128) << 32 | (r.ecx as u128) << 64 | (r.edx as u128) << 96 @@ -51,7 +18,7 @@ impl Default for SnowflakeSource { impl SnowflakeSource { pub fn new() -> Self { Self { - machine: Self::gen_machine_id(), + machine: Self::get_machine_id(), sequence: std::sync::atomic::AtomicU32::new(0), instance: capnpc::generate_random_id(), } @@ -74,29 +41,41 @@ impl SnowflakeSource { } #[cfg(target_os = "windows")] - pub fn gen_machine_id() -> u64 { - unsafe { - get_mac().unwrap_or_else(|| { - let id = get_cpuid(); - (id >> 64) as u64 | id as u64 - }) - } + pub fn get_machine_id() -> u64 { + u128_hash( + if let Ok(s) = windows_registry::LOCAL_MACHINE + .get_string("SOFTWARE\\Microsoft\\Cryptography\\MachineGuid") + && let Ok(u) = uuid::Uuid::parse_str(&s) + { + u.as_u128() + } else { + get_cpuid() + }, + ) } #[cfg(target_os = "linux")] - pub fn gen_machine_id() -> u64 { - if let Ok(key) = sshkeys::PublicKey::from_path("/etc/ssh/ssh_host_ed25519_key.pub") { - if let sshkeys::PublicKeyKind::Ed25519(kind) = key.kind { - return u64::from_le_bytes(kind.key[0..8].try_into().unwrap()); - } - } - if let Ok(key) = sshkeys::PublicKey::from_path("/etc/ssh/ssh_host_rsa_key.pub") { - if let sshkeys::PublicKeyKind::Rsa(kind) = key.kind { - return u64::from_le_bytes(kind.e[0..8].try_into().unwrap()); - } - } + pub fn get_machine_id() -> u64 { + u128_hash( + if let Ok(s) = std::fs::read_to_string("/etc/machine-id") + && let Ok(u) = uuid::Uuid::parse_str(&s) + { + u.as_u128() + } else { + get_cpuid() + }, + ) + } - let id = get_cpuid(); - (id >> 64) as u64 | id as u64 + #[cfg(not(any(target_os = "linux", target_os = "windows")))] + pub fn get_machine_id() -> u64 { + u128_hash(get_cpuid()) } } + +fn u128_hash(i: u128) -> u64 { + use std::hash::Hasher; + let mut hash = std::hash::DefaultHasher::new(); + hash.write_u128(i); + hash.finish() +}