rightkit-daemon 0.2.7

One discovery/auth convention for Right Suite local services: service.json, ranking and takeover, current-user-only Unix socket / named pipe, per-app request allowlist.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
//! Host side of the outbound link (`rk-link/1`): the upgrade handshake, the table of live
//! connections, and the handling of every frame a device sends.
//!
//! The logic is transport-free. The HTTP adapter ([`crate::remote::http`]) upgrades the
//! connection, then pumps lines in (one [`Daemon::link_frame`] per line) and frames out
//! (the receiver of [`Accepted`]). Tests drive the same functions without a socket.

use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};

use base64::engine::general_purpose::URL_SAFE_NO_PAD;
use base64::Engine as _;
use serde_json::{json, Value};
use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender};
use tokio::sync::Notify;

use super::{Daemon, Request, Response};
use crate::link::frame::{open_c2s, s2c_line, FrameError, HEARTBEAT_SECS, SCOPE, UPGRADE_TOKEN};
use crate::remote::routes::LINK_ROUTE;
use crate::remote::state::TaskStatus;
use crate::remote::util::{hex, random_bytes};

/// Something to write to a link's connection.
#[derive(Debug, Clone, PartialEq)]
pub enum Out {
    /// A frame body; the writer stamps `n` and the tag.
    Frame(Value),
    /// Send a `close` frame with this reason, then end the connection.
    Close(String),
}

/// What the adapter does after one inbound line.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FrameOutcome {
    Continue,
    /// End the connection; the reason is reported in `LINK_DOWN`.
    Close(String),
}

/// One live (or just-superseded) link connection of one device.
pub struct LinkSession {
    device: String,
    link_id: String,
    key: [u8; 32],
    public_key: Vec<u8>,
    tx: UnboundedSender<Out>,
    /// Sequence number of the last accepted machine-to-host frame.
    c2s: Mutex<u64>,
    /// Sequence number of the last host-to-machine frame written.
    s2c: Mutex<u64>,
    ready: AtomicBool,
    closed: AtomicBool,
    /// Wakes the reader of the connection when the session is closed from elsewhere.
    pub(crate) stop: Notify,
}

impl LinkSession {
    pub fn device(&self) -> &str {
        &self.device
    }

    pub fn link_id(&self) -> &str {
        &self.link_id
    }

    /// Queue a frame body for the connection. Dropped once the session is closed.
    pub(crate) fn send(&self, frame: Value) {
        if !self.closed.load(Ordering::SeqCst) {
            let _ = self.tx.send(Out::Frame(frame));
        }
    }

    /// End the session: the connection writes a `close` frame and ends, its reader stops.
    /// Idempotent.
    pub fn close(&self, reason: &str) {
        if !self.closed.swap(true, Ordering::SeqCst) {
            let _ = self.tx.send(Out::Close(reason.to_string()));
            self.stop.notify_one();
        }
    }

    pub fn is_closed(&self) -> bool {
        self.closed.load(Ordering::SeqCst)
    }

    /// Number and tag a host-to-machine frame: the line to write. The sequence number is taken
    /// under a lock, so call it from the one writer in write order.
    pub fn encode(&self, mut frame: Value) -> String {
        let n = {
            let mut g = self.s2c.lock().unwrap_or_else(|e| e.into_inner());
            *g += 1;
            *g
        };
        frame["n"] = json!(n);
        s2c_line(&self.key, &self.link_id, &frame)
    }
}

/// The result of a successful upgrade request: the session, the receiver the connection writes
/// from, and the values for the `101` response.
pub struct Accepted {
    pub session: Arc<LinkSession>,
    pub(crate) rx: UnboundedReceiver<Out>,
    /// Connection id (response header `x-rk-link-id`).
    pub link_id: String,
    /// Host-to-machine session key, hex (response header `x-rk-link-key`).
    pub key_hex: String,
}

impl Accepted {
    /// Next queued item without waiting (for drivers that have no async runtime).
    pub fn try_next(&mut self) -> Option<Out> {
        self.rx.try_recv().ok()
    }

