Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 52 additions & 15 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -67,4 +66,4 @@ chrono = { version = "0.4", default-features = false, features = [
"alloc",
"clock",
] }
atomic-take = "1.1.0"
atomic-take = "1.1.0"
2 changes: 2 additions & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,15 @@ 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"
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"
Expand Down
5 changes: 0 additions & 5 deletions core/src/posix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,4 @@ pub fn get_file_hash(source: &cap_std::fs::File) -> std::io::Result<u64> {
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
}
*/
2 changes: 2 additions & 0 deletions core/src/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ impl SqliteDatabase {
}

pub fn new_connection(conn: Connection) -> capnp::Result<Self> {
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,
Expand Down
87 changes: 33 additions & 54 deletions core/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,3 @@
#[cfg(target_os = "windows")]
unsafe fn get_mac() -> Option<u64> {
use std::mem::MaybeUninit;
use windows_sys::Win32::{Foundation::ERROR_SUCCESS, NetworkManagement::IpHelper};

let mut info: [MaybeUninit<IpHelper::IP_ADAPTER_INFO>; 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
Expand All @@ -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(),
}
Expand All @@ -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()
}
Loading