convergio-mesh 0.1.13

Peer discovery, delta sync, delegation tracking
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
//! HTTP API routes for mesh sync and peer management.
//! - GET  /api/mesh           — mesh status (peers online, sync stats)
//! - GET  /api/mesh/peers     — list peers from heartbeat table
//! - GET  /api/sync/export    — export changes since timestamp (query: table, since)
//! - POST /api/sync/import    — apply SyncChange[] from a peer
//! - GET  /api/sync/status    — sync metadata per peer
//! - POST /api/heartbeat      — receive heartbeat from a peer

use std::sync::Arc;

use axum::extract::{Query, State};
use axum::http::StatusCode;
use axum::response::{IntoResponse, Json, Response};
use axum::routing::{get, post};
use axum::Router;
use rusqlite::params;
use serde::Deserialize;
use serde_json::json;

use convergio_db::pool::ConnPool;

use crate::routes_sync_repo::handle_sync_repo;
use crate::sync_apply;
use crate::types::SyncChange;

/// Maximum age (seconds) for HMAC-signed requests to prevent replay attacks.
const HMAC_MAX_AGE_SECS: i64 = 300;

pub struct MeshState {
    pub pool: ConnPool,
    /// Pre-loaded shared secret for HMAC verification (from peers.conf).
    pub shared_secret: Option<Vec<u8>>,
}

pub fn mesh_routes(state: Arc<MeshState>) -> Router {
    Router::new()
        .route("/api/mesh", get(handle_status))
        .route("/api/mesh/peers", get(handle_peers))
        .route("/api/mesh/sync-repo", post(handle_sync_repo))
        .route("/api/node/readiness", get(handle_node_readiness))
        .route("/api/sync/export", get(handle_export))
        .route("/api/sync/import", post(handle_import))
        .route("/api/sync/status", get(handle_sync_status))
        .route("/api/heartbeat", post(handle_heartbeat))
        .with_state(state)
}

async fn handle_status(State(state): State<Arc<MeshState>>) -> Json<serde_json::Value> {
    let conn = match state.pool.get() {
        Ok(c) => c,
        Err(e) => return Json(json!({"error": e.to_string()})),
    };
    let peers_online: u64 = conn
        .query_row(
            "SELECT count(*) FROM peer_heartbeats \
             WHERE last_seen > unixepoch() - 600",
            [],
            |r| r.get(0),
        )
        .unwrap_or(0);
    // +1 for local node (doesn't heartbeat itself)
    let peers_online = peers_online + 1;
    let total_synced: u64 = conn
        .query_row(
            "SELECT COALESCE(SUM(total_applied), 0) FROM mesh_sync_stats",
            [],
            |r| r.get(0),
        )
        .unwrap_or(0);
    Json(json!({
        "peers_online": peers_online,
        "total_synced": total_synced,
    }))
}

async fn handle_peers(State(state): State<Arc<MeshState>>) -> Json<serde_json::Value> {
    let conn = match state.pool.get() {
        Ok(c) => c,
        Err(e) => return Json(json!({"error": e.to_string()})),
    };
    let mut stmt = match conn.prepare(
        "SELECT peer_name, last_seen, version, \
         CASE WHEN last_seen > unixepoch() - 600 THEN 'online' \
         ELSE 'offline' END as status, role \
         FROM peer_heartbeats ORDER BY peer_name",
    ) {
        Ok(s) => s,
        Err(e) => return Json(json!({"error": e.to_string()})),
    };
    let mut peers: Vec<serde_json::Value> = match stmt.query_map([], |row| {
        Ok(json!({
            "peer": row.get::<_, String>(0)?,
            "last_seen": row.get::<_, i64>(1)?,
            "version": row.get::<_, Option<String>>(2)?,
            "status": row.get::<_, String>(3)?,
            "role": row.get::<_, Option<String>>(4)?,
        }))
    }) {
        Ok(rows) => rows.filter_map(|r| r.ok()).collect(),
        Err(_) => vec![],
    };

    // Inject local node as always-online (it doesn't heartbeat itself)
    let local_name = hostname::get()
        .map(|h| h.to_string_lossy().to_string())
        .unwrap_or_else(|_| "unknown".into());
    let local_role = std::env::var("CONVERGIO_NODE_ROLE").unwrap_or_default();
    let is_local = |name: &str| -> bool {
        name == local_name
            || name.starts_with(&format!("{local_name}."))
            || local_name.starts_with(&format!("{name}."))
    };
    let already_listed = peers.iter().any(|p| {
        p.get("peer")
            .and_then(|v| v.as_str())
            .map(&is_local)
            .unwrap_or(false)
    });
    if !already_listed {
        peers.insert(
            0,
            json!({
                "peer": local_name,
                "last_seen": chrono::Utc::now().timestamp(),
                "version": env!("CARGO_PKG_VERSION"),
                "status": "online",
                "role": local_role,
            }),
        );
    } else {
        // Fix stale self-entry: force it online with current version
        for p in &mut peers {
            let is_self = p
                .get("peer")
                .and_then(|v| v.as_str())
                .map(&is_local)
                .unwrap_or(false);
            if is_self {
                p["status"] = json!("online");
                p["version"] = json!(env!("CARGO_PKG_VERSION"));
                p["last_seen"] = json!(chrono::Utc::now().timestamp());
            }
        }
    }

    Json(json!(peers))
}