    pub fn into_parts(self) -> (Arc<LinkSession>, UnboundedReceiver<Out>) {
        (self.session, self.rx)
    }
}

impl Daemon {
    /// Authenticate an `rk-link/1` upgrade request and register the connection. The request is the
    /// ordinary signed device request (`GET /v1/link`, signature, nonce, timestamp); the device
    /// needs scope `link`. A second connection of the same device supersedes the first: the older
    /// session is closed with reason `superseded` and can complete nothing from then on.
    pub fn link_accept(&self, req: &Request) -> Result<Accepted, Response> {
        let route = &LINK_ROUTE;
        let upgrade = req
            .header("upgrade")
            .is_some_and(|v| v.trim().eq_ignore_ascii_case(UPGRADE_TOKEN));
        let connection = req.header("connection").is_some_and(|v| {
            v.split(',')
                .any(|t| t.trim().eq_ignore_ascii_case("upgrade"))
        });
        if req.method != "GET" || !upgrade || !connection {
            return Err(self.reject(route, "bad_upgrade", 400, "bad_request"));
        }
        let now = self.clock.now_secs();
        let mut g = self.lock();
        let device = self.authenticate(&mut g, route, req, now)?;
        let Some(record) = g.state.devices.get(&device) else {
            return Err(self.reject(route, "unknown_device", 401, "unauthenticated"));
        };
        if !record.scopes.iter().any(|s| s == SCOPE) {
            return Err(self.reject(route, "insufficient_scope", 403, "forbidden"));
        }
        let public_key = URL_SAFE_NO_PAD
            .decode(&record.public_key)
            .unwrap_or_default();
        // The nonce is durable before the connection exists.
        if let Err(e) = self.flush(&mut g) {
            return Err(self.storage_error(e));
        }
        drop(g);

        let link_id = hex(&random_bytes::<16>());
        let key = random_bytes::<32>();
        let (tx, rx) = unbounded_channel();
        let session = Arc::new(LinkSession {
            device: device.clone(),
            link_id: link_id.clone(),
            key,
            public_key,
            tx,
            c2s: Mutex::new(0),
            s2c: Mutex::new(0),
            ready: AtomicBool::new(false),
            closed: AtomicBool::new(false),
            stop: Notify::new(),
        });
        let old = self
            .links
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .insert(device.clone(), session.clone());
        if let Some(old) = old {
            old.close("superseded");
            self.emit(
                "LINK_DOWN",
                json!({ "deviceId": device, "linkId": old.link_id, "reason": "superseded" }),
            );
        }
        Ok(Accepted {
            session,
            rx,
            link_id,
            key_hex: hex(&key),
        })
    }

    /// Whether `s` is the current connection of its device (not superseded or closed).
    pub(super) fn link_is_current(&self, s: &Arc<LinkSession>) -> bool {
        !s.is_closed()
            && self
                .links
                .lock()
                .unwrap_or_else(|e| e.into_inner())
                .get(&s.device)
                .is_some_and(|c| Arc::ptr_eq(c, s))
    }

    /// Sessions that completed `hello`, in device order.
    pub(super) fn ready_links(&self) -> Vec<Arc<LinkSession>> {
        self.links
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .values()
            .filter(|s| s.ready.load(Ordering::SeqCst) && !s.is_closed())
            .cloned()
            .collect()
    }

    /// Devices with a live, ready link.
    pub fn linked_devices(&self) -> Vec<String> {
        self.ready_links()
            .iter()
            .map(|s| s.device.clone())
            .collect()
    }

    /// Close the current connection of `device` (operator action; the device may reconnect).
    pub fn close_link(&self, device: &str, reason: &str) -> bool {
        let s = self
            .links
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .get(device)
            .cloned();
        match s {
            Some(s) => {
                s.close(reason);
                true
            }
            None => false,
        }
    }

