interloper-server 0.1.1

HTTP API over interloper: issue inboxes, read mail, long-poll, SSE and webhooks.
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
//! HTTP API over interloper.
//!
//! | route | |
//! |---|---|
//! | `GET /health` | liveness, no auth, not rate limited |
//! | `GET /v1/providers` | measured health matrix |
//! | `DELETE /v1/providers/{name}/quarantine` | return a quarantined provider to routing |
//! | `POST /v1/inboxes` | issue an address |
//! | `GET /v1/inboxes/{addr}/messages` | list |
//! | `GET /v1/inboxes/{addr}/messages/{id}` | read one, with its OTP |
//! | `DELETE /v1/inboxes/{addr}/messages/{id}` | delete |
//! | `GET /v1/inboxes/{addr}/wait` | long-poll for the next (matching) message |
//! | `GET /v1/inboxes/{addr}/events` | Server-Sent Events: every message as it lands |
//! | `POST/GET /v1/webhooks`, `DELETE /v1/webhooks/{id}` | one-shot outbound webhooks |
//!
//! Every response is `{"success", "data", "error"}`. Auth is `X-API-Key`.
//! With a Postgres URL, instances share addresses and health: see [`fleet`].

pub mod config;
pub mod error;
pub mod fleet;
pub mod ratelimit;
pub mod service;
pub mod store;
pub mod webhooks;

use std::convert::Infallible;
use std::net::SocketAddr;
use std::sync::Arc;
use std::time::Duration;

use axum::extract::{ConnectInfo, Path, Query, Request as HttpRequest, State};
use axum::http::{HeaderValue, Method};
use axum::middleware::{self, Next};
use axum::response::sse::{Event, KeepAlive, Sse};
use axum::response::{IntoResponse, Response};
use axum::routing::{delete, get, post};
use axum::{Json, Router};
use futures::StreamExt;
use interloper::health::Status;
use interloper::regex::Regex;
use interloper::{Interloper, Kind, MailFilter, Message, Request};
use serde::Deserialize;
use serde_json::{Value, json};
use subtle::ConstantTimeEq;
use tower_http::cors::{AllowOrigin, CorsLayer};

use crate::config::Config;
use crate::error::ApiError;
use crate::ratelimit::Limiter;
use crate::service::Service;
use crate::store::Store;
use crate::webhooks::Webhooks;

/// The one response shape. `success` is derived from `error`, so a body can
/// never claim success while carrying one.
pub fn envelope(data: Value) -> Value {
    json!({"success": true, "data": data, "error": null})
}

pub fn envelope_err(msg: &str) -> Value {
    json!({"success": false, "data": null, "error": msg})
}

/// The one shape a message takes on the wire: `.../messages/{id}`, `wait`,
/// SSE and webhook payloads alike.
pub fn message_json(m: &Message, otp: Option<&str>) -> Value {
    json!({
        "id": m.id,
        "sender": m.sender,
        "subject": m.subject,
        "received_at": m.received_at.map(|t| t.to_string()),
        "html": m.html,
        "text": m.text,
        "links": m.links,
        "otp": otp,
    })
}

pub struct AppState {
    pub config: Config,
    pub service: Arc<Service>,
    pub webhooks: Arc<Webhooks>,
    limiter: Limiter,
}

type St = State<Arc<AppState>>;

impl AppState {
    pub fn new(config: Config, il: Interloper, store: Store) -> Arc<Self> {
        Self::from_service(config, Service::new(il, store))
    }

    pub fn from_service(config: Config, service: Arc<Service>) -> Arc<Self> {
        let webhooks = Webhooks::new(
            service.clone(),
            config.webhook_allow_private,
            config.webhook_secret.clone(),
        );
        let limiter = Limiter::new(config.rate_limit.0, config.rate_limit.1);
        Arc::new(AppState {
            config,
            service,
            webhooks,
            limiter,
        })
    }

    fn key_valid(&self, candidate: &str) -> bool {
        // Constant-time against every configured key, so timing cannot
        // recover a key byte by byte.
        let mut ok = 0u8;
        for k in &self.config.api_keys {
            ok |= u8::from(bool::from(candidate.as_bytes().ct_eq(k.as_bytes())));
        }
        ok == 1
    }
}

async fn auth(State(s): St, req: HttpRequest, next: Next) -> Result<Response, ApiError> {
    let key = req
        .headers()
        .get("x-api-key")
        .and_then(|v| v.to_str().ok())
        .unwrap_or("");
    if key.is_empty() || !s.key_valid(key) {
        return Err(ApiError::Unauthorized);
    }
    Ok(next.run(req).await)
}