#[derive(Debug, Deserialize)]
pub struct ExportQuery {
    pub table: String,
    pub since: Option<String>,
}

async fn handle_export(
    State(state): State<Arc<MeshState>>,
    Query(params): Query<ExportQuery>,
) -> Response {
    // SECURITY: only allow exporting from the SYNC_TABLES allowlist
    if !crate::types::SYNC_TABLES.contains(&params.table.as_str()) {
        return (
            StatusCode::FORBIDDEN,
            Json(json!({"error": "table not in sync allowlist"})),
        )
            .into_response();
    }
    let conn = match state.pool.get() {
        Ok(c) => c,
        Err(e) => return Json(json!({"error": e.to_string()})).into_response(),
    };
    match sync_apply::export_changes_since(&conn, &params.table, params.since.as_deref()) {
        Ok(changes) => Json(json!({"changes": changes})).into_response(),
        Err(e) => Json(json!({"error": e.to_string()})).into_response(),
    }
}

/// Wrapper for the import payload sent by `send_changes_to_peer`.
#[derive(Debug, Deserialize)]
struct ImportPayload {
    changes: Vec<SyncChange>,
}

async fn handle_import(
    State(state): State<Arc<MeshState>>,
    headers: axum::http::HeaderMap,
    body: axum::body::Bytes,
) -> Response {
    // HMAC verification: if shared secret is configured, require valid signature
    if let Some(secret) = &state.shared_secret {
        let sig_header = headers
            .get("x-mesh-signature")
            .and_then(|v| v.to_str().ok())
            .unwrap_or("");
        let timestamp = headers
            .get("x-mesh-timestamp")
            .and_then(|v| v.to_str().ok())
            .unwrap_or("");
        // Reject stale timestamps to prevent replay attacks
        if let Ok(ts) = timestamp.parse::<i64>() {
            let now = chrono::Utc::now().timestamp();
            if (now - ts).abs() > HMAC_MAX_AGE_SECS {
                return (
                    StatusCode::UNAUTHORIZED,
                    Json(json!({"error": "HMAC timestamp expired"})),
                )
                    .into_response();
            }
        }
        let body_hash_header = headers
            .get("x-mesh-body-hash")
            .and_then(|v| v.to_str().ok())
            .unwrap_or("");
        // Reconstruct the signed message: timestamp:method:path:bodyhash
        let message = if body_hash_header.is_empty() {
            format!("{timestamp}:POST:/api/sync/import")
        } else {
            format!("{timestamp}:POST:/api/sync/import:{body_hash_header}")
        };
        let sig_bytes = hex::decode(sig_header).unwrap_or_default();
        match crate::auth::verify_hmac(secret, message.as_bytes(), &sig_bytes) {
            Ok(true) => {}
            _ => {
                return (
                    StatusCode::UNAUTHORIZED,
                    Json(json!({"error": "HMAC verification failed"})),
                )
                    .into_response()
            }
        }
    }
    let payload: ImportPayload = match serde_json::from_slice(&body) {
        Ok(p) => p,
        Err(e) => return Json(json!({"error": format!("invalid JSON: {e}")})).into_response(),
    };
    let conn = match state.pool.get() {
        Ok(c) => c,
        Err(e) => return Json(json!({"error": e.to_string()})).into_response(),
    };
    match sync_apply::apply_changes(&conn, &payload.changes) {
        Ok(applied) => {
            // Wake IPC long-poll receivers if ipc_messages were synced
            if applied > 0
                && payload
                    .changes
                    .iter()
                    .any(|c| c.table_name == "ipc_messages")
            {
                // Poke the IPC notify endpoint so receive_wait wakes up
                tokio::spawn(async {
                    let _ = reqwest::Client::new()
                        .post("http://localhost:8420/api/ipc/notify-sync")
                        .header("Authorization", convergio_types::dev_auth_header())
                        .send()
                        .await;
                });
            }
            Json(json!({"applied": applied})).into_response()
        }
        Err(e) => Json(json!({"error": e.to_string()})).into_response(),
    }
}

