use std::path::{Path, PathBuf};
use std::time::Duration;
use crate::output::{self, CHANNEL_NOTIFICATIONS, OutputLevel};
use crate::prefs::NotificationSink;
pub const DEFAULT_NTFY_SERVER: &str = "https://ntfy.sh";
pub const DEFAULT_MIN_DURATION: Duration = Duration::from_secs(10);
const CONNECT_TIMEOUT: Duration = Duration::from_secs(5);
const REQUEST_TIMEOUT: Duration = Duration::from_secs(15);
const RETRY_AFTER: Duration = Duration::from_secs(2);
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Event {
CommandFinished {
pane: String,
cmd: String,
exit: Option<i32>,
dur: Duration,
cwd: Option<PathBuf>,
host: Option<String>,
},
TestsFailed {
failed: usize,
passed: usize,
reported: bool,
},
Osc9 { pane: String, message: String },
#[allow(dead_code)]
AgentWaiting { pane: String },
ApprovalPending {
agent: String,
file: String,
token: String,
},
}
impl Event {
pub fn kind(&self) -> &'static str {
match self {
Event::CommandFinished { .. } => "command_finished",
Event::TestsFailed { .. } => "tests_failed",
Event::Osc9 { .. } => "osc9",
Event::AgentWaiting { .. } => "agent_waiting",
Event::ApprovalPending { .. } => "approval_pending",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Notification {
pub event: &'static str,
pub title: String,
pub body: String,
pub workspace: String,
pub host: String,
pub link: String,
pub actions: Vec<Action>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Action {
pub label: &'static str,
pub link: String,
}
impl Notification {
pub fn new(event: &Event, workspace: &Path, host: &str) -> Self {
let name = workspace
.file_name()
.and_then(|n| n.to_str())
.unwrap_or("croft");
let body = match event {
Event::CommandFinished {
pane,
cmd,
exit,
dur,
host: cmd_host,
..
} => {
let code = exit.map_or_else(|| String::from("?"), |c| c.to_string());
let mut s = format!(
"Command in {pane} finished: exit {code} in {}",
crate::widgets::terminal::human_duration(*dur)
);
if !cmd.trim().is_empty() {
s.push_str(" \u{2014} ");
s.push_str(cmd.trim());
}
if let Some(h) = cmd_host {
s.push_str(&format!(" (on {h})"));
}
s
}
Event::TestsFailed {
failed,
passed,
reported,
} => {
if *reported {
format!("Tests: {failed} failed, {passed} passed")
} else {
String::from(
"Test run failed before reporting any result (compile error, or the runner died)",
)
}
}
Event::Osc9 { pane, message } => format!("{pane}: {message}"),
Event::AgentWaiting { pane } => format!("Agent in {pane} is waiting for input"),
Event::ApprovalPending { agent, file, .. } => {
format!("{agent} wants to edit {file}")
}
};
let path = workspace.display().to_string();
let target = format!("host={}&path={}", encode(host), encode(&path));
let actions = match event {
Event::ApprovalPending { token, .. } => ["allow", "deny"]
.into_iter()
.map(|decision| Action {
label: if decision == "allow" {
"Approve"
} else {
"Deny"
},
link: format!(
"croft://decide?{target}&token={}&decision={decision}",
encode(token)
),
})
.collect(),
_ => Vec::new(),
};
Self {
event: event.kind(),
title: format!("croft \u{b7} {name}"),
body,
workspace: path.clone(),
host: host.to_string(),
link: format!(
"croft://attach?{target}{}",
match event {
Event::TestsFailed { .. } => "",
Event::ApprovalPending { .. } => "&focus=approval",
_ => "&focus=terminal",
}
),
actions,
}
}
}
fn encode(s: &str) -> String {
let mut out = String::with_capacity(s.len());
for b in s.bytes() {
match b {
b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' | b'/' => {
out.push(b as char)
}
_ => out.push_str(&format!("%{b:02X}")),
}
}
out
}
pub fn wants(sink: &NotificationSink, event: &Event) -> bool {
if !sink.events.is_empty() && !sink.events.iter().any(|e| e == event.kind()) {
return false;
}
match event {
Event::CommandFinished { dur, .. } => {
let min = sink
.min_duration_secs
.map_or(DEFAULT_MIN_DURATION, Duration::from_secs);
*dur >= min
}
_ => true,
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Request {
pub url: String,
pub headers: Vec<(String, String)>,
pub body: String,
}
pub fn endpoint_allowed(url: &str) -> bool {
let lower = url.trim().to_ascii_lowercase();
if lower.starts_with("https://") {
return true;
}
if let Some(rest) = lower.strip_prefix("http://") {
let host = host_of(rest);
let bare = if let Some(inner) = host.strip_prefix('[') {
inner.split(']').next().unwrap_or("")
} else {
host.split(':').next().unwrap_or(&host)
};
return matches!(bare, "localhost" | "127.0.0.1" | "::1");
}
false
}
pub fn ntfy_request(sink: &NotificationSink, n: &Notification) -> Option<Request> {
let topic = sink.topic.as_deref()?.trim();
if topic.is_empty() {
return None;
}
let server = sink
.server
.as_deref()
.map(|s| s.trim_end_matches('/'))
.filter(|s| !s.is_empty())
.unwrap_or(DEFAULT_NTFY_SERVER);
if !endpoint_allowed(server) {
return None;
}
Some(Request {
url: format!("{server}/{topic}"),
headers: {
let mut h = vec![
(String::from("Title"), n.title.clone()),
(String::from("Click"), n.link.clone()),
(String::from("Tags"), n.event.replace('_', "-")),
];
if !n.actions.is_empty() {
h.push((
String::from("Actions"),
n.actions
.iter()
.map(|a| format!("view, {}, {}, clear=true", a.label, a.link))
.collect::<Vec<_>>()
.join("; "),
));
}
h
},
body: n.body.clone(),
})
}
pub fn webhook_request(sink: &NotificationSink, n: &Notification) -> Option<Request> {
let url = sink.url.as_deref()?.trim();
if url.is_empty() || !endpoint_allowed(url) {
return None;
}
let body = serde_json::json!({
"event": n.event,
"title": n.title,
"body": n.body,
"workspace": n.workspace,
"host": n.host,
"link": n.link,
"actions": n
.actions
.iter()
.map(|a| serde_json::json!({"label": a.label, "link": a.link}))
.collect::<Vec<_>>(),
});
let mut headers = vec![(
String::from("content-type"),
String::from("application/json"),
)];
headers.extend(sink.headers.iter().map(|(k, v)| (k.clone(), v.clone())));
Some(Request {
url: url.to_string(),
headers,
body: body.to_string(),
})
}
pub fn command_argv(sink: &NotificationSink, n: &Notification) -> Option<Vec<String>> {
match sink.kind.as_str() {
"termux" => {
let mut argv = vec![
String::from("termux-notification"),
String::from("--title"),
n.title.clone(),
String::from("--content"),
n.body.clone(),
];
if !n.actions.is_empty() {
argv.push(String::from("--action"));
argv.push(format!("croft open-link '{}'", n.link));
}
for (i, a) in n.actions.iter().enumerate().take(3) {
argv.push(format!("--button{}", i + 1));
argv.push(a.label.to_string());
argv.push(format!("--button{}-action", i + 1));
argv.push(format!("croft open-link '{}'", a.link));
}
Some(argv)
}
"command" if !sink.argv.is_empty() => Some(sink.argv.clone()),
_ => None,
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Failure {
pub message: String,
pub transient: bool,
}
impl Failure {
fn fixed(message: String) -> Self {
Self {
message,
transient: false,
}
}
fn transient(message: String) -> Self {
Self {
message,
transient: true,
}
}
}
pub const COMMAND_TIMEOUT: Duration = Duration::from_secs(30);
pub fn deliver(sink: &NotificationSink, n: &Notification) -> Result<(), Failure> {
match sink.kind.as_str() {
"ntfy" | "webhook" => {
let req = if sink.kind == "ntfy" {
ntfy_request(sink, n)
} else {
webhook_request(sink, n)
}
.ok_or_else(|| {
let target = if sink.kind == "ntfy" {
sink.server.as_deref().unwrap_or(DEFAULT_NTFY_SERVER)
} else {
sink.url.as_deref().unwrap_or("")
};
Failure::fixed(if !target.is_empty() && !endpoint_allowed(target) {
format!(
"{} sink refuses a plain-http endpoint ({}): use https, or http only to localhost",
sink.kind,
host_of(target)
)
} else {
format!(
"{} sink is missing its {}",
sink.kind,
if sink.kind == "ntfy" { "topic" } else { "url" }
)
})
})?;
post(&req)
}
"termux" | "command" => {
let argv = command_argv(sink, n)
.ok_or_else(|| Failure::fixed(String::from("command sink has an empty argv")))?;
let mut cmd = std::process::Command::new(&argv[0]);
cmd.args(&argv[1..])
.env("CROFT_TITLE", &n.title)
.env("CROFT_BODY", &n.body)
.env("CROFT_LINK", &n.link)
.env("CROFT_EVENT", n.event)
.envs(n.actions.iter().map(|a| {
(
format!("CROFT_{}_LINK", a.label.to_ascii_uppercase()),
a.link.clone(),
)
}))
.stdin(std::process::Stdio::null())
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::piped());
run_bounded(cmd, &argv[0], COMMAND_TIMEOUT)
}
other => Err(Failure::fixed(format!("unknown sink kind {other:?}"))),
}
}
fn run_bounded(
mut cmd: std::process::Command,
name: &str,
timeout: Duration,
) -> Result<(), Failure> {
let mut child = cmd
.spawn()
.map_err(|e| Failure::fixed(format!("{name} not runnable: {e}")))?;
let started = std::time::Instant::now();
loop {
match child.try_wait() {
Ok(Some(status)) => {
if status.success() {
return Ok(());
}
let mut stderr = String::new();
if let Some(mut pipe) = child.stderr.take() {
let _ = std::io::Read::read_to_string(&mut pipe, &mut stderr);
}
return Err(Failure::transient(format!(
"{name} exited {status}: {}",
stderr.trim()
)));
}
Ok(None) if started.elapsed() < timeout => {
std::thread::sleep(Duration::from_millis(50));
}
Ok(None) => {
let _ = child.kill();
let _ = child.wait();
return Err(Failure::transient(format!(
"{name} did not finish within {timeout:?} and was killed"
)));
}
Err(e) => return Err(Failure::fixed(format!("{name}: {e}"))),
}
}
}
fn post(req: &Request) -> Result<(), Failure> {
let agent = ureq3::Agent::config_builder()
.timeout_connect(Some(CONNECT_TIMEOUT))
.max_redirects(0)
.http_status_as_error(false)
.build()
.new_agent();
let mut post = agent
.post(&req.url)
.config()
.timeout_global(Some(REQUEST_TIMEOUT))
.build();
for (k, v) in &req.headers {
post = post.header(k.as_str(), v.as_str());
}
let resp = post
.send(req.body.as_str())
.map_err(|e| Failure::transient(format!("{}: {e}", host_of(&req.url))))?;
let status = resp.status().as_u16();
if (200..300).contains(&status) {
Ok(())
} else if (500..600).contains(&status) || status == 429 {
Err(Failure::transient(format!(
"{}: HTTP {status}",
host_of(&req.url)
)))
} else {
Err(Failure::fixed(format!(
"{}: HTTP {status}",
host_of(&req.url)
)))
}
}
fn host_of(url: &str) -> String {
let rest = url.split("://").nth(1).unwrap_or(url);
let authority = rest.split(['/', '?', '#']).next().unwrap_or(rest);
authority
.rsplit('@')
.next()
.unwrap_or(authority)
.to_string()
}
const QUEUE_DEPTH: usize = 64;
type Job = (Vec<NotificationSink>, Notification);
fn worker() -> &'static std::sync::mpsc::SyncSender<Job> {
static TX: std::sync::OnceLock<std::sync::mpsc::SyncSender<Job>> = std::sync::OnceLock::new();
TX.get_or_init(|| {
let (tx, rx) = std::sync::mpsc::sync_channel::<Job>(QUEUE_DEPTH);
std::thread::spawn(move || {
while let Ok((sinks, n)) = rx.recv() {
deliver_all(&sinks, &n);
}
});
tx
})
}
#[derive(Debug, Clone, Default)]
pub struct Notifier {
sinks: Vec<NotificationSink>,
}
impl Notifier {
pub fn new(sinks: &[NotificationSink]) -> Self {
let mut keep = Vec::new();
for s in sinks {
let endpoint = match s.kind.as_str() {
"ntfy" => Some(s.server.as_deref().unwrap_or(DEFAULT_NTFY_SERVER)),
"webhook" => s.url.as_deref(),
_ => None,
};
if let Some(url) = endpoint
&& !url.trim().is_empty()
&& !endpoint_allowed(url)
{
output::push(
CHANNEL_NOTIFICATIONS,
OutputLevel::Warn,
&format!(
"notifications: {} sink at {} is plain http and will be refused; use https, or http only to localhost",
s.kind,
host_of(url)
),
);
}
match s.kind.as_str() {
"ntfy" | "webhook" | "termux" | "command" => keep.push(s.clone()),
other => output::push(
CHANNEL_NOTIFICATIONS,
OutputLevel::Warn,
&format!("notifications: unknown sink kind {other:?} ignored"),
),
}
}
Self { sinks: keep }
}
pub fn matching(&self, event: &Event) -> Vec<NotificationSink> {
self.sinks
.iter()
.filter(|s| wants(s, event))
.cloned()
.collect()
}
pub fn emit(&self, event: Event, workspace: &Path, host: &str) {
let sinks = self.matching(&event);
if sinks.is_empty() {
return;
}
let n = Notification::new(&event, workspace, host);
if let Err(std::sync::mpsc::TrySendError::Full(_)) = worker().try_send((sinks, n)) {
output::push(
CHANNEL_NOTIFICATIONS,
OutputLevel::Warn,
&format!(
"notifications: {} pending deliveries, dropped a {} event",
QUEUE_DEPTH,
event.kind()
),
);
}
}
}
pub fn deliver_all(sinks: &[NotificationSink], n: &Notification) {
for sink in sinks {
let first = match deliver(sink, n) {
Ok(()) => continue,
Err(f) => f,
};
let last = if first.transient {
std::thread::sleep(RETRY_AFTER);
match deliver(sink, n) {
Ok(()) => continue,
Err(f) => f,
}
} else {
first
};
output::push(
CHANNEL_NOTIFICATIONS,
OutputLevel::Warn,
&format!(
"{} sink failed for {}{}: {}",
sink.kind,
n.event,
if last.transient { " (twice)" } else { "" },
last.message
),
);
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::collections::BTreeMap;
fn sink(kind: &str) -> NotificationSink {
NotificationSink {
kind: kind.to_string(),
..Default::default()
}
}
fn finished(secs: u64) -> Event {
Event::CommandFinished {
pane: "Terminal 2".into(),
cmd: "cargo build".into(),
exit: Some(0),
dur: Duration::from_secs(secs),
cwd: None,
host: None,
}
}
#[test]
fn a_sink_filters_by_event_kind_and_a_command_by_duration() {
let mut s = sink("ntfy");
assert!(wants(&s, &finished(30)), "an empty filter takes everything");
assert!(!wants(&s, &finished(3)), "under the default 10s threshold");
s.min_duration_secs = Some(2);
assert!(wants(&s, &finished(3)));
s.events = vec![String::from("tests_failed")];
assert!(!wants(&s, &finished(30)));
assert!(wants(
&s,
&Event::TestsFailed {
failed: 1,
passed: 9,
reported: true
}
));
s.events = vec![String::from("command_finished"), String::from("osc9")];
assert!(wants(
&s,
&Event::Osc9 {
pane: "T1".into(),
message: "done".into()
}
));
assert!(!wants(&s, &Event::AgentWaiting { pane: "T1".into() }));
}
#[test]
fn the_payload_names_the_workspace_host_and_a_deep_link() {
let n = Notification::new(&finished(75), Path::new("/home/t/my proj"), "box.local");
assert_eq!(n.title, "croft \u{b7} my proj");
assert!(
n.body
.starts_with("Command in Terminal 2 finished: exit 0 in "),
"{}",
n.body
);
assert!(n.body.ends_with("\u{2014} cargo build"), "{}", n.body);
assert_eq!(n.host, "box.local");
assert_eq!(
n.link,
"croft://attach?host=box.local&path=/home/t/my%20proj&focus=terminal"
);
let n = Notification::new(
&Event::TestsFailed {
failed: 2,
passed: 40,
reported: true,
},
Path::new("/w"),
"h",
);
assert_eq!(n.body, "Tests: 2 failed, 40 passed");
assert_eq!(n.event, "tests_failed");
let n = Notification::new(
&Event::TestsFailed {
failed: 0,
passed: 0,
reported: false,
},
Path::new("/w"),
"h",
);
assert!(
n.body.starts_with("Test run failed before reporting"),
"{}",
n.body
);
}
#[test]
fn ntfy_posts_to_the_topic_with_title_and_click_headers() {
let n = Notification::new(&finished(30), Path::new("/w"), "h");
let mut s = sink("ntfy");
assert_eq!(ntfy_request(&s, &n), None, "no topic, no request");
s.topic = Some("croft-alerts".into());
let r = ntfy_request(&s, &n).unwrap();
assert_eq!(r.url, "https://ntfy.sh/croft-alerts");
assert_eq!(r.body, n.body);
assert!(
r.headers
.contains(&(String::from("Title"), n.title.clone()))
);
assert!(r.headers.contains(&(String::from("Click"), n.link.clone())));
assert!(
r.headers
.contains(&(String::from("Tags"), String::from("command-finished")))
);
s.server = Some("https://ntfy.example.org/".into());
assert_eq!(
ntfy_request(&s, &n).unwrap().url,
"https://ntfy.example.org/croft-alerts"
);
}
#[test]
fn a_webhook_posts_json_with_the_configured_headers() {
let n = Notification::new(&finished(30), Path::new("/w"), "h");
let mut s = sink("webhook");
assert_eq!(webhook_request(&s, &n), None);
s.url = Some("https://hooks.example.org/x".into());
s.headers =
BTreeMap::from([(String::from("authorization"), String::from("Bearer s3cret"))]);
let r = webhook_request(&s, &n).unwrap();
assert_eq!(r.url, "https://hooks.example.org/x");
assert!(
r.headers
.contains(&(String::from("authorization"), String::from("Bearer s3cret")))
);
assert_eq!(
r.headers[0],
(
String::from("content-type"),
String::from("application/json")
)
);
let v: serde_json::Value = serde_json::from_str(&r.body).unwrap();
assert_eq!(v["event"], "command_finished");
assert_eq!(v["link"], n.link);
assert_eq!(v["body"], n.body);
}
fn approval() -> Event {
Event::ApprovalPending {
agent: "claude-code".into(),
file: "src/a b.rs".into(),
token: "0123456789abcdef0123456789abcdef".into(),
}
}
#[test]
fn an_approval_notification_carries_approve_and_deny_links_that_parse_back() {
let n = Notification::new(&approval(), Path::new("/srv/my app"), "devbox");
assert_eq!(n.event, "approval_pending");
assert_eq!(n.body, "claude-code wants to edit src/a b.rs");
let open = crate::deep_link::parse(&n.link).unwrap();
assert_eq!(open.focus, Some(crate::deep_link::Focus::Approval));
assert_eq!(open.decide, None);
let labels: Vec<_> = n.actions.iter().map(|a| a.label).collect();
assert_eq!(labels, ["Approve", "Deny"]);
for (action, allow) in n.actions.iter().zip([true, false]) {
let l = crate::deep_link::parse(&action.link).unwrap();
assert_eq!(l.host.as_deref(), Some("devbox"));
assert_eq!(l.path.as_deref(), Some("/srv/my app"));
assert_eq!(
l.decide,
Some(crate::deep_link::Decide {
token: "0123456789abcdef0123456789abcdef".into(),
allow,
})
);
}
assert!(
Notification::new(&finished(30), Path::new("/w"), "h")
.actions
.is_empty()
);
}
#[test]
fn every_sink_kind_offers_the_approval_buttons() {
let n = Notification::new(&approval(), Path::new("/w"), "h");
let (approve, deny) = (&n.actions[0].link, &n.actions[1].link);
let mut s = sink("ntfy");
s.topic = Some("t".into());
let actions = ntfy_request(&s, &n)
.unwrap()
.headers
.into_iter()
.find(|(k, _)| k == "Actions")
.map(|(_, v)| v)
.expect("an Actions header");
assert_eq!(
actions,
format!("view, Approve, {approve}, clear=true; view, Deny, {deny}, clear=true")
);
let plain = Notification::new(&finished(30), Path::new("/w"), "h");
assert!(
!ntfy_request(&s, &plain)
.unwrap()
.headers
.iter()
.any(|(k, _)| k == "Actions")
);
let mut w = sink("webhook");
w.url = Some("https://hooks.example.org/x".into());
let body: serde_json::Value =
serde_json::from_str(&webhook_request(&w, &n).unwrap().body).unwrap();
assert_eq!(body["actions"][0]["label"], "Approve");
assert_eq!(body["actions"][1]["link"], deny.as_str());
let t = command_argv(&sink("termux"), &n).unwrap();
let after = |flag: &str| {
let i = t.iter().position(|a| a == flag).expect(flag);
t[i + 1].clone()
};
assert_eq!(after("--action"), format!("croft open-link '{}'", n.link));
assert_eq!(after("--button1"), "Approve");
assert_eq!(
after("--button1-action"),
format!("croft open-link '{approve}'")
);
assert_eq!(after("--button2"), "Deny");
assert_eq!(
after("--button2-action"),
format!("croft open-link '{deny}'")
);
assert!(n.actions.iter().all(|a| !a.link.contains('\'')));
assert!(!n.link.contains('\''));
}
#[test]
fn termux_and_command_sinks_build_their_argv() {
let n = Notification::new(&finished(30), Path::new("/w"), "h");
let t = command_argv(&sink("termux"), &n).unwrap();
assert_eq!(t[0], "termux-notification");
assert_eq!(t[1..3], [String::from("--title"), n.title.clone()]);
assert_eq!(t[3..5], [String::from("--content"), n.body.clone()]);
assert_eq!(
command_argv(&sink("command"), &n),
None,
"an empty argv is no sink"
);
let mut c = sink("command");
c.argv = vec!["/bin/sh".into(), "-c".into(), "true".into()];
assert_eq!(command_argv(&c, &n).unwrap(), c.argv);
}
#[test]
fn a_command_sink_runs_with_the_notification_in_its_environment() {
let dir = tempfile::tempdir().unwrap();
let out = dir.path().join("got.txt");
let n = Notification::new(&finished(30), dir.path(), "box");
let mut c = sink("command");
c.argv = vec![
"/bin/sh".into(),
"-c".into(),
format!(
"printf '%s|%s|%s' \"$CROFT_EVENT\" \"$CROFT_TITLE\" \"$CROFT_LINK\" > '{}'",
out.display()
),
];
deliver(&c, &n).expect("the program ran");
let got = std::fs::read_to_string(&out).unwrap();
let parts: Vec<&str> = got.split('|').collect();
assert_eq!(parts[0], "command_finished");
assert_eq!(parts[1], n.title);
assert_eq!(parts[2], n.link);
c.argv = vec![
"/bin/sh".into(),
"-c".into(),
"echo nope >&2; exit 3".into(),
];
let err = deliver(&c, &n).unwrap_err();
assert!(
err.message.contains("exited") && err.message.contains("nope"),
"{err:?}"
);
assert!(err.transient, "a failing program may pass next time");
c.argv = vec!["/definitely/not/here".into()];
let err = deliver(&c, &n).unwrap_err();
assert!(err.message.contains("not runnable"));
assert!(
!err.transient,
"a missing program will not appear in two seconds"
);
}
#[test]
fn a_hung_command_sink_is_killed_at_the_bound() {
let started = std::time::Instant::now();
let mut cmd = std::process::Command::new("/bin/sh");
cmd.args(["-c", "sleep 30"]);
let err = run_bounded(cmd, "/bin/sh", Duration::from_millis(300)).unwrap_err();
assert!(err.message.contains("was killed"), "{err:?}");
assert!(
started.elapsed() < Duration::from_secs(5),
"waited {:?}",
started.elapsed()
);
}
#[test]
fn a_plain_http_endpoint_is_refused_unless_it_is_loopback() {
for ok in [
"https://ntfy.sh",
"https://hooks.example.org/x",
"http://localhost:8080/hook",
"http://127.0.0.1/hook",
"http://[::1]:9/x",
] {
assert!(endpoint_allowed(ok), "{ok}");
}
for bad in [
"http://ntfy.example.org",
"http://10.0.0.5:8080/x",
"ftp://x",
"hooks.example.org",
] {
assert!(!endpoint_allowed(bad), "{bad}");
}
let n = Notification::new(&finished(30), Path::new("/w"), "h");
let mut s = sink("webhook");
s.url = Some("http://hooks.example.org/x".into());
assert_eq!(webhook_request(&s, &n), None);
let err = deliver(&s, &n).unwrap_err();
assert!(
err.message.contains("plain-http") && !err.transient,
"{err:?}"
);
let mut t = sink("ntfy");
t.topic = Some("x".into());
t.server = Some("http://ntfy.example.org".into());
assert_eq!(ntfy_request(&t, &n), None);
t.server = Some("http://localhost:2586".into());
assert_eq!(ntfy_request(&t, &n).unwrap().url, "http://localhost:2586/x");
}
#[test]
fn a_log_line_names_the_host_never_the_path_query_or_userinfo() {
assert_eq!(
host_of("https://hooks.example.org/T0K3N/abc"),
"hooks.example.org"
);
assert_eq!(host_of("http://10.0.0.5:8080/x?token=1"), "10.0.0.5:8080");
assert_eq!(host_of("https://user:pass@host.io/x"), "host.io");
assert_eq!(host_of("https://h.io?token=zz"), "h.io");
assert_eq!(host_of("https://h.io#frag"), "h.io");
assert_eq!(host_of("https://u:p@h.io"), "h.io");
}
#[test]
fn emit_delivers_through_the_worker_queue() {
let dir = tempfile::tempdir().unwrap();
let out = dir.path().join("hit.txt");
let mut c = sink("command");
c.min_duration_secs = Some(0);
c.argv = vec![
"/bin/sh".into(),
"-c".into(),
format!("printf '%s' \"$CROFT_EVENT\" > '{}'", out.display()),
];
let notifier = Notifier::new(&[c]);
notifier.emit(finished(1), dir.path(), "h");
let mut waited = 0;
while !out.exists() && waited < 8000 {
std::thread::sleep(Duration::from_millis(40));
waited += 40;
}
assert_eq!(std::fs::read_to_string(&out).unwrap(), "command_finished");
}
#[test]
fn an_unknown_sink_kind_is_dropped_and_only_matching_sinks_are_returned() {
let mut ntfy = sink("ntfy");
ntfy.topic = Some("t".into());
ntfy.events = vec![String::from("tests_failed")];
let notifier = Notifier::new(&[ntfy.clone(), sink("pigeon")]);
assert_eq!(
notifier.matching(&finished(30)),
Vec::<NotificationSink>::new()
);
assert_eq!(
notifier.matching(&Event::TestsFailed {
failed: 1,
passed: 0,
reported: true
}),
vec![ntfy]
);
}
}