async fn rate_limit(State(s): St, req: HttpRequest, next: Next) -> Result<Response, ApiError> {
    let ip = req
        .extensions()
        .get::<ConnectInfo<SocketAddr>>()
        .map_or_else(|| "unknown".to_string(), |c| c.0.ip().to_string());
    let key = req
        .headers()
        .get("x-api-key")
        .and_then(|v| v.to_str().ok())
        .unwrap_or("");
    let bucket = if !key.is_empty() && s.key_valid(key) {
        format!("{ip}|{key}")
    } else {
        ip
    };
    if !s.limiter.allow(&bucket) {
        return Err(ApiError::RateLimited);
    }
    Ok(next.run(req).await)
}

async fn health() -> Json<Value> {
    Json(envelope(json!({"status": "ok"})))
}

async fn providers(State(s): St) -> Json<Value> {
    let il = &s.service.il;
    let rows: Vec<Value> = il
        .registry()
        .specs()
        .map(|spec| {
            let h = il.health();
            let mut status = h.status(spec.name).to_string();
            if h.status(spec.name) == Status::Down && h.tier(spec.name).is_some() {
                status = "probation".into();
            }
            // Routable by this instance's own measurements, but taken out by
            // a majority of its peers (fleet mode only).
            let fleet_down = s.service.fleet.as_ref().is_some_and(|f| {
                h.status(spec.name) != Status::Quarantined
                    && f.view()
                        .vetoes(spec.name, h.get(spec.name).as_ref(), jiff::Timestamp::now())
            });
            json!({
                "provider": spec.name,
                "status": status,
                "fleet_down": fleet_down,
                "p50": h.p50(spec.name),
                "kind": spec.caps.kind.iter().map(|k| k.as_str()).collect::<Vec<_>>(),
                "sites": spec.caps.sites,
                "needs_solver": spec.caps.needs_solver,
            })
        })
        .collect();
    Json(envelope(Value::Array(rows)))
}

async fn clear_quarantine(State(s): St, Path(name): Path<String>) -> Result<Json<Value>, ApiError> {
    if !s.service.il.registry().contains(&name) {
        return Err(ApiError::NotFound);
    }
    let was = s.service.clear_quarantine(&name).await?;
    Ok(Json(envelope(json!({"cleared": was}))))
}

#[derive(Deserialize, Default)]
#[serde(deny_unknown_fields)]
struct CreateInbox {
    provider: Option<String>,
    kind: Option<Kind>,
    local: Option<String>,
    domain: Option<String>,
    #[serde(default)]
    domain_not_in: Vec<String>,
    address_ttl_seconds: Option<u64>,
    message_ttl_seconds: Option<u64>,
}

async fn create_inbox(
    State(s): St,
    body: Option<Json<CreateInbox>>,
) -> Result<Json<Value>, ApiError> {
    let b = body.map(|Json(b)| b).unwrap_or_default();
    let req = Request {
        provider: b.provider,
        kind: b.kind,
        local: b.local,
        domain: b.domain,
        domain_not_in: b.domain_not_in,
        address_ttl: b.address_ttl_seconds.map(Duration::from_secs),
        message_ttl: b.message_ttl_seconds.map(Duration::from_secs),
        ..Default::default()
    };
    let a = s.service.create(&req).await?;
    // `state` is deliberately absent: a provider credential the server holds
    // so the client never has to.
    Ok(Json(envelope(json!({
        "address": a.value,
        "provider": a.provider,
        "expires_at": a.expires_at.map(|t| t.to_string()),
    }))))
}

async fn list_messages(State(s): St, Path(addr): Path<String>) -> Result<Json<Value>, ApiError> {
    let rows = s.service.inbox(&addr).await?.messages().await?;
    let data: Vec<Value> = rows
        .iter()
        .map(|r| {
            json!({"id": r.id, "sender": r.sender, "subject": r.subject,
                   "received_at": r.received_at.map(|t| t.to_string()), "locked": r.locked})
        })
        .collect();
    Ok(Json(envelope(Value::Array(data))))
}

async fn get_message(
    State(s): St,
    Path((addr, id)): Path<(String, String)>,
) -> Result<Json<Value>, ApiError> {
    let (m, code) = s.service.get_message(&addr, &id).await?;
    Ok(Json(envelope(message_json(&m, code.as_deref()))))
}

