The job store is to keep its database in the directory that holds the ntfy subscriptions' state files, so the directory moves to a section of its own: [state] dir, or UBERBRINGER_STATE_DIR, in place of [ntfy] state_dir. For now only the subscriptions use it. It still has no default and is still required with a subscription, and the checks at start name state.dir in their errors. install.sh writes [state] dir into new configs, and the Docker image sets UBERBRINGER_STATE_DIR. ntfy.state_dir has never been in a release, so its CHANGELOG entry changes in place and there is no fallback: a config that still sets it fails to load with an unknown key until the line moves to [state]. The image itself was not built, since the workstation has no Docker. Part of stage 1b of docs/plans/job-store.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
192 lines
8.4 KiB
Markdown
192 lines
8.4 KiB
Markdown
# ntfy subscriptions
|
|
|
|
The daemon can subscribe to [ntfy](https://ntfy.sh) topics itself and print
|
|
each message as a [notification card](api.md#notification-cards), so the
|
|
phone's ntfy app, or anything else that publishes, is the only client. To
|
|
have the ntfy command line client post messages instead, see
|
|
[Printing from the ntfy client](api.md#printing-from-the-ntfy-client).
|
|
|
|
## Contents
|
|
|
|
- [Setting one up](#setting-one-up)
|
|
- [How a subscription behaves](#how-a-subscription-behaves)
|
|
- [Securing the topics](#securing-the-topics)
|
|
- [The printer's access token](#the-printers-access-token)
|
|
|
|
## Setting one up
|
|
|
|
Each `[[ntfy.subscriptions]]` table in `config.toml` is one connection to
|
|
one server, for one or more topics. Subscriptions are set in the file only,
|
|
not by environment variables. [configuration.md](configuration.md#ntfy-subscriptions)
|
|
lists every key, and [ntfy](configuration.md#ntfy) the settings that shape
|
|
the cards.
|
|
|
|
```toml
|
|
[state]
|
|
dir = "/var/lib/uberbringer"
|
|
|
|
[[ntfy.subscriptions]]
|
|
name = "home"
|
|
server = "https://ntfy.example.com"
|
|
topics = ["alerts", "backups"]
|
|
token_file = "/etc/uberbringer/ntfy.token"
|
|
|
|
[ntfy.subscriptions.quiet]
|
|
start = "22:00"
|
|
end = "07:00"
|
|
```
|
|
|
|
`install.sh` and the Docker image set `state.dir` already. In Docker, mount
|
|
a volume for the state and the token file next to the config, as
|
|
[docker.md](docker.md#ntfy-subscriptions) shows.
|
|
|
|
`/healthz` lists each subscription's state, counts and last error; see
|
|
[Health](api.md#health).
|
|
|
|
## How a subscription behaves
|
|
|
|
- **First start.** With no state yet, it prints only messages published
|
|
from then on, also after a reconnect soon after.
|
|
- **Restarts and outages.** It keeps a cursor, the newest server time it
|
|
has dealt with, and when it reconnects or starts again it asks for the
|
|
messages since then, two minutes earlier to catch late ones. Of those,
|
|
it prints only the ones no older than `max_backlog`, measured by ntfy's
|
|
own clock as the stream tells it (the time of its first, `open` event,
|
|
or else the answer's `Date`), never by this host's, and leaves the
|
|
older ones out as they arrive; they stay out, even after a restart with
|
|
a longer backlog. It keeps the IDs of the messages a replay could send
|
|
again, at least the last 512 and at most 20,000, so a message the server
|
|
sends again, after a reconnect or as ntfy's own repeat, never prints
|
|
twice, even after a flood; past 20,000, the next replay starts after the
|
|
IDs it forgot.
|
|
- **Failures.** A dropped connection is opened again after 5, 10, 20, 30,
|
|
60 and then every 120 seconds, each ± 20%, from 5 seconds again once a
|
|
connection has lasted two minutes. A `429` waits as long as its
|
|
`Retry-After` says. Refused credentials (`401` or `403`) are checked at
|
|
startup, and while the server refuses them the subscription is
|
|
`unauthorized` in `/healthz` and tries every five minutes. Printing goes
|
|
on meanwhile.
|
|
- **A full print queue** pauses the subscription: it stops reading the
|
|
stream, offers the message again after 1 second, then after twice as
|
|
long each time, up to 30 seconds, and moves its cursor only once the
|
|
message is queued. A digest that cannot be queued waits the same way.
|
|
- **The paper budget.** At most `rate_limit` receipts print in any
|
|
`rate_window`, digests included. Messages over it are held and print
|
|
together on one [digest](api.md#digests) once the budget has room.
|
|
Outside quiet hours, messages of `bypass_priority` and above print at
|
|
once.
|
|
- **Quiet hours.** During the window, only `quiet.min_priority` and above
|
|
print. The rest are held for an `Overnight` digest when the window ends,
|
|
or left out with `action = "drop"`.
|
|
- **Updates.** ntfy lets a publisher replace a notification with a later
|
|
message of the same sequence (`sequence_id`). When a message's sequence
|
|
printed before, on a card or a digest row (not only counted on a
|
|
digest), and is among the last 512 it remembers, its card says
|
|
`(updated)`, or with `ntfy.format.updates = "skip"` it is left out. The
|
|
first message of a sequence prints plain, even when its publisher named
|
|
the sequence.
|
|
- **State.** The cursor, the IDs, the budget and the held messages are in
|
|
`<state.dir>/ntfy-<name>.json`, written through a temporary file and a
|
|
rename, so a crash leaves the old file or the new one. It is written
|
|
within a second of a message being printed, held or left out, at most
|
|
every 10 minutes when only keepalives have moved the cursor, and at
|
|
shutdown. A damaged file is ignored with a warning, and the subscription
|
|
starts as if new. Held messages survive a restart.
|
|
- **Jobs.** Each card and digest is an ordinary job with the source
|
|
`ntfy:<name>`. Like any job, one that is still waiting when the daemon
|
|
shuts down is marked failed, and is not printed after the restart.
|
|
|
|
## Securing the topics
|
|
|
|
> [!WARNING]
|
|
> Whoever can publish to a subscribed topic prints on your printer, and
|
|
> with priority 5 past the paper budget. On the public `ntfy.sh`, anyone
|
|
> who knows a topic's name can publish to it: reserve the topic so that
|
|
> others may not write to it, or at the very least use a long random name.
|
|
> Your own ntfy server lets everyone publish too, unless access control is
|
|
> on: set `auth-file`, set `auth-default-access` to `deny-all` (or
|
|
> `read-only`), since it defaults to `read-write`, and give write access
|
|
> only to your publishers.
|
|
|
|
On your own ntfy server, with in its `server.yml`
|
|
|
|
```yaml
|
|
auth-file: /var/lib/ntfy/user.db
|
|
auth-default-access: deny-all
|
|
```
|
|
|
|
give the printer a user of its own that may only read its topics, and
|
|
each publisher one that may only write to them:
|
|
|
|
```sh
|
|
sudo ntfy user add printer # asks for a password
|
|
sudo ntfy access printer alerts read-only # once for each topic
|
|
sudo ntfy user add phone
|
|
sudo ntfy access phone alerts write-only
|
|
```
|
|
|
|
## The printer's access token
|
|
|
|
Create the printer's access token and write it straight into a file only
|
|
the daemon can read, so that it never shows on the screen, on a command
|
|
line or in your shell history. The scripts below take both of
|
|
`ntfy token add`'s output streams, since ntfy 2.13 and earlier print the
|
|
token on standard error, and hand the token to `install` on its standard
|
|
input. `umask 077` keeps the file private from the moment it is created,
|
|
also with BusyBox's `install`, which sets the mode only after writing.
|
|
|
|
With ntfy on the same host:
|
|
|
|
```sh
|
|
out=$(sudo ntfy token add printer 2>&1)
|
|
token=$(printf '%s\n' "$out" | grep -o 'tk_[A-Za-z0-9]*')
|
|
if [ -z "$token" ]; then
|
|
printf 'ntfy made no token:\n%s\n' "$out" | sed 's/tk_[A-Za-z0-9]*/tk_.../g' >&2
|
|
elif printf '%s\n' "$token" | (umask 077; sudo install -m 600 -o uberbringer -g uberbringer /dev/stdin /etc/uberbringer/ntfy.token); then
|
|
unset token
|
|
else
|
|
cat >&2 <<'EOF'
|
|
The token was made but not stored. Fix the cause, then run:
|
|
printf '%s\n' "$token" | (umask 077; sudo install -m 600 -o uberbringer -g uberbringer /dev/stdin /etc/uberbringer/ntfy.token) && unset token
|
|
EOF
|
|
fi
|
|
unset out
|
|
```
|
|
|
|
With ntfy on another host, write the token there into a file of your own,
|
|
`ntfy.token` in the current directory:
|
|
|
|
```sh
|
|
out=$(sudo ntfy token add printer 2>&1)
|
|
token=$(printf '%s\n' "$out" | grep -o 'tk_[A-Za-z0-9]*')
|
|
if [ -z "$token" ]; then
|
|
printf 'ntfy made no token:\n%s\n' "$out" | sed 's/tk_[A-Za-z0-9]*/tk_.../g' >&2
|
|
elif printf '%s\n' "$token" | (umask 077; install -m 600 /dev/stdin ntfy.token); then
|
|
unset token
|
|
else
|
|
cat >&2 <<'EOF'
|
|
The token was made but not stored. Fix the cause, then run:
|
|
printf '%s\n' "$token" | (umask 077; install -m 600 /dev/stdin ntfy.token) && unset token
|
|
EOF
|
|
fi
|
|
unset out
|
|
```
|
|
|
|
Copy it to the printer's host, place it there with
|
|
`sudo install -m 600 -o uberbringer -g uberbringer ntfy.token /etc/uberbringer/ntfy.token`,
|
|
and delete both copies of `ntfy.token`.
|
|
|
|
When no token comes out, the script shows what ntfy said, with any token
|
|
in it masked, such as that the user does not exist. When a token was made
|
|
but could not be stored, for example because `/etc/uberbringer` is
|
|
missing, nothing is written, and the token stays in `$token` until the
|
|
printed line stores it. Never look it up with `ntfy token list`, which
|
|
prints every token in full; removing one with `ntfy token remove` needs
|
|
the token on its command line.
|
|
|
|
If the ntfy web app lets users sign in (`enable-login: true`, which is off
|
|
by default), you can also create the token there under Account and paste
|
|
it in with `sudoedit`, after
|
|
`sudo install -m 600 -o uberbringer -g uberbringer /dev/null /etc/uberbringer/ntfy.token`
|
|
has made the empty file.
|