v0.0.38: FC-P4 complete — session versioning, wire envelope, auto-backup
FC-P4-T1: Session State Versioning - RatchetState serialize/deserialize with [MAGIC:0xFC][VERSION:1][bincode] - Legacy (raw bincode) still loads — backward compatible - Client + WASM both use versioned format - 2 new tests: roundtrip + legacy compat FC-P4-T2: WireMessage Versioning Envelope - Format: [WZ magic][version:u8][length:u32 BE][bincode payload] - Server + client + WASM accept both envelope and legacy on receive - Client still sends raw bincode (server handles both) - Future version → "update required" error instead of crash - 3 new tests: roundtrip, legacy compat, future version rejection FC-P4-T3: Periodic Auto-Backup - Every 5 minutes, encrypts sessions+contacts+sender_keys to ~/.warzone/backups/ - HKDF-derived key from seed, ChaCha20-Poly1305 AEAD - Atomic writes (temp file + rename), rotates to keep last 3 - /backup command for manual trigger 127 tests passing (was 122) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -54,7 +54,8 @@ impl LocalDb {
|
||||
/// Save a ratchet session for a peer.
|
||||
pub fn save_session(&self, peer: &Fingerprint, state: &RatchetState) -> Result<()> {
|
||||
let key = peer.to_hex();
|
||||
let data = bincode::serialize(state).context("failed to serialize session")?;
|
||||
let data = state.serialize_versioned()
|
||||
.map_err(|e| anyhow::anyhow!("{}", e))?;
|
||||
self.sessions.insert(key.as_bytes(), data)?;
|
||||
self.sessions.flush()?;
|
||||
Ok(())
|
||||
@@ -73,8 +74,8 @@ impl LocalDb {
|
||||
let key = peer.to_hex();
|
||||
match self.sessions.get(key.as_bytes())? {
|
||||
Some(data) => {
|
||||
let state = bincode::deserialize(&data)
|
||||
.context("failed to deserialize session")?;
|
||||
let state = RatchetState::deserialize_versioned(&data)
|
||||
.map_err(|e| anyhow::anyhow!("{}", e))?;
|
||||
Ok(Some(state))
|
||||
}
|
||||
None => Ok(None),
|
||||
@@ -272,6 +273,87 @@ impl LocalDb {
|
||||
}))
|
||||
}
|
||||
|
||||
/// Create an encrypted backup of all session data.
|
||||
/// Returns the backup file path.
|
||||
pub fn create_backup(&self, seed: &[u8; 32]) -> Result<std::path::PathBuf> {
|
||||
use std::io::Write;
|
||||
|
||||
let backup_dir = crate::keystore::data_dir().join("backups");
|
||||
std::fs::create_dir_all(&backup_dir)?;
|
||||
|
||||
// Collect all data
|
||||
let mut data = serde_json::Map::new();
|
||||
|
||||
// Sessions
|
||||
let mut sessions = serde_json::Map::new();
|
||||
for item in self.sessions.iter() {
|
||||
if let Ok((key, value)) = item {
|
||||
let k = String::from_utf8_lossy(&key).to_string();
|
||||
sessions.insert(k, serde_json::Value::String(base64::Engine::encode(
|
||||
&base64::engine::general_purpose::STANDARD, &value
|
||||
)));
|
||||
}
|
||||
}
|
||||
data.insert("sessions".into(), serde_json::Value::Object(sessions));
|
||||
|
||||
// Contacts
|
||||
let mut contacts = serde_json::Map::new();
|
||||
for item in self.contacts.iter() {
|
||||
if let Ok((key, value)) = item {
|
||||
let k = String::from_utf8_lossy(&key).to_string();
|
||||
if let Ok(v) = serde_json::from_slice::<serde_json::Value>(&value) {
|
||||
contacts.insert(k, v);
|
||||
}
|
||||
}
|
||||
}
|
||||
data.insert("contacts".into(), serde_json::Value::Object(contacts));
|
||||
|
||||
// Sender keys
|
||||
let mut sender_keys = serde_json::Map::new();
|
||||
for item in self.sender_keys.iter() {
|
||||
if let Ok((key, value)) = item {
|
||||
let k = String::from_utf8_lossy(&key).to_string();
|
||||
sender_keys.insert(k, serde_json::Value::String(base64::Engine::encode(
|
||||
&base64::engine::general_purpose::STANDARD, &value
|
||||
)));
|
||||
}
|
||||
}
|
||||
data.insert("sender_keys".into(), serde_json::Value::Object(sender_keys));
|
||||
|
||||
// Serialize and encrypt
|
||||
let plaintext = serde_json::to_vec(&serde_json::Value::Object(data))?;
|
||||
let key_bytes = warzone_protocol::crypto::hkdf_derive(seed, b"", b"warzone-backup", 32);
|
||||
let mut key = [0u8; 32];
|
||||
key.copy_from_slice(&key_bytes);
|
||||
let encrypted = warzone_protocol::crypto::aead_encrypt(&key, &plaintext, b"warzone-backup-aad");
|
||||
|
||||
// Write to temp file then rename (atomic)
|
||||
let timestamp = chrono::Utc::now().format("%Y%m%d_%H%M%S").to_string();
|
||||
let filename = format!("backup_{}.wzbk", timestamp);
|
||||
let path = backup_dir.join(&filename);
|
||||
let tmp_path = backup_dir.join(format!(".{}.tmp", filename));
|
||||
|
||||
let mut file = std::fs::File::create(&tmp_path)?;
|
||||
file.write_all(&encrypted)?;
|
||||
file.sync_all()?;
|
||||
std::fs::rename(&tmp_path, &path)?;
|
||||
|
||||
// Rotate: keep last 3 backups
|
||||
let mut backups: Vec<_> = std::fs::read_dir(&backup_dir)?
|
||||
.filter_map(|e| e.ok())
|
||||
.filter(|e| e.file_name().to_string_lossy().ends_with(".wzbk"))
|
||||
.collect();
|
||||
backups.sort_by_key(|e| e.file_name());
|
||||
while backups.len() > 3 {
|
||||
if let Some(old) = backups.first() {
|
||||
let _ = std::fs::remove_file(old.path());
|
||||
backups.remove(0);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(path)
|
||||
}
|
||||
|
||||
/// Import data from JSON backup (merges, doesn't overwrite existing).
|
||||
pub fn import_all(&self, data: &serde_json::Value) -> Result<usize> {
|
||||
let mut count = 0;
|
||||
|
||||
@@ -47,6 +47,7 @@ impl App {
|
||||
" /info Show your fingerprint",
|
||||
" /eth Show Ethereum address",
|
||||
" /seed Show recovery mnemonic (24 words)",
|
||||
" /backup Create encrypted backup now",
|
||||
" /peer <fp>, /p Set DM peer by fingerprint",
|
||||
" /peer @alias Set DM peer by alias",
|
||||
" /reply, /r Reply to last DM sender",
|
||||
@@ -189,6 +190,19 @@ impl App {
|
||||
}
|
||||
return;
|
||||
}
|
||||
if text == "/backup" {
|
||||
if let Ok(seed) = crate::keystore::load_seed_raw() {
|
||||
match db.create_backup(&seed) {
|
||||
Ok(path) => {
|
||||
self.add_message(ChatLine { sender: "system".into(), text: format!("Backup saved: {}", path.display()), is_system: true, is_self: false, message_id: None, timestamp: Local::now() });
|
||||
}
|
||||
Err(e) => {
|
||||
self.add_message(ChatLine { sender: "system".into(), text: format!("Backup failed: {}", e), is_system: true, is_self: false, message_id: None, timestamp: Local::now() });
|
||||
}
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
if text == "/friend" || text == "/friends" {
|
||||
// Fetch encrypted friend list from server, decrypt locally
|
||||
let url = format!("{}/v1/friends", client.base_url);
|
||||
|
||||
@@ -48,6 +48,23 @@ pub async fn run_tui(
|
||||
network::poll_loop(poll_messages, poll_receipts, poll_pending_files, poll_fp, poll_identity, poll_db, poll_client, poll_last_dm, poll_connected).await;
|
||||
});
|
||||
|
||||
// Spawn periodic backup task (every 5 minutes)
|
||||
{
|
||||
let backup_db = db.clone();
|
||||
tokio::spawn(async move {
|
||||
let mut interval = tokio::time::interval(std::time::Duration::from_secs(300));
|
||||
loop {
|
||||
interval.tick().await;
|
||||
if let Ok(seed) = crate::keystore::load_seed_raw() {
|
||||
match backup_db.create_backup(&seed) {
|
||||
Ok(path) => tracing::debug!("Auto-backup created: {}", path.display()),
|
||||
Err(e) => tracing::warn!("Auto-backup failed: {}", e),
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Auto-join #ops if no peer set (create if needed)
|
||||
if app.peer_fp.is_none() {
|
||||
let fp_clean: String = our_fp.chars().filter(|c| c.is_ascii_hexdigit()).collect::<String>().to_lowercase();
|
||||
|
||||
@@ -93,7 +93,7 @@ pub fn process_incoming(
|
||||
eth_cache: &EthCache,
|
||||
last_dm_peer: &Arc<Mutex<Option<String>>>,
|
||||
) {
|
||||
match bincode::deserialize::<WireMessage>(raw) {
|
||||
match warzone_protocol::message::deserialize_envelope(raw) {
|
||||
Ok(wire) => process_wire_message(wire, identity, db, messages, receipts, pending_files, our_fp, client, last_dm_peer, eth_cache),
|
||||
Err(_) => {}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user