aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 1 | use std::path::Path; |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 2 | |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 3 | use crate::{Allowlist, Sender, Vapid}; |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 4 | |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 5 | /// `MALICE_PUBLIC_URL`, the origin browsers and the homeserver reach this server at. |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 6 | pub fn public_url() -> Option<String> { |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 7 | let url = std::env::var("MALICE_PUBLIC_URL").ok()?; |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 8 | let url = url.trim().trim_end_matches('/'); |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 9 | match url::Url::parse(url) { |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 10 | Ok(parsed) if matches!(parsed.scheme(), "http" | "https") && parsed.has_host() => { |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 11 | Some(url.to_owned()) |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 12 | } |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 13 | _ => { |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 14 | tracing::warn!("ignoring MALICE_PUBLIC_URL, it is not an http(s) URL"); |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 15 | None |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 16 | } |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 17 | } |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 18 | } |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 19 | |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 20 | /// The VAPID `sub`: `MALICE_PUSH_CONTACT`, else the public URL. |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 21 | pub fn contact(public_url: Option<&str>) -> String { |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 22 | std::env::var("MALICE_PUSH_CONTACT") |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 23 | .ok() |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 24 | .map(|contact| contact.trim().to_owned()) |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 25 | .filter(|contact| contact.starts_with("mailto:") || contact.starts_with("https://")) |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 26 | .or_else(|| { |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 27 | public_url |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 28 | .filter(|url| url.starts_with("https://")) |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 29 | .map(str::to_owned) |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 30 | }) |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 31 | .unwrap_or_else(|| "mailto:webpush@localhost".to_owned()) |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 32 | } |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 33 | |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 34 | /// A sender with the VAPID key kept in `dir`, configured from the environment. |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 35 | pub fn sender(dir: &Path, public_url: Option<&str>) -> Result<Sender, String> { |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 36 | let vapid = Vapid::load_or_create(&dir.join("vapid.pk8"))?; |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 37 | Sender::new(vapid, contact(public_url), Allowlist::from_env()) |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 38 | } |