Skip to main content

openbutler_protocol/
backend.rs

1//! The client-side backend record: where a client points, and who it signs in
2//! as.
3//!
4//! This file never holds a credential. The session token lives in a separate
5//! private file, so this record can be read, copied, diffed, and printed
6//! without exposing anything. The split is the point: a target is
7//! shareable, a token is not.
8//!
9//! Every client (CLI, web, mobile) stores its own copy of this shape: a phone
10//! cannot read a laptop's file and must not need to. What is shared is the
11//! shape itself, defined here so the clients cannot drift apart. The daemon
12//! never reads it.
13
14use serde::{Deserialize, Serialize};
15
16/// The record's current `version` value.
17pub const BACKEND_TARGET_VERSION: u32 = 1;
18
19/// One stored backend: the address the client dials and the account it signs
20/// in as.
21///
22/// Ranges the writer is responsible for: `host` is an address or name of
23/// 1..=255 bytes, `port` is 1..=65535, `username` and `device` are 1..=64 JS
24/// string length units. `saved_at` is the client's own stamp (ISO-8601,
25/// millisecond, `Z`) and is informational.
26#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
27#[serde(rename_all = "camelCase")]
28pub struct BackendTarget {
29    /// [`BACKEND_TARGET_VERSION`]; a newer client may write a higher one.
30    pub version: u32,
31    /// The daemon's address (a tailnet IP, a loopback IP, or a name).
32    pub host: String,
33    /// The daemon's TCP port.
34    pub port: u16,
35    /// The account username the client signs in with.
36    pub username: String,
37    /// The client's own name for itself, sent as `login`'s `device` label.
38    pub device: String,
39    /// When the client wrote the record.
40    pub saved_at: String,
41}
42
43impl BackendTarget {
44    /// Builds a record at the current version.
45    pub fn new(
46        host: impl Into<String>,
47        port: u16,
48        username: impl Into<String>,
49        device: impl Into<String>,
50        saved_at: impl Into<String>,
51    ) -> Self {
52        Self {
53            version: BACKEND_TARGET_VERSION,
54            host: host.into(),
55            port,
56            username: username.into(),
57            device: device.into(),
58            saved_at: saved_at.into(),
59        }
60    }
61}