1use semver::Version;
5use subc_os::local_peer::{CapturedPeer, CodeRequirementOutcome, ObservedPeer};
6use subc_protocol::peer::OperatorApp;
7
8pub const MACOS_OPERATOR_APP_ENABLED: bool = true;
9pub const WINDOWS_OPERATOR_APP_ENABLED: bool = false;
10pub const LINUX_OPERATOR_APP_ENABLED: bool = false;
11pub const PHONE_OPERATOR_ENABLED: bool = false;
12
13pub const RELEASE_FLOOR: Version = Version::new(u64::MAX, u64::MAX, u64::MAX);
15
16fn effective_release_floor() -> Version {
17 #[cfg(feature = "dev-desktop-approval")]
18 {
19 option_env!("SUBC_DEV_OPERATOR_APP_FLOOR")
20 .and_then(|floor| Version::parse(floor).ok())
21 .unwrap_or(RELEASE_FLOOR)
22 }
23 #[cfg(not(feature = "dev-desktop-approval"))]
24 {
25 RELEASE_FLOOR
26 }
27}
28
29#[cfg(feature = "dev-desktop-approval")]
30pub(crate) fn development_floor_diagnostic() -> Option<String> {
31 option_env!("SUBC_DEV_OPERATOR_APP_FLOOR")
32 .map(|_| format!("SUBC_DEV_OPERATOR_APP_FLOOR={}", effective_release_floor()))
33}
34
35pub const MACOS_REQUIREMENT: &str = concat!(
38 "anchor apple generic and certificate 1[field.1.2.840.113635.100.6.2.6] exists ",
39 "and certificate leaf[field.1.2.840.113635.100.6.1.13] exists ",
40 "and certificate leaf[subject.OU] = \"5R5846NBPW\" ",
41 "and identifier \"io.cortexkit.alfonso.desktop\" ",
42 "and entitlement[\"com.apple.security.get-task-allow\"] absent ",
43 "and entitlement[\"com.apple.security.cs.disable-library-validation\"] absent ",
44 "and entitlement[\"com.apple.security.cs.allow-dyld-environment-variables\"] absent"
45);
46
47#[derive(Clone, Copy, Debug)]
48#[non_exhaustive]
49pub struct CompiledApp {
50 pub id: Option<&'static str>,
51 pub macos_bundle_id: &'static str,
52 pub macos_executable: &'static str,
53 pub macos_bundle: &'static str,
54 pub windows_executable: &'static str,
55 pub linux_executable: &'static str,
56 pub windows_publisher: Option<&'static str>,
57 pub windows_path: Option<&'static str>,
58 pub linux_path: Option<&'static str>,
59 pub linux_release_index_identity: Option<&'static str>,
60}
61
62pub const COMPILED_APP: CompiledApp = CompiledApp {
63 id: Some("alfonso-desktop"),
64 macos_bundle_id: "io.cortexkit.alfonso.desktop",
65 macos_executable: "Alfonso",
66 macos_bundle: "Alfonso.app",
67 windows_executable: "Alfonso.exe",
68 linux_executable: "alfonso",
69 windows_publisher: None,
70 windows_path: None,
71 linux_path: None,
72 linux_release_index_identity: None,
73};
74
75pub fn meets_release_floor(version: Option<&str>, floor: &Version) -> bool {
76 *floor != RELEASE_FLOOR
78 && version
79 .and_then(|version| Version::parse(version).ok())
80 .is_some_and(|version| version.cmp_precedence(floor).is_ge())
81}
82
83#[allow(dead_code)]
90#[derive(Clone, Copy, Debug, PartialEq, Eq)]
91pub(crate) enum AppOpenPlatform {
92 MacOS,
93 Windows,
94 Linux,
95 Other,
96}
97
98impl AppOpenPlatform {
99 pub fn current() -> Self {
100 #[cfg(target_os = "macos")]
101 return Self::MacOS;
102 #[cfg(windows)]
103 return Self::Windows;
104 #[cfg(target_os = "linux")]
105 return Self::Linux;
106 #[cfg(not(any(target_os = "macos", windows, target_os = "linux")))]
107 return Self::Other;
108 }
109}
110
111#[derive(Clone, Debug)]
112pub(crate) struct OperatorAppStamp {
113 pub app: OperatorApp,
114 peer: CapturedPeer,
115 requirement: &'static str,
116 floor: Version,
117 pub(crate) launch_incarnation: Option<u64>,
121}
122
123impl OperatorAppStamp {
124 #[cfg(test)]
125 pub(crate) fn fixture(app: &str, pid: u32) -> Self {
126 Self {
127 app: OperatorApp::new(app, pid),
128 peer: ObservedPeer::Unobserved.into(),
129 requirement: MACOS_REQUIREMENT,
130 floor: RELEASE_FLOOR,
131 launch_incarnation: None,
132 }
133 }
134
135 #[cfg(windows)]
139 pub(crate) fn launch_stamp(app: OperatorApp, peer: CapturedPeer, incarnation: u64) -> Self {
140 Self {
141 app,
142 peer,
143 requirement: MACOS_REQUIREMENT,
144 floor: RELEASE_FLOOR,
145 launch_incarnation: Some(incarnation),
146 }
147 }
148
149 pub(crate) fn delivery_enabled(&self, policy: &OperatorAppPolicy) -> bool {
153 if self.launch_incarnation.is_some() {
154 policy.windows_enabled
155 } else {
156 policy.macos_enabled
157 }
158 }
159
160 pub(crate) fn revalidate_with_launches(&self, launches: &crate::app_open::LaunchTable) -> bool {
163 if let Some(incarnation) = self.launch_incarnation {
164 return launches.is_live_incarnation(&self.app.app, incarnation);
165 }
166 self.revalidate()
167 }
168
169 pub(crate) fn revalidate(&self) -> bool {
170 matches!(self.peer.check_code_requirement(self.requirement),
171 CodeRequirementOutcome::Satisfied(code)
172 if meets_release_floor(code.short_version.as_deref(), &self.floor))
173 }
174}
175
176#[derive(Clone, Debug)]
177pub(crate) struct OperatorAppPolicy {
178 pub macos_enabled: bool,
179 pub windows_enabled: bool,
180 pub linux_enabled: bool,
181 pub phone_enabled: bool,
182 app: CompiledApp,
183 requirement: &'static str,
184 floor: Version,
185 #[cfg(test)]
186 requirement_checks: std::sync::Arc<std::sync::atomic::AtomicUsize>,
187 pub windows_path: Option<std::path::PathBuf>,
191 pub linux_path: Option<std::path::PathBuf>,
192 #[cfg_attr(not(windows), allow(dead_code))]
198 pub windows_breakaway: bool,
199 #[cfg_attr(not(target_os = "linux"), allow(dead_code))]
204 pub linux_force_direct: bool,
205}
206
207impl Default for OperatorAppPolicy {
208 fn default() -> Self {
209 Self {
210 macos_enabled: MACOS_OPERATOR_APP_ENABLED,
211 windows_enabled: WINDOWS_OPERATOR_APP_ENABLED,
212 linux_enabled: LINUX_OPERATOR_APP_ENABLED,
213 phone_enabled: PHONE_OPERATOR_ENABLED,
214 app: COMPILED_APP,
215 requirement: MACOS_REQUIREMENT,
216 floor: effective_release_floor(),
217 #[cfg(test)]
218 requirement_checks: Default::default(),
219 windows_path: COMPILED_APP.windows_path.map(std::path::PathBuf::from),
220 linux_path: COMPILED_APP.linux_path.map(std::path::PathBuf::from),
221 windows_breakaway: true,
222 linux_force_direct: false,
223 }
224 }
225}
226
227impl OperatorAppPolicy {
228 pub(crate) fn app_id(&self) -> Option<&'static str> {
234 self.app.id
235 }
236
237 #[cfg(windows)]
238 pub(crate) fn release_floor(&self) -> &Version {
239 &self.floor
240 }
241
242 pub(crate) fn installed_path(&self, platform: AppOpenPlatform) -> Option<std::path::PathBuf> {
243 match platform {
244 AppOpenPlatform::Windows => self.windows_path.clone(),
245 AppOpenPlatform::Linux => self.linux_path.clone(),
246 AppOpenPlatform::MacOS | AppOpenPlatform::Other => None,
247 }
248 }
249
250 pub(crate) fn switch_enabled(&self, platform: AppOpenPlatform) -> bool {
251 match platform {
252 AppOpenPlatform::MacOS => self.macos_enabled,
253 AppOpenPlatform::Windows => self.windows_enabled,
254 AppOpenPlatform::Linux => self.linux_enabled,
255 AppOpenPlatform::Other => false,
256 }
257 }
258
259 #[cfg(windows)]
264 pub(crate) fn windows_publisher(&self) -> Option<&'static str> {
265 self.app.windows_publisher
266 }
267
268 pub(crate) fn stamp(&self, peer: CapturedPeer) -> Option<OperatorAppStamp> {
269 if !self.macos_enabled {
270 return None;
271 }
272 self.config_identity(peer)
273 }
274
275 pub(crate) fn config_identity(&self, peer: CapturedPeer) -> Option<OperatorAppStamp> {
278 if self.floor == RELEASE_FLOOR || !cfg!(target_os = "macos") {
281 return None;
282 }
283 let app = self.app.id?;
284 let ObservedPeer::Observed(observation) = peer.observation() else {
285 return None;
286 };
287 let pid = observation.pid;
288 #[cfg(test)]
289 self.requirement_checks
290 .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
291 let CodeRequirementOutcome::Satisfied(code) = peer.check_code_requirement(self.requirement)
292 else {
293 return None;
294 };
295 if !meets_release_floor(code.short_version.as_deref(), &self.floor) {
296 return None;
297 }
298 Some(OperatorAppStamp {
299 app: OperatorApp::new(app, pid),
300 peer,
301 requirement: self.requirement,
302 floor: self.floor.clone(),
303 launch_incarnation: None,
304 })
305 }
306}
307
308#[cfg(any(test, feature = "test-support"))]
312#[derive(Clone, Debug)]
313pub struct TestOperatorAppPolicy {
314 pub macos_enabled: bool,
315 pub windows_enabled: bool,
316 pub linux_enabled: bool,
317 pub app: &'static str,
318 pub requirement: &'static str,
319 pub floor: Version,
320 pub windows_publisher: Option<&'static str>,
321 pub windows_path: Option<std::path::PathBuf>,
322 pub linux_path: Option<std::path::PathBuf>,
323 pub windows_breakaway: bool,
324 pub linux_force_direct: bool,
325}
326
327#[cfg(any(test, feature = "test-support"))]
328impl From<TestOperatorAppPolicy> for OperatorAppPolicy {
329 fn from(test: TestOperatorAppPolicy) -> Self {
330 Self {
331 macos_enabled: test.macos_enabled,
332 windows_enabled: test.windows_enabled,
333 linux_enabled: test.linux_enabled,
334 phone_enabled: PHONE_OPERATOR_ENABLED,
335 app: CompiledApp {
336 id: Some(test.app),
337 windows_publisher: test.windows_publisher,
338 ..COMPILED_APP
339 },
340 requirement: test.requirement,
341 floor: test.floor,
342 #[cfg(test)]
343 requirement_checks: Default::default(),
344 windows_path: test.windows_path,
345 linux_path: test.linux_path,
346 windows_breakaway: test.windows_breakaway,
347 linux_force_direct: test.linux_force_direct,
348 }
349 }
350}
351
352#[cfg(test)]
353mod tests;