store in client

This commit is contained in:
yves committed 2026-11-20 22:54:13 +01:00
1 parent 64ee239d1d
commit a520ddb557
10 files changed
+575 -347

No files matched your search

+51 -6
View File
@@ -20,6 +20,10 @@ struct Cli {
/// Password protecting the identity file.
#[arg(short, long, env = "MEOW_PASSWORD", hide_env_values = true, global = true)]
password: Option<String>,
/// Hidden-space password(s) to try after opening (repeatable). A password that opens
/// nothing is silently ignored.
#[arg(short = 'u', long = "unlock", global = true)]
unlock: Vec<String>,
/// Use very weak key derivation (tests only!).
#[arg(long, env = "MEOW_INSECURE_FAST_KDF", hide = true, global = true)]
insecure_fast_kdf: bool,
@@ -87,6 +91,11 @@ enum IdentityCmd {
},
/// Show the identity summary.
Show,
/// Change the main password.
Password {
#[arg(long)]
new_password: String,
},
}
#[derive(Subcommand)]
@@ -104,8 +113,23 @@ enum ServerCmd {
#[derive(Subcommand)]
enum ContactCmd {
List,
Show { name: String },
Remove { name: String },
Show {
name: String,
},
Remove {
name: String,
},
/// Move a contact (with its history) into the hidden space opened by a password
/// (created if needed). Several contacts may share one.
Hide {
name: String,
#[arg(long)]
space_password: String,
},
/// Move a hidden contact (unlock its space with -u) back to the main space.
Reveal {
name: String,
},
}
#[derive(Args)]
@@ -186,15 +210,20 @@ fn default_identity() -> PathBuf {
struct Ctx {
identity: PathBuf,
password: String,
unlock: Vec<String>,
kdf: KdfParams,
downloads: PathBuf,
}
impl Ctx {
async fn open(&self) -> Result<MeowClient> {
MeowClient::open(&self.identity, &self.password, self.kdf)
let mut c = MeowClient::open(&self.identity, &self.password)
.await
.with_context(|| format!("cannot open identity {}", self.identity.display()))
.with_context(|| format!("cannot open identity {}", self.identity.display()))?;
for pw in &self.unlock {
c.unlock(pw).await?;
}
Ok(c)
}
}
@@ -223,6 +252,7 @@ async fn run() -> Result<()> {
let ctx = Ctx {
identity: cli.identity.unwrap_or_else(default_identity),
password,
unlock: cli.unlock,
kdf: if cli.insecure_fast_kdf {
KdfParams::INSECURE_TEST
} else {
@@ -245,6 +275,10 @@ async fn run() -> Result<()> {
println!("servers: {}", c.servers().len());
println!("contacts: {}", c.contacts().len());
}
Cmd::Identity(IdentityCmd::Password { new_password }) => {
ctx.open().await?.change_password(&new_password).await?;
println!("password changed");
}
Cmd::Server(ServerCmd::Add { url }) => {
let mut c = ctx.open().await?;
let s = c.add_server(&url).await?;
@@ -263,7 +297,8 @@ async fn run() -> Result<()> {
Cmd::Contact(ContactCmd::List) => {
let c = ctx.open().await?;
for k in c.contacts() {
println!("{}\t{:?}", k.name, k.state);
let hidden = if c.is_hidden(&k.name)? { "\t[hidden]" } else { "" };
println!("{}\t{:?}{hidden}", k.name, k.state);
}
}
Cmd::Contact(ContactCmd::Show { name }) => {
@@ -300,6 +335,14 @@ async fn run() -> Result<()> {
ctx.open().await?.remove_contact(&name).await?;
println!("removed {name}");
}
Cmd::Contact(ContactCmd::Hide { name, space_password }) => {
ctx.open().await?.hide_contact(&name, &space_password).await?;
println!("{name} is now hidden");
}
Cmd::Contact(ContactCmd::Reveal { name }) => {
ctx.open().await?.reveal_contact(&name).await?;
println!("{name} is now visible");
}
Cmd::Invitation(cmd) => invitation(&ctx, cmd).await?,
Cmd::ServerInvitation(ServerInvitationCmd::Create {
contact,
@@ -455,7 +498,9 @@ fn print_events(ctx: &Ctx, events: &[Event]) -> Result<()> {
for e in events {
match e {
Event::Message { contact, text, .. } => println!("[{contact}] {text}"),
Event::File { contact, name, data } => {
Event::File {
contact, name, data, ..
} => {
std::fs::create_dir_all(&ctx.downloads)?;
let path = ctx.downloads.join(safe_name(name));
std::fs::write(&path, data)?;
+1 -2
View File
@@ -9,11 +9,10 @@ license.workspace = true
meow-core = { path = "../meow-core" }
meow-proto = { path = "../meow-proto" }
meow-crypto = { path = "../meow-crypto" }
meow-store = { path = "../meow-store" }
reqwest = { version = "0.12", default-features = false, features = ["rustls-tls"] }
tokio = { version = "1", features = ["fs", "time", "macros"] }
futures-util = "0.3"
serde = { version = "1", features = ["derive"] }
postcard = { version = "1", default-features = false, features = ["alloc"] }
thiserror = "2"
tracing = "0.1"
+380 -218
View File
@@ -1,20 +1,26 @@
//! High-level async client API for Meow v2.
//!
//! [`MeowClient`] owns one identity (profile) and exposes servers, contacts,
//! invitations, messaging and synchronisation. It is the API mirrored by the CLI
//! and, later, by the Flutter FFI layer (`meow-ffi`).
//! [`MeowClient`] owns one identity store ([`meow_store`]) and exposes servers,
//! contacts, invitations, messaging and synchronisation. It is the API mirrored by the
//! CLI and, later, by the Flutter FFI layer (`meow-ffi`).
//!
//! **Spaces.** The main password opens the main space (nickname, servers, visible
//! contacts). [`MeowClient::unlock`] opens hidden spaces, each holding one or several
//! contacts; their contacts then behave like the others (listed, polled, usable) until
//! the client is dropped. Only spaces that changed are written back, so an unused
//! hidden space is never touched on disk.
//!
//! Invariant: any ratchet/contact state change is **persisted before** the
//! resulting bytes leave the device, so a crash can never make us reuse a key.
//! resulting bytes leave the device (or the server is told to delete a message), so
//! a crash can never make us reuse a key or lose a message.
#![forbid(unsafe_code)]
#![cfg_attr(not(test), warn(clippy::unwrap_used, clippy::expect_used))]
pub mod profile;
pub mod transport;
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use std::path::Path;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use futures_util::future::join_all;
@@ -26,10 +32,10 @@ use meow_crypto::password::KdfParams;
use meow_crypto::{SignPublic, SigningKeyPair};
use meow_proto::v2::{self, request::Body as Req, response::Body as Resp};
use meow_proto::Message;
use meow_store::{Batch, RetiredMailbox, SpaceId, State as SpaceState, Store};
pub use meow_core::contact::ContactState as State;
use profile::RetiredMailbox;
pub use profile::{Content, HistoryEntry, PendingAck};
pub use meow_store::{Content, HistoryEntry, HistoryPage, PendingAck};
/// Maximum file size sent in a single message (chunking: roadmap 5.7).
pub const MAX_FILE_BYTES: usize = 4 * 1024 * 1024;
@@ -38,10 +44,12 @@ pub const DEFAULT_INVITATION_TTL: u64 = 7 * 86_400;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("wrong password or corrupted identity file")]
#[error("wrong password")]
BadPassword,
#[error("corrupted identity file: {0}")]
Corrupted(String),
#[error("identity {0} is in use by another process")]
Locked(String),
#[error("storage: {0}")]
Store(meow_store::Error),
#[error("i/o: {0}")]
Io(#[from] std::io::Error),
#[error("network: {0}")]
@@ -60,8 +68,18 @@ pub enum Error {
NameTaken(String),
#[error("{0}")]
Invalid(String),
#[error("identity {0} is in use by another process")]
Locked(String),
}
impl From<meow_store::Error> for Error {
fn from(e: meow_store::Error) -> Self {
match e {
meow_store::Error::BadPassword => Error::BadPassword,
meow_store::Error::Locked(p) => Error::Locked(p),
meow_store::Error::Exists(p) => Error::Invalid(format!("{p} already exists (use --force)")),
meow_store::Error::Io(e) => Error::Io(e),
other => Error::Store(other),
}
}
}
pub type Result<T> = std::result::Result<T, Error>;
@@ -94,8 +112,10 @@ pub enum Event {
text: String,
sent_at_ms: u64,
},
/// A file was received (its content is also kept: [`MeowClient::file`]).
File {
contact: String,
id: Vec<u8>,
name: String,
data: Vec<u8>,
},
@@ -109,85 +129,195 @@ pub struct SendReport {
pub delivered_to: usize,
}
/// One identity, with its servers, contacts and history.
/// Acknowledged ids sent per mailbox and pull (server limit: 1024).
const MAX_ACKS_PER_MAILBOX: usize = 512;
#[derive(Default)]
struct PullRound {
/// Messages to process.
new: Vec<v2::StoredMessage>,
/// Messages to process, with the index of the space owning their mailbox.
new: Vec<(usize, v2::StoredMessage)>,
/// A server held messages back.
more: bool,
}
pub struct MeowClient {
path: PathBuf,
password: String,
kdf: KdfParams,
profile: profile::Profile,
transport: transport::Transport,
/// Exclusive OS lock on `<identity>.lock`, held for the client's lifetime:
/// two processes saving the same ratchet state would roll sessions back.
_lock: std::fs::File,
/// One unlocked space, in memory.
struct Space {
id: SpaceId,
state: SpaceState,
/// History and files waiting for the next save of this space.
batch: Batch,
/// Changed since the last save.
dirty: bool,
}
fn lock_identity(path: &Path) -> Result<std::fs::File> {
let mut lock_path = path.as_os_str().to_owned();
lock_path.push(".lock");
let f = std::fs::OpenOptions::new()
.create(true)
.truncate(false)
.write(true)
.open(PathBuf::from(lock_path))?;
match f.try_lock() {
Ok(()) => Ok(f),
Err(std::fs::TryLockError::WouldBlock) => Err(Error::Locked(path.display().to_string())),
Err(std::fs::TryLockError::Error(e)) => Err(Error::Io(e)),
}
/// One identity: its store and unlocked spaces. Index 0 is always the main space.
pub struct MeowClient {
/// Behind a mutex only to make the client `Sync` (SQLite connections are not);
/// never held across an `.await`.
store: std::sync::Mutex<Store>,
spaces: Vec<Space>,
transport: transport::Transport,
}
type Mailboxes = Vec<(SigningKeyPair, Vec<ServerInfo>)>;
fn lock(store: &std::sync::Mutex<Store>) -> Result<std::sync::MutexGuard<'_, Store>> {
store.lock().map_err(|_| Error::Invalid("store lock poisoned".into()))
}
impl MeowClient {
// ───────────────────────── lifecycle ─────────────────────────
/// Create a new identity file. Fails if it exists, unless `overwrite`.
/// Create a new identity. Fails if `path` exists, unless `overwrite`.
pub async fn create(path: &Path, nickname: &str, password: &str, kdf: KdfParams, overwrite: bool) -> Result<Self> {
if !overwrite && path.exists() {
return Err(Error::Invalid(format!(
"{} already exists (use --force)",
path.display()
)));
}
let c = Self {
path: path.to_path_buf(),
password: password.to_string(),
kdf,
profile: profile::Profile::new(nickname),
transport: transport::Transport::new()?,
_lock: lock_identity(path)?,
};
c.save().await?;
Ok(c)
let store = Store::create(path, nickname, password, kdf, overwrite)?;
Self::with_store(store)
}
/// Open an existing identity file. `kdf` is used for subsequent saves.
pub async fn open(path: &Path, password: &str, kdf: KdfParams) -> Result<Self> {
/// Open an existing identity with its main password.
pub async fn open(path: &Path, password: &str) -> Result<Self> {
Self::with_store(Store::open(path, password)?)
}
fn with_store(mut store: Store) -> Result<Self> {
let state = store.load(SpaceId::MAIN)?;
Ok(Self {
path: path.to_path_buf(),
password: password.to_string(),
kdf,
_lock: lock_identity(path)?,
profile: profile::Profile::load(path, password).await?,
store: std::sync::Mutex::new(store),
spaces: vec![Space {
id: SpaceId::MAIN,
state,
batch: Batch::default(),
dirty: false,
}],
transport: transport::Transport::new()?,
})
}
pub async fn save(&self) -> Result<()> {
self.profile.save(&self.path, &self.password, self.kdf).await
/// Write every changed space.
pub async fn save(&mut self) -> Result<()> {
for i in 0..self.spaces.len() {
if self.spaces[i].dirty {
self.save_space(i)?;
}
}
Ok(())
}
fn save_space(&mut self, i: usize) -> Result<()> {
let sp = &mut self.spaces[i];
let batch = std::mem::take(&mut sp.batch);
lock(&self.store)?.save(sp.id, &sp.state, batch)?;
sp.dirty = false;
Ok(())
}
/// Change the main password (instantaneous: only the space root moves).
pub async fn change_password(&mut self, new_password: &str) -> Result<()> {
Ok(lock(&self.store)?.change_password(SpaceId::MAIN, new_password)?)
}
pub fn nickname(&self) -> &str {
&self.profile.nickname
&self.main().nickname
}
fn main(&self) -> &SpaceState {
&self.spaces[0].state
}
// ───────────────────────── hidden spaces ─────────────────────────
/// Try a hidden-space password; `true` if it opened one (its contacts are now
/// available). Same cost whether or not such a space exists.
pub async fn unlock(&mut self, password: &str) -> Result<bool> {
let found = lock(&self.store)?.unlock(password)?;
match found {
Some(id) => {
self.space_index(id)?;
Ok(true)
}
None => Ok(false),
}
}
/// Index of an unlocked space in `self.spaces`, loading it if needed.
fn space_index(&mut self, id: SpaceId) -> Result<usize> {
if let Some(i) = self.spaces.iter().position(|s| s.id == id) {
return Ok(i);
}
let state = lock(&self.store)?.load(id)?;
self.spaces.push(Space {
id,
state,
batch: Batch::default(),
dirty: false,
});
Ok(self.spaces.len() - 1)
}
/// Move a contact, with its history and files, into the hidden space opened by
/// `password` (created if none). Several contacts may share a space.
pub async fn hide_contact(&mut self, name: &str, password: &str) -> Result<()> {
let found = lock(&self.store)?.unlock(password)?;
let id = match found {
Some(id) => id,
None => lock(&self.store)?.create_space(password)?,
};
if id == SpaceId::MAIN {
return Err(Error::Invalid("this is the main password".into()));
}
let target = self.space_index(id)?;
self.move_contact(name, target)
}
/// Move a hidden contact back to the main space.
pub async fn reveal_contact(&mut self, name: &str) -> Result<()> {
self.move_contact(name, 0)
}
/// Whether a contact lives in a hidden space.
pub fn is_hidden(&self, name: &str) -> Result<bool> {
Ok(self.locate(name)?.0 != 0)
}
fn move_contact(&mut self, name: &str, target: usize) -> Result<()> {
let (si, ci) = self.locate(name)?;
if si == target {
return Ok(());
}
let contact = self.spaces[si].state.contacts[ci].clone();
let src = self.spaces[si].id;
let history = lock(&self.store)?.full_history(src, &contact.id)?;
let mut files = Vec::new();
for h in history.iter().filter(|h| !h.outbound) {
if matches!(h.content, Content::File { .. }) {
if let Some(data) = lock(&self.store)?.file(src, &contact.id, &h.id)? {
files.push((contact.id.clone(), h.id.clone(), data));
}
}
}
let mailboxes: Vec<Vec<u8>> = contact
.mailboxes()
.iter()
.map(|(kp, _)| kp.public().as_bytes().to_vec())
.collect();
let acks: Vec<PendingAck> = self.spaces[si]
.state
.pending_acks
.iter()
.filter(|a| mailboxes.contains(&a.mailbox))
.cloned()
.collect();
// Write the destination first: a crash in between leaves a copy, never a loss.
let t = &mut self.spaces[target];
t.state.contacts.push(contact);
t.state.pending_acks.extend(acks);
t.batch.history.extend(history);
t.batch.files.extend(files);
self.save_space(target)?;
let s = &mut self.spaces[si];
s.state.contacts.remove(ci);
s.state.pending_acks.retain(|a| !mailboxes.contains(&a.mailbox));
self.save_space(si)
}
// ───────────────────────── servers ─────────────────────────
@@ -199,7 +329,7 @@ impl MeowClient {
tracing::warn!("server announces {} (contacted as {url})", info.url);
}
if let Some(known) = self
.profile
.main()
.servers
.iter()
.find(|s| s.url == info.url || s.sign_key == info.sign_key)
@@ -209,38 +339,39 @@ impl MeowClient {
}
return Ok(known.clone());
}
self.profile.servers.push(info.clone());
self.save().await?;
self.spaces[0].state.servers.push(info.clone());
self.save_space(0)?;
Ok(info)
}
pub fn servers(&self) -> &[ServerInfo] {
&self.profile.servers
&self.main().servers
}
pub async fn remove_server(&mut self, url_or_name: &str) -> Result<()> {
let u = canonical_url(url_or_name);
let before = self.profile.servers.len();
self.profile.servers.retain(|s| s.url != u && s.name != url_or_name);
if self.profile.servers.len() == before {
let servers = &mut self.spaces[0].state.servers;
let before = servers.len();
servers.retain(|s| s.url != u && s.name != url_or_name);
if servers.len() == before {
return Err(Error::UnknownServer(url_or_name.to_string()));
}
self.save().await
self.save_space(0)
}
/// Resolve URLs to known servers; an empty list means "all my servers".
fn resolve_servers(&self, urls: &[String]) -> Result<Vec<ServerInfo>> {
let servers = &self.main().servers;
if urls.is_empty() {
if self.profile.servers.is_empty() {
if servers.is_empty() {
return Err(Error::Invalid("no server configured: add one first".into()));
}
return Ok(self.profile.servers.clone());
return Ok(servers.clone());
}
urls.iter()
.map(|u| {
let cu = canonical_url(u);
self.profile
.servers
servers
.iter()
.find(|s| s.url == cu || s.name == *u)
.cloned()
@@ -251,48 +382,68 @@ impl MeowClient {
// ───────────────────────── contacts ─────────────────────────
pub fn contacts(&self) -> &[Contact] {
&self.profile.contacts
/// Contacts of every unlocked space (main space first).
pub fn contacts(&self) -> Vec<&Contact> {
self.spaces.iter().flat_map(|s| s.state.contacts.iter()).collect()
}
pub fn contact(&self, name: &str) -> Result<&Contact> {
self.profile
.contacts
.iter()
.find(|c| c.name == name)
let (si, ci) = self.locate(name)?;
Ok(&self.spaces[si].state.contacts[ci])
}
/// (space index, contact index) of a contact.
fn locate(&self, name: &str) -> Result<(usize, usize)> {
self.find(|c| c.name == name)
.ok_or_else(|| Error::UnknownContact(name.to_string()))
}
fn contact_index(&self, name: &str) -> Result<usize> {
self.profile
.contacts
fn find(&self, pred: impl Fn(&Contact) -> bool) -> Option<(usize, usize)> {
self.spaces
.iter()
.position(|c| c.name == name)
.ok_or_else(|| Error::UnknownContact(name.to_string()))
.enumerate()
.find_map(|(si, s)| s.state.contacts.iter().position(&pred).map(|ci| (si, ci)))
}
fn ensure_free_name(&self, name: &str) -> Result<()> {
if self.profile.contacts.iter().any(|c| c.name == name) {
if self.find(|c| c.name == name).is_some() {
return Err(Error::NameTaken(name.to_string()));
}
Ok(())
}
/// Delete a contact with its history and files.
pub async fn remove_contact(&mut self, name: &str) -> Result<()> {
let i = self.contact_index(name)?;
let before = self.contact_mailboxes();
let c = self.profile.contacts.remove(i);
self.profile.history.retain(|h| h.contact != c.id);
self.retire_mailboxes(before);
self.save().await
let (si, ci) = self.locate(name)?;
let before = self.space_mailboxes(si);
self.spaces[si].state.contacts.remove(ci);
self.retire_mailboxes(si, before);
self.save_space(si)
}
pub fn history(&self, name: &str) -> Result<Vec<&HistoryEntry>> {
let id = &self.contact(name)?.id;
Ok(self.profile.history.iter().filter(|h| &h.contact == id).collect())
/// Whole conversation with a contact, oldest first.
pub fn history(&self, name: &str) -> Result<Vec<HistoryEntry>> {
let (si, ci) = self.locate(name)?;
let sp = &self.spaces[si];
Ok(lock(&self.store)?.full_history(sp.id, &sp.state.contacts[ci].id)?)
}
/// One segment of a conversation (`None`: the latest), oldest first.
pub fn history_page(&self, name: &str, page: Option<usize>) -> Result<HistoryPage> {
let (si, ci) = self.locate(name)?;
let sp = &self.spaces[si];
Ok(lock(&self.store)?.history(sp.id, &sp.state.contacts[ci].id, page)?)
}
/// Content of a received file.
pub fn file(&self, name: &str, message_id: &[u8]) -> Result<Option<Vec<u8>>> {
let (si, ci) = self.locate(name)?;
let sp = &self.spaces[si];
Ok(lock(&self.store)?.file(sp.id, &sp.state.contacts[ci].id, message_id)?)
}
// ───────────────────────── invitations ─────────────────────────
// New contacts are created in the main space; hide them afterwards if needed.
/// Step 1: create an invitation for `contact_name`. Returns the encoded offer.
pub async fn create_invitation(
@@ -304,9 +455,9 @@ impl MeowClient {
) -> Result<Vec<u8>> {
self.ensure_free_name(contact_name)?;
let servers = self.resolve_servers(server_urls)?;
let offer = invitation::create_offer(contact_name, &self.profile.nickname, servers, message, now(), ttl)?;
self.profile.contacts.push(offer.contact);
self.save().await?;
let offer = invitation::create_offer(contact_name, self.nickname(), servers, message, now(), ttl)?;
self.spaces[0].state.contacts.push(offer.contact);
self.save_space(0)?;
Ok(offer.offer_bytes)
}
@@ -324,51 +475,40 @@ impl MeowClient {
let info = self.inspect_invitation(offer_bytes)?;
let name = contact_name.unwrap_or(&info.initiator_name).to_string();
self.ensure_free_name(&name)?;
if self
.profile
.contacts
.iter()
.any(|c| c.invitation_id == info.invitation_id)
{
if self.find(|c| c.invitation_id == info.invitation_id).is_some() {
return Err(Error::Invalid("this invitation was already answered".into()));
}
let servers = self.resolve_servers(server_urls)?;
let (contact, answer) = invitation::answer(offer_bytes, &name, &self.profile.nickname, servers, now())?;
self.profile.contacts.push(contact);
self.save().await?;
let (contact, answer) = invitation::answer(offer_bytes, &name, self.nickname(), servers, now())?;
self.spaces[0].state.contacts.push(contact);
self.save_space(0)?;
Ok(answer.encode_to_vec())
}
fn by_invitation(&mut self, id: &[u8]) -> Result<&mut Contact> {
self.profile
.contacts
.iter_mut()
.find(|c| c.invitation_id.as_slice() == id)
fn by_invitation(&self, id: &[u8]) -> Result<(usize, usize)> {
self.find(|c| c.invitation_id.as_slice() == id)
.ok_or_else(|| Error::Invalid("no pending invitation matches".into()))
}
/// Step 3 (file flow): process an answer, returns the encoded confirmation.
pub async fn accept_answer(&mut self, answer_bytes: &[u8]) -> Result<Vec<u8>> {
let msg = v2::InvitationMessage::decode(answer_bytes).map_err(meow_core::Error::from)?;
let contact = self.by_invitation(&msg.invitation_id)?;
let confirm = invitation::accept(contact, &msg, now())?;
self.save().await?;
let (si, ci) = self.by_invitation(&msg.invitation_id)?;
let confirm = invitation::accept(&mut self.spaces[si].state.contacts[ci], &msg, now())?;
self.save_space(si)?;
Ok(confirm.encode_to_vec())
}
/// Step 4 (file flow): process a confirmation and send the ack through the network.
pub async fn finalize_invitation(&mut self, confirm_bytes: &[u8]) -> Result<String> {
let msg = v2::InvitationMessage::decode(confirm_bytes).map_err(meow_core::Error::from)?;
let contact = self.by_invitation(&msg.invitation_id)?;
let (si, ci) = self.by_invitation(&msg.invitation_id)?;
let contact = &mut self.spaces[si].state.contacts[ci];
let ack = invitation::finalize(contact, &msg)?;
let deposit = messaging::deposit_for(contact, &ack)?;
let servers = contact
.peer
.as_ref()
.map(|p| p.pull_servers.clone())
.unwrap_or_default();
let servers = peer_servers(contact);
let name = contact.name.clone();
self.save().await?;
self.save_space(si)?;
self.deliver(&servers, vec![deposit]).await?;
Ok(name)
}
@@ -473,16 +613,13 @@ impl MeowClient {
}
async fn send(&mut self, contact_name: &str, msg: v2::UserMessage, content: Content) -> Result<SendReport> {
let i = self.contact_index(contact_name)?;
let contact = &mut self.profile.contacts[i];
let (si, ci) = self.locate(contact_name)?;
let sp = &mut self.spaces[si];
let contact = &mut sp.state.contacts[ci];
let env = messaging::encrypt(contact, &msg)?;
let deposit = messaging::deposit_for(contact, &env)?;
let servers = contact
.peer
.as_ref()
.map(|p| p.pull_servers.clone())
.unwrap_or_default();
self.profile.history.push(HistoryEntry {
let servers = peer_servers(contact);
sp.batch.history.push(HistoryEntry {
id: msg.id.clone(),
contact: contact.id.clone(),
outbound: true,
@@ -490,7 +627,7 @@ impl MeowClient {
received_at: now(),
content,
});
self.save().await?;
self.save_space(si)?;
let delivered_to = self.deliver(&servers, vec![deposit]).await?;
Ok(SendReport {
message_id: msg.id,
@@ -532,9 +669,10 @@ impl MeowClient {
// ───────────────────────── sync ─────────────────────────
/// Pull every mailbox on every server (one request per server), process what
/// arrived and return the resulting events. With `wait > 0` the servers hold the
/// request until something arrives or the delay expires (long-poll).
/// Pull every mailbox of every unlocked space on every server (one request per
/// server), process what arrived and return the resulting events. With `wait > 0`
/// the servers hold the request until something arrives or the delay expires
/// (long-poll).
///
/// Delivery is at-least-once: servers keep messages until they are acknowledged,
/// and a message is acknowledged (in the next pull) only once its effects are saved.
@@ -569,43 +707,49 @@ impl MeowClient {
}
/// One pull per server, carrying the pending acks. New (not yet processed) messages
/// are returned and recorded as pending acks; they are saved by [`Self::process`].
/// are returned and recorded as pending acks of the space owning their mailbox;
/// they are saved by [`Self::process`].
async fn pull_round(&mut self, wait: Duration) -> Result<PullRound> {
// server (by signing key) → (server, mailbox keys)
let pending = &self.profile.pending_acks;
self.profile.retired_mailboxes.retain(|r| {
pending
// server (by signing key) → (server, [(space index, mailbox key)])
let mut jobs: BTreeMap<SignPublic, (ServerInfo, Vec<(usize, SigningKeyPair)>)> = BTreeMap::new();
for si in 0..self.spaces.len() {
let st = &mut self.spaces[si].state;
let pending = &st.pending_acks;
st.retired_mailboxes.retain(|r| {
pending
.iter()
.any(|a| a.mailbox.as_slice() == r.key.public().as_bytes().as_slice())
});
let retired: Mailboxes = self.spaces[si]
.state
.retired_mailboxes
.iter()
.any(|a| a.mailbox.as_slice() == r.key.public().as_bytes().as_slice())
});
let mut jobs: BTreeMap<SignPublic, (ServerInfo, Vec<SigningKeyPair>)> = BTreeMap::new();
let retired = self
.profile
.retired_mailboxes
.iter()
.map(|r| (r.key.clone(), r.servers.clone()));
for (kp, servers) in self.contact_mailboxes().into_iter().chain(retired) {
for s in servers {
jobs.entry(s.sign_key)
.or_insert_with(|| (s.clone(), Vec::new()))
.1
.push(kp.clone());
.map(|r| (r.key.clone(), r.servers.clone()))
.collect();
for (kp, servers) in self.space_mailboxes(si).into_iter().chain(retired) {
for s in servers {
jobs.entry(s.sign_key)
.or_insert_with(|| (s.clone(), Vec::new()))
.1
.push((si, kp.clone()));
}
}
}
// Drop acks nobody can send any more (contact or server removed).
self.profile.pending_acks.retain(|a| {
jobs.get(&a.server).is_some_and(|(_, kps)| {
kps.iter()
.any(|kp| kp.public().as_bytes().as_slice() == a.mailbox.as_slice())
})
});
for (si, sp) in self.spaces.iter_mut().enumerate() {
sp.state.pending_acks.retain(|a| {
jobs.get(&a.server).is_some_and(|(_, kps)| {
kps.iter()
.any(|(s, kp)| *s == si && kp.public().as_bytes().as_slice() == a.mailbox.as_slice())
})
});
}
let mut round = PullRound::default();
if jobs.is_empty() {
return Ok(round);
}
let wait_s = wait.as_secs().min(u32::MAX as u64) as u32;
let pending = &self.profile.pending_acks;
let mut sent_acks: Vec<Vec<PendingAck>> = Vec::with_capacity(jobs.len());
let mut sent_acks: Vec<Vec<(usize, PendingAck)>> = Vec::with_capacity(jobs.len());
let mut requests = Vec::with_capacity(jobs.len());
for (server, kps) in jobs.values() {
let rid = meow_crypto::random_bytes::<16>().to_vec();
@@ -613,13 +757,15 @@ impl MeowClient {
let mut sent = Vec::new();
let auths = kps
.iter()
.map(|kp| {
.map(|(si, kp)| {
let mb = kp.public();
let acks: Vec<Vec<u8>> = pending
let acks: Vec<Vec<u8>> = self.spaces[*si]
.state
.pending_acks
.iter()
.filter(|a| a.server == server.sign_key && a.mailbox.as_slice() == mb.as_bytes().as_slice())
.take(MAX_ACKS_PER_MAILBOX)
.inspect(|a| sent.push((*a).clone()))
.inspect(|a| sent.push((*si, (*a).clone())))
.map(|a| a.id.clone())
.collect();
rpc::mailbox_auth(kp, server, &rid, acks, ts)
@@ -643,23 +789,34 @@ impl MeowClient {
let mut failures = 0;
let mut last_err = None;
for (((server, _), r), sent) in jobs.values().zip(results).zip(sent_acks) {
for (((server, kps), r), sent) in jobs.values().zip(results).zip(sent_acks) {
match r {
Ok(Resp::PullResult(p)) => {
// The server applied our acks before answering. Forgetting them
// needs no save: at worst they are sent again (acks are idempotent).
self.profile.pending_acks.retain(|a| !sent.contains(a));
for (si, a) in &sent {
self.spaces[*si].state.pending_acks.retain(|x| x != a);
}
round.more |= p.more;
for m in p.messages {
let Some((si, _)) = kps
.iter()
.find(|(_, kp)| kp.public().as_bytes().as_slice() == m.mailbox.as_slice())
else {
tracing::debug!("server sent a message for a mailbox we did not pull");
continue;
};
let ack = PendingAck {
server: server.sign_key,
mailbox: m.mailbox.clone(),
id: m.id.clone(),
};
let sp = &mut self.spaces[*si];
// Already processed (its ack was lost): only re-acknowledge.
if !self.profile.pending_acks.contains(&ack) {
self.profile.pending_acks.push(ack);
round.new.push(m);
if !sp.state.pending_acks.contains(&ack) {
sp.state.pending_acks.push(ack);
sp.dirty = true;
round.new.push((*si, m));
}
}
}
@@ -677,9 +834,10 @@ impl MeowClient {
Ok(round)
}
/// Every mailbox currently used by a contact, with its pull servers.
fn contact_mailboxes(&self) -> Vec<(SigningKeyPair, Vec<ServerInfo>)> {
self.profile
/// Every mailbox currently used by a contact of a space, with its pull servers.
fn space_mailboxes(&self, si: usize) -> Mailboxes {
self.spaces[si]
.state
.contacts
.iter()
.flat_map(|c| c.mailboxes())
@@ -687,62 +845,58 @@ impl MeowClient {
.collect()
}
/// Keep the keys of mailboxes that disappeared (`before` → now) while they still
/// have pending acks, so the next pull can acknowledge their last messages.
fn retire_mailboxes(&mut self, before: Vec<(SigningKeyPair, Vec<ServerInfo>)>) {
let current: Vec<SignPublic> = self.contact_mailboxes().iter().map(|(kp, _)| kp.public()).collect();
/// Keep the keys of mailboxes that disappeared from a space (`before` → now) while
/// they still have pending acks, so the next pull can acknowledge their last messages.
fn retire_mailboxes(&mut self, si: usize, before: Mailboxes) {
let current: Vec<SignPublic> = self.space_mailboxes(si).iter().map(|(kp, _)| kp.public()).collect();
let st = &mut self.spaces[si].state;
for (key, servers) in before {
let pk = key.public();
let has_acks = self
.profile
let has_acks = st
.pending_acks
.iter()
.any(|a| a.mailbox.as_slice() == pk.as_bytes().as_slice());
let known = self.profile.retired_mailboxes.iter().any(|r| r.key.public() == pk);
let known = st.retired_mailboxes.iter().any(|r| r.key.public() == pk);
if has_acks && !known && !current.contains(&pk) {
self.profile.retired_mailboxes.push(RetiredMailbox { key, servers });
st.retired_mailboxes.push(RetiredMailbox { key, servers });
}
}
}
async fn process(&mut self, stored: Vec<v2::StoredMessage>) -> Result<Vec<Event>> {
let mailboxes_before = self.contact_mailboxes();
async fn process(&mut self, stored: Vec<(usize, v2::StoredMessage)>) -> Result<Vec<Event>> {
let mailboxes_before: Vec<Mailboxes> = (0..self.spaces.len()).map(|si| self.space_mailboxes(si)).collect();
// Handshake messages first (step 2 before step 3), then ratchet messages by arrival.
let mut envs: Vec<(u32, u64, Vec<u8>, v2::PeerEnvelope)> = stored
let mut envs: Vec<(u32, u64, usize, Vec<u8>, v2::PeerEnvelope)> = stored
.into_iter()
.filter_map(|m| {
.filter_map(|(si, m)| {
let env = v2::PeerEnvelope::decode(m.payload.as_slice()).ok()?;
let rank = match &env.body {
Some(v2::peer_envelope::Body::Invitation(i)) => i.step,
_ => 10,
};
Some((rank, m.received_at, m.mailbox, env))
Some((rank, m.received_at, si, m.mailbox, env))
})
.collect();
envs.sort_by_key(|(rank, at, _, _)| (*rank, *at));
envs.sort_by_key(|(rank, at, _, _, _)| (*rank, *at));
let mut events = Vec::new();
let mut outgoing: Vec<(Vec<ServerInfo>, v2::Deposit)> = Vec::new();
for (_, received_at, mailbox, env) in envs {
let Some(i) = self.profile.contacts.iter().position(|c| {
for (_, received_at, si, mailbox, env) in envs {
let sp = &mut self.spaces[si];
let Some(ci) = sp.state.contacts.iter().position(|c| {
c.mailboxes()
.iter()
.any(|(kp, _)| kp.public().as_bytes().as_slice() == mailbox.as_slice())
}) else {
tracing::debug!("message for an unknown mailbox");
tracing::debug!("message for a retired mailbox");
continue;
};
let contact = &mut self.profile.contacts[i];
let contact = &mut sp.state.contacts[ci];
match env.body {
Some(v2::peer_envelope::Body::Invitation(inv)) if inv.step == 2 => {
match invitation::accept(contact, &inv, now()) {
Ok(confirm) => {
let servers = contact
.peer
.as_ref()
.map(|p| p.pull_servers.clone())
.unwrap_or_default();
outgoing.push((servers, invitation::confirm_deposit(contact, &confirm)?));
outgoing.push((peer_servers(contact), invitation::confirm_deposit(contact, &confirm)?));
events.push(Event::InvitationAccepted {
contact: contact.name.clone(),
});
@@ -753,12 +907,7 @@ impl MeowClient {
Some(v2::peer_envelope::Body::Invitation(inv)) if inv.step == 3 => {
match invitation::finalize(contact, &inv) {
Ok(ack) => {
let servers = contact
.peer
.as_ref()
.map(|p| p.pull_servers.clone())
.unwrap_or_default();
outgoing.push((servers, messaging::deposit_for(contact, &ack)?));
outgoing.push((peer_servers(contact), messaging::deposit_for(contact, &ack)?));
events.push(Event::ContactActivated {
contact: contact.name.clone(),
});
@@ -781,19 +930,17 @@ impl MeowClient {
if was != ContactState::Active && contact.state == ContactState::Active {
events.push(Event::ContactActivated { contact: name.clone() });
}
let mut record = |content| {
self.profile.history.push(HistoryEntry {
id: msg.id.clone(),
contact: cid.clone(),
outbound: false,
sent_at_ms: msg.sent_at_ms,
received_at,
content,
})
let entry = |content| HistoryEntry {
id: msg.id.clone(),
contact: cid.clone(),
outbound: false,
sent_at_ms: msg.sent_at_ms,
received_at,
content,
};
match &msg.body {
Some(v2::user_message::Body::Text(t)) => {
record(Content::Text(t.text.clone()));
sp.batch.history.push(entry(Content::Text(t.text.clone())));
events.push(Event::Message {
contact: name,
id: msg.id.clone(),
@@ -802,12 +949,14 @@ impl MeowClient {
});
}
Some(v2::user_message::Body::File(f)) => {
record(Content::File {
sp.batch.history.push(entry(Content::File {
name: f.filename.clone(),
size: f.size,
});
}));
sp.batch.files.push((cid.clone(), msg.id.clone(), f.data.clone()));
events.push(Event::File {
contact: name,
id: msg.id.clone(),
name: f.filename.clone(),
data: f.data.clone(),
});
@@ -817,8 +966,13 @@ impl MeowClient {
}
_ => tracing::debug!("ignoring unsupported envelope"),
}
sp.dirty = true;
}
for (si, before) in mailboxes_before.into_iter().enumerate() {
if self.spaces[si].dirty {
self.retire_mailboxes(si, before);
}
}
self.retire_mailboxes(mailboxes_before);
// Persist before anything leaves the device.
self.save().await?;
for (servers, deposit) in outgoing {
@@ -829,3 +983,11 @@ impl MeowClient {
Ok(events)
}
}
fn peer_servers(contact: &Contact) -> Vec<ServerInfo> {
contact
.peer
.as_ref()
.map(|p| p.pull_servers.clone())
.unwrap_or_default()
}
-98
View File
@@ -1,98 +0,0 @@
//! Provisional persistence: one encrypted file per identity (`*.mwid2`).
//!
//! Replaced by the SQLite store of `meow-store` in roadmap phase 4. The whole
//! profile is serialised with postcard and encrypted with Argon2id +
//! XChaCha20-Poly1305 ([`meow_crypto::password`]).
use std::path::Path;
use meow_core::contact::{Contact, ContactId};
use meow_core::server::ServerInfo;
use meow_crypto::password::{self, KdfParams};
use meow_crypto::{SignPublic, SigningKeyPair};
use serde::{Deserialize, Serialize};
use crate::{Error, Result};
const FORMAT_VERSION: u32 = 2;
/// One entry of a conversation history.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct HistoryEntry {
pub id: Vec<u8>,
pub contact: ContactId,
pub outbound: bool,
pub sent_at_ms: u64,
pub received_at: u64,
pub content: Content,
}
/// Displayable content of a message.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub enum Content {
Text(String),
File { name: String, size: u64 },
}
/// A message received from a server and processed (its effects are in this profile),
/// whose deletion the server has not confirmed yet. Sent with the next pull to that
/// server; also used to ignore a redelivery of the same message.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct PendingAck {
pub server: SignPublic,
pub mailbox: Vec<u8>,
pub id: Vec<u8>,
}
/// A mailbox no longer in use (temporary invitation mailbox, removed contact) that
/// still has pending acks: its key is kept just long enough to send them.
#[derive(Clone, Serialize, Deserialize)]
pub struct RetiredMailbox {
pub key: SigningKeyPair,
pub servers: Vec<ServerInfo>,
}
#[derive(Clone, Serialize, Deserialize)]
pub struct Profile {
pub format: u32,
pub nickname: String,
pub servers: Vec<ServerInfo>,
pub contacts: Vec<Contact>,
pub history: Vec<HistoryEntry>,
pub pending_acks: Vec<PendingAck>,
pub retired_mailboxes: Vec<RetiredMailbox>,
}
impl Profile {
pub fn new(nickname: &str) -> Self {
Self {
format: FORMAT_VERSION,
nickname: nickname.to_string(),
servers: vec![],
contacts: vec![],
history: vec![],
pending_acks: vec![],
retired_mailboxes: vec![],
}
}
pub async fn load(path: &Path, password: &str) -> Result<Self> {
let blob = tokio::fs::read(path).await?;
let plain = password::decrypt(password.as_bytes(), &blob).map_err(|_| Error::BadPassword)?;
let p: Profile = postcard::from_bytes(&plain).map_err(|e| Error::Corrupted(e.to_string()))?;
if p.format != FORMAT_VERSION {
return Err(Error::Corrupted(format!("unsupported profile format {}", p.format)));
}
Ok(p)
}
/// Atomic save (write to a temporary file then rename).
pub async fn save(&self, path: &Path, password: &str, kdf: KdfParams) -> Result<()> {
let plain = postcard::to_allocvec(self).map_err(|e| Error::Corrupted(e.to_string()))?;
let blob = password::encrypt(password.as_bytes(), &plain, kdf)?;
let tmp = path.with_extension("tmp");
tokio::fs::write(&tmp, blob).await?;
tokio::fs::rename(&tmp, path).await?;
Ok(())
}
}
+94 -16
View File
@@ -107,11 +107,23 @@ async fn file_and_server_invitations_then_messaging() {
std::fs::write(&f, b"file content").unwrap();
bob.send_file("alice", &f).await.unwrap();
let ev = alice.sync(Duration::ZERO).await.unwrap();
assert!(ev.contains(&Event::File {
contact: "bob".into(),
name: "note.txt".into(),
data: b"file content".to_vec()
}));
let file_id = ev
.iter()
.find_map(|e| match e {
Event::File {
contact,
id,
name,
data,
} if contact == "bob" && name == "note.txt" => {
assert_eq!(data, b"file content");
Some(id.clone())
}
_ => None,
})
.expect("file event");
// The content is kept in the store.
assert_eq!(alice.file("bob", &file_id).unwrap().unwrap(), b"file content");
// ── server invitation ──
let url = bob
@@ -148,7 +160,7 @@ async fn file_and_server_invitations_then_messaging() {
let path = dir.join("alice.mwid2");
drop(alice);
let waiter = tokio::spawn(async move {
let mut alice = MeowClient::open(&path, "pw", KdfParams::INSECURE_TEST).await.unwrap();
let mut alice = MeowClient::open(&path, "pw").await.unwrap();
let start = std::time::Instant::now();
let ev = alice.sync(Duration::from_secs(20)).await.unwrap();
(texts(&ev), start.elapsed())
@@ -161,20 +173,14 @@ async fn file_and_server_invitations_then_messaging() {
// ── one process at a time per identity ──
assert!(matches!(
MeowClient::open(&dir.join("bob.mwid2"), "pw", KdfParams::INSECURE_TEST).await,
MeowClient::open(&dir.join("bob.mwid2"), "pw").await,
Err(meow_client::Error::Locked(_))
));
drop(bob);
// ── persistence & wrong password ──
assert!(
MeowClient::open(&dir.join("bob.mwid2"), "nope", KdfParams::INSECURE_TEST)
.await
.is_err()
);
let bob2 = MeowClient::open(&dir.join("bob.mwid2"), "pw", KdfParams::INSECURE_TEST)
.await
.unwrap();
assert!(MeowClient::open(&dir.join("bob.mwid2"), "nope").await.is_err());
let bob2 = MeowClient::open(&dir.join("bob.mwid2"), "pw").await.unwrap();
assert_eq!(bob2.contacts().len(), 2);
assert_eq!(bob2.history("alice").unwrap().len(), 6); // 5 sent + "hi bob"
@@ -306,7 +312,7 @@ async fn no_message_lost_when_the_client_dies() {
assert_eq!(raw_pull(&server, &mailbox).await.len(), 1);
let path = dir.join("alice.mwid2");
drop(alice);
let mut alice = MeowClient::open(&path, "pw", KdfParams::INSECURE_TEST).await.unwrap();
let mut alice = MeowClient::open(&path, "pw").await.unwrap();
assert!(alice.sync(Duration::ZERO).await.unwrap().is_empty());
store.drop_acks.store(false, std::sync::atomic::Ordering::SeqCst);
assert!(alice.sync(Duration::ZERO).await.unwrap().is_empty());
@@ -337,3 +343,75 @@ async fn no_message_lost_when_the_client_dies() {
let _ = std::fs::remove_dir_all(&dir);
}
// ───────────── hidden spaces (roadmap lot 4.5) ─────────────
/// File invitation from `a` to `b`, completed.
async fn connect(a: &mut MeowClient, b: &mut MeowClient, b_name: &str) {
let offer = a.create_invitation(b_name, &[], "", 3600).await.unwrap();
let answer = b.answer_invitation(&offer, None, &[]).await.unwrap();
let confirm = a.accept_answer(&answer).await.unwrap();
b.finalize_invitation(&confirm).await.unwrap();
a.sync(Duration::ZERO).await.unwrap();
}
fn names(c: &MeowClient) -> Vec<String> {
c.contacts().iter().map(|k| k.name.clone()).collect()
}
#[tokio::test(flavor = "multi_thread")]
async fn hidden_contacts() {
let servers = vec![spawn_server().await];
let dir = tmpdir();
let path = dir.join("alice.mwid2");
let mut alice = client(&dir, "alice", &servers).await;
let mut bob = client(&dir, "bob", &servers).await;
let mut carol = client(&dir, "carol", &servers).await;
connect(&mut alice, &mut bob, "bob").await;
connect(&mut alice, &mut carol, "carol").await;
bob.send_text("alice", "before hiding").await.unwrap();
assert_eq!(texts(&alice.sync(Duration::ZERO).await.unwrap()), vec!["before hiding"]);
// Hide bob, with his history.
alice.hide_contact("bob", "bob-pw").await.unwrap();
assert!(alice.is_hidden("bob").unwrap());
assert!(!alice.is_hidden("carol").unwrap());
assert_eq!(alice.history("bob").unwrap().len(), 1);
drop(alice);
// Without the hidden password, bob does not exist and is not even polled.
let mut alice = MeowClient::open(&path, "pw").await.unwrap();
assert_eq!(names(&alice), vec!["carol"]);
assert!(alice.history("bob").is_err());
bob.send_text("alice", "while locked").await.unwrap();
carol.send_text("alice", "hi").await.unwrap();
assert_eq!(texts(&alice.sync(Duration::ZERO).await.unwrap()), vec!["hi"]);
// With it, bob is back and his messages were waiting on the server.
assert!(!alice.unlock("wrong").await.unwrap());
assert!(alice.unlock("bob-pw").await.unwrap());
assert_eq!(names(&alice), vec!["carol", "bob"]);
assert_eq!(texts(&alice.sync(Duration::ZERO).await.unwrap()), vec!["while locked"]);
alice.send_text("bob", "from the hidden side").await.unwrap();
assert_eq!(
texts(&bob.sync(Duration::ZERO).await.unwrap()),
vec!["from the hidden side"]
);
assert_eq!(alice.history("bob").unwrap().len(), 3);
// Several contacts can share a hidden space.
alice.hide_contact("carol", "bob-pw").await.unwrap();
drop(alice);
let mut alice = MeowClient::open(&path, "pw").await.unwrap();
assert!(names(&alice).is_empty());
assert!(alice.unlock("bob-pw").await.unwrap());
assert_eq!(names(&alice), vec!["bob", "carol"]);
// Back to the main space.
alice.reveal_contact("bob").await.unwrap();
drop(alice);
let alice = MeowClient::open(&path, "pw").await.unwrap();
assert_eq!(names(&alice), vec!["bob"]);
assert_eq!(alice.history("bob").unwrap().len(), 3);
let _ = std::fs::remove_dir_all(&dir);
}