bg check ajustments
This commit is contained in:
1 parent
7eed94d82c
commit
923f4e397e
14 files changed
+649
-46
No files matched your search
@@ -13,6 +13,8 @@ meow-store = { path = "../meow-store" }
|
||||
reqwest = { version = "0.12", default-features = false, features = ["rustls-tls"] }
|
||||
tokio = { version = "1", features = ["fs", "time", "macros", "sync", "rt"] }
|
||||
futures-util = "0.3"
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
postcard = { version = "1", default-features = false, features = ["alloc"] }
|
||||
thiserror = "2"
|
||||
tracing = "0.1"
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
pub mod background;
|
||||
pub mod outbox;
|
||||
pub mod transport;
|
||||
pub mod watch;
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::path::Path;
|
||||
@@ -43,6 +44,7 @@ pub use meow_core::server::ServerInfo;
|
||||
pub use meow_proto::PROTOCOL_VERSION;
|
||||
pub use meow_store::{Content, HistoryEntry, HistoryPage, PendingAck, Settings, Status};
|
||||
pub use outbox::{PendingSend, RetryPolicy};
|
||||
pub use watch::watch_check;
|
||||
|
||||
/// Maximum file size sent in a single message (chunking: roadmap 5.7).
|
||||
pub const MAX_FILE_BYTES: usize = 4 * 1024 * 1024;
|
||||
@@ -233,6 +235,8 @@ pub struct MeowClient {
|
||||
/// Bumped at every save: the background task restarts its wait when the state
|
||||
/// changed under it (new contact to poll, unlocked space, queued message).
|
||||
changes: tokio::sync::watch::Sender<u64>,
|
||||
/// Background watch file (lot 7.7), when enabled.
|
||||
watch: Option<watch::WatchConfig>,
|
||||
}
|
||||
|
||||
type Mailboxes = Vec<(SigningKeyPair, Vec<ServerInfo>)>;
|
||||
@@ -270,6 +274,7 @@ impl MeowClient {
|
||||
pending_events: Vec::new(),
|
||||
events: tokio::sync::broadcast::channel(EVENT_BUFFER).0,
|
||||
changes: tokio::sync::watch::channel(0).0,
|
||||
watch: None,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -288,7 +293,13 @@ impl MeowClient {
|
||||
let batch = std::mem::take(&mut sp.batch);
|
||||
lock(&self.store)?.save(sp.id, &sp.state, batch)?;
|
||||
sp.dirty = false;
|
||||
let main = sp.id == SpaceId::MAIN;
|
||||
self.changes.send_modify(|v| *v = v.wrapping_add(1));
|
||||
if main {
|
||||
if let Err(e) = self.refresh_watch() {
|
||||
tracing::warn!("cannot update the watch file: {e}");
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
@@ -56,6 +56,11 @@ impl RetryPolicy {
|
||||
}
|
||||
}
|
||||
|
||||
/// Uniform random index in `0..n` (n > 0); the tiny modulo bias does not matter here.
|
||||
pub(crate) fn rand_below(n: usize) -> usize {
|
||||
(u64::from_le_bytes(meow_crypto::random_bytes::<8>()) % n as u64) as usize
|
||||
}
|
||||
|
||||
/// A queued deposit, as shown to the user.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct PendingSend {
|
||||
|
||||
@@ -0,0 +1,279 @@
|
||||
//! Background watching (roadmap lot 7.7).
|
||||
//!
|
||||
//! While the identity is open, the client keeps a small **watch file** up to date: for
|
||||
//! each mailbox of the *main* space, the server to pull it from and the mailbox key that
|
||||
//! signs pulls. Nothing else: no names, no ratchet keys, no history. Hidden spaces are
|
||||
//! never included (their activity would betray them).
|
||||
//!
|
||||
//! A background task (Android WorkManager / foreground service, or the app while locked)
|
||||
//! calls [`watch_check`] **without the password**: it *peeks* (pull without
|
||||
//! acknowledgement, only messages after the last one seen) and returns how many new
|
||||
//! messages are waiting. Nothing is stored locally: the messages stay on the servers
|
||||
//! until the app, opened and unlocked, processes and acknowledges them normally.
|
||||
//!
|
||||
//! The file is encrypted with a device key held by the platform (Android Keystore,
|
||||
//! desktop keyring), not by the password; it is padded with decoy mailboxes to a fixed
|
||||
//! bucket so that it does not reveal the number of contacts.
|
||||
|
||||
use std::collections::{BTreeMap, HashMap};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::time::Duration;
|
||||
|
||||
use futures_util::future::join_all;
|
||||
use meow_core::rpc;
|
||||
use meow_core::server::ServerInfo;
|
||||
use meow_crypto::aead::{self, Key};
|
||||
use meow_crypto::{SignPublic, SigningKeyPair};
|
||||
use meow_proto::v2::{self, request::Body as Req, response::Body as Resp};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{now, transport::Transport, Error, MeowClient, Result};
|
||||
|
||||
const PROFILE_AAD: &[u8] = b"meow/v2/watch-profile";
|
||||
const CURSOR_AAD: &[u8] = b"meow/v2/watch-cursors";
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct WatchMailbox {
|
||||
server: ServerInfo,
|
||||
key: SigningKeyPair,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Default)]
|
||||
struct WatchProfile {
|
||||
mailboxes: Vec<WatchMailbox>,
|
||||
}
|
||||
|
||||
/// Last message id seen per (server, mailbox).
|
||||
#[derive(Serialize, Deserialize, Default)]
|
||||
struct Cursors(BTreeMap<String, Vec<u8>>);
|
||||
|
||||
fn cursor_key(server: &SignPublic, mailbox: &SignPublic) -> String {
|
||||
format!("{}/{}", server.to_hex(), mailbox.to_hex())
|
||||
}
|
||||
|
||||
/// Smallest bucket (8, 16, 32…) holding `n` entries.
|
||||
fn bucket(n: usize) -> usize {
|
||||
n.max(1).next_power_of_two().max(8)
|
||||
}
|
||||
|
||||
fn seal<T: Serialize>(key: &Key, aad: &[u8], v: &T) -> Result<Vec<u8>> {
|
||||
let plain = postcard::to_allocvec(v).map_err(|e| Error::Invalid(e.to_string()))?;
|
||||
Ok(aead::encrypt(key, &plain, aad))
|
||||
}
|
||||
|
||||
fn open<T: for<'de> Deserialize<'de>>(key: &Key, aad: &[u8], data: &[u8]) -> Result<T> {
|
||||
let plain = aead::decrypt(key, data, aad)?;
|
||||
postcard::from_bytes(&plain).map_err(|e| Error::Invalid(format!("watch file: {e}")))
|
||||
}
|
||||
|
||||
/// Atomic write, owner-only permissions.
|
||||
fn write_private(path: &Path, data: &[u8]) -> Result<()> {
|
||||
let tmp = path.with_extension("tmp");
|
||||
{
|
||||
use std::io::Write;
|
||||
let mut o = std::fs::OpenOptions::new();
|
||||
o.write(true).create(true).truncate(true);
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::fs::OpenOptionsExt;
|
||||
o.mode(0o600);
|
||||
}
|
||||
let mut f = o.open(&tmp)?;
|
||||
f.write_all(data)?;
|
||||
f.sync_all()?;
|
||||
}
|
||||
std::fs::rename(&tmp, path)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn cursor_path(watch: &Path) -> PathBuf {
|
||||
let mut p = watch.as_os_str().to_owned();
|
||||
p.push(".cursors");
|
||||
PathBuf::from(p)
|
||||
}
|
||||
|
||||
/// Where the watch file goes and its device key.
|
||||
pub(crate) struct WatchConfig {
|
||||
path: PathBuf,
|
||||
key: Key,
|
||||
/// Hash of the real mailboxes last written (decoys excluded), to skip rewrites.
|
||||
written: Option<[u8; 32]>,
|
||||
}
|
||||
|
||||
impl MeowClient {
|
||||
/// Keep the watch file at `path` up to date, encrypted with `key` (32 bytes, held
|
||||
/// by the platform's key store). Written now and whenever the main space's
|
||||
/// mailboxes change.
|
||||
pub fn enable_watch(&mut self, path: &Path, key: [u8; 32]) -> Result<()> {
|
||||
self.watch = Some(WatchConfig {
|
||||
path: path.to_path_buf(),
|
||||
key: Key(key),
|
||||
written: None,
|
||||
});
|
||||
self.refresh_watch()
|
||||
}
|
||||
|
||||
/// Stop maintaining the watch file and delete it (and its cursors).
|
||||
pub fn disable_watch(&mut self) -> Result<()> {
|
||||
if let Some(w) = self.watch.take() {
|
||||
for p in [w.path.clone(), cursor_path(&w.path)] {
|
||||
if p.exists() {
|
||||
std::fs::remove_file(p)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Rewrite the watch file if the main space's mailboxes changed.
|
||||
pub(crate) fn refresh_watch(&mut self) -> Result<()> {
|
||||
let Some(w) = self.watch.as_ref() else {
|
||||
return Ok(());
|
||||
};
|
||||
// Main space only: hidden spaces must stay invisible.
|
||||
let mut real: Vec<WatchMailbox> = Vec::new();
|
||||
for (kp, servers) in self.space_mailboxes(0) {
|
||||
for s in servers {
|
||||
real.push(WatchMailbox {
|
||||
server: s,
|
||||
key: kp.clone(),
|
||||
});
|
||||
}
|
||||
}
|
||||
let fingerprint: Vec<u8> = real
|
||||
.iter()
|
||||
.flat_map(|m| {
|
||||
[
|
||||
m.server.sign_key.as_bytes().to_vec(),
|
||||
m.key.public().as_bytes().to_vec(),
|
||||
]
|
||||
.concat()
|
||||
})
|
||||
.collect();
|
||||
let hash = meow_crypto::sha256(&[&fingerprint]);
|
||||
if w.written == Some(hash) && w.path.exists() {
|
||||
return Ok(());
|
||||
}
|
||||
// Decoys on the user's servers, up to the bucket size, then shuffled.
|
||||
let servers = self.main().servers.clone();
|
||||
let target = bucket(real.len());
|
||||
let mut all = real;
|
||||
if !servers.is_empty() {
|
||||
while all.len() < target {
|
||||
let s = servers[crate::outbox::rand_below(servers.len())].clone();
|
||||
all.push(WatchMailbox {
|
||||
server: s,
|
||||
key: SigningKeyPair::generate(),
|
||||
});
|
||||
}
|
||||
}
|
||||
for i in (1..all.len()).rev() {
|
||||
all.swap(i, crate::outbox::rand_below(i + 1));
|
||||
}
|
||||
let data = seal(&w.key, PROFILE_AAD, &WatchProfile { mailboxes: all })?;
|
||||
write_private(&w.path, &data)?;
|
||||
if let Some(w) = self.watch.as_mut() {
|
||||
w.written = Some(hash);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Peek for new messages with the watch file, without the password. Waits up to
|
||||
/// `wait` (long-poll) when nothing is new. Returns the number of new messages (a
|
||||
/// message deposited on two servers counts twice). Never acknowledges anything.
|
||||
pub async fn watch_check(path: &Path, key: [u8; 32], wait: Duration) -> Result<usize> {
|
||||
let key = Key(key);
|
||||
let profile: WatchProfile = open(&key, PROFILE_AAD, &std::fs::read(path)?)?;
|
||||
let cpath = cursor_path(path);
|
||||
let mut cursors: Cursors = match std::fs::read(&cpath) {
|
||||
Ok(d) => open(&key, CURSOR_AAD, &d).unwrap_or_default(),
|
||||
Err(_) => Cursors::default(),
|
||||
};
|
||||
|
||||
// One pull per server.
|
||||
let mut by_server: BTreeMap<SignPublic, (ServerInfo, Vec<SigningKeyPair>)> = BTreeMap::new();
|
||||
for m in profile.mailboxes {
|
||||
by_server
|
||||
.entry(m.server.sign_key)
|
||||
.or_insert_with(|| (m.server.clone(), Vec::new()))
|
||||
.1
|
||||
.push(m.key);
|
||||
}
|
||||
let transport = Transport::new()?;
|
||||
let wait_s = wait.as_secs().min(u32::MAX as u64) as u32;
|
||||
let calls = by_server.values().map(|(server, keys)| {
|
||||
let rid = meow_crypto::random_bytes::<16>().to_vec();
|
||||
let ts = now();
|
||||
let auths = keys
|
||||
.iter()
|
||||
.map(|kp| {
|
||||
let mut a = rpc::mailbox_auth(kp, server, &rid, Vec::new(), ts);
|
||||
a.after = cursors
|
||||
.0
|
||||
.get(&cursor_key(&server.sign_key, &kp.public()))
|
||||
.cloned()
|
||||
.unwrap_or_default();
|
||||
a
|
||||
})
|
||||
.collect();
|
||||
let req = v2::Request {
|
||||
request_id: rid,
|
||||
timestamp: ts,
|
||||
body: Some(Req::Pull(v2::Pull {
|
||||
auths,
|
||||
wait_seconds: wait_s,
|
||||
})),
|
||||
};
|
||||
transport.call_request(server, req, wait)
|
||||
});
|
||||
let results = join_all(calls).await;
|
||||
|
||||
let mut new = 0;
|
||||
let mut ok = 0;
|
||||
let mut last_err = None;
|
||||
let mut latest: HashMap<String, Vec<u8>> = HashMap::new();
|
||||
for ((server, _), r) in by_server.values().zip(results) {
|
||||
match r {
|
||||
Ok(Resp::PullResult(p)) => {
|
||||
ok += 1;
|
||||
new += p.messages.len();
|
||||
// Oldest first: the last one per mailbox is the new cursor.
|
||||
for m in p.messages {
|
||||
if let Ok(mb) = SignPublic::from_slice(&m.mailbox) {
|
||||
latest.insert(cursor_key(&server.sign_key, &mb), m.id);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
tracing::debug!("watch: {} unreachable: {e}", server.url);
|
||||
last_err = Some(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
if ok == 0 {
|
||||
if let Some(e) = last_err {
|
||||
return Err(e);
|
||||
}
|
||||
}
|
||||
if !latest.is_empty() {
|
||||
cursors.0.extend(latest);
|
||||
write_private(&cpath, &seal(&key, CURSOR_AAD, &cursors)?)?;
|
||||
}
|
||||
Ok(new)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::bucket;
|
||||
|
||||
#[test]
|
||||
fn buckets() {
|
||||
assert_eq!(bucket(0), 8);
|
||||
assert_eq!(bucket(3), 8);
|
||||
assert_eq!(bucket(8), 8);
|
||||
assert_eq!(bucket(9), 16);
|
||||
assert_eq!(bucket(40), 64);
|
||||
}
|
||||
}
|
||||
@@ -226,7 +226,7 @@ impl meow_server::store::ServerStore for FlakyStore {
|
||||
}
|
||||
async fn fetch(
|
||||
&self,
|
||||
mbs: &[[u8; 32]],
|
||||
mbs: &[([u8; 32], Vec<u8>)],
|
||||
limit: usize,
|
||||
) -> meow_server::store::StoreResult<Vec<meow_proto::v2::StoredMessage>> {
|
||||
self.inner.fetch(mbs, limit).await
|
||||
@@ -849,3 +849,71 @@ async fn one_shot_invitation_completes_through_servers() {
|
||||
);
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
// ───────────── background watch (roadmap lot 7.7) ─────────────
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn background_watch_peeks_without_consuming() {
|
||||
let url = spawn_server().await;
|
||||
let servers = vec![url.clone()];
|
||||
let dir = tmpdir();
|
||||
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;
|
||||
alice.hide_contact("carol", "carol-secret").await.unwrap();
|
||||
bob.sync(Duration::ZERO).await.unwrap();
|
||||
carol.sync(Duration::ZERO).await.unwrap();
|
||||
alice.sync(Duration::ZERO).await.unwrap();
|
||||
|
||||
let path = dir.join("alice.watch");
|
||||
let key = [7u8; 32];
|
||||
alice.enable_watch(&path, key).unwrap();
|
||||
// Encrypted, and padded: neither the servers nor the number of contacts show.
|
||||
let raw = std::fs::read(&path).unwrap();
|
||||
assert!(!raw.windows(9).any(|w| w == b"127.0.0.1"));
|
||||
assert!(
|
||||
meow_client::watch_check(&path, [8u8; 32], Duration::ZERO)
|
||||
.await
|
||||
.is_err(),
|
||||
"wrong key"
|
||||
);
|
||||
// Baseline (whatever was pending), then nothing new.
|
||||
meow_client::watch_check(&path, key, Duration::ZERO).await.unwrap();
|
||||
assert_eq!(meow_client::watch_check(&path, key, Duration::ZERO).await.unwrap(), 0);
|
||||
|
||||
// A new message is counted once, and not consumed.
|
||||
bob.send_text("alice", "ping").await.unwrap();
|
||||
assert_eq!(meow_client::watch_check(&path, key, Duration::ZERO).await.unwrap(), 1);
|
||||
assert_eq!(meow_client::watch_check(&path, key, Duration::ZERO).await.unwrap(), 0);
|
||||
|
||||
// Hidden contacts are never watched.
|
||||
carol.send_text("alice", "from the hidden side").await.unwrap();
|
||||
assert_eq!(meow_client::watch_check(&path, key, Duration::ZERO).await.unwrap(), 0);
|
||||
|
||||
// The app still gets everything (nothing was acknowledged by the watcher).
|
||||
let got = texts(&alice.sync(Duration::ZERO).await.unwrap());
|
||||
assert!(got.contains(&"ping".to_string()) && got.contains(&"from the hidden side".to_string()));
|
||||
|
||||
// Long-poll: wakes up as soon as something arrives.
|
||||
let p = path.clone();
|
||||
let waiter = tokio::spawn(async move {
|
||||
let start = std::time::Instant::now();
|
||||
(
|
||||
meow_client::watch_check(&p, key, Duration::from_secs(20))
|
||||
.await
|
||||
.unwrap(),
|
||||
start.elapsed(),
|
||||
)
|
||||
});
|
||||
tokio::time::sleep(Duration::from_millis(500)).await;
|
||||
bob.send_text("alice", "wake").await.unwrap();
|
||||
let (n, elapsed) = waiter.await.unwrap();
|
||||
assert_eq!(n, 1);
|
||||
assert!(elapsed < Duration::from_secs(10), "{elapsed:?}");
|
||||
|
||||
alice.disable_watch().unwrap();
|
||||
assert!(!path.exists());
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
Reference in new issue
Block a user