second-brain-sync 0.13.3

Bidirectional sync for second-brain: SSH transport, JSONL change log, conflict resolution
Documentation
use std::time::Duration;

use second_brain_core::keys::Envelope;
use serde::{Deserialize, Serialize};

use crate::protocol::HandshakeKeys;

mod channel;
mod code;
mod host;
mod join;

pub use channel::{Accepted, Channel, ChannelClosed, IO_TIMEOUT};
pub use code::{CODE_WORDS, CodeKind, HostSecrets, JoinCode, pairing_address};
pub use host::{HostOutcome, HostStatus, PairingHost};
pub use join::{Taken, fetch_keys, join, reachable};

/// The pairing listener's port when nothing else holds it.
pub const PAIR_PORT: u16 = 7201;
/// How long `sb pair` keeps a listener open.
pub const PAIR_LIFETIME: Duration = Duration::from_secs(10 * 60);
/// Wrong codes a listener takes before it retires its code for a new one.
pub const MAX_WRONG_TRIES: u32 = 3;
/// A file in the data dir that `sb setup` writes before the daemon first
/// starts when the user chose to join an identity. The daemon reads it once
/// at start: an empty store waits for its bootstrap instead of ingesting.
pub const JOIN_PENDING_FILE: &str = "pair-join-pending";

/// What the joining device sends once the code is proven.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct JoinRequest {
    pub machine_id: String,
    pub name: String,
    pub age_recipient: String,
    pub ssh_public_key: String,
    /// Hex of the ed25519 key that signs the rows this device authors.
    pub signing_public_key: String,
    #[serde(default)]
    pub ssh: Option<SshServer>,
    #[serde(default)]
    pub addresses: Vec<String>,
    pub sync_protocol: u32,
    pub schema_version: i64,
}

/// A device's sshd as its peers would reach it.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SshServer {
    pub login: Option<String>,
    pub port: u16,
    /// OpenSSH public key lines (`type base64`), ed25519 first.
    pub host_keys: Vec<String>,
}

impl SshServer {
    /// The host key a device row carries: the first, so ed25519 when the
    /// sshd has one.
    pub fn row_host_key(&self) -> Option<&str> {
        self.host_keys.first().map(String::as_str)
    }
}

/// The pairing device's answer to a proven join.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum HostReply {
    Admitted(Box<Admission>),
    Refused { reason: String },
    KeysHandedOver(Box<KeyHandover>),
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum KeyPurpose {
    RestoreKeys,
}

/// What a device restoring a backup sends a device running
/// `sb pair --restore-keys` once the code is proven. It asks to be admitted
/// to no identity.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct KeyRequest {
    pub purpose: KeyPurpose,
    pub name: String,
    pub age_recipient: String,
}

/// Every epoch of `K_id` the handing device holds, of any identity, wrapped
/// for the restoring device. No device row is written for it.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct KeyHandover {
    pub machine_id: String,
    pub name: String,
    pub envelopes: Vec<Envelope>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Admission {
    pub machine_id: String,
    pub name: String,
    /// The identity, every epoch wrapped for the joiner, the device list
    /// with the joiner's first row signed by this device, and the
    /// revocations behind the epochs.
    pub keys: HandshakeKeys,
    /// The recovery recipients this device trusts, which the joiner adds to
    /// its own trusted set.
    #[serde(default)]
    pub recovery_recipients: Vec<String>,
    #[serde(default)]
    pub ssh: Option<SshServer>,
    #[serde(default)]
    pub addresses: Vec<String>,
    pub sync_protocol: u32,
    pub schema_version: i64,
}

/// The joiner's last frame: whether it stored what it was sent.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum JoinerAck {
    Joined,
    Failed { reason: String },
}