use bitcode::{Decode, Encode}; pub const ALPN: &[u8] = b"yumush-update/1"; pub const FILE_MAX: u64 = 512 << 20; pub const MARK: &str = "yumush-target:"; pub const PROTOCOL_MARK_PREFIX: &str = "yumush-protocol:"; macro_rules! server_target { () => { "server-x86_64-linux" }; } pub const SERVER_TARGET: &str = server_target!(); pub const SERVER_MARK: &str = concat!("yumush-target:", server_target!(), ";"); pub const SERVER_VERSION: &str = "yumush-server"; const TARGET_MAX: usize = 32; pub type Sha = [u8; 32]; pub type Install = [u8; 16]; #[derive(Debug, Clone, Encode, Decode)] pub struct Me { pub install: Install, pub device: String, pub token: Option, pub build: Sha, } #[derive(Debug, Clone, Encode, Decode)] pub enum Ask { Check { me: Me, targets: Vec }, Download { me: Me, file: Sha }, Report { me: Me, progress: Progress }, } #[derive(Debug, Clone, Copy, PartialEq, Eq, Encode, Decode)] pub enum Progress { Installing, Ready, Failed, } #[derive(Debug, Clone, Copy, PartialEq, Eq, Encode, Decode)] pub enum Status { Unknown, Latest, Offer(Offer), } #[derive(Debug, Clone, Copy, PartialEq, Eq, Encode, Decode)] pub struct Offer { pub seq: u64, pub required: bool, pub file: Sha, pub size: u64, pub packed: u64, } pub fn valid_target(target: &str) -> bool { !target.is_empty() && target.len() <= TARGET_MAX && target.bytes().all(|byte| { byte.is_ascii_lowercase() || byte.is_ascii_digit() || byte == b'_' || byte == b'-' }) } pub fn sha256(bytes: &[u8]) -> Sha { use sha2::Digest; sha2::Sha256::digest(bytes).into() } pub fn hex(sha: &Sha) -> String { sha.iter().map(|byte| format!("{byte:02x}")).collect() } fn marked(bytes: &[u8], mark: &str, valid: fn(&str) -> bool) -> Option { let mark = mark.as_bytes(); let mut found: Option = None; let mut start = 0; while let Some(offset) = bytes[start..] .windows(mark.len()) .position(|window| window == mark) { let after = start + offset + mark.len(); start = after; let tail = &bytes[after..bytes.len().min(after + TARGET_MAX + 1)]; let Some(end) = tail.iter().position(|byte| *byte == b';') else { continue; }; let Ok(value) = std::str::from_utf8(&tail[..end]) else { continue; }; if !valid(value) { continue; } match &found { Some(seen) if seen != value => return None, _ => found = Some(value.to_string()), } } found } pub fn target_of(bytes: &[u8]) -> Option { let target = marked(bytes, MARK, valid_target)?; let executable = if target.ends_with("-linux") { bytes.starts_with(b"\x7fELF") } else if target.ends_with("-windows") { bytes.starts_with(b"MZ") } else { false }; executable.then_some(target) } pub fn protocol_of(bytes: &[u8]) -> Option { marked(bytes, PROTOCOL_MARK_PREFIX, |value| { value.starts_with("yumush/") && valid_target(&value.replace('/', "-")) }) } #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Side { Server, Client, Both, } impl Side { pub fn takes(self, target: &str) -> bool { match self { Self::Server => target == SERVER_TARGET, Self::Client => target != SERVER_TARGET, Self::Both => true, } } } impl std::str::FromStr for Side { type Err = &'static str; fn from_str(side: &str) -> Result { match side { "server" => Ok(Self::Server), "client" => Ok(Self::Client), "both" => Ok(Self::Both), _ => Err("server, client or both"), } } } #[cfg(test)] mod tests { use super::*; #[test] fn finds_target() { let mut elf = b"\x7fELF....yumush-target:x86_64_v3-linux;...yumush-target:".to_vec(); assert_eq!(target_of(&elf).as_deref(), Some("x86_64_v3-linux")); elf.extend_from_slice(b"yumush-target:x86_64_v1-linux;"); assert_eq!(target_of(&elf), None); assert_eq!(target_of(b"MZ yumush-target:x86_64_v3-linux;"), None); assert_eq!( target_of(b"MZ yumush-target:x86_64_v1-windows;").as_deref(), Some("x86_64_v1-windows") ); } #[test] fn finds_protocol() { let bytes = format!("..{}..", crate::transport::PROTOCOL_MARK); assert_eq!( protocol_of(bytes.as_bytes()).as_deref(), Some(crate::transport::PROTOCOL) ); assert_eq!( protocol_of(b"yumush-protocol:yumush/5;yumush-protocol:yumush/6;"), None ); } }