first commit
This commit is contained in:
commit
6d3ea3e2a6
43 files changed
+8204
No files matched your search
@@ -0,0 +1,34 @@
|
||||
[package]
|
||||
name = "meow-server"
|
||||
description = "Meow v2 relay server (meowsrv)"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
|
||||
[[bin]]
|
||||
name = "meowsrv"
|
||||
path = "src/main.rs"
|
||||
|
||||
[dependencies]
|
||||
meow-core = { path = "../meow-core" }
|
||||
meow-proto = { path = "../meow-proto" }
|
||||
meow-crypto = { path = "../meow-crypto" }
|
||||
axum = "0.8"
|
||||
tokio = { version = "1", features = ["rt-multi-thread", "macros", "net", "time", "sync", "signal"] }
|
||||
async-trait = "0.1"
|
||||
redis = { version = "0.32", default-features = false, features = ["aio", "tokio-comp"] }
|
||||
futures-util = "0.3"
|
||||
clap = { version = "4", features = ["derive", "env"] }
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
toml = "0.9"
|
||||
tracing = "0.1"
|
||||
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
|
||||
thiserror = "2"
|
||||
anyhow = "1"
|
||||
subtle = "2"
|
||||
|
||||
[dev-dependencies]
|
||||
reqwest = { version = "0.12", default-features = false }
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
@@ -0,0 +1,139 @@
|
||||
//! Server configuration: command line > environment (`MEOWSRV_*`) > TOML file > defaults.
|
||||
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::time::Duration;
|
||||
|
||||
use clap::Parser;
|
||||
use meow_core::server::ServerKeys;
|
||||
use meow_crypto::{DhKeyPair, SigningKeyPair};
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Parser, Debug, Default)]
|
||||
#[command(name = "meowsrv", version, about = "Meow v2 relay server")]
|
||||
pub struct Args {
|
||||
/// TOML configuration file.
|
||||
#[arg(short, long, env = "MEOWSRV_CONFIG")]
|
||||
pub config: Option<PathBuf>,
|
||||
/// Listen address, e.g. 0.0.0.0:8080.
|
||||
#[arg(long, env = "MEOWSRV_LISTEN")]
|
||||
pub listen: Option<String>,
|
||||
/// Public URL announced in the server card (defaults to http://<listen>).
|
||||
#[arg(long, env = "MEOWSRV_PUBLIC_URL")]
|
||||
pub public_url: Option<String>,
|
||||
#[arg(long, env = "MEOWSRV_NAME")]
|
||||
pub name: Option<String>,
|
||||
#[arg(long, env = "MEOWSRV_DESCRIPTION")]
|
||||
pub description: Option<String>,
|
||||
/// `memory` or a Redis URL (`redis://host:6379/0`).
|
||||
#[arg(long, env = "MEOWSRV_STORAGE")]
|
||||
pub storage: Option<String>,
|
||||
/// Private key file, created with mode 0600 if missing.
|
||||
#[arg(long, env = "MEOWSRV_KEY_FILE")]
|
||||
pub key_file: Option<PathBuf>,
|
||||
/// Days a message is kept in a mailbox (Redis backend).
|
||||
#[arg(long, env = "MEOWSRV_MAILBOX_TTL_DAYS")]
|
||||
pub mailbox_ttl_days: Option<u64>,
|
||||
/// Log filter (tracing EnvFilter syntax), e.g. `info` or `meow_server=debug`.
|
||||
#[arg(long, env = "MEOWSRV_LOG")]
|
||||
pub log: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug, Default)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct FileConfig {
|
||||
listen: Option<String>,
|
||||
public_url: Option<String>,
|
||||
name: Option<String>,
|
||||
description: Option<String>,
|
||||
storage: Option<String>,
|
||||
key_file: Option<PathBuf>,
|
||||
mailbox_ttl_days: Option<u64>,
|
||||
log: Option<String>,
|
||||
}
|
||||
|
||||
/// Resolved configuration.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Config {
|
||||
pub listen: String,
|
||||
pub public_url: String,
|
||||
pub name: String,
|
||||
pub description: String,
|
||||
pub storage: String,
|
||||
pub key_file: PathBuf,
|
||||
pub mailbox_ttl: Duration,
|
||||
pub log: String,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub fn resolve(args: Args) -> anyhow::Result<Self> {
|
||||
let file: FileConfig = match &args.config {
|
||||
Some(p) => toml::from_str(&std::fs::read_to_string(p)?)?,
|
||||
None => FileConfig::default(),
|
||||
};
|
||||
let listen = args.listen.or(file.listen).unwrap_or_else(|| "127.0.0.1:8080".into());
|
||||
let public_url = args
|
||||
.public_url
|
||||
.or(file.public_url)
|
||||
.unwrap_or_else(|| format!("http://{listen}"));
|
||||
Ok(Self {
|
||||
public_url,
|
||||
listen,
|
||||
name: args.name.or(file.name).unwrap_or_else(|| "meow server".into()),
|
||||
description: args.description.or(file.description).unwrap_or_default(),
|
||||
storage: args.storage.or(file.storage).unwrap_or_else(|| "memory".into()),
|
||||
key_file: args.key_file.or(file.key_file).unwrap_or_else(|| "server.key".into()),
|
||||
mailbox_ttl: Duration::from_secs(86_400 * args.mailbox_ttl_days.or(file.mailbox_ttl_days).unwrap_or(30)),
|
||||
log: args.log.or(file.log).unwrap_or_else(|| "info".into()),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Load the server keys, or generate them (file mode 0600) on first start.
|
||||
///
|
||||
/// Format: one line of hex, `sign_secret (32) ‖ dh_secret (32)`.
|
||||
pub fn load_or_create_keys(path: &Path) -> anyhow::Result<ServerKeys> {
|
||||
if path.exists() {
|
||||
let s = std::fs::read_to_string(path)?;
|
||||
let b = meow_core::from_hex(s.trim()).ok_or_else(|| anyhow::anyhow!("key file is not hex"))?;
|
||||
anyhow::ensure!(b.len() == 64, "key file must contain 64 bytes");
|
||||
let mut sign = [0u8; 32];
|
||||
let mut dh = [0u8; 32];
|
||||
sign.copy_from_slice(&b[..32]);
|
||||
dh.copy_from_slice(&b[32..]);
|
||||
return Ok(ServerKeys {
|
||||
sign: SigningKeyPair::from_secret(sign),
|
||||
dh: DhKeyPair::from_secret(dh),
|
||||
});
|
||||
}
|
||||
let keys = ServerKeys::generate();
|
||||
let hex: String = keys
|
||||
.sign
|
||||
.secret_bytes()
|
||||
.iter()
|
||||
.chain(keys.dh.secret_bytes())
|
||||
.map(|x| format!("{x:02x}"))
|
||||
.collect();
|
||||
if let Some(dir) = path.parent().filter(|d| !d.as_os_str().is_empty()) {
|
||||
std::fs::create_dir_all(dir)?;
|
||||
}
|
||||
write_private(path, hex.as_bytes())?;
|
||||
tracing::warn!("generated new server keys in {}", path.display());
|
||||
Ok(keys)
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
fn write_private(path: &Path, data: &[u8]) -> std::io::Result<()> {
|
||||
use std::io::Write;
|
||||
use std::os::unix::fs::OpenOptionsExt;
|
||||
let mut f = std::fs::OpenOptions::new()
|
||||
.write(true)
|
||||
.create_new(true)
|
||||
.mode(0o600)
|
||||
.open(path)?;
|
||||
f.write_all(data)
|
||||
}
|
||||
|
||||
#[cfg(not(unix))]
|
||||
fn write_private(path: &Path, data: &[u8]) -> std::io::Result<()> {
|
||||
std::fs::write(path, data)
|
||||
}
|
||||
@@ -0,0 +1,233 @@
|
||||
//! Meow v2 relay server.
|
||||
//!
|
||||
//! Routes:
|
||||
//! * `GET /v2/card` – self-signed `ServerCard` (protobuf);
|
||||
//! * `POST /v2/rpc` – `SealedRequest` → `SealedResponse`;
|
||||
//! * `GET /healthz`.
|
||||
//!
|
||||
//! The server never sees message contents nor who talks to whom: deposits are
|
||||
//! anonymous, and pulls are authorised by a signature of the mailbox key.
|
||||
|
||||
#![forbid(unsafe_code)]
|
||||
#![cfg_attr(not(test), warn(clippy::unwrap_used, clippy::expect_used))]
|
||||
|
||||
pub mod config;
|
||||
pub mod store;
|
||||
|
||||
use std::collections::HashSet;
|
||||
use std::sync::Arc;
|
||||
use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
||||
|
||||
use axum::body::Bytes;
|
||||
use axum::extract::{DefaultBodyLimit, State};
|
||||
use axum::http::{header, StatusCode};
|
||||
use axum::response::IntoResponse;
|
||||
use axum::routing::{get, post};
|
||||
use axum::Router;
|
||||
use meow_core::rpc;
|
||||
use meow_core::server::ServerKeys;
|
||||
use meow_proto::v2::{self, error::Code, request::Body as Req, response::Body as Resp};
|
||||
use meow_proto::Message;
|
||||
use tokio::sync::broadcast;
|
||||
|
||||
use crate::store::{Mailbox, ServerStore, StoreError};
|
||||
|
||||
/// Operational limits.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Limits {
|
||||
pub max_body_bytes: usize,
|
||||
pub max_deposits: usize,
|
||||
pub max_payload_bytes: usize,
|
||||
pub max_mailboxes_per_pull: usize,
|
||||
pub max_wait: Duration,
|
||||
pub max_invitation_ttl: Duration,
|
||||
pub replay_ttl: Duration,
|
||||
}
|
||||
|
||||
impl Default for Limits {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
max_body_bytes: 16 * 1024 * 1024,
|
||||
max_deposits: 128,
|
||||
max_payload_bytes: 8 * 1024 * 1024,
|
||||
max_mailboxes_per_pull: 512,
|
||||
max_wait: Duration::from_secs(120),
|
||||
max_invitation_ttl: Duration::from_secs(7 * 86_400),
|
||||
replay_ttl: Duration::from_secs(2 * rpc::AUTH_WINDOW_SECS),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Shared state of a running server.
|
||||
pub struct AppState {
|
||||
pub keys: ServerKeys,
|
||||
pub card: Vec<u8>,
|
||||
pub store: Arc<dyn ServerStore>,
|
||||
pub notify: broadcast::Sender<Mailbox>,
|
||||
pub limits: Limits,
|
||||
}
|
||||
|
||||
pub fn now() -> u64 {
|
||||
SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.map(|d| d.as_secs())
|
||||
.unwrap_or(0)
|
||||
}
|
||||
|
||||
/// Build the HTTP router.
|
||||
pub fn router(state: Arc<AppState>) -> Router {
|
||||
let limit = state.limits.max_body_bytes;
|
||||
Router::new()
|
||||
.route("/v2/card", get(card))
|
||||
.route("/v2/rpc", post(rpc_handler))
|
||||
.route("/healthz", get(|| async { "ok" }))
|
||||
.layer(DefaultBodyLimit::max(limit))
|
||||
.with_state(state)
|
||||
}
|
||||
|
||||
async fn card(State(st): State<Arc<AppState>>) -> impl IntoResponse {
|
||||
([(header::CONTENT_TYPE, "application/x-protobuf")], st.card.clone())
|
||||
}
|
||||
|
||||
async fn rpc_handler(State(st): State<Arc<AppState>>, body: Bytes) -> impl IntoResponse {
|
||||
let (req, key) = match rpc::open_request(&st.keys, &body) {
|
||||
Ok(x) => x,
|
||||
Err(_) => return (StatusCode::BAD_REQUEST, Vec::new()),
|
||||
};
|
||||
let rid = req.request_id.clone();
|
||||
let resp = match handle(&st, req).await {
|
||||
Ok(body) => v2::Response {
|
||||
request_id: rid,
|
||||
body: Some(body),
|
||||
},
|
||||
Err((code, msg)) => {
|
||||
tracing::debug!(?code, msg, "request rejected");
|
||||
rpc::error_response(rid, code, msg)
|
||||
}
|
||||
};
|
||||
(StatusCode::OK, rpc::seal_response(&key, &resp))
|
||||
}
|
||||
|
||||
type HResult = Result<Resp, (Code, &'static str)>;
|
||||
|
||||
fn store_err(e: StoreError) -> (Code, &'static str) {
|
||||
match e {
|
||||
StoreError::NotFound => (Code::NotFound, "not found"),
|
||||
StoreError::Unauthorized => (Code::Unauthorized, "bad password"),
|
||||
StoreError::Locked => (Code::Unauthorized, "invitation destroyed after too many attempts"),
|
||||
StoreError::Backend(e) => {
|
||||
tracing::error!("store error: {e}");
|
||||
(Code::Internal, "storage error")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn handle(st: &AppState, req: v2::Request) -> HResult {
|
||||
let now = now();
|
||||
if req.request_id.len() != 16 {
|
||||
return Err((Code::BadRequest, "bad request id"));
|
||||
}
|
||||
if req.timestamp.abs_diff(now) > rpc::AUTH_WINDOW_SECS {
|
||||
return Err((Code::Expired, "request timestamp out of window"));
|
||||
}
|
||||
if !st
|
||||
.store
|
||||
.mark_request(&req.request_id, st.limits.replay_ttl)
|
||||
.await
|
||||
.map_err(store_err)?
|
||||
{
|
||||
return Err((Code::Unauthorized, "replayed request"));
|
||||
}
|
||||
match req.body.ok_or((Code::BadRequest, "empty request"))? {
|
||||
Req::Ping(_) => Ok(Resp::Pong(v2::Pong { server_time: now })),
|
||||
Req::Post(p) => post_messages(st, p, now).await,
|
||||
Req::Pull(p) => pull(st, &req.request_id, p, now).await,
|
||||
Req::InvPublish(p) => {
|
||||
if p.payload.len() > 64 * 1024 {
|
||||
return Err((Code::TooLarge, "invitation too large"));
|
||||
}
|
||||
let ttl = Duration::from_secs(p.ttl_seconds.max(60) as u64).min(st.limits.max_invitation_ttl);
|
||||
let len = (p.shortcode_len as usize).clamp(6, 64);
|
||||
let code = st
|
||||
.store
|
||||
.put_invitation(p.payload, &p.password, ttl, len)
|
||||
.await
|
||||
.map_err(store_err)?;
|
||||
Ok(Resp::InvPublished(v2::InvitationPublished {
|
||||
shortcode: code,
|
||||
expires_at: now + ttl.as_secs(),
|
||||
}))
|
||||
}
|
||||
Req::InvFetch(f) => {
|
||||
let payload = st
|
||||
.store
|
||||
.get_invitation(&f.shortcode, &f.password)
|
||||
.await
|
||||
.map_err(store_err)?;
|
||||
Ok(Resp::InvFetched(v2::InvitationFetched { payload }))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn post_messages(st: &AppState, p: v2::Post, now: u64) -> HResult {
|
||||
if p.deposits.is_empty() || p.deposits.len() > st.limits.max_deposits {
|
||||
return Err((Code::BadRequest, "bad number of deposits"));
|
||||
}
|
||||
let mut accepted = 0u32;
|
||||
for d in p.deposits {
|
||||
let mb: Mailbox = d
|
||||
.mailbox
|
||||
.as_slice()
|
||||
.try_into()
|
||||
.map_err(|_| (Code::BadRequest, "bad mailbox"))?;
|
||||
if d.payload.len() > st.limits.max_payload_bytes {
|
||||
return Err((Code::TooLarge, "payload too large"));
|
||||
}
|
||||
st.store.post(mb, d.payload, now).await.map_err(store_err)?;
|
||||
accepted += 1;
|
||||
}
|
||||
Ok(Resp::PostAck(v2::PostAck { accepted }))
|
||||
}
|
||||
|
||||
async fn pull(st: &AppState, request_id: &[u8], p: v2::Pull, now: u64) -> HResult {
|
||||
if p.auths.is_empty() || p.auths.len() > st.limits.max_mailboxes_per_pull {
|
||||
return Err((Code::BadRequest, "bad number of mailboxes"));
|
||||
}
|
||||
let sign = st.keys.sign.public();
|
||||
let mut mailboxes = Vec::with_capacity(p.auths.len());
|
||||
for a in &p.auths {
|
||||
let mb = rpc::verify_mailbox_auth(&sign, request_id, a, now)
|
||||
.map_err(|_| (Code::Unauthorized, "bad mailbox auth"))?;
|
||||
mailboxes.push(mb.0);
|
||||
}
|
||||
// Subscribe before draining, so a deposit landing in between is not missed.
|
||||
let mut rx = st.notify.subscribe();
|
||||
let mut messages = st.store.drain(&mailboxes).await.map_err(store_err)?;
|
||||
let wait = Duration::from_secs(p.wait_seconds as u64).min(st.limits.max_wait);
|
||||
if messages.is_empty() && !wait.is_zero() {
|
||||
let wanted: HashSet<Mailbox> = mailboxes.iter().copied().collect();
|
||||
let _ = tokio::time::timeout(wait, async {
|
||||
loop {
|
||||
match rx.recv().await {
|
||||
Ok(mb) if wanted.contains(&mb) => break,
|
||||
Ok(_) => continue,
|
||||
// Lagged: we may have missed ours, check anyway. Closed: stop.
|
||||
Err(_) => break,
|
||||
}
|
||||
}
|
||||
})
|
||||
.await;
|
||||
messages = st.store.drain(&mailboxes).await.map_err(store_err)?;
|
||||
}
|
||||
Ok(Resp::PullResult(v2::PullResult { messages }))
|
||||
}
|
||||
|
||||
/// Serve on an already bound listener until the future is dropped.
|
||||
pub async fn serve(listener: tokio::net::TcpListener, state: Arc<AppState>) -> std::io::Result<()> {
|
||||
axum::serve(listener, router(state)).await
|
||||
}
|
||||
|
||||
/// Encode the card for `GET /v2/card`.
|
||||
pub fn encode_card(keys: &ServerKeys, url: &str, name: &str, description: &str) -> Vec<u8> {
|
||||
keys.card(url, name, description, now()).encode_to_vec()
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
//! `meowsrv`: Meow v2 relay server.
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use clap::Parser;
|
||||
use meow_server::config::{load_or_create_keys, Args, Config};
|
||||
use meow_server::store::{MemoryStore, RedisStore, ServerStore};
|
||||
use meow_server::{encode_card, router, AppState, Limits};
|
||||
use tokio::sync::broadcast;
|
||||
|
||||
const MAX_PER_MAILBOX: usize = 10_000;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
let cfg = Config::resolve(Args::parse())?;
|
||||
tracing_subscriber::fmt()
|
||||
.with_env_filter(tracing_subscriber::EnvFilter::new(&cfg.log))
|
||||
.init();
|
||||
|
||||
let keys = load_or_create_keys(&cfg.key_file)?;
|
||||
let (notify, _) = broadcast::channel(4096);
|
||||
let store: Arc<dyn ServerStore> = if cfg.storage == "memory" {
|
||||
tracing::warn!("using in-memory storage: messages are lost on restart");
|
||||
Arc::new(MemoryStore::new(notify.clone(), MAX_PER_MAILBOX))
|
||||
} else {
|
||||
Arc::new(RedisStore::connect(&cfg.storage, notify.clone(), cfg.mailbox_ttl, MAX_PER_MAILBOX).await?)
|
||||
};
|
||||
let card = encode_card(&keys, &cfg.public_url, &cfg.name, &cfg.description);
|
||||
tracing::info!(
|
||||
url = %cfg.public_url,
|
||||
sign_key = %keys.sign.public().fingerprint(),
|
||||
"server identity"
|
||||
);
|
||||
let state = Arc::new(AppState {
|
||||
keys,
|
||||
card,
|
||||
store,
|
||||
notify,
|
||||
limits: Limits::default(),
|
||||
});
|
||||
|
||||
let listener = tokio::net::TcpListener::bind(&cfg.listen).await?;
|
||||
tracing::info!("listening on {}", cfg.listen);
|
||||
axum::serve(listener, router(state))
|
||||
.with_graceful_shutdown(async {
|
||||
let _ = tokio::signal::ctrl_c().await;
|
||||
})
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,456 @@
|
||||
//! Server storage abstraction with an in-memory and a Redis backend.
|
||||
//!
|
||||
//! New-message notifications go through a `broadcast` channel owned by the app
|
||||
//! state: the memory store publishes to it directly; the Redis store publishes on a
|
||||
//! Redis channel and a single background task per server instance forwards those
|
||||
//! events (so several `meowsrv` instances can share one Redis).
|
||||
|
||||
use std::collections::{HashMap, VecDeque};
|
||||
use std::sync::Mutex;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use async_trait::async_trait;
|
||||
use meow_proto::{v2, Message};
|
||||
use subtle::ConstantTimeEq;
|
||||
use tokio::sync::broadcast;
|
||||
|
||||
pub type Mailbox = [u8; 32];
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum StoreError {
|
||||
#[error("not found")]
|
||||
NotFound,
|
||||
#[error("unauthorized")]
|
||||
Unauthorized,
|
||||
#[error("invitation destroyed after too many failed attempts")]
|
||||
Locked,
|
||||
#[error("backend: {0}")]
|
||||
Backend(String),
|
||||
}
|
||||
|
||||
impl From<redis::RedisError> for StoreError {
|
||||
fn from(e: redis::RedisError) -> Self {
|
||||
StoreError::Backend(e.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
pub type StoreResult<T> = Result<T, StoreError>;
|
||||
|
||||
/// Maximum failed password attempts before an invitation is destroyed.
|
||||
pub const MAX_INVITATION_ATTEMPTS: i64 = 3;
|
||||
|
||||
#[async_trait]
|
||||
pub trait ServerStore: Send + Sync {
|
||||
/// Append a payload to a mailbox and notify waiters.
|
||||
async fn post(&self, mailbox: Mailbox, payload: Vec<u8>, now: u64) -> StoreResult<()>;
|
||||
/// Atomically take every message of the given mailboxes.
|
||||
async fn drain(&self, mailboxes: &[Mailbox]) -> StoreResult<Vec<v2::StoredMessage>>;
|
||||
/// Record a request id; returns `false` if it was already seen (replay).
|
||||
async fn mark_request(&self, request_id: &[u8], ttl: Duration) -> StoreResult<bool>;
|
||||
/// Store an invitation under a fresh shortcode.
|
||||
async fn put_invitation(
|
||||
&self,
|
||||
payload: Vec<u8>,
|
||||
password: &str,
|
||||
ttl: Duration,
|
||||
code_len: usize,
|
||||
) -> StoreResult<String>;
|
||||
/// Fetch an invitation, enforcing the password and the attempt limit.
|
||||
async fn get_invitation(&self, code: &str, password: &str) -> StoreResult<Vec<u8>>;
|
||||
}
|
||||
|
||||
pub fn random_shortcode(len: usize) -> String {
|
||||
const ALPHABET: &[u8] = b"abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789";
|
||||
// Rejection sampling for an unbiased draw.
|
||||
let limit = 256 - (256 % ALPHABET.len());
|
||||
let mut out = String::with_capacity(len);
|
||||
while out.len() < len {
|
||||
for b in meow_crypto::random_bytes::<32>() {
|
||||
if (b as usize) < limit && out.len() < len {
|
||||
out.push(ALPHABET[b as usize % ALPHABET.len()] as char);
|
||||
}
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn password_ok(expected: &str, given: &str) -> bool {
|
||||
expected.as_bytes().ct_eq(given.as_bytes()).into()
|
||||
}
|
||||
|
||||
// ───────────────────────────── Memory ─────────────────────────────
|
||||
|
||||
struct MemInvitation {
|
||||
payload: Vec<u8>,
|
||||
password: String,
|
||||
failures: i64,
|
||||
expires: Instant,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct MemInner {
|
||||
mailboxes: HashMap<Mailbox, VecDeque<v2::StoredMessage>>,
|
||||
requests: HashMap<Vec<u8>, Instant>,
|
||||
invitations: HashMap<String, MemInvitation>,
|
||||
}
|
||||
|
||||
/// In-memory store, for development and tests. Data is lost on restart.
|
||||
pub struct MemoryStore {
|
||||
inner: Mutex<MemInner>,
|
||||
notify: broadcast::Sender<Mailbox>,
|
||||
max_per_mailbox: usize,
|
||||
}
|
||||
|
||||
impl MemoryStore {
|
||||
pub fn new(notify: broadcast::Sender<Mailbox>, max_per_mailbox: usize) -> Self {
|
||||
Self {
|
||||
inner: Mutex::new(MemInner::default()),
|
||||
notify,
|
||||
max_per_mailbox,
|
||||
}
|
||||
}
|
||||
|
||||
fn lock(&self) -> StoreResult<std::sync::MutexGuard<'_, MemInner>> {
|
||||
self.inner
|
||||
.lock()
|
||||
.map_err(|_| StoreError::Backend("poisoned lock".into()))
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl ServerStore for MemoryStore {
|
||||
async fn post(&self, mailbox: Mailbox, payload: Vec<u8>, now: u64) -> StoreResult<()> {
|
||||
{
|
||||
let mut g = self.lock()?;
|
||||
let q = g.mailboxes.entry(mailbox).or_default();
|
||||
q.push_back(v2::StoredMessage {
|
||||
mailbox: mailbox.to_vec(),
|
||||
payload,
|
||||
received_at: now,
|
||||
});
|
||||
while q.len() > self.max_per_mailbox {
|
||||
q.pop_front();
|
||||
}
|
||||
}
|
||||
let _ = self.notify.send(mailbox);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn drain(&self, mailboxes: &[Mailbox]) -> StoreResult<Vec<v2::StoredMessage>> {
|
||||
let mut g = self.lock()?;
|
||||
Ok(mailboxes
|
||||
.iter()
|
||||
.filter_map(|m| g.mailboxes.remove(m))
|
||||
.flatten()
|
||||
.collect())
|
||||
}
|
||||
|
||||
async fn mark_request(&self, request_id: &[u8], ttl: Duration) -> StoreResult<bool> {
|
||||
let mut g = self.lock()?;
|
||||
let now = Instant::now();
|
||||
g.requests.retain(|_, exp| *exp > now);
|
||||
Ok(g.requests.insert(request_id.to_vec(), now + ttl).is_none())
|
||||
}
|
||||
|
||||
async fn put_invitation(
|
||||
&self,
|
||||
payload: Vec<u8>,
|
||||
password: &str,
|
||||
ttl: Duration,
|
||||
code_len: usize,
|
||||
) -> StoreResult<String> {
|
||||
let mut g = self.lock()?;
|
||||
let now = Instant::now();
|
||||
g.invitations.retain(|_, i| i.expires > now);
|
||||
let code = loop {
|
||||
let c = random_shortcode(code_len);
|
||||
if !g.invitations.contains_key(&c) {
|
||||
break c;
|
||||
}
|
||||
};
|
||||
g.invitations.insert(
|
||||
code.clone(),
|
||||
MemInvitation {
|
||||
payload,
|
||||
password: password.to_string(),
|
||||
failures: 0,
|
||||
expires: now + ttl,
|
||||
},
|
||||
);
|
||||
Ok(code)
|
||||
}
|
||||
|
||||
async fn get_invitation(&self, code: &str, password: &str) -> StoreResult<Vec<u8>> {
|
||||
let mut g = self.lock()?;
|
||||
let now = Instant::now();
|
||||
let inv = match g.invitations.get_mut(code) {
|
||||
Some(i) if i.expires > now => i,
|
||||
_ => return Err(StoreError::NotFound),
|
||||
};
|
||||
if !inv.password.is_empty() && !password_ok(&inv.password, password) {
|
||||
inv.failures += 1;
|
||||
if inv.failures >= MAX_INVITATION_ATTEMPTS {
|
||||
g.invitations.remove(code);
|
||||
return Err(StoreError::Locked);
|
||||
}
|
||||
return Err(StoreError::Unauthorized);
|
||||
}
|
||||
inv.failures = 0;
|
||||
Ok(inv.payload.clone())
|
||||
}
|
||||
}
|
||||
|
||||
// ───────────────────────────── Redis ─────────────────────────────
|
||||
|
||||
/// Redis channel used to fan out new-message notifications between instances.
|
||||
pub const NOTIFY_CHANNEL: &str = "meow:v2:new";
|
||||
|
||||
/// Redis-backed store.
|
||||
///
|
||||
/// Keys: `meow:v2:mb:{hex}` (list of encoded `StoredMessage`), `meow:v2:rq:{hex}`
|
||||
/// (replay cache), `meow:v2:inv:{code}`, `meow:v2:invpw:{code}`, `meow:v2:invfa:{code}`.
|
||||
pub struct RedisStore {
|
||||
conn: redis::aio::MultiplexedConnection,
|
||||
mailbox_ttl: Duration,
|
||||
max_per_mailbox: usize,
|
||||
}
|
||||
|
||||
fn hexs(b: &[u8]) -> String {
|
||||
b.iter().map(|x| format!("{x:02x}")).collect()
|
||||
}
|
||||
|
||||
impl RedisStore {
|
||||
/// Connect and start the notification forwarder.
|
||||
pub async fn connect(
|
||||
url: &str,
|
||||
notify: broadcast::Sender<Mailbox>,
|
||||
mailbox_ttl: Duration,
|
||||
max_per_mailbox: usize,
|
||||
) -> StoreResult<Self> {
|
||||
let client = redis::Client::open(url)?;
|
||||
let conn = client.get_multiplexed_async_connection().await?;
|
||||
let mut pubsub = client.get_async_pubsub().await?;
|
||||
pubsub.subscribe(NOTIFY_CHANNEL).await?;
|
||||
tokio::spawn(async move {
|
||||
use futures_util::StreamExt;
|
||||
let mut stream = pubsub.on_message();
|
||||
while let Some(msg) = stream.next().await {
|
||||
if let Ok(mb) = <Mailbox>::try_from(msg.get_payload_bytes()) {
|
||||
let _ = notify.send(mb);
|
||||
}
|
||||
}
|
||||
tracing::error!("redis notification stream closed");
|
||||
});
|
||||
Ok(Self {
|
||||
conn,
|
||||
mailbox_ttl,
|
||||
max_per_mailbox,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl ServerStore for RedisStore {
|
||||
async fn post(&self, mailbox: Mailbox, payload: Vec<u8>, now: u64) -> StoreResult<()> {
|
||||
let key = format!("meow:v2:mb:{}", hexs(&mailbox));
|
||||
let item = v2::StoredMessage {
|
||||
mailbox: mailbox.to_vec(),
|
||||
payload,
|
||||
received_at: now,
|
||||
}
|
||||
.encode_to_vec();
|
||||
let mut c = self.conn.clone();
|
||||
let max = self.max_per_mailbox as isize;
|
||||
let _: () = redis::pipe()
|
||||
.atomic()
|
||||
.rpush(&key, item)
|
||||
.ignore()
|
||||
.ltrim(&key, -max, -1)
|
||||
.ignore()
|
||||
.expire(&key, self.mailbox_ttl.as_secs() as i64)
|
||||
.ignore()
|
||||
.publish(NOTIFY_CHANNEL, mailbox.as_slice())
|
||||
.ignore()
|
||||
.query_async(&mut c)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn drain(&self, mailboxes: &[Mailbox]) -> StoreResult<Vec<v2::StoredMessage>> {
|
||||
let mut out = Vec::new();
|
||||
let mut c = self.conn.clone();
|
||||
for mb in mailboxes {
|
||||
let key = format!("meow:v2:mb:{}", hexs(mb));
|
||||
let (items, _): (Vec<Vec<u8>>, i64) = redis::pipe()
|
||||
.atomic()
|
||||
.lrange(&key, 0, -1)
|
||||
.del(&key)
|
||||
.query_async(&mut c)
|
||||
.await?;
|
||||
for it in items {
|
||||
match v2::StoredMessage::decode(it.as_slice()) {
|
||||
Ok(m) => out.push(m),
|
||||
Err(e) => tracing::warn!("dropping corrupted stored message: {e}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
async fn mark_request(&self, request_id: &[u8], ttl: Duration) -> StoreResult<bool> {
|
||||
let mut c = self.conn.clone();
|
||||
let r: Option<String> = redis::cmd("SET")
|
||||
.arg(format!("meow:v2:rq:{}", hexs(request_id)))
|
||||
.arg(1)
|
||||
.arg("NX")
|
||||
.arg("EX")
|
||||
.arg(ttl.as_secs())
|
||||
.query_async(&mut c)
|
||||
.await?;
|
||||
Ok(r.is_some())
|
||||
}
|
||||
|
||||
async fn put_invitation(
|
||||
&self,
|
||||
payload: Vec<u8>,
|
||||
password: &str,
|
||||
ttl: Duration,
|
||||
code_len: usize,
|
||||
) -> StoreResult<String> {
|
||||
let mut c = self.conn.clone();
|
||||
loop {
|
||||
let code = random_shortcode(code_len);
|
||||
let ok: Option<String> = redis::cmd("SET")
|
||||
.arg(format!("meow:v2:inv:{code}"))
|
||||
.arg(payload.as_slice())
|
||||
.arg("NX")
|
||||
.arg("EX")
|
||||
.arg(ttl.as_secs())
|
||||
.query_async(&mut c)
|
||||
.await?;
|
||||
if ok.is_some() {
|
||||
if !password.is_empty() {
|
||||
let _: () = redis::cmd("SET")
|
||||
.arg(format!("meow:v2:invpw:{code}"))
|
||||
.arg(password)
|
||||
.arg("EX")
|
||||
.arg(ttl.as_secs())
|
||||
.query_async(&mut c)
|
||||
.await?;
|
||||
}
|
||||
return Ok(code);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn get_invitation(&self, code: &str, password: &str) -> StoreResult<Vec<u8>> {
|
||||
let mut c = self.conn.clone();
|
||||
let inv_key = format!("meow:v2:inv:{code}");
|
||||
let pw_key = format!("meow:v2:invpw:{code}");
|
||||
let fa_key = format!("meow:v2:invfa:{code}");
|
||||
let (payload, expected): (Option<Vec<u8>>, Option<String>) =
|
||||
redis::pipe().get(&inv_key).get(&pw_key).query_async(&mut c).await?;
|
||||
let payload = payload.ok_or(StoreError::NotFound)?;
|
||||
if let Some(expected) = expected {
|
||||
if !password_ok(&expected, password) {
|
||||
let (n, _): (i64, i64) = redis::pipe()
|
||||
.atomic()
|
||||
.incr(&fa_key, 1)
|
||||
.expire(&fa_key, 86_400)
|
||||
.query_async(&mut c)
|
||||
.await?;
|
||||
if n >= MAX_INVITATION_ATTEMPTS {
|
||||
let _: () = redis::pipe()
|
||||
.del(&inv_key)
|
||||
.del(&pw_key)
|
||||
.del(&fa_key)
|
||||
.query_async(&mut c)
|
||||
.await?;
|
||||
return Err(StoreError::Locked);
|
||||
}
|
||||
return Err(StoreError::Unauthorized);
|
||||
}
|
||||
let _: () = redis::cmd("DEL").arg(&fa_key).query_async(&mut c).await?;
|
||||
}
|
||||
Ok(payload)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn shortcodes() {
|
||||
let c = random_shortcode(8);
|
||||
assert_eq!(c.len(), 8);
|
||||
assert!(c.chars().all(|x| x.is_ascii_alphanumeric()));
|
||||
assert_ne!(random_shortcode(16), random_shortcode(16));
|
||||
}
|
||||
|
||||
async fn exercise(store: &dyn ServerStore, rx: &mut broadcast::Receiver<Mailbox>) {
|
||||
let mb = meow_crypto::random_bytes::<32>();
|
||||
store.post(mb, b"one".to_vec(), 1).await.unwrap();
|
||||
store.post(mb, b"two".to_vec(), 2).await.unwrap();
|
||||
assert_eq!(rx.recv().await.unwrap(), mb);
|
||||
let got = store.drain(&[mb]).await.unwrap();
|
||||
assert_eq!(
|
||||
got.iter().map(|m| m.payload.clone()).collect::<Vec<_>>(),
|
||||
vec![b"one".to_vec(), b"two".to_vec()]
|
||||
);
|
||||
assert!(store.drain(&[mb]).await.unwrap().is_empty());
|
||||
|
||||
let rid = meow_crypto::random_bytes::<16>();
|
||||
assert!(store.mark_request(&rid, Duration::from_secs(60)).await.unwrap());
|
||||
assert!(!store.mark_request(&rid, Duration::from_secs(60)).await.unwrap());
|
||||
|
||||
let code = store
|
||||
.put_invitation(b"inv".to_vec(), "pw", Duration::from_secs(60), 10)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(store.get_invitation(&code, "pw").await.unwrap(), b"inv");
|
||||
assert!(matches!(
|
||||
store.get_invitation(&code, "x").await,
|
||||
Err(StoreError::Unauthorized)
|
||||
));
|
||||
assert!(matches!(
|
||||
store.get_invitation(&code, "x").await,
|
||||
Err(StoreError::Unauthorized)
|
||||
));
|
||||
assert!(matches!(
|
||||
store.get_invitation(&code, "x").await,
|
||||
Err(StoreError::Locked)
|
||||
));
|
||||
assert!(matches!(
|
||||
store.get_invitation(&code, "pw").await,
|
||||
Err(StoreError::NotFound)
|
||||
));
|
||||
|
||||
let open = store
|
||||
.put_invitation(b"open".to_vec(), "", Duration::from_secs(60), 6)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(store.get_invitation(&open, "anything").await.unwrap(), b"open");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn memory_store() {
|
||||
let (tx, mut rx) = broadcast::channel(16);
|
||||
let store = MemoryStore::new(tx, 100);
|
||||
exercise(&store, &mut rx).await;
|
||||
}
|
||||
|
||||
/// Runs only when `MEOW_TEST_REDIS` is set (e.g. `redis://127.0.0.1:6379/15`).
|
||||
#[tokio::test]
|
||||
async fn redis_store() {
|
||||
let Ok(url) = std::env::var("MEOW_TEST_REDIS") else {
|
||||
eprintln!("MEOW_TEST_REDIS not set: skipping");
|
||||
return;
|
||||
};
|
||||
let (tx, mut rx) = broadcast::channel(16);
|
||||
let store = RedisStore::connect(&url, tx, Duration::from_secs(60), 100)
|
||||
.await
|
||||
.unwrap();
|
||||
exercise(&store, &mut rx).await;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user