[relay] Wake-up: a push endpoint per mailbox, and a constant wake to it when a blob arrives
ci / check (pull_request) Has been cancelled
ci / check (pull_request) Has been cancelled
This commit is contained in:
@@ -23,6 +23,12 @@ pub const MAX_BLOBS_PER_FETCH: usize = 100;
|
||||
pub const BLOB_TTL_SECS: i64 = 30 * 24 * 3600;
|
||||
/// An unclaimed invite expires after this many seconds (7 days, `PROTOCOL.md` §4).
|
||||
pub const INVITE_TTL_SECS: i64 = 7 * 24 * 3600;
|
||||
/// The longest push endpoint a mailbox may hold (UnifiedPush's own limit, `wake-up.md` §2.1).
|
||||
pub const MAX_PUSH_ENDPOINT_BYTES: usize = 1000;
|
||||
/// The body of every wake: a constant, so it carries nothing (`PROTOCOL.md` §6).
|
||||
pub const WAKE_BODY: &[u8] = b"wake";
|
||||
/// A mailbox is woken at most once in this many seconds.
|
||||
pub const WAKE_EVERY_SECS: i64 = 60;
|
||||
|
||||
/// Why a value from the wire was refused. Carries no part of the input.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
@@ -35,6 +41,8 @@ pub enum ParseError {
|
||||
Scheme,
|
||||
/// More bytes than the protocol allows.
|
||||
TooLarge { max: usize },
|
||||
/// Not a push endpoint the protocol accepts (`wake-up.md` §2.1).
|
||||
Endpoint,
|
||||
}
|
||||
|
||||
impl fmt::Display for ParseError {
|
||||
@@ -46,6 +54,7 @@ impl fmt::Display for ParseError {
|
||||
}
|
||||
ParseError::Scheme => write!(f, "expected 'Bearer <token>'"),
|
||||
ParseError::TooLarge { max } => write!(f, "more than {max} bytes"),
|
||||
ParseError::Endpoint => write!(f, "not an https push endpoint"),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -327,3 +336,108 @@ pub fn hour_of(unix_secs: i64) -> i64 {
|
||||
pub fn day_of(unix_secs: i64) -> i64 {
|
||||
unix_secs.div_euclid(86_400)
|
||||
}
|
||||
|
||||
/// A push endpoint (`PROTOCOL.md` §6, `.agents/features/wake-up.md` §2.1): a capability URL at the
|
||||
/// push server the mailbox's owner chose. `https://` with a host, no user info, no fragment, printable
|
||||
/// ASCII, at most [`MAX_PUSH_ENDPOINT_BYTES`]; `http://` only to a loopback host, which a relay
|
||||
/// accepts only when its operator allows it (tests).
|
||||
#[derive(Clone, PartialEq, Eq)]
|
||||
pub struct PushEndpoint {
|
||||
url: String,
|
||||
host: String,
|
||||
https: bool,
|
||||
}
|
||||
|
||||
impl PushEndpoint {
|
||||
pub fn parse(text: &str) -> Result<Self, ParseError> {
|
||||
let bad = || ParseError::Endpoint;
|
||||
if text.len() > MAX_PUSH_ENDPOINT_BYTES {
|
||||
return Err(ParseError::TooLarge {
|
||||
max: MAX_PUSH_ENDPOINT_BYTES,
|
||||
});
|
||||
}
|
||||
if !text.bytes().all(|b| (0x21..=0x7e).contains(&b)) || text.contains('#') {
|
||||
return Err(bad());
|
||||
}
|
||||
let (https, rest) = if let Some(r) = text.strip_prefix("https://") {
|
||||
(true, r)
|
||||
} else if let Some(r) = text.strip_prefix("http://") {
|
||||
(false, r)
|
||||
} else {
|
||||
return Err(bad());
|
||||
};
|
||||
let authority = &rest[..rest.find(['/', '?']).unwrap_or(rest.len())];
|
||||
if authority.contains('@') {
|
||||
return Err(bad());
|
||||
}
|
||||
let (host, port) = if let Some(v6) = authority.strip_prefix('[') {
|
||||
let end = v6.find(']').ok_or_else(bad)?;
|
||||
(&v6[..end], &v6[end + 1..])
|
||||
} else {
|
||||
match authority.find(':') {
|
||||
Some(i) => (&authority[..i], &authority[i..]),
|
||||
None => (authority, ""),
|
||||
}
|
||||
};
|
||||
if host.is_empty() || host.contains(['[', ']']) && !authority.starts_with('[') {
|
||||
return Err(bad());
|
||||
}
|
||||
if !port.is_empty() {
|
||||
let digits = port.strip_prefix(':').ok_or_else(bad)?;
|
||||
match digits.parse::<u16>() {
|
||||
Ok(p) if p > 0 && !digits.starts_with('0') => {}
|
||||
_ => return Err(bad()),
|
||||
}
|
||||
}
|
||||
let host = host.to_ascii_lowercase();
|
||||
if !https && !matches!(host.as_str(), "127.0.0.1" | "::1" | "localhost") {
|
||||
return Err(bad());
|
||||
}
|
||||
Ok(Self {
|
||||
url: text.to_string(),
|
||||
host,
|
||||
https,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn as_str(&self) -> &str {
|
||||
&self.url
|
||||
}
|
||||
|
||||
/// The host, lower case, without brackets or port.
|
||||
pub fn host(&self) -> &str {
|
||||
&self.host
|
||||
}
|
||||
|
||||
/// `http://` to a loopback host: only for a relay whose operator allows it.
|
||||
pub fn is_loopback_http(&self) -> bool {
|
||||
!self.https
|
||||
}
|
||||
}
|
||||
|
||||
/// The endpoint is a capability: `Debug` shows its host only.
|
||||
impl fmt::Debug for PushEndpoint {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "PushEndpoint({})", self.host)
|
||||
}
|
||||
}
|
||||
|
||||
impl Serialize for PushEndpoint {
|
||||
fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
|
||||
s.serialize_str(&self.url)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Deserialize<'de> for PushEndpoint {
|
||||
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
|
||||
let text = Cow::<'de, str>::deserialize(d)?;
|
||||
PushEndpoint::parse(&text).map_err(serde::de::Error::custom)
|
||||
}
|
||||
}
|
||||
|
||||
/// `PUT /v0/mailboxes/{id}/push`: where to wake the mailbox's owner (`PROTOCOL.md` §6).
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
||||
pub struct SetPush {
|
||||
pub endpoint: PushEndpoint,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user