Files
Amicitia-Tabellarius/crates/relay/src/lib.rs
T

69 lines
2.5 KiB
Rust

//! The Tabellarius relay: holds ciphertext addressed to a mailbox until its owner fetches and
//! acknowledges it. Stores exactly `docs/PROTOCOL.md` §5 and logs no request.
pub mod http;
pub mod join;
pub mod store;
pub mod wake;
use std::net::SocketAddr;
use std::path::PathBuf;
/// Loopback unless told otherwise: a relay reachable from outside is a deliberate choice (F23).
pub const DEFAULT_LISTEN: &str = "127.0.0.1:8470";
pub const DEFAULT_DB: &str = "tabellarius-relay.db";
#[derive(Debug, PartialEq, Eq)]
pub struct Args {
pub listen: SocketAddr,
pub db: PathBuf,
pub limits: store::Limits,
/// `--push-loopback`: accept and wake `http://` loopback push endpoints. Tests only.
pub push_loopback: bool,
}
/// `--listen <addr:port>`, `--db <path>`, and the relay-wide quotas `--max-bytes`,
/// `--max-mailboxes`, `--max-invites` (positive integers), and `--push-loopback` (tests). Anything else is refused with a usage
/// line.
pub fn parse_args<I: IntoIterator<Item = String>>(args: I) -> Result<Args, String> {
let usage = "usage: tabellarius-relay [--listen 127.0.0.1:8470] [--db tabellarius-relay.db] \
[--max-bytes 1073741824] [--max-mailboxes 100000] [--max-invites 10000] \n [--push-loopback]";
let mut listen: SocketAddr = DEFAULT_LISTEN.parse().map_err(|_| usage.to_string())?;
let mut db = PathBuf::from(DEFAULT_DB);
let mut limits = store::Limits::default();
let mut push_loopback = false;
let mut it = args.into_iter();
let positive = |v: Option<String>| -> Result<i64, String> {
v.and_then(|v| v.parse::<i64>().ok())
.filter(|&n| n > 0)
.ok_or_else(|| usage.to_string())
};
while let Some(arg) = it.next() {
match arg.as_str() {
"--max-bytes" => limits.total_bytes = positive(it.next())?,
"--max-mailboxes" => limits.mailboxes = positive(it.next())?,
"--max-invites" => limits.invites = positive(it.next())?,
"--push-loopback" => push_loopback = true,
"--listen" => {
listen = it
.next()
.and_then(|v| v.parse().ok())
.ok_or_else(|| usage.to_string())?;
}
"--db" => {
db = it
.next()
.map(PathBuf::from)
.ok_or_else(|| usage.to_string())?
}
_ => return Err(usage.to_string()),
}
}
Ok(Args {
listen,
db,
limits,
push_loopback,
})
}