    /// Close every link (server shutdown).
    pub fn close_all_links(&self, reason: &str) {
        let all: Vec<Arc<LinkSession>> = self
            .links
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .values()
            .cloned()
            .collect();
        for s in all {
            s.close(reason);
        }
    }

    /// The connection ended: close the session and, if it was the device's current one, drop it
    /// from the table (`LINK_DOWN`). Leases are kept; the device resumes them on reconnect.
    pub fn link_closed(&self, s: &Arc<LinkSession>, reason: &str) {
        s.close(reason);
        let was_current = {
            let mut l = self.links.lock().unwrap_or_else(|e| e.into_inner());
            let current = l.get(&s.device).is_some_and(|c| Arc::ptr_eq(c, s));
            if current {
                l.remove(&s.device);
            }
            current
        };
        if was_current {
            self.emit(
                "LINK_DOWN",
                json!({ "deviceId": s.device, "linkId": s.link_id, "reason": reason }),
            );
        }
    }

    /// Periodic housekeeping, called once per heartbeat by a connection's writer: re-queue lapsed
    /// leases (once each) and dispatch whatever is now ready. There is no timer thread.
    pub fn link_tick(&self) {
        let _ = self.tasks_sweep();
    }

    fn frame_refused(&self, s: &Arc<LinkSession>, reason: &str) -> FrameOutcome {
        self.emit(
            "LINK_FRAME_REJECTED",
            json!({ "deviceId": s.device, "linkId": s.link_id, "reason": reason }),
        );
        FrameOutcome::Close(reason.to_string())
    }

    /// Handle one inbound line from the device behind `s`. Every line is authenticated (device
    /// signature bound to this connection), must carry the next sequence number, and is refused
    /// (ending the connection) when the session has been superseded.
    pub fn link_frame(&self, s: &Arc<LinkSession>, line: &str) -> FrameOutcome {
        if !self.link_is_current(s) {
            return FrameOutcome::Close("superseded".into());
        }
        let v = match open_c2s(&s.public_key, &s.link_id, line) {
            Ok(v) => v,
            Err(FrameError::BadTag) => return self.frame_refused(s, "bad_tag"),
            Err(FrameError::Malformed) => return self.frame_refused(s, "malformed"),
        };
        let Some(n) = v.get("n").and_then(Value::as_u64) else {
            return self.frame_refused(s, "malformed");
        };
        {
            let mut last = s.c2s.lock().unwrap_or_else(|e| e.into_inner());
            let expected = *last + 1;
            if n != expected {
                drop(last);
                let why = if n < expected {
                    "replayed"
                } else {
                    "sequence_gap"
                };
                return self.frame_refused(s, why);
            }
            *last = n;
        }
        let t = v.get("t").and_then(Value::as_str).unwrap_or("");
        let ready = s.ready.load(Ordering::SeqCst);
        match (t, ready) {
            ("hello", false) => self.on_hello(s, &v),
            ("hello", true) => self.frame_refused(s, "duplicate_hello"),
            (_, false) => self.frame_refused(s, "expected_hello"),
            ("ping", true) => self.on_alive(s),
            ("ack", true) => self.on_ack(s, &v),
            ("result", true) => self.on_result(s, &v),
            ("request", true) => self.on_request(s, &v),
            _ => self.frame_refused(s, "unknown_frame"),
        }
    }

    /// A valid frame arrived: the device is alive, so its leases are renewed. A revoked device is
    /// cut off.
    fn on_alive(&self, s: &Arc<LinkSession>) -> FrameOutcome {
        if self.touch_leases(&s.device) {
            FrameOutcome::Continue
        } else {
            FrameOutcome::Close("revoked".into())
        }
    }

