forge
Explore
Log in

Don't you dare go Hollow

yumush

Clone
★ 0

0 watching0 forksRustAGPL-3.06.8 MiB27ceb05Tahinli8 hours ago399 commits

update.rs121 lines, 3.2 KiBHistoryRawDownload
1use bitcode::{Decode, Encode};
2
3pub const ALPN: &[u8] = b"yumush-update/1";
4pub const FILE_MAX: u64 = 512 << 20;
5pub const MARK: &str = "yumush-target:";
6pub const SERVER_TARGET: &str = "server-x86_64-linux";
7pub const SERVER_MARK: &str = "yumush-target:server-x86_64-linux;";
8pub const SERVER_VERSION: &str = "yumush-server";
9const TARGET_MAX: usize = 32;
10
11pub type Sha = [u8; 32];
12pub type Install = [u8; 16];
13
14#[derive(Debug, Clone, Encode, Decode)]
15pub struct Me {
16 pub install: Install,
17 pub device: String,
18 pub token: Option<String>,
19 pub build: Sha,
20}
21
22#[derive(Debug, Clone, Encode, Decode)]
23pub enum Ask {
24 Check { me: Me, targets: Vec<String> },
25 Download { me: Me, file: Sha },
26 Report { me: Me, progress: Progress },
27}
28
29#[derive(Debug, Clone, Copy, PartialEq, Eq, Encode, Decode)]
30pub enum Progress {
31 Installing,
32 Ready,
33 Failed,
34}
35
36#[derive(Debug, Clone, Copy, PartialEq, Eq, Encode, Decode)]
37pub enum Status {
38 Unknown,
39 Latest,
40 Offer(Offer),
41}
42
43#[derive(Debug, Clone, Copy, PartialEq, Eq, Encode, Decode)]
44pub struct Offer {
45 pub seq: u64,
46 pub required: bool,
47 pub file: Sha,
48 pub size: u64,
49 pub packed: u64,
50}
51
52pub fn valid_target(target: &str) -> bool {
53 !target.is_empty()
54 && target.len() <= TARGET_MAX
55 && target.bytes().all(|byte| {
56 byte.is_ascii_lowercase() || byte.is_ascii_digit() || byte == b'_' || byte == b'-'
57 })
58}
59
60pub fn sha256(bytes: &[u8]) -> Sha {
61 use sha2::Digest;
62 sha2::Sha256::digest(bytes).into()
63}
64
65pub fn hex(sha: &Sha) -> String {
66 sha.iter().map(|byte| format!("{byte:02x}")).collect()
67}
68
69pub fn target_of(bytes: &[u8]) -> Option<String> {
70 let mark = MARK.as_bytes();
71 let mut found: Option<String> = None;
72 let mut start = 0;
73 while let Some(offset) = bytes[start..]
74 .windows(mark.len())
75 .position(|window| window == mark)
76 {
77 let after = start + offset + mark.len();
78 start = after;
79 let tail = &bytes[after..bytes.len().min(after + TARGET_MAX + 1)];
80 let Some(end) = tail.iter().position(|byte| *byte == b';') else {
81 continue;
82 };
83 let Ok(target) = std::str::from_utf8(&tail[..end]) else {
84 continue;
85 };
86 if !valid_target(target) {
87 continue;
88 }
89 match &found {
90 Some(seen) if seen != target => return None,
91 _ => found = Some(target.to_string()),
92 }
93 }
94 let target = found?;
95 let executable = if target.ends_with("-linux") {
96 bytes.starts_with(b"\x7fELF")
97 } else if target.ends_with("-windows") {
98 bytes.starts_with(b"MZ")
99 } else {
100 false
101 };
102 executable.then_some(target)
103}
104
105#[cfg(test)]
106mod tests {
107 use super::*;
108
109 #[test]
110 fn finds_target() {
111 let mut elf = b"\x7fELF....yumush-target:x86_64_v3-linux;...yumush-target:".to_vec();
112 assert_eq!(target_of(&elf).as_deref(), Some("x86_64_v3-linux"));
113 elf.extend_from_slice(b"yumush-target:x86_64_v1-linux;");
114 assert_eq!(target_of(&elf), None);
115 assert_eq!(target_of(b"MZ yumush-target:x86_64_v3-linux;"), None);
116 assert_eq!(
117 target_of(b"MZ yumush-target:x86_64_v1-windows;").as_deref(),
118 Some("x86_64_v1-windows")
119 );
120 }
121}