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

98 lines
3.9 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 arrivals;
pub mod http;
pub mod join;
pub mod store;
pub mod turn;
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,
/// `--turn-secret-file <path>`: the secret shared with the TURN server (calls); none means no TURN route.
pub turn_secret_file: Option<PathBuf>,
/// `--turn-url <url>` (repeatable): the TURN servers named in each credential.
pub turn_urls: Vec<String>,
}
/// `--listen <addr:port>`, `--db <path>`, the relay-wide quotas `--max-bytes`, `--max-mailboxes`,
/// `--max-invites`, `--max-files`, `--max-file-bytes` (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 [--max-files 10000] [--max-file-bytes 4294967296] [--push-loopback] [--turn-secret-file <path> --turn-url <turn:host:port?transport=udp> ...]";
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 turn_secret_file = None;
let mut turn_urls = Vec::new();
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())?,
"--max-files" => limits.files = positive(it.next())?,
"--max-file-bytes" => limits.file_bytes = positive(it.next())?,
"--push-loopback" => push_loopback = true,
"--turn-secret-file" => {
turn_secret_file = Some(
it.next()
.map(PathBuf::from)
.ok_or_else(|| usage.to_string())?,
)
}
"--turn-url" => {
let url = it
.next()
.filter(|u| u.starts_with("turn:") || u.starts_with("turns:"));
turn_urls.push(url.ok_or_else(|| usage.to_string())?);
}
"--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()),
}
}
// A secret without a URL, or URLs without a secret, is a mistake in the deployment.
if turn_secret_file.is_some() != !turn_urls.is_empty() {
return Err(usage.to_string());
}
Ok(Args {
listen,
db,
limits,
push_loopback,
turn_secret_file,
turn_urls,
})
}