    fn on_hello(&self, s: &Arc<LinkSession>, v: &Value) -> FrameOutcome {
        if v.get("device").and_then(Value::as_str) != Some(s.device.as_str()) {
            return self.frame_refused(s, "bad_hello");
        }
        let resume_seq = v.get("resume_seq").and_then(Value::as_u64).unwrap_or(0);
        if !self.touch_leases(&s.device) {
            return FrameOutcome::Close("revoked".into());
        }
        s.ready.store(true, Ordering::SeqCst);
        self.emit(
            "LINK_UP",
            json!({ "deviceId": s.device, "linkId": s.link_id, "resumeSeq": resume_seq }),
        );
        s.send(json!({
            "t": "welcome",
            "device": s.device,
            "link_id": s.link_id,
            "heartbeat_secs": HEARTBEAT_SECS,
        }));
        self.resume_tasks(s);
        self.dispatch();
        FrameOutcome::Continue
    }

    fn on_ack(&self, s: &Arc<LinkSession>, v: &Value) -> FrameOutcome {
        let (Some(id), Some(epoch)) = (
            v.get("id").and_then(Value::as_str),
            v.get("epoch").and_then(Value::as_u64),
        ) else {
            return self.frame_refused(s, "malformed");
        };
        if !self.touch_leases(&s.device) {
            return FrameOutcome::Close("revoked".into());
        }
        if let Err(code) = self.ack_task(s, id, epoch) {
            s.send(json!({
                "t": "settled", "id": id, "epoch": epoch, "state": "refused", "code": code,
            }));
        }
        FrameOutcome::Continue
    }

    fn on_result(&self, s: &Arc<LinkSession>, v: &Value) -> FrameOutcome {
        let (Some(id), Some(epoch), Some(ok)) = (
            v.get("id").and_then(Value::as_str),
            v.get("epoch").and_then(Value::as_u64),
            v.get("ok").and_then(Value::as_bool),
        ) else {
            return self.frame_refused(s, "malformed");
        };
        if !self.touch_leases(&s.device) {
            return FrameOutcome::Close("revoked".into());
        }
        let outcome = if ok {
            json!({ "ok": true, "result": v.get("result").cloned().unwrap_or(Value::Null) })
        } else {
            json!({ "ok": false, "error": v.get("error").cloned().unwrap_or(Value::Null) })
        };
        let (state, code) = self.complete_task(s, id, epoch, ok, outcome);
        let mut reply = json!({ "t": "settled", "id": id, "epoch": epoch, "state": state });
        if let Some(code) = code {
            reply["code"] = json!(code);
        }
        s.send(reply);
        FrameOutcome::Continue
    }

    /// A machine-initiated call: the ordinary device contract, run for this device.
    fn on_request(&self, s: &Arc<LinkSession>, v: &Value) -> FrameOutcome {
        let (Some(id), Some(method)) = (
            v.get("id").and_then(Value::as_str),
            v.get("method").and_then(Value::as_str),
        ) else {
            return self.frame_refused(s, "malformed");
        };
        if !self.touch_leases(&s.device) {
            return FrameOutcome::Close("revoked".into());
        }
        let (status, body) = match self.calls.as_ref() {
            None => (404, json!({ "error": { "code": "not_found" } })),
            Some(calls) => {
                let scopes = self
                    .lock()
                    .state
                    .devices
                    .get(&s.device)
                    .map(|d| d.scopes.clone())
                    .unwrap_or_default();
                let params = v.get("params").cloned().unwrap_or_else(|| json!({}));
                let bytes = serde_json::to_vec(&params).unwrap_or_default();
                calls.run(&s.device, scopes, method, &bytes)
            }
        };
        s.send(json!({ "t": "response", "id": id, "status": status, "body": body }));
        FrameOutcome::Continue
    }

    /// Number of live links and tasks per state, for status.
    pub(super) fn link_counts(&self) -> (u64, u64, u64) {
        let links = self.links.lock().unwrap_or_else(|e| e.into_inner()).len() as u64;
        let g = self.lock();
        let queued = g
            .state
            .tasks
            .values()
            .filter(|t| t.status == TaskStatus::Queued)
            .count() as u64;
        let leased = g
            .state
            .tasks
            .values()
            .filter(|t| t.status == TaskStatus::Leased)
            .count() as u64;
        (links, queued, leased)
    }
}