async fn handle_sync_status(State(state): State<Arc<MeshState>>) -> Json<serde_json::Value> {
    let conn = match state.pool.get() {
        Ok(c) => c,
        Err(e) => return Json(json!({"error": e.to_string()})),
    };
    let mut stmt = match conn.prepare(
        "SELECT peer_name, total_applied, last_sync_at, \
         last_latency_ms, consecutive_failures \
         FROM mesh_sync_stats ORDER BY peer_name",
    ) {
        Ok(s) => s,
        Err(e) => return Json(json!({"error": e.to_string()})),
    };
    let rows: Vec<serde_json::Value> = match stmt.query_map([], |row| {
        Ok(json!({
            "peer": row.get::<_, String>(0)?,
            "total_applied": row.get::<_, i64>(1)?,
            "last_sync_at": row.get::<_, Option<String>>(2)?,
            "latency_ms": row.get::<_, Option<i64>>(3)?,
            "failures": row.get::<_, i64>(4)?,
        }))
    }) {
        Ok(rows) => rows.filter_map(|r| r.ok()).collect(),
        Err(_) => vec![],
    };
    Json(json!(rows))
}

/// Node readiness: aggregates mesh peers, sync stats, and DB health.
async fn handle_node_readiness(State(state): State<Arc<MeshState>>) -> Json<serde_json::Value> {
    let conn = match state.pool.get() {
        Ok(c) => c,
        Err(e) => return Json(json!({"ready": false, "error": e.to_string()})),
    };
    let peers_online: u64 = conn
        .query_row(
            "SELECT count(*) FROM peer_heartbeats WHERE last_seen > unixepoch() - 600",
            [],
            |r| r.get(0),
        )
        .unwrap_or(0);
    let db_ok = conn
        .query_row("SELECT 1", [], |r| r.get::<_, i64>(0))
        .is_ok();
    let sync_healthy: bool = conn
        .query_row(
            "SELECT COALESCE(MAX(consecutive_failures), 0) FROM mesh_sync_stats",
            [],
            |r| r.get::<_, i64>(0),
        )
        .map(|f| f < 5)
        .unwrap_or(true);
    let ready = db_ok && sync_healthy;
    Json(json!({
        "ready": ready,
        "checks": {
            "database": if db_ok { "ok" } else { "down" },
            "mesh_peers": peers_online,
            "sync_healthy": sync_healthy,
        }
    }))
}

#[derive(Debug, Deserialize)]
pub struct HeartbeatRequest {
    pub peer: String,
    #[serde(default)]
    pub version: Option<String>,
    #[serde(default)]
    pub capabilities: Option<String>,
    #[serde(default)]
    pub role: Option<String>,
}

async fn handle_heartbeat(
    State(state): State<Arc<MeshState>>,
    headers: axum::http::HeaderMap,
    raw_body: axum::body::Bytes,
) -> Response {
    // HMAC verification: if shared secret is configured, require valid signature
    if let Some(secret) = &state.shared_secret {
        let sig_header = headers
            .get("x-mesh-signature")
            .and_then(|v| v.to_str().ok())
            .unwrap_or("");
        let timestamp = headers
            .get("x-mesh-timestamp")
            .and_then(|v| v.to_str().ok())
            .unwrap_or("");
        // Reject stale timestamps to prevent replay attacks
        if let Ok(ts) = timestamp.parse::<i64>() {
            let now = chrono::Utc::now().timestamp();
            if (now - ts).abs() > HMAC_MAX_AGE_SECS {
                return (
                    StatusCode::UNAUTHORIZED,
                    Json(json!({"error": "HMAC timestamp expired"})),
                )
                    .into_response();
            }
        }
        let body_hash_header = headers
            .get("x-mesh-body-hash")
            .and_then(|v| v.to_str().ok())
            .unwrap_or("");
        let message = if body_hash_header.is_empty() {
            format!("{timestamp}:POST:/api/heartbeat")
        } else {
            format!("{timestamp}:POST:/api/heartbeat:{body_hash_header}")
        };
        let sig_bytes = hex::decode(sig_header).unwrap_or_default();
        match crate::auth::verify_hmac(secret, message.as_bytes(), &sig_bytes) {
            Ok(true) => {}
            _ => {
                return (
                    StatusCode::UNAUTHORIZED,
                    Json(json!({"error": "HMAC verification failed"})),
                )
                    .into_response()
            }
        }
    }
    let body: HeartbeatRequest = match serde_json::from_slice(&raw_body) {
        Ok(b) => b,
        Err(e) => return Json(json!({"error": format!("invalid JSON: {e}")})).into_response(),
    };
    let conn = match state.pool.get() {
        Ok(c) => c,
        Err(e) => return Json(json!({"error": e.to_string()})).into_response(),
    };
    match conn.execute(
        "INSERT INTO peer_heartbeats (peer_name, last_seen, version, capabilities, role) \
         VALUES (?1, unixepoch(), ?2, ?3, ?4) \
         ON CONFLICT(peer_name) DO UPDATE SET \
         last_seen = unixepoch(), version = excluded.version, \
         capabilities = excluded.capabilities, role = excluded.role",
        params![body.peer, body.version, body.capabilities, body.role],
    ) {
        Ok(_) => Json(json!({"status": "ok"})).into_response(),
        Err(e) => Json(json!({"error": e.to_string()})).into_response(),
    }
}