async fn delete_message(
    State(s): St,
    Path((addr, id)): Path<(String, String)>,
) -> Result<Json<Value>, ApiError> {
    s.service.inbox(&addr).await?.delete(&id).await?;
    Ok(Json(envelope(json!({"deleted": id}))))
}

#[derive(Deserialize)]
struct WaitQuery {
    timeout: Option<u64>,
    #[serde(default)]
    otp: bool,
    pattern: Option<String>,
    from: Option<String>,
    subject: Option<String>,
}

fn compile(pattern: Option<&str>) -> Result<Option<Regex>, ApiError> {
    pattern
        .map(|p| {
            // Bounded: a caller-supplied regex must not be able to build an
            // enormous automaton on the server.
            interloper::regex::RegexBuilder::new(p)
                .size_limit(1 << 20)
                .build()
                .map_err(|_| ApiError::BadRequest("invalid pattern".into()))
        })
        .transpose()
}

async fn wait(
    State(s): St,
    Path(addr): Path<String>,
    Query(q): Query<WaitQuery>,
) -> Result<Json<Value>, ApiError> {
    if q.timeout == Some(0) {
        return Err(ApiError::BadRequest("timeout must be at least 1".into()));
    }
    let held = s.config.clamp_wait(q.timeout);
    let pattern = compile(q.pattern.as_deref())?;
    let filter = MailFilter {
        from: q.from,
        subject: q.subject,
    };
    let found = s
        .service
        .wait(&addr, held, pattern.as_ref(), q.otp, &filter)
        .await?;
    Ok(Json(envelope(match found {
        Some((m, code)) => message_json(&m, code.as_deref()),
        None => Value::Null,
    })))
}

