mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-28 13:02:21 +01:00
Cache the machine ID; keep .deliver.json out of bundles copied to the agent host, and let a copied marker stop holding back retention once its Mac would have given up; show 'session not found' for a 404 whatever OpenCode said; test opencode2 discovery with injected paths; bound opencode2's stdout, stderr, and the body read for OpenCode's message.
82 lines
2.5 KiB
Rust
82 lines
2.5 KiB
Rust
//! Small helpers the router shares with its host.
|
|
|
|
use std::path::PathBuf;
|
|
use std::time::{SystemTime, UNIX_EPOCH};
|
|
|
|
pub fn now_epoch_ms() -> u64 {
|
|
SystemTime::now()
|
|
.duration_since(UNIX_EPOCH)
|
|
.unwrap_or_default()
|
|
.as_millis() as u64
|
|
}
|
|
|
|
/// `scrawl-<ms>-<pid>[-n]`: unique within this process even in the same millisecond.
|
|
pub fn make_bundle_id() -> String {
|
|
use std::sync::atomic::{AtomicU64, Ordering};
|
|
static COUNT: AtomicU64 = AtomicU64::new(0);
|
|
let ms = now_epoch_ms();
|
|
let pid = std::process::id();
|
|
match COUNT.fetch_add(1, Ordering::Relaxed) {
|
|
0 => format!("scrawl-{ms}-{pid:x}"),
|
|
n => format!("scrawl-{ms}-{pid:x}-{n}"),
|
|
}
|
|
}
|
|
|
|
pub fn percent_decode(raw: &str) -> String {
|
|
let bytes = raw.as_bytes();
|
|
let mut out = Vec::with_capacity(bytes.len());
|
|
let mut i = 0;
|
|
while i < bytes.len() {
|
|
if bytes[i] == b'%'
|
|
&& i + 2 < bytes.len()
|
|
&& let Some(byte) = std::str::from_utf8(&bytes[i + 1..i + 3])
|
|
.ok()
|
|
.and_then(|hex| u8::from_str_radix(hex, 16).ok())
|
|
{
|
|
out.push(byte);
|
|
i += 3;
|
|
} else {
|
|
out.push(bytes[i]);
|
|
i += 1;
|
|
}
|
|
}
|
|
String::from_utf8_lossy(&out).into_owned()
|
|
}
|
|
|
|
/// The executable a running process was started from (`proc_pidpath`, no processes spawned).
|
|
#[cfg(target_os = "macos")]
|
|
pub fn process_executable(pid: i32) -> Option<PathBuf> {
|
|
use std::os::unix::ffi::OsStringExt;
|
|
let mut buf = vec![0_u8; libc::PROC_PIDPATHINFO_MAXSIZE as usize];
|
|
let len = unsafe { libc::proc_pidpath(pid, buf.as_mut_ptr().cast(), buf.len() as u32) };
|
|
let len = usize::try_from(len).ok().filter(|&len| len > 0)?;
|
|
buf.truncate(len);
|
|
Some(PathBuf::from(std::ffi::OsString::from_vec(buf)))
|
|
}
|
|
|
|
#[cfg(not(target_os = "macos"))]
|
|
pub fn process_executable(_pid: i32) -> Option<PathBuf> {
|
|
None
|
|
}
|
|
|
|
/// This Mac's hardware UUID (`gethostuuid`), which, unlike its hostname, doesn't change with
|
|
/// the network.
|
|
#[cfg(target_os = "macos")]
|
|
pub fn machine_id() -> Option<String> {
|
|
static ID: std::sync::OnceLock<Option<String>> = std::sync::OnceLock::new();
|
|
ID.get_or_init(|| {
|
|
let mut id = [0_u8; 16];
|
|
let wait = libc::timespec {
|
|
tv_sec: 1,
|
|
tv_nsec: 0,
|
|
};
|
|
(unsafe { libc::gethostuuid(id.as_mut_ptr(), &wait) } == 0)
|
|
.then(|| id.iter().map(|byte| format!("{byte:02X}")).collect())
|
|
})
|
|
.clone()
|
|
}
|
|
|
|
#[cfg(not(target_os = "macos"))]
|
|
pub fn machine_id() -> Option<String> {
|
|
None
|
|
}
|