/// Every message as it lands, as Server-Sent Events. Long-poll `wait`
/// answers one message per request; this holds one connection for a stream.
async fn events(State(s): St, Path(addr): Path<String>) -> Result<impl IntoResponse, ApiError> {
    let inbox = s.service.inbox(&addr).await?;
    let stream = async_stream::stream! {
        let mut mail = inbox.watch(None);
        while let Some(m) = mail.next().await {
            match m {
                Ok(m) => {
                    let code = interloper::parse::extract_otp(&m.subject, &m.text, None);
                    yield Ok::<_, Infallible>(Event::default().event("message").json_data(message_json(&m, code.as_deref())).expect("json"));
                }
                Err(e) => {
                    let (_, msg) = error::status_for(&e);
                    yield Ok(Event::default().event("error").data(msg));
                    break;
                }
            }
        }
    };
    Ok(Sse::new(stream).keep_alive(KeepAlive::default()))
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct CreateWebhook {
    address: String,
    url: String,
    timeout: Option<u64>,
}

async fn create_webhook(
    State(s): St,
    Json(b): Json<CreateWebhook>,
) -> Result<Json<Value>, ApiError> {
    // Resolve first, so an address this server never issued fails now with a
    // 404 rather than later as a silent timeout.
    s.service.inbox(&b.address).await?;
    if b.timeout == Some(0) {
        return Err(ApiError::BadRequest("timeout must be at least 1".into()));
    }
    let timeout = s.config.clamp_webhook(b.timeout);
    // The one place a raw error reaches a client: it is about the caller's
    // own URL, which they already have.
    let id = s
        .webhooks
        .create(b.address.to_lowercase(), &b.url, timeout)
        .await
        .map_err(ApiError::BadRequest)?;
    Ok(Json(envelope(json!({"id": id}))))
}

async fn list_webhooks(State(s): St) -> Json<Value> {
    Json(envelope(serde_json::to_value(s.webhooks.list()).unwrap()))
}

async fn delete_webhook(State(s): St, Path(id): Path<String>) -> Json<Value> {
    Json(envelope(json!({"cancelled": s.webhooks.cancel(&id)})))
}

async fn fallback() -> ApiError {
    ApiError::NotFound
}

pub fn router(state: Arc<AppState>) -> Router {
    let v1 = Router::new()
        .route("/providers", get(providers))
        .route("/providers/{name}/quarantine", delete(clear_quarantine))
        .route("/inboxes", post(create_inbox))
        .route("/inboxes/{addr}/messages", get(list_messages))
        .route(
            "/inboxes/{addr}/messages/{id}",
            get(get_message).delete(delete_message),
        )
        .route("/inboxes/{addr}/wait", get(wait))
        .route("/inboxes/{addr}/events", get(events))
        .route("/webhooks", post(create_webhook).get(list_webhooks))
        .route("/webhooks/{id}", delete(delete_webhook))
        .route_layer(middleware::from_fn_with_state(state.clone(), auth))
        .layer(middleware::from_fn_with_state(state.clone(), rate_limit));
    let mut app = Router::new()
        .route("/health", get(health))
        .nest("/v1", v1)
        .fallback(fallback)
        .with_state(state.clone());
    if !state.config.allowed_origins.is_empty() {
        let origins: Vec<HeaderValue> = state
            .config
            .allowed_origins
            .iter()
            .filter_map(|o| o.parse().ok())
            .collect();
        app = app.layer(
            CorsLayer::new()
                .allow_origin(AllowOrigin::list(origins))
                .allow_methods([Method::GET, Method::POST, Method::DELETE])
                .allow_headers([
                    axum::http::header::CONTENT_TYPE,
                    "x-api-key".parse().unwrap(),
                ]),
        );
    }
    app
}

/// Run the server until Ctrl-C/SIGTERM. Refuses to start without API keys.
pub async fn serve(config: Config, il: Interloper) -> Result<(), String> {
    if config.api_keys.is_empty() {
        return Err("INTERLOPER_API_KEYS is empty; refusing to start an open server".into());
    }
    let store = match (&config.database_url, &config.database) {
        // A silent fall back to memory while an operator believes state is
        // durable would be invisible until reads start missing.
        (Some(url), _) => Store::postgres(url)
            .await
            .map_err(|e| format!("postgres database: {e}"))?,
        (None, Some(p)) => {
            Store::sqlite(p).map_err(|e| format!("database {}: {e}", p.display()))?
        }
        (None, None) => Store::memory(),
    };
    let fleet = match store.pg_pool() {
        Some(pool) => {
            let sync = fleet::HealthSync::new(
                pool.clone(),
                il.health().clone(),
                config.instance_id.clone(),
                config.health_sync_every * 3,
            );
            sync.load()
                .await
                .map_err(|e| format!("loading fleet health: {e}"))?;
            tracing::info!(instance = %config.instance_id, "fleet mode: health shared via postgres");
            Some(sync)
        }
        None => None,
    };
    let bind = config.bind;
    let every = config.maintenance_every;
    let sync_every = config.health_sync_every;
    let state = AppState::from_service(config, Service::with_fleet(il, store, fleet.clone()));
    let maint = state.clone();
    let maintenance = tokio::spawn(async move {
        loop {
            tokio::time::sleep(every).await;
            let _ = maint.service.il.save_health();
            if let Ok(n) = maint.service.store.sweep().await
                && n > 0
            {
                tracing::info!(reaped = n, "expired addresses swept");
            }
        }
    });
    // Never fatal: a database blip leaves this instance routing on its own
    // measurements until the next round succeeds.
    let syncer = fleet.clone().map(|f| {
        tokio::spawn(async move {
            loop {
                tokio::time::sleep(sync_every).await;
                if let Err(e) = f.sync().await {
                    tracing::warn!(error = %e, "fleet health sync failed; routing on local health");
                }
            }
        })
    });
    let listener = tokio::net::TcpListener::bind(bind)
        .await
        .map_err(|e| format!("bind {bind}: {e}"))?;
    tracing::info!(%bind, "interloper server listening");
    let app = router(state.clone()).into_make_service_with_connect_info::<SocketAddr>();
    let result = axum::serve(listener, app)
        .with_graceful_shutdown(shutdown_signal())
        .await;
    maintenance.abort();
    if let Some(t) = syncer {
        t.abort();
    }
    if let Some(f) = &fleet {
        let _ = f.sync().await;
    }
    state.webhooks.shutdown();
    // Measurements taken while serving are worth keeping; a read-only disk
    // must not fail shutdown.
    let _ = state.service.il.save_health();
    result.map_err(|e| e.to_string())
}

async fn shutdown_signal() {
    let ctrl_c = async {
        let _ = tokio::signal::ctrl_c().await;
    };
    #[cfg(unix)]
    let term = async {
        if let Ok(mut s) = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())
        {
            s.recv().await;
        }
    };
    #[cfg(not(unix))]
    let term = std::future::pending::<()>();
    tokio::select! { _ = ctrl_c => {}, _ = term => {} }
}

#[cfg(test)]
mod tests;

#[cfg(test)]
mod fleet_tests;