Compare commits
142 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 4421d1d083 | |||
| c441215c62 | |||
| 4eb6fbb286 | |||
| a57820a078 | |||
| 9ece0ff17c | |||
| 8512851e4b | |||
| aba6bd52a7 | |||
| 7a059f4460 | |||
| 279510c1f3 | |||
| 4120bcd369 | |||
| 349426cbf9 | |||
| 6939fd1dcc | |||
| 9c9c513419 | |||
| d5037fb76d | |||
| c191357442 | |||
| 02d485cd10 | |||
| 7a3e3b4c63 | |||
| bd7452db8f | |||
| 02ee6d74c1 | |||
| 2faca684da | |||
| 1fb1edd6b0 | |||
| c65b80a06c | |||
| 2ebe77b21e | |||
| 54bfa5faf6 | |||
| 9035306e22 | |||
| b046e66e54 | |||
| 0cafe02599 | |||
| f652e6906b | |||
|
|
2b06db9d26 | ||
|
|
0fe2bf90ab | ||
| 236bd02d88 | |||
|
|
baa6661622 | ||
| a96be2f14c | |||
| c0998f1ac7 | |||
| 2671d13489 | |||
| b22016cbe5 | |||
| eda77e87c7 | |||
| d1ba83bb7a | |||
| 614f270a90 | |||
| 0222c0b69a | |||
| f9f50fcf07 | |||
| 61c38bacc1 | |||
| 373f862588 | |||
| abee943bf5 | |||
|
|
5d0ff1ac57 | ||
| 47e093e313 | |||
| 757ce5fbb3 | |||
| 45ef1e83f2 | |||
| b3636e648f | |||
| c133adf398 | |||
| ac246682f0 | |||
|
|
679a674fc9 | ||
|
|
171c77475a | ||
|
|
69f1a38e9f | ||
| 6d6bfe6e1d | |||
| a55f08d4ff | |||
| 75d099474c | |||
| 3a3900e959 | |||
| f9e4c1d800 | |||
| dccf9e1762 | |||
|
|
2bb7ed2109 | ||
| 711b0395f1 | |||
|
|
f844cf349a | ||
| 8f888ae10e | |||
|
|
da6cda28a5 | ||
|
|
bd535851b0 | ||
|
|
e6414941ab | ||
|
|
b455c1ec8c | ||
|
|
cf0dad7380 | ||
|
|
2e031763fa | ||
|
|
ecc248307e | ||
|
|
05e522ac93 | ||
|
|
9541f6c6f4 | ||
|
|
ca54e83f9c | ||
|
|
8d4c70a571 | ||
|
|
4957c297b7 | ||
|
|
3626a30b52 | ||
|
|
7d4456bb2f | ||
|
|
f6042e9b5f | ||
| a581e3fc88 | |||
| 6d64cba374 | |||
| b209f55d2c | |||
| c6dcb7f34e | |||
| 88f4e4765b | |||
| 1b0b155226 | |||
| 5a8a7fb0cc | |||
| 2c4d03668f | |||
| 07923d665b | |||
| f5a3847a21 | |||
| ba18b445ec | |||
| 8acf57961e | |||
| 46392c55e1 | |||
| f6ba08d506 | |||
| 7994f93f31 | |||
| 5c5dd91d21 | |||
| ee5610ba4e | |||
| 8e8307b569 | |||
| c00d5383de | |||
| 2db42b5fae | |||
| 1253b470a5 | |||
| 980fbc3f66 | |||
| 3bb59c215a | |||
| b62182e38e | |||
| a92e81aba2 | |||
| bda2ef07be | |||
| 194a9250e7 | |||
| 352e2b77a5 | |||
| f8408a3d26 | |||
| b509ea4b35 | |||
| af69b699a8 | |||
| 742f76969e | |||
| e5388b443f | |||
| bbcf69062e | |||
| d3b8c4cd01 | |||
| 8a66f9c7ee | |||
| 1b2343cf28 | |||
| 5c1970e8ee | |||
| 385589ff77 | |||
| c8cbb3047a | |||
| 245fa79fbf | |||
| 72e4a97f87 | |||
| 90bbcfb40e | |||
| 3f27dfdfdb | |||
| 08b28f0598 | |||
| 5ee633e700 | |||
| 070b39e840 | |||
| c4f0a362d8 | |||
| dd91602a49 | |||
| c788112190 | |||
| 6352324bcb | |||
| 1d54f5141d | |||
| eae0ef5061 | |||
| 2c7e23c346 | |||
| cad14ec09d | |||
| cbc0c48f46 | |||
| 56778df9c5 | |||
| 4249393f2a | |||
| 75afaa291c | |||
| ea5c15cc58 | |||
| 6f38c0ad9b | |||
| c6ca4d1a91 | |||
| 474be3e0a8 |
@@ -3,7 +3,8 @@
|
||||
"allow": [
|
||||
"Bash(ffprobe -v error *)",
|
||||
"Bash(env)",
|
||||
"mcp__outline__read_document"
|
||||
"mcp__outline__read_document",
|
||||
"Bash(sh scripts/build-tui.sh)"
|
||||
]
|
||||
},
|
||||
"hooks": {
|
||||
|
||||
@@ -1,22 +1,24 @@
|
||||
---
|
||||
name: ipx-prod-check
|
||||
description: Look for problems in production ipx (the ipx-web, iPX and ipx-directory containers on Tower, Tower itself and the monitoring that watches them) from their logs in Loki, their traces in Tempo and the metrics in Prometheus, and file what is found as Gitea issues. Use when asked to check on production, look for issues or errors in ipx, see why something is slow or failing in production, review the logs, or investigate a report about the live site.
|
||||
description: Look for problems in production ipx (the ipx-web, iPX, ipx-directory and ipx-play containers on Tower, Tower itself and the monitoring that watches them) from their logs in Loki, their traces in Tempo and the metrics in Prometheus, and file what is found as Gitea issues. Use when asked to check on production, look for issues or errors in ipx, see why something is slow or failing in production, review the logs, or investigate a report about the live site.
|
||||
---
|
||||
|
||||
# Checking production ipx
|
||||
|
||||
Production is three containers in the Arcane project `ipx` on Tower
|
||||
Production is four containers in the Arcane project `ipx` on Tower
|
||||
(`/mnt/fast/arcane/projects/ipx/compose.yaml`), one request passing through them in this order:
|
||||
|
||||
| container | what | logs, in Loki | traces, in Tempo | metrics, in Prometheus |
|
||||
|---|---|---|---|---|
|
||||
| `ipx-web` | Caddy: serves the page, passes `/api` and `/media` to ipx (#203). Port 8099, the LAN and the tunnel | `{container="ipx-web"}`, JSON: a line a request (`http.log.access`), its own errors | service `ipx-web`: a span a request, passing `traceparent` on to ipx (#213) | none |
|
||||
| `ipx-web` | Caddy: serves the page, passes `/api` and `/media` to ipx (#203). Port 8099, the LAN and the tunnel | `{container="ipx-web"}`, JSON: a line a request (`http.log.access`), its own errors | service `ipx-web`: a span a request, passing `traceparent` on to ipx (#213) | `job="ipx-web"`: Caddy's own `caddy_http_*`, from `METRICS_BIND`, published as `192.168.1.130:9190` (#371) |
|
||||
| `iPX` | the daemon: API, scans, downloads, DeadAir | `{container="iPX"}`, JSON (`IPX_LOG_FORMAT=json`) | service `ipx`; passes `traceparent` to the directory | `job="ipx"`, its `/metrics` |
|
||||
| `ipx-directory` | the directory of public podcasts, `directory.ipxreader.app` (#204, #214) | `{container="ipx-directory"}`, JSON (`DIRECTORY_LOG_FORMAT=json`) | service `ipx-directory`: a span a request, joining ipx's trace when ipx called; a trace a refresher round (#213) | none |
|
||||
| `ipx-directory` | the directory of public podcasts, `directory.ipxreader.app` (#204, #214) | `{container="ipx-directory"}`, JSON (`DIRECTORY_LOG_FORMAT=json`) | service `ipx-directory`: a span a request, joining ipx's trace when ipx called; a trace a refresher round (#213) | `job="ipx-directory"`, its `/metrics` |
|
||||
| `ipx-play` | the page a shared episode opens, `play.ipxreader.app` (#447); ipx sends it the episodes shared | `{container="ipx-play"}`, JSON (`PLAY_LOG_FORMAT=json`): a line a request (`ipx_play::http`), one a share | service `ipx-play`: a span a request, joining ipx's trace on a share | `job="ipx-play"`, its `/metrics` on `metrics_bind` (production `192.168.1.130:9191`, LAN only, #458) |
|
||||
|
||||
Under them, Tower itself: `job="node"` (the node exporter: load, CPU, memory, disks). The Grafana
|
||||
Grafana's alert rules (`grafana/alerts.py`, #365) watch all of it between checks: start by asking
|
||||
which are firing (the monitoring section). Under them, Tower itself: `job="node"` (the node exporter: load, CPU, memory, disks). The Grafana
|
||||
dashboard `iPX` draws all of it in that order (`grafana/dashboard.py`). A trace without the
|
||||
production tag (`resource.deployment.environment.name="production"`, on all three) is a daemon run
|
||||
production tag (`resource.deployment.environment.name="production"`, on all four) is a daemon run
|
||||
by hand, not production. Since #213 a request is one trace across the services it touched: a
|
||||
Directory page is ipx-web, then ipx, then the directory, so start from the trace, not the logs. Alloy ships the logs; Loki, Tempo and
|
||||
Prometheus publish no query port to this machine, so use the script beside this file:
|
||||
@@ -37,7 +39,7 @@ $Q flame '<TraceQL>' [since] [out.svg] # where the matching spans' time went,
|
||||
"Since the last deploy" is since that container started (each deploys on its own):
|
||||
|
||||
```sh
|
||||
for c in iPX ipx-web ipx-directory; do S=$(docker inspect $c -f '{{.State.StartedAt}}'); echo "$c $(( ($(date +%s) - $(date -d "$S" +%s)) / 60 + 1 ))m"; done
|
||||
for c in iPX ipx-web ipx-directory ipx-play; do S=$(docker inspect $c -f '{{.State.StartedAt}}'); echo "$c $(( ($(date +%s) - $(date -d "$S" +%s)) / 60 + 1 ))m"; done
|
||||
```
|
||||
|
||||
Start every check by seeing that all three run, since Loki cannot see a container that is not:
|
||||
@@ -60,19 +62,24 @@ Lines inside a traced span, requests and scans, also carry `trace_id` and `span_
|
||||
lines had no trace id, the access log's time was `ms`, and each event was logged twice, words under
|
||||
`ipx::scan` and fields under `ipx::io`.)
|
||||
|
||||
What is routine is logged at debug, which goes to the admin page's Log view and not to Loki: a
|
||||
request that went well (#364), a scan's end (`scan_done`, #363), a feed not due (`feed_skip`), a
|
||||
feed starting (`feed_start`), and a feed that brought nothing (`feed_done` with every count 0).
|
||||
Count those from the metrics, not the log.
|
||||
|
||||
| target | fields | what |
|
||||
|---|---|---|
|
||||
| `ipx::http` | `method`, `path`, `route`, `status`, `duration_ms` | one per web request; `route` is the pattern, empty for an unrouted path |
|
||||
| `ipx::http` | `method`, `path`, `route`, `status`, `duration_ms` | a warning for each request answered 4xx or 5xx; `route` is the pattern, empty for an unrouted path. Every request, as info, until #364; now those that went well are only in `ipx_http_request_duration_seconds` |
|
||||
| `ipx::scan` | `ev` and the event's own: `feed`, `new`, `downloaded`, `failed`, `bytes`, `msg`, `url`, `feeds`, `reason`, `from`, `to`; on a failure `error.type` and, from an HTTP error, `http.response.status_code` | the daemon's events, one line each, in words; warnings are feed and download failures |
|
||||
| `ipx::io` | `ev`, `feeds`, `pending`, `downloaded` on the `status` reply | commands arriving (`-> {...}`) and the healthcheck's answer |
|
||||
| `ipx::io` | `ev`, `feeds`, `pending`, `downloaded` on the `status` reply | commands arriving on the socket (`-> {...}`) and the answer to `ipxd status` |
|
||||
| `ipx::daemon`, `ipx::cli`, `ipx::catalogue`, `ipx::scan`, ... | message, sometimes fields | start-up, shutdown, account and config messages, under the module that logs them; plain `ipx` before 2026-10-24, when main.rs was split (#356) |
|
||||
| `ipx::client` | `ev`, `user`, `feed`, `enclosure`, `deadair`; `msg`, `src`, `line` on an error; `ms` on a timing | what the page reports from the browser (#190): `page_error`, `rejection` and `player_error` as warnings; `play_start` (ms from play to sound), `stall` (ms waiting mid-play) and `list_shown` (ms from a click to its list drawn, with `feed` the view and `filter`, #246) as info. Lines carry the trace of the action in the page: its API requests send `traceparent`, one trace id per tap or key |
|
||||
| `ipx::client` | `ev`, `user`, `feed`, `enclosure`, `deadair`; `msg`, `src`, `line` on an error; `ms` on a timing | what the page reports from the browser (#190): `page_error`, `rejection`, `player_error` and `scroll_stuck` (a drag that should have scrolled the reader and did not, its snapshot in `state`, #384) as warnings; `swipe` (a swipe in the reader: `msg` next, prev, list, end or short, `state` its position, `5/50 of 312, more to load`, #384), `play_start` (ms from play to sound), `stall` (ms waiting mid-play) and `list_shown` (ms from a click to its list drawn, with `feed` the view and `filter`, #246) as info. Lines carry the trace of the action in the page: its API requests send `traceparent`, one trace id per tap or key |
|
||||
|
||||
Events (`ev`): `feed_start`, `feed_done` (new, downloaded, failed, torrents), `feed_skip` (not due,
|
||||
routine), `feed_error` (msg), `feed_moved` (from, to: a permanent redirect followed, the address
|
||||
Events (`ev`): `feed_start` (debug), `feed_done` (new, downloaded, failed, torrents), `feed_skip`
|
||||
(not due, debug), `feed_error` (msg), `feed_moved` (from, to: a permanent redirect followed, the address
|
||||
updated), `download_done` (bytes), `download_error` (msg, url),
|
||||
`torrent_deferred`, `reaped`, `scan_done` (feeds checked), `reap_done`, `status` (feeds, pending,
|
||||
downloaded: the healthcheck's, every 30s), `deadair_done` (feed, enclosure, `saved_s` at the
|
||||
`torrent_deferred`, `reaped`, `reap_done`, `scan_done` (feeds; debug), `status` (feeds, pending, downloaded: the answer to
|
||||
`ipxd status`, only when someone runs it since #218), `deadair_done` (feed, enclosure, `saved_s` at the
|
||||
default pause), `error` (msg). DeadAir also logs `DeadAir found the silences` (`cuts`, `saved_s`,
|
||||
`took_ms`) under `ipx::deadair`, and warns `DeadAir could not read a file` when ffmpeg fails on
|
||||
one, which it then passes over until the next restart.
|
||||
@@ -89,7 +96,7 @@ cheaper than parsing every line. Lines before 2026-09-29 14:00 UTC are text, not
|
||||
The daemon's own state between requests, which no log line or trace holds (#194): ipx serves
|
||||
`/metrics` on `[web] metrics_bind` (production: `192.168.1.130:9187`, LAN only), on a thread and
|
||||
port of its own, from memory alone, and Prometheus scrapes it every 15 s as `job="ipx"`. Kept
|
||||
from 2026-10-11 15:15 UTC. All gauges:
|
||||
from 2026-10-11 15:15 UTC. Gauges, but for the counters (`_total`) and the one histogram:
|
||||
|
||||
| metric | what |
|
||||
|---|---|
|
||||
@@ -102,10 +109,14 @@ from 2026-10-11 15:15 UTC. All gauges:
|
||||
| `ipx_process_resident_bytes`, `ipx_process_open_fds` | memory and open files and sockets; mimalloc gives back what is freed, so memory follows use and a rise that never comes down is a leak (#224) |
|
||||
| `ipx_feed_fetches_total`, `ipx_feed_bytes_total` | counters: feeds fetched (304s included) and the bytes of their bodies, read whole, so what scans allocate |
|
||||
| `ipx_uptime_seconds`, `ipx_build_info{version}` | since the daemon started; what runs |
|
||||
| `ipx_worker_busy`, `ipx_worker_job_seconds{kind}` | the worker's job now (`scan`, `reap`, `download`, `status`, `reload`) and for how long; the healthcheck passes while it is stuck |
|
||||
| `ipx_worker_busy`, `ipx_worker_job_seconds{kind}` | the worker's job now (`scan`, `reap`, `download`, `status`, `reload`) and for how long; `ipxd status` still answers while it is stuck |
|
||||
| `ipx_jobs_total{kind}`, `ipx_job_seconds_total{kind}` | counters: jobs finished, and their seconds all told |
|
||||
| `ipx_last_scan_age_seconds` | since a scan last finished; absent until one has since starting |
|
||||
| `ipx_http_requests_in_flight`, `ipx_scan_feeds_fetching`, `ipx_downloads_active` | work in progress |
|
||||
| `ipx_db_query_seconds{kind}`, `ipx_db_wait_seconds` | histograms (#367): each statement's time once it has a connection, by its first word (`select`, `insert`, ...); and the wait for a connection, measured for hand-written SQL only, about half the queries. A wait growing while queries stay flat is the pool full |
|
||||
| `ipx_page_seconds{ev}` | histogram: what the page timed, `list_shown`, `play_start`, `stall`, as the `ipx::client` lines say one by one (#369) |
|
||||
| `ipx_feed_failures_total{error_type}`, `ipx_new_items_total`, `ipx_downloads_total{result="saved"\|"failed"}`, `ipx_deadair_saved_seconds_total` | counters of what the events say (#368): feeds failed by kind, as the `feed_error` lines' `error.type`; new items; downloads; seconds of silence DeadAir found |
|
||||
| `ipx_http_request_duration_seconds{method,route,status}` | histogram: each request answered and its seconds, buckets 5 ms to 10 s; `_count` is requests, `_sum` their time. `route` is the pattern, `""` for an unrouted path; the event stream is left out (#364). From 2026-10-25 |
|
||||
| `ipx_download_bytes_total` | counter; `rate()` is the download speed |
|
||||
| `ipx_event_queue`, `ipx_events_missed_total` | live updates unread by some listener; counter of those dropped for one that fell behind |
|
||||
| `ipx_deadair_backlog`, `ipx_deadair_busy_seconds` | files of shows with DeadAir on not yet looked at (episodes everyone heard through stay counted); how long it has been on its file |
|
||||
@@ -116,8 +127,8 @@ from 2026-10-11 15:15 UTC. All gauges:
|
||||
| `ipx_jev_requests_total{result="ok"\|"key_refused"\|"malformed"\|"rate_limited"\|"error"}`, `ipx_jev_seconds_total`, `ipx_jev_paced_seconds_total` | counters: calls to TypeSafe's Jev, Signals' and icons', by how they ended; their seconds, the wait for a turn included; and that wait alone. `malformed` is ipx's request at fault (#257); `rate_limited` is a 429 from TypeSafe, whose limits are 80 calls and 100,000 input tokens a second, which ipx paces itself under at 60 and 80,000 (#277) |
|
||||
| `ipx_jev_tokens_total{kind="input"\|"output",user,key}` | counter: tokens TypeSafe counted, as its answers say, by person and whose key paid: what Signals, icon picks and key checks cost (#275, #278). A call shared on the server's key is split by each person's share of its questions |
|
||||
| `ipx_signals_last_pass_age_seconds`, `ipx_signals_keys_refused` | since the Signals judge last finished a pass (at most 5 minutes when well; absent until one has); people whose key TypeSafe refused |
|
||||
| `directory_process_resident_bytes`, `directory_process_open_fds`, `directory_feed_fetches_total`, `directory_feed_bytes_total`, `directory_uptime_seconds`, `directory_build_info` | the directory's, `job="ipx-directory"`, from its `metrics_bind` (production `192.168.1.130:9188`, LAN only) |
|
||||
| `ipx_feeds`, `ipx_downloads_pending`, `ipx_files_downloaded` | the library, as the healthcheck last counted it (every 30 s) |
|
||||
| `directory_process_resident_bytes`, `directory_process_open_fds`, `directory_feed_fetches_total`, `directory_feed_bytes_total`, `directory_uptime_seconds`, `directory_build_info`, `directory_http_request_duration_seconds{method,route,status}`, `directory_feed_checks_total`, `directory_feed_checks_failed_total`, `directory_snapshots_total` | the directory's (the last four #370), `job="ipx-directory"`, from its `metrics_bind` (production `192.168.1.130:9188`, LAN only) |
|
||||
| `ipx_feeds`, `ipx_downloads_pending`, `ipx_files_downloaded` | the library, as the daemon counts it every 30 s |
|
||||
|
||||
### The checks
|
||||
|
||||
@@ -140,16 +151,20 @@ a count says something happened, the lines and traces say why.
|
||||
browser, a redirect loop ipx should follow, or the same failure on many feeds at once is ipx.
|
||||
3. **Server errors and slow requests.**
|
||||
```
|
||||
$Q metric 'sum by (method, route, status) (count_over_time({container="iPX"} |= "\"target\":\"ipx::http\"" | json | __error__="" | status >= 500 [$range]))'
|
||||
$Q metric 'topk(10, quantile_over_time(0.95, {container="iPX"} |= "\"target\":\"ipx::http\"" | json | __error__="" | route != "" | route != "/api/events" | unwrap duration_ms [$range]) by (method, route))'
|
||||
$Q prom 'sum by (method, route, status) (increase(ipx_http_request_duration_seconds_count{job="ipx",status=~"4..|5.."}[$range])) > 0' 24h
|
||||
$Q prom 'topk(10, histogram_quantile(0.95, sum by (method, route, le) (rate(ipx_http_request_duration_seconds_bucket{job="ipx",route!=""}[$range]))))' 24h
|
||||
$Q logs '{container="iPX"} |= "\"target\":\"ipx::http\""' 24h
|
||||
```
|
||||
Any 5xx is worth a look. 401s are people signing in, not a problem unless one address is
|
||||
hammering. For a slow route, find its traces (check 5) and see which span holds the time.
|
||||
Any 5xx is worth a look; the log has each failed request's own line, with its trace id. 401s
|
||||
are people signing in, not a problem unless one address is hammering. The 95th percentile is
|
||||
read off the buckets, so it says which bucket, not the exact time: for a slow route, find its
|
||||
spans (check 5) and see which step holds the time. The histogram restarts at 0 with the daemon,
|
||||
which `increase` and `rate` allow for.
|
||||
4. **Is the worker keeping up?** Scans should finish regularly, the queue should drain, and the
|
||||
daemon should not be restarting on its own.
|
||||
```
|
||||
$Q metric 'sum(count_over_time({container="iPX"} |= "\"ev\":\"scan_done\"" [$range]))' 6h
|
||||
$Q logs '{container="iPX"} |= "\"ev\":\"status\"" | json | line_format "{{.timestamp}} pending={{.pending}} downloaded={{.downloaded}}"' 6h
|
||||
$Q prom 'sum(increase(ipx_jobs_total{job="ipx",kind="scan"}[$range]))' 6h
|
||||
$Q prom 'max_over_time(ipx_downloads_pending{job="ipx"}[$range])' 6h
|
||||
$Q logs '{container="iPX"} |= "daemon started"' 7d
|
||||
```
|
||||
A `daemon started` not matched by a deploy (see `git log` and the image's build time) is a
|
||||
@@ -158,8 +173,9 @@ a count says something happened, the lines and traces say why.
|
||||
keeping up or not running.
|
||||
|
||||
Since 2026-10-02 the daemon sleeps until the next feed is due (at most 10 minutes) instead of
|
||||
scanning every minute (#114), so expect tens of scans in 6 hours, not 360, nearly all with
|
||||
`feeds` above 0; none at all for over 10 minutes means the worker is stuck. And `pending` is
|
||||
scanning every minute (#114), so expect a scan every few minutes, most of one or two feeds;
|
||||
none at all for over 10 minutes means the worker is stuck. A scan's end (`scan_done`) is
|
||||
logged at debug since #363, so it is in the page's log view and not in Loki. And `pending` is
|
||||
the real queue (#113): files a scan will download on its own. Back-catalogue files are
|
||||
`held`, listed but not counted, so it is usually 0 or a handful.
|
||||
5. **Slow and failed traces.** Look at ipx's requests as spans, not traces: a trace started by a
|
||||
@@ -170,9 +186,15 @@ a count says something happened, the lines and traces say why.
|
||||
R='resource.deployment.environment.name="production" && resource.service.name="ipx"'
|
||||
$Q spans "{$R && span.http.route != \"\" && span.http.route != \"/api/events\" && span.http.route !~ \"/media/.*\" && duration > 1s} | select(span.http.route)"
|
||||
$Q spans "{$R && name=\"step\" && duration > 500ms} | select(span.of)"
|
||||
$Q traces '{resource.deployment.environment.name="production" && status = error}'
|
||||
$Q traces '{resource.deployment.environment.name="production" && resource.service.name=~"ipx|ipx-web|ipx-directory" && status = error}'
|
||||
$Q trace <id>
|
||||
```
|
||||
Other services on Tower send production traces too (hnopml's `poll`, a Hacker News fetch that
|
||||
times out now and then): `traces` names each trace's service and `trace` prints an event's
|
||||
fields (its `error`), so say whose a failure is before reading on. Tempo keeps about two weeks. A trace id from a log line that Tempo answers 404 for, or a
|
||||
search that finds only the last quarter of an hour, is Tempo not reading its stored blocks: see
|
||||
the monitoring section.
|
||||
|
||||
Group the first by hour and by route (`awk '{print substr($3,1,2), $NF}' | sort | uniq -c`),
|
||||
then lay the hours beside Tower's (the Tower section): load over 100 with processes blocked
|
||||
and memory under 4 GB is the host stalling, and every request of those minutes is slow with
|
||||
@@ -193,9 +215,18 @@ a count says something happened, the lines and traces say why.
|
||||
since #193, so a reap is about a second), `trim_artwork`, with what it removed on the `reap`
|
||||
span. DeadAir's work on a file is a `deadair` trace (feed, enclosure, cuts) with `probe`,
|
||||
`silencedetect`, `frame_bytes`, `store_plans`: seconds each, more is a file ffmpeg struggles
|
||||
with. A trace with no name in `$Q traces` started in the page (#190, #192): its root is the tap
|
||||
in the browser, which sends no span, so it holds the requests that tap made (API calls, the
|
||||
playlist and audio of a play, position saves, the page's own report) side by side.
|
||||
with. A trace begun in the page (#190, #192) has the tap or key as its root span, `tap` or
|
||||
`key` (#376), from the moment it began to its last API answer, with the requests it made under
|
||||
it (API calls, the playlist and audio of a play, position saves, the page's own report): a tap
|
||||
long with its requests short is the page itself, drawing. The page reports the tap when the next
|
||||
one begins or it is hidden, so the root arrives last; a trace with no name in `$Q traces` is
|
||||
requests no tap began (a live update's, a timer's), or a tap not reported yet.
|
||||
|
||||
Every command the worker runs from the socket, the CLI or the page is a `command` span (`cmd`,
|
||||
#373) with its job inside; a scan on the clock is a `scan` of its own. A list's sync is
|
||||
`list_sync` (`list`, `listed`) with steps `upsert_listed`, `subscribe_readers`, `drop_unlisted`,
|
||||
and an OPML upload `opml_import` (#372). The daemon's start is a `start` trace (#374), from
|
||||
`open_database` and `assemble_config` to `daemon started`: how long a deploy took to answer.
|
||||
|
||||
6. **The daemon's state, from Prometheus.** Pressure and leaks that requests and traces only
|
||||
hint at.
|
||||
@@ -213,6 +244,8 @@ a count says something happened, the lines and traces say why.
|
||||
$Q prom 'max(max_over_time(ipx_last_scan_age_seconds{job="ipx"}[$range]))' 24h
|
||||
$Q prom 'sum by (kind) (increase(ipx_job_seconds_total{job="ipx"}[$range]))' 24h
|
||||
$Q prom 'max(max_over_time(ipx_http_requests_in_flight{job="ipx"}[$range]))' 24h
|
||||
$Q prom 'histogram_quantile(0.99, sum by (le) (rate(ipx_db_wait_seconds_bucket{job="ipx"}[$range])))' 24h
|
||||
$Q prom 'histogram_quantile(0.95, sum by (le, kind) (rate(ipx_db_query_seconds_bucket{job="ipx"}[$range])))' 24h
|
||||
$Q prom 'sum(increase(ipx_events_missed_total{job="ipx"}[$range]))' 24h
|
||||
$Q prom 'max(max_over_time(ipx_deadair_busy_seconds{job="ipx"}[$range]))' 24h
|
||||
$Q prom 'sum by (result) (increase(ipx_jev_requests_total{job="ipx"}[$range]))' 24h
|
||||
@@ -228,10 +261,10 @@ a count says something happened, the lines and traces say why.
|
||||
reset of the uptime is a restart: match it with a deploy, or it is a crash (check 4).
|
||||
|
||||
The worker: one job over 30 minutes (`ipx_worker_job_seconds`), or a scan last finished over
|
||||
15 minutes ago, is the worker stuck, which the healthcheck does not notice; its trace and the
|
||||
15 minutes ago, is the worker stuck, which `ipxd status` does not show; its trace and the
|
||||
log lines of that time say on what. A kind whose share of `ipx_job_seconds_total` jumps is
|
||||
where the worker's time went (reap was most of it until #193). Web requests in flight near
|
||||
the pool's max mean the pool is what they wait on. Live updates missed mean a page showed stale
|
||||
the pool's max mean the pool is what they wait on, and `ipx_db_wait_seconds` for how long. Live updates missed mean a page showed stale
|
||||
counts until reloaded. DeadAir over a minute on one file is a file ffmpeg chokes on.
|
||||
Any `rate_limited` Jev call means the pacing is not holding ipx under TypeSafe's limits (#277); each
|
||||
`jev` span has `paced_ms`, its wait for a turn. Any `malformed` Jev call is ipx asking wrongly (#257); `key_refused` is someone's key; the Signals
|
||||
@@ -242,6 +275,9 @@ a count says something happened, the lines and traces say why.
|
||||
$Q metric 'sum by (ev, msg) (count_over_time({container="iPX"} |= "\"target\":\"ipx::client\"" | json | __error__="" | level="WARN" [$range]))' 7d
|
||||
$Q logs '{container="iPX"} |= "\"target\":\"ipx::client\"" | json | ev=~"play_start|stall" | line_format "{{.timestamp}} {{.user}} {{.ev}} {{.ms}}ms deadair={{.deadair}} {{.feed}}"' 24h
|
||||
$Q metric 'quantile_over_time(0.95, {container="iPX"} |= "\"ev\":\"list_shown\"" | json | __error__="" | unwrap ms [$range]) by (feed, filter)' 24h
|
||||
$Q metric 'sum by (msg) (count_over_time({container="iPX"} |= "\"ev\":\"swipe\"" | json | __error__="" [$range]))' 7d
|
||||
$Q logs '{container="iPX"} |= "\"ev\":\"swipe\"" |= "\"msg\":\"end\"" | json | line_format "{{.timestamp}} {{.user}} {{.feed}} {{.filter}} {{.state}}"' 7d
|
||||
$Q logs '{container="iPX"} |= "\"ev\":\"scroll_stuck\"" | json | line_format "{{.timestamp}} {{.user}} {{.feed}} {{.state}}"' 7d
|
||||
```
|
||||
A `player_error` is playback that failed in front of someone: `hls.js: ...` is DeadAir's
|
||||
playlist (its trace has the playlist and audio requests), `media error 4` a file the browser
|
||||
@@ -249,6 +285,19 @@ a count says something happened, the lines and traces say why.
|
||||
the minified `app.js`, so match the message to `web/src`. A `list_shown` over 1000 ms is a list someone waited for: its trace says whether the API or the network held it. `play_start` over 2000 ms, or a
|
||||
`stall`, is worth a trace: was it the server, Cloudflare, or the network.
|
||||
|
||||
Swipes and a reader that will not scroll (#383, #384). An `end` swipe is the reader bouncing
|
||||
off the last item loaded: one whose `state` says `more to load` is the page failing to fetch
|
||||
the next page in time, and several in a row from one person at the same position is #383 back.
|
||||
An `end` without `more to load` is someone at the real end of the list, and fine. Many `short`
|
||||
against few `next` is swipes not being taken, worth a look at `swipeMin` in `gestures.ts`.
|
||||
Any `scroll_stuck` is a reader someone could not scroll, the bug #383 reported and never
|
||||
found: read its `state` before anything else. `under the finger` other than `#detail` or
|
||||
something inside it names a layer lying over the reader; `inert` other than `#dpeek`, `dialog
|
||||
open`, a `transform` other than `none`, or `peek left behind` name what the page left in a wrong
|
||||
state; `shown Ns ago` small says it followed a return from the background. File it with the
|
||||
`state` line and that person's swipes just before it (`$Q logs` on `ev=~"swipe|scroll_stuck"` and
|
||||
the `user`), even when the cause is not clear yet: it is the one record of the bug.
|
||||
|
||||
`docker logs --since 10m iPX 2>&1 | tail -5` shows the newest lines when Loki is behind.
|
||||
|
||||
## ipx-web, the page
|
||||
@@ -256,23 +305,27 @@ a count says something happened, the lines and traces say why.
|
||||
Caddy logs one JSON line a request (#210), logger `http.log.access.log0`: Loki's `| json` gives
|
||||
`request_uri`, `request_method`, `request_remote_ip`, `status`, `duration` (seconds, not ms), `size`.
|
||||
The request's headers and a `?token=` are filtered out before it is written. Requests from
|
||||
`127.0.0.1` are local checks; leave them out. `/api` and `/media` pass on to ipx and
|
||||
are in ipx's own access log too, with ipx's time; Caddy's `duration` for them adds the hop.
|
||||
`127.0.0.1` are local checks; leave them out. `/api` and `/media` pass on to ipx, which counts
|
||||
them in `ipx_http_request_duration_seconds` with its own time; Caddy's `duration` for them adds
|
||||
the hop. This is the only log with a line for every request. Caddy's own metrics, `job="ipx-web"`
|
||||
(`caddy_http_request_duration_seconds{code,handler,method}` and the like), count every request too,
|
||||
502s included, which ipx never sees.
|
||||
|
||||
```sh
|
||||
W='{container="ipx-web"} |= "http.log.access" | json | __error__="" | request_remote_ip != "127.0.0.1"'
|
||||
$Q metric "sum by (status) (count_over_time($W [\$range]))"
|
||||
$Q metric "sum by (request_uri) (count_over_time($W | status >= 500 [\$range]))"
|
||||
$Q metric "topk(10, quantile_over_time(0.95, $W | request_uri !~ \"/(api|media)/.*\" | unwrap duration [\$range]) by (request_uri))"
|
||||
$Q logs '{container="ipx-web"} | json | __error__="" | level=~"warn|error" | msg !~ "admin endpoint disabled|automatic HTTPS is completely disabled|HTTP/[23] skipped.*|exiting; byeee.*"'
|
||||
$Q logs '{container="ipx-web"} | json | __error__="" | level=~"warn|error" | msg !~ "admin endpoint disabled|automatic HTTPS is completely disabled|HTTP/[23] skipped.*|exiting; byeee.*" | request_uri != "/api/events"'
|
||||
```
|
||||
|
||||
A 502 is ipx not answering Caddy: a restart (match the deploy) or ipx down; Caddy's error line
|
||||
names it (`dial tcp ...:8099: connect: connection refused`). The page itself (`/`, `/admin`)
|
||||
waits on `/api/page-check` before it is sent, so a slow page is usually a slow page-check: look it
|
||||
up in ipx's access log. 401 and 303 on `/` are people signed out being sent to `/login`, not a
|
||||
waits on `/api/page-check` before it is sent, so a slow page is usually a slow page-check: its
|
||||
route in the histogram (check 3), or ipx's span inside Caddy's in the trace. 401 and 303 on `/` are people signed out being sent to `/login`, not a
|
||||
problem. Caddy's own notices at each start (no admin endpoint, no HTTPS or HTTP/3, its goodbye on
|
||||
SIGTERM) are by design and filtered out above.
|
||||
SIGTERM) are by design and filtered out above, and so is `aborting with incomplete response` on
|
||||
`/api/events`: a page closing or reconnecting its live-update stream, about 600 a week.
|
||||
|
||||
Each access line carries the request's `traceID` (`| json` gives `traceID`): give it to `$Q trace`
|
||||
to see where the time went, Caddy's span with ipx's inside it. A slow page with ipx's span short is
|
||||
@@ -297,10 +350,13 @@ It logs a line a request (`target` `ipx_directory::http`: `method`, `path`, `sta
|
||||
line a refresher round (`feeds checked`: `checked`), a line a failed feed check (`feed check
|
||||
failed`: `feed`, the directory's id, and `error`), the admin's actions (`feed removed and
|
||||
blocked`, `feed unblocked`, `snapshot dropped`, each with `by`), and `config read again` when its
|
||||
token file changes. It has no metrics. Its traces (#213): a span a request (`http`), a child of
|
||||
token file changes. Its lines carry their own fields and no span (#363). Its metrics, as
|
||||
`job="ipx-directory"` (production `192.168.1.130:9188`): `directory_process_resident_bytes`,
|
||||
`directory_process_open_fds`, `directory_feed_fetches_total`, `directory_feed_bytes_total`,
|
||||
`directory_uptime_seconds`, `directory_build_info`. Its traces (#213): a span a request (`http`), a child of
|
||||
ipx's when ipx called with a `traceparent`, and a trace a refresher round (`refresh`, `feeds`)
|
||||
with a `feed` span for each check. Its log lines carry no trace id; find the trace by time or by
|
||||
the feed.
|
||||
with a `feed` span for each check. Its lines inside a span carry `trace_id` and `span_id`, as
|
||||
ipx's do (#375): a `feed check failed` leads to its check's span.
|
||||
|
||||
```sh
|
||||
$Q traces '{resource.service.name="ipx-directory" && duration > 2s}'
|
||||
@@ -367,11 +423,41 @@ container's lines are reaching Loki:
|
||||
```sh
|
||||
curl -s http://192.168.1.130:9090/api/v1/targets | python3 -c 'import json,sys
|
||||
for t in json.load(sys.stdin)["data"]["activeTargets"]: print(t["health"], t["labels"]["job"], t["scrapeUrl"], t.get("lastError","")[:80])'
|
||||
for c in iPX ipx-web ipx-directory; do echo "$c $($Q metric "sum(count_over_time({container=\"$c\"} [1h]))" 1h | tail -1)"; done
|
||||
for c in iPX ipx-web ipx-directory ipx-play; do echo "$c $($Q metric "sum(count_over_time({container=\"$c\"} [24h]))" 24h | tail -1)"; done
|
||||
docker ps -a --filter label=com.docker.compose.project=monitoring --format '{{.Names}} {{.Status}}'
|
||||
docker logs --since 1h tempo 2>&1 | grep -E 'failed to poll|blocklist poll complete' | tail -2
|
||||
```
|
||||
|
||||
`ipx` and `node` should be `up`. Every monitoring container should be running; after Docker
|
||||
`ipx`, `ipx-directory`, `ipx-play`, `ipx-web`, `node` and the monitoring's own, `prometheus`, `tempo`, `loki`
|
||||
and `alloy` (#366), should be `up`. Alloy is scraped at `alloy:12345` because its command sets
|
||||
`--server.http.listen-addr`; it listens on 127.0.0.1 without it.
|
||||
|
||||
The alert rules (#365) are `grafana/alerts.py`, written to
|
||||
`/mnt/fast/arcane/projects/monitoring/grafana-provisioning/alerting/ipx.json` and loaded by
|
||||
`POST /api/admin/provisioning/alerting/reload`. There is no contact point yet, so one firing is
|
||||
only in Grafana: ask it which are, at the start of every check. The password is in the
|
||||
monitoring project's compose file; never print it.
|
||||
|
||||
```sh
|
||||
PW=$(grep -oP 'GF_SECURITY_ADMIN_PASSWORD=\K\S+' /mnt/fast/arcane/projects/monitoring/compose.yaml)
|
||||
curl -s -u "admin:$PW" http://192.168.1.130:3444/api/prometheus/grafana/api/v1/rules | python3 -c 'import json,sys
|
||||
for g in json.load(sys.stdin)["data"]["groups"]:
|
||||
for r in g["rules"]: print(r["state"], r["name"])' | sort
|
||||
```
|
||||
|
||||
Tempo looks over its stored blocks every 5 minutes. `failed to poll or create index for tenant`
|
||||
means it could not, and then searches and trace lookups see only what it still holds in memory,
|
||||
about the last 15 minutes, while its 2,000 blocks sit unread on disk (its search log says
|
||||
`total_blocks=0`). On 2026-10-25 that was one block left empty, every file 0 bytes, by a write cut
|
||||
short the night before: `unexpected end of JSON input`. Find it and move it out of
|
||||
`/mnt/user/logs/tempo/blocks/single-tenant/`; the next look over, within 5 minutes, logs
|
||||
`writing tenant index` and finds the rest. Tell the user, since it is the monitoring's, not ipx's:
|
||||
|
||||
```sh
|
||||
for d in /mnt/user/logs/tempo/blocks/single-tenant/*/; do python3 -c 'import json,sys; json.load(open(sys.argv[1]))' "$d/meta.json" 2>/dev/null || echo "$d"; done
|
||||
```
|
||||
|
||||
Every monitoring container should be running; after Docker
|
||||
restarts, one can be left `Exited (255)` though its policy is `unless-stopped` (Prometheus, on
|
||||
2026-10-12, when Docker lost its exit during an overload): `docker start` it and say so. blocky
|
||||
and nut on 192.168.2.21 are another machine, not ipx's; leave them. A container with no lines in
|
||||
@@ -380,7 +466,7 @@ container after a deploy.
|
||||
|
||||
## Rules
|
||||
|
||||
`docker logs` and `docker exec iPX ipx ...` are fine. On production's Postgres, `EXPLAIN (ANALYZE,
|
||||
`docker logs` and `docker exec iPX ipxd ...` are fine. On production's Postgres, `EXPLAIN (ANALYZE,
|
||||
BUFFERS)` of any query is allowed, read only, as CLAUDE.md's "Production's database" shows; any
|
||||
other query, ask the user first.
|
||||
|
||||
@@ -409,6 +495,8 @@ check is for finding them.
|
||||
## When the checks come back empty
|
||||
|
||||
Check that there is data before concluding all is well (the monitoring section's per-container
|
||||
count): iPX should have hundreds of lines an hour or more, ipx-web at least its healthchecks'
|
||||
120, ipx-directory a few dozen. Nothing at all means Alloy is not shipping it, or the container
|
||||
is down.
|
||||
count over a day): iPX a few hundred (new items, Signals, shares with the directory, the page's
|
||||
timings; more on a busy day), ipx-web two thousand or so while the page is in use (a line a
|
||||
request), ipx-directory several hundred. Since #363 and #364 iPX can go an hour at night with no
|
||||
line at all, so judge it by the day. Nothing in a day means Alloy is not shipping it, or the
|
||||
container is down.
|
||||
|
||||
@@ -157,7 +157,8 @@ def main():
|
||||
when = time.strftime("%m-%d %H:%M:%S", time.gmtime(int(t["startTimeUnixNano"]) / 1e9))
|
||||
# No root name: its root span is a tap in the page, which the browser never sends.
|
||||
name = t.get("rootTraceName") or "(from the page)"
|
||||
print(f"{t.get('durationMs', 0):>8}ms {when}Z {t['traceID']} {name}")
|
||||
# Production holds other services' traces too (hnopml's `poll`); say whose.
|
||||
print(f"{t.get('durationMs', 0):>8}ms {when}Z {t['traceID']} {t.get('rootServiceName', '?')} {name}")
|
||||
print(f"-- {len(traces)} trace(s)", file=sys.stderr)
|
||||
elif kind == "spans":
|
||||
# The spans that matched, each with its own time: a trace's length says nothing about a
|
||||
@@ -199,7 +200,12 @@ def main():
|
||||
f.write(flame_svg(stacks, n, q))
|
||||
print(f"-- {len(roots)} span(s) in {len(found)} trace(s): {out}, {out}.folded", file=sys.stderr)
|
||||
elif kind == "trace":
|
||||
spans = trace_spans(fetch(f"http://tempo:3200/api/traces/{q}"))
|
||||
tr = fetch(f"http://tempo:3200/api/traces/{q}")
|
||||
spans = trace_spans(tr)
|
||||
service = {sp["spanId"]: next((a["value"].get("stringValue") for a in b.get("resource", {}).get("attributes", [])
|
||||
if a["key"] == "service.name"), "?")
|
||||
for b in tr.get("batches", tr.get("resourceSpans", []))
|
||||
for ss in b.get("scopeSpans", b.get("instrumentationLibrarySpans", [])) for sp in ss["spans"]}
|
||||
kids = {}
|
||||
for sp in spans:
|
||||
kids.setdefault(sp.get("parentSpanId", ""), []).append(sp)
|
||||
@@ -209,13 +215,19 @@ def main():
|
||||
attrs = {a["key"]: next(iter(a["value"].values()), None) for a in sp.get("attributes", [])
|
||||
if not a["key"].startswith(("code.", "thread.")) and a["key"] not in ("busy_ns", "idle_ns", "target")}
|
||||
err = " ERROR" if sp.get("status", {}).get("code") in (2, "STATUS_CODE_ERROR") else ""
|
||||
print(f"{' ' * depth}{sp['name']} {ms:.0f}ms{err} {json.dumps(attrs) if attrs else ''}")
|
||||
parent = next((p for p in spans if p["spanId"] == sp.get("parentSpanId")), None)
|
||||
# The service where it changes: a root, or a hop from ipx-web to ipx to the directory.
|
||||
svc = f"[{service.get(sp['spanId'])}] " if parent is None or service.get(parent["spanId"]) != service.get(sp["spanId"]) else ""
|
||||
print(f"{' ' * depth}{svc}{sp['name']} {ms:.0f}ms{err} {json.dumps(attrs) if attrs else ''}")
|
||||
for e in sp.get("events", []):
|
||||
msg = next((a["value"].get("stringValue") for a in e.get("attributes", []) if a["key"] == "message"), e.get("name"))
|
||||
fields = {a["key"]: next(iter(a["value"].values()), None) for a in e.get("attributes", [])}
|
||||
msg = fields.pop("message", None) or e.get("name")
|
||||
# An event's own fields carry the why: `poll failed` alone hid hnopml's timeout.
|
||||
extra = {k: v for k, v in fields.items() if not k.startswith(("code.", "thread.")) and k not in ("level", "target")}
|
||||
# A scan logs a skip for every feed not due; they bury what happened.
|
||||
if '"ev":"feed_skip"' in (msg or ""):
|
||||
continue
|
||||
print(f"{' ' * depth} - {msg}")
|
||||
print(f"{' ' * depth} - {msg}{' ' + json.dumps(extra) if extra else ''}")
|
||||
for k in sorted(kids.get(sp["spanId"], []), key=lambda s: int(s["startTimeUnixNano"])):
|
||||
show(k, depth + 1)
|
||||
|
||||
|
||||
@@ -1,18 +1,22 @@
|
||||
#!/bin/sh
|
||||
# Refuses a commit whose Rust is not as rustfmt writes it, or that clippy warns about. Clippy only
|
||||
# checks and never links, so it is light beside a test build (CLAUDE.md, Tests). It checks the
|
||||
# crates the commit touches and those built on them, not the workspace: ipx uses ipx-directory,
|
||||
# and both use ipx-feed. A commit to the page alone runs nothing here.
|
||||
# crates the commit touches and those built on them, not the workspace: ipx uses ipx-play and
|
||||
# ipx-directory, ipx-play uses ipx-directory, and all use ipx-feed and ipx-wire. A commit to the page alone runs nothing here.
|
||||
staged=$(git diff --cached --name-only --diff-filter=ACMD)
|
||||
echo "$staged" | grep -q -e '\.rs$' -e 'Cargo\.toml$' -e '^Cargo\.lock$' || exit 0
|
||||
if ! cargo fmt --all --check >/dev/null 2>&1; then
|
||||
echo "pre-commit: the Rust is not formatted; run cargo fmt --all" >&2
|
||||
exit 1
|
||||
fi
|
||||
if echo "$staged" | grep -q -e '^Cargo\.toml$' -e '^Cargo\.lock$' -e '^crates/feed/'; then
|
||||
pkgs="ipx-feed ipx-directory ipx"
|
||||
if echo "$staged" | grep -q -e '^Cargo\.toml$' -e '^Cargo\.lock$' -e '^crates/feed/' -e '^crates/wire/'; then
|
||||
pkgs="ipx-feed ipx-wire ipx-directory ipx-play ipx ipx-tui"
|
||||
elif echo "$staged" | grep -q '^crates/tui/'; then
|
||||
pkgs="ipx-tui"
|
||||
elif echo "$staged" | grep -q '^crates/directory/'; then
|
||||
pkgs="ipx-directory ipx"
|
||||
pkgs="ipx-directory ipx-play ipx"
|
||||
elif echo "$staged" | grep -q '^crates/play/'; then
|
||||
pkgs="ipx-play ipx"
|
||||
elif echo "$staged" | grep -q '^crates/ipx/'; then
|
||||
pkgs="ipx"
|
||||
else
|
||||
|
||||
189
CLAUDE.md
189
CLAUDE.md
@@ -20,6 +20,7 @@ service of the `content` project, beside audiobookshelf, immich and Plex (#208).
|
||||
| Old database | `/mnt/user/ipx/state.db`, SQLite, used until the move to Postgres on 2026-09-18 and kept for rollback | `/data/state.db` |
|
||||
| Downloads | `/mnt/user/ipx/downloads` | `/downloads` |
|
||||
| Directory | `ipx-directory` in the same project, `192.168.1.130:8100`, public at `directory.ipxreader.app` through the tunnel with no Access in front: its pages are anyone's to browse, read only (#215), and its API (`/v1`) takes a token for everything. Until 2026-10-12 it was `ipx-dir.sdf1.net` (#214). Config `/mnt/fast/appdata/ipx-directory/directory.toml` (token hashes; Tower's own token in `tower.token` beside it, and entered in ipx's Admin, Directory, which keeps it in the database's `settings`, with the address ipx reaches it at: `http://ipx-directory:8100`, inside Docker, not the public address, which went out through Cloudflare and the tunnel, 170-340 ms a call where the directory answers in 16 ms), database `ipx_directory` on the same Postgres, URL in `directory.env` beside the compose file. See [docs/directory.md](docs/directory.md) | `/config/directory.toml` |
|
||||
| Shared episodes | `ipx-play` in the same project, `192.168.1.130:8102`, public at `play.ipxreader.app` through the tunnel with no Access in front: the page a shared episode opens, anyone's to see (#447). `/v1` takes a token. Config `/mnt/fast/appdata/ipx-play/play.toml` (token hashes; Tower's token entered in ipx's Admin, Directory, Sharing episodes, with the address `http://ipx-play:8102`), database `ipx_play` on the same Postgres, URL in `play.env` beside the compose file. Metrics for Prometheus on `192.168.1.130:9191`, LAN only, as `job="ipx-play"` (#458). See [docs/play.md](docs/play.md) | `/config/play.toml` |
|
||||
| Home page | `ipxreader.app` (and `www`): a static page from `site/`, on the Cloudflare Pages project `ipxreader`, not on Tower. See below | |
|
||||
| Web UI | `ipx-web` (Caddy) in the same project, at `192.168.1.130:8099`, also `ipodderx.sdf1.net` via a Cloudflare tunnel. It serves the page and passes `/api` and `/media` to ipx, whose own port is published nowhere; its address on `ipx_default` is pinned to `192.168.64.10` (#203) | ipx `0.0.0.0:8099`, Caddy `:8080` |
|
||||
| Sign-in via the tunnel | Cloudflare Access app `ipodderx`, with Authentik as its identity provider; see [docs/sso.md](docs/sso.md) | Caddy passes `Cf-Access-*` only from `192.168.64.1`, the `ipx_default` gateway cloudflared arrives from (`TUNNEL_PEER`; the subnet is pinned in the compose file); ipx trusts them only from Caddy, `192.168.64.10`, and only with a valid `Cf-Access-Jwt-Assertion` (`access_team`, `access_aud`) |
|
||||
@@ -48,9 +49,18 @@ Labels: `bug` for something wrong, `enhancement` for something missing. A proble
|
||||
for later stays open, and that is how it gets picked up again.
|
||||
|
||||
Deploying a change is: build and push the image, then pull it and recreate the container.
|
||||
**Build only with `scripts/build-image.sh`, never a bare `docker buildx build`.** On 2026-11-04
|
||||
a plain build of ipx, its dependency layer invalidated by a `Cargo.toml` change, rebuilt
|
||||
everything in release on all twelve threads with 5 GB free; Tower thrashed for three hours with
|
||||
every core pegged and had to be rebooted. The script refuses to start under 6 GB available or
|
||||
above load 12, and builds in the BuildKit container `ipx-capped` (made on first use), held to 6
|
||||
GB of memory and six threads by its cgroup: a build that outgrows that is killed, and production
|
||||
keeps running. If one is killed, say so and wait for the user to decide; do not raise the caps or go
|
||||
around the script. Run the build in the foreground on its own, never chained after a cargo
|
||||
command, and look at `MemAvailable` and the load again when it ends.
|
||||
|
||||
```sh
|
||||
docker buildx build --tag 192.168.1.130:5000/ipx:latest . --push
|
||||
sh scripts/build-image.sh ipx
|
||||
docker compose -f /mnt/fast/arcane/projects/ipx/compose.yaml pull ipx
|
||||
docker compose -f /mnt/fast/arcane/projects/ipx/compose.yaml up -d ipx
|
||||
docker logs --tail 20 iPX
|
||||
@@ -59,27 +69,42 @@ docker logs --tail 20 iPX
|
||||
The directory is the same, with its own image:
|
||||
|
||||
```sh
|
||||
docker buildx build -f Dockerfile.directory --tag 192.168.1.130:5000/ipx-directory:latest . --push
|
||||
sh scripts/build-image.sh directory
|
||||
docker compose -f /mnt/fast/arcane/projects/ipx/compose.yaml pull ipx-directory
|
||||
docker compose -f /mnt/fast/arcane/projects/ipx/compose.yaml up -d ipx-directory
|
||||
```
|
||||
|
||||
ipx-play the same:
|
||||
|
||||
```sh
|
||||
sh scripts/build-image.sh play
|
||||
docker compose -f /mnt/fast/arcane/projects/ipx/compose.yaml pull ipx-play
|
||||
docker compose -f /mnt/fast/arcane/projects/ipx/compose.yaml up -d ipx-play
|
||||
```
|
||||
|
||||
And the page, `ipx-web`, the same again; a change to `web/` alone needs only this one:
|
||||
|
||||
```sh
|
||||
docker buildx build -f Dockerfile.web --tag 192.168.1.130:5000/ipx-web:latest . --push
|
||||
sh scripts/build-image.sh web
|
||||
docker compose -f /mnt/fast/arcane/projects/ipx/compose.yaml pull ipx-web
|
||||
docker compose -f /mnt/fast/arcane/projects/ipx/compose.yaml up -d ipx-web
|
||||
```
|
||||
|
||||
The home page (`site/`, #216) is not on Tower. `sh site/build.sh` puts it in `site/dist` with the
|
||||
typeface, logo and icon from `web/`, and it goes to the Cloudflare Pages project `ipxreader` by
|
||||
direct upload, since there is no wrangler login here: hash each file as wrangler does (BLAKE3 of
|
||||
its base64 contents plus its extension, the first 32 hex digits, with the `blake3-wasm` package),
|
||||
ask the project for an upload token (`GET .../pages/projects/ipxreader/upload-token`), send the
|
||||
files to `/pages/assets/upload` with that token, then create a deployment
|
||||
(`POST .../pages/projects/ipxreader/deployments`) with the manifest of paths and hashes and
|
||||
`_headers`. Its screenshots (`site/shots`) come from a scratch iPX on public feeds, never
|
||||
typeface, logo and icon from `web/`, and it goes to the Cloudflare Pages project `ipxreader` with
|
||||
wrangler, which takes `CLOUDFLARE_API_TOKEN` and `CLOUDFLARE_ACCOUNT_ID` from `/src/.envrc` in
|
||||
place of a login:
|
||||
|
||||
```sh
|
||||
sh site/build.sh
|
||||
. /src/.envrc && npx -y wrangler@4.150.0 pages deploy site/dist --project-name ipxreader --branch main --commit-hash "$(git rev-parse HEAD)"
|
||||
curl -sL https://ipxreader.app/ | head -5
|
||||
```
|
||||
|
||||
`--branch main` makes it the production deployment that ipxreader.app serves; another branch is a
|
||||
preview at its own `*.ipxreader.pages.dev` address. The token is an account API token with
|
||||
Cloudflare Pages: Edit on this account and nothing else (dashboard, My Profile, API Tokens). If it
|
||||
is missing or expired, ask the owner for one rather than working around it. Its screenshots (`site/shots`) come from a scratch iPX on public feeds, never
|
||||
production's, which would show whose subscriptions are whose.
|
||||
|
||||
Testing sign-in from Tower itself proves nothing about the tunnel: Tower's own requests reach the
|
||||
@@ -94,24 +119,18 @@ block page, and ipx then logged a feed as failing with `invalid peer certificate
|
||||
**Name the service.** A bare `up -d` recreates every container in the project, which once the
|
||||
split lands means the frontend and directory too. Run `pull` before `up`, because `up` reuses whatever `latest` the host already has.
|
||||
|
||||
**A build that fails with `429 Too Many Requests` on a base image** is Docker Hub rate-limiting
|
||||
this host. There is no Docker Hub login here, and the build asks about `debian:trixie-slim` and
|
||||
`rust:1-slim-trixie` every time unless they are already stored locally. Pull them from Google's
|
||||
mirror and tag them; the build then uses the local copies without asking Docker Hub:
|
||||
|
||||
```sh
|
||||
docker pull mirror.gcr.io/library/debian:trixie-slim
|
||||
docker tag mirror.gcr.io/library/debian:trixie-slim debian:trixie-slim
|
||||
docker pull mirror.gcr.io/library/rust:1-slim-trixie
|
||||
docker tag mirror.gcr.io/library/rust:1-slim-trixie rust:1-slim-trixie
|
||||
```
|
||||
|
||||
Run those again now and then, or the local copies go stale.
|
||||
**Base images come from Google's mirror** (`mirror.gcr.io`, set in `scripts/buildkitd.toml`),
|
||||
not Docker Hub, which rate-limits this host with `429 Too Many Requests`. The capped builder
|
||||
cannot see images the Docker daemon has stored, so pulling and tagging them locally does nothing
|
||||
for it. If `buildkitd.toml` changes, remove the builder (`docker buildx rm ipx-capped`) so the
|
||||
next build makes it again with the new config. The builder also keeps the compiled crates
|
||||
between builds (cache mounts, #438), so a `Cargo.lock` change compiles only what changed; removing
|
||||
it throws them away, and the next build of each image compiles all of them again.
|
||||
|
||||
The containers have no Docker healthcheck (#218): Docker runs one with `exec`, which on Tower
|
||||
fails whenever root is logged out, because elogind removes `/run/user/0`, where containerd puts
|
||||
each exec's state. Whether ipx is well is Prometheus's question (`up`, the worker's job and time)
|
||||
and `ipx status` against the control socket, which says the daemon answers and can read its
|
||||
and `ipxd status` against the control socket, which says the daemon answers and can read its
|
||||
database. The socket answers `status` itself instead of queuing it behind the worker's current
|
||||
job, so a worker stuck on one job still answers it. The containers restart on their own after a
|
||||
reboot.
|
||||
@@ -123,12 +142,23 @@ a daemon by hand for testing, **stop it by its own PID**: start it with `& echo
|
||||
production's daemon as well (issue #38). Never `pkill -f ipx` either: `-f` matches the shell
|
||||
running the command and kills the session (exit 144), which has happened more than once.
|
||||
|
||||
## The iOS app
|
||||
|
||||
`ios/` is the page in a `WKWebView` with the phone's own player behind `web/src/native.ts` (#418);
|
||||
see [ios/README.md](ios/README.md). It builds only on a Mac with Xcode, from `ios/project.yml`
|
||||
through XcodeGen: nothing here compiles or checks the Swift, so say so when handing over a change
|
||||
to it. A change to the bridge's messages is a change to `native.ts`, `tests/native-bridge.ts` and
|
||||
`ios/Sources/Bridge.swift` together, and the page deploys first. The app's own calls to ipx (the
|
||||
player, the badge, CarPlay) are protobuf, from `ios/Sources/Wire`, generated from
|
||||
`crates/wire/proto` by `sh ios/scripts/proto.sh`: run it after changing a `.proto`, and deploy ipx
|
||||
before shipping an app that needs a new field.
|
||||
|
||||
## Before you touch the page
|
||||
|
||||
There are three pages: the app (`web/index.html`), the admin page (`web/admin.html`, sent to
|
||||
admins only) and sign-in (`web/login.html`). The app and admin pages share one stylesheet,
|
||||
`web/app.css`, and their script is TypeScript in `web/src/`; `web/build.mjs` lists which files
|
||||
make up each page's script. The page is its own image, **`ipx-web`** (#203): `web/build.mjs` uses
|
||||
`web/app.css`, and their script is TypeScript in `web/src/`; `web/build.mts` lists which files
|
||||
make up each page's script. The page is its own image, **`ipx-web`** (#203): `web/build.mts` uses
|
||||
swc to strip the types and minify each script (`app.js`, `admin.js`, `login.js`), minifies the
|
||||
pages, and puts them with the icons and typeface in `web/dist`, which Caddy serves
|
||||
(`web/Caddyfile`, `Dockerfile.web`), passing `/api` and `/media` to ipx on the same origin. ipx
|
||||
@@ -142,23 +172,43 @@ not signed in to `/login` and anyone but an admin away from `/admin` (and refuse
|
||||
`/admin.js`); `/api/login-check` sends someone Cloudflare Access vouches for past the form; a page
|
||||
asked for with `?token=` goes to `/api/signin`, which sets the cookie. The page is the same file
|
||||
for everyone, so what ipx used to write into it per person is the page's own job now:
|
||||
`web/boot.js`, in `<head>`, puts the theme from its cookie and an admin's link from the last visit
|
||||
`web/boot.ts`, in `<head>`, puts the theme from its cookie and an admin's link from the last visit
|
||||
on the page before the first frame, and `/api/me` brings the rest (version, the account's old
|
||||
theme).
|
||||
|
||||
The files in `web/src` are not modules. They are one script split up, concatenated in the order
|
||||
`web/build.mjs` lists them, sharing one top-level scope as the single inline script did; a new
|
||||
`web/build.mts` lists them, sharing one top-level scope as the single inline script did; a new
|
||||
file goes into that list. Top-level names are kept as they are, because markup calls some by
|
||||
name (`onclick="closeModal()"`) and the browser tests reach others through `page.evaluate`.
|
||||
|
||||
After any edit to it:
|
||||
|
||||
```sh
|
||||
npx tsc -p .
|
||||
node tests/page-smoke.js
|
||||
node --test tests/page-unit.js
|
||||
npm run -s typecheck
|
||||
node tests/page-smoke.ts
|
||||
node --test tests/page-unit.ts
|
||||
```
|
||||
|
||||
**All the code here is TypeScript** (#392): the page, the service worker, the tests, the build, the
|
||||
home page's generator and the load tests. Node runs a `.ts` file as it is, by stripping the types,
|
||||
so it takes only TypeScript that erases (`erasableSyntaxOnly`: no enums, namespaces or constructor
|
||||
parameter properties) and imports name their `.ts` files; k6 strips its own. What a browser loads
|
||||
on its own is compiled by `web/build.mts` with swc (`boot.ts`, the worker). The directory's
|
||||
`site.js` is compiled from `crates/directory/src/site.ts` and committed, since its build has no
|
||||
Node: after editing `site.ts`, `node web/build.mts --directory-script`, which
|
||||
`node tests/directory-script.ts` checks. `npm run -s typecheck` checks five programs, each with its
|
||||
own globals: the page (`tsconfig.json`, which also writes the page's declarations to
|
||||
`target/page-types` for the browser tests' `page.evaluate` calls), the worker, Node's
|
||||
(`tsconfig.tools.json`), and k6's (`tsconfig.k6.json`).
|
||||
|
||||
The service worker that keeps iPX readable offline (#389) is TypeScript too, `web/sw/sw.ts`,
|
||||
a script of its own built to `/sw.js`, and checked by `tsconfig.sw.json` against a worker's
|
||||
globals, which cannot share a program with the page's. It goes to the network first for
|
||||
everything, so online nothing is an old copy, but for an episode someone kept on the device
|
||||
(#412): the page asks for it as `/media/...?offline`, and the worker answers from its
|
||||
`ipx-episodes` cache, byte ranges and all. The browser tests block it
|
||||
(`serviceWorkers: 'block'`), since it answers before `page.route` can; the offline tests allow it.
|
||||
|
||||
The first type-checks `web/src` in TypeScript's `strict` mode: every parameter and variable has a
|
||||
type, what the API sends is typed in `web/src/types.ts`, which mirrors the Rust structs and changes
|
||||
with them, and a value that may be null has to be checked before use. `$<T>` returns the element
|
||||
@@ -181,13 +231,17 @@ cargo clippy --workspace --all-targets -- -D warnings # no warnings, ever
|
||||
cargo deny check # known vulnerabilities, licences, sources (deny.toml); reads Cargo.lock, builds nothing
|
||||
cargo machete # dependencies a crate lists and does not use; now and then, it can be wrong
|
||||
cargo test # parsing, filters, retention, schedules, SQL, per-user state
|
||||
npx tsc -p . # type-checks web/src
|
||||
node tests/page-smoke.js
|
||||
node --test tests/page-unit.js # the page's own functions, called directly, in a second
|
||||
node tests/native-bridge.js # the page hands playback to a native shell
|
||||
node tests/contrast.js # every theme's palette against WCAG AA
|
||||
npm run -s typecheck # the page, the service worker, the tests', build's and load tests' TypeScript
|
||||
node tests/page-smoke.ts
|
||||
node tests/directory-script.ts # the directory's site.js is its site.ts as it stands
|
||||
node --test tests/page-unit.ts # the page's own functions, called directly, in a second
|
||||
node tests/native-bridge.ts # the page hands playback to a native shell
|
||||
node tests/contrast.ts # every theme's palette against WCAG AA
|
||||
npx playwright test # the browser tests, against a real daemon on fixture feeds
|
||||
node tests/load/run.js # k6: many people at once against a scratch daemon with 1,500 feeds
|
||||
node tests/load/run.ts # k6: many people at once against a scratch daemon with 1,500 feeds
|
||||
sh scripts/coverage.sh # which Rust lines the cargo tests reach, capped like a build (#390)
|
||||
sh scripts/coverage.sh api # which Rust lines the browser suite reaches through the API (#394)
|
||||
npm run coverage:page # which lines of web/src the browser tests reach (#390)
|
||||
```
|
||||
|
||||
**Builds share Tower with everything else, production included.** On 2026-10-16 a build of the
|
||||
@@ -198,15 +252,19 @@ session was stopped. So:
|
||||
run in the background beside another, and never two crates' builds at once. Do not tune `-j`.
|
||||
* Look at the load before and after each (`cat /proc/loadavg`). Above about 24, Tower's twelve
|
||||
threads twice over, wait for it to come down before starting another.
|
||||
* Test only what the change is in. ipx uses ipx-directory, and both use ipx-feed, so:
|
||||
* Test only what the change is in. ipx uses ipx-play and ipx-directory, ipx-play uses
|
||||
ipx-directory, and all use ipx-feed and ipx-wire, so:
|
||||
|
||||
| changed | run |
|
||||
|---|---|
|
||||
| `web/` alone | the three page checks; `npx playwright test -g '<what changed>'`; no cargo |
|
||||
| `crates/ipx/` | `cargo test -p ipx`; the browser tests too if the API changed |
|
||||
| `crates/directory/` | `cargo test -p ipx-directory`, then `-p ipx` |
|
||||
| `crates/feed/` | `cargo test -p ipx-feed`, then `-p ipx-directory`, then `-p ipx` |
|
||||
| `Cargo.toml`, `Cargo.lock` | all three, one at a time |
|
||||
| `crates/directory/` | `cargo test -p ipx-directory`, then `-p ipx-play`, then `-p ipx` |
|
||||
| `crates/play/` | `cargo test -p ipx-play`, then `-p ipx` |
|
||||
| `crates/feed/` | `cargo test -p ipx-feed`, then `-p ipx-directory`, `-p ipx-play`, then `-p ipx` |
|
||||
| `crates/tui/` | `cargo test -p ipx-tui`; it uses only the API, none of the other crates |
|
||||
| `crates/wire/` | `cargo test -p ipx-wire`, then `-p ipx-play`, `-p ipx-directory`, then `-p ipx`; the browser suite, whose debug builds check every answer against the `.proto` |
|
||||
| `Cargo.toml`, `Cargo.lock` | all six, one at a time |
|
||||
|
||||
The pre-commit hook's clippy follows the same table. The whole browser suite once, at the end
|
||||
of a change to the page or the API; never `cargo test` for the workspace in one command.
|
||||
@@ -254,8 +312,8 @@ seeking through files, a flood of wrong passwords. Things about them:
|
||||
127.0.0.1, as production trusts Cloudflare Access's; only the sign-in test uses a password.
|
||||
* Each threshold is a budget well above what the request takes now: it is there to catch a query
|
||||
that has started asking once per feed, or writers queueing on a lock, not a slow minute.
|
||||
* `ipx status` runs every second throughout, and a run fails if one takes 5s.
|
||||
* One at a time: `node tests/load/run.js signin`. All four take about six minutes.
|
||||
* `ipxd status` runs every second throughout, and a run fails if one takes 5s.
|
||||
* One at a time: `node tests/load/run.ts signin`. All four take about six minutes.
|
||||
* The daemon is on **SQLite** unless `--postgres`, which puts it in `IPX_TEST_DATABASE_URL`, in
|
||||
a schema of its own (`ipx_load`) dropped and made again each run. That URL, in `/src/.envrc`, is
|
||||
`ipodderx_test` on the `databases` project's server, production's, as `ipodderx`, taken from
|
||||
@@ -265,6 +323,30 @@ seeking through files, a flood of wrong passwords. Things about them:
|
||||
* The Rust tests run on Postgres with the same URL: `. /src/.envrc && cargo test`. Each test
|
||||
makes a schema `ipxt_<pid>_<n>`, and the next run drops those an earlier one left.
|
||||
|
||||
Coverage (#390, #394) says what the tests leave untested; read it before deciding a change is
|
||||
covered. `sh scripts/coverage.sh` runs the cargo tests instrumented in the `ipx-coverage` image
|
||||
(`sh scripts/build-image.sh coverage` makes it), in a container held to the builder's 6 GB and six
|
||||
threads, one crate at a time, into `target/coverage/cargo` (`summary.txt`, `lcov.info`, `html/`).
|
||||
`sh scripts/coverage.sh api` builds an instrumented ipx and ipx-directory there and runs the
|
||||
browser suite here against them (`IPX_API_COVERAGE`), into `target/coverage/api`: what the API
|
||||
and the daemon really run, which the cargo tests alone put at 17% for `web.rs`. The image is
|
||||
Ubuntu 24.04, as code-server is, or the binaries it builds would not start here (glibc); the
|
||||
suite stops its servers with SIGTERM in that mode, since an instrumented binary writes its profile
|
||||
as it exits. The same run measures the page; `npm run coverage:page` does that alone, on a build of
|
||||
the page with a source map to each `web/src` file (`IPX_COVERAGE=1`, `web/concat-map.mts`), into
|
||||
`target/coverage/page`. The specs take `test` and `expect` from `tests/ui/coverage.ts`, not from
|
||||
`@playwright/test`, so every test is counted, every page it opens (a second person's context
|
||||
too), and each document before it unloads: the fixture stops it in `beforeunload` at a breakpoint
|
||||
of its own to take what it ran, since a reload or sign-in otherwise throws that away (#407). The
|
||||
unit tests and the native-bridge check are measured in Node first (`scripts/coverage-node.sh`);
|
||||
the service worker is not.
|
||||
|
||||
Each run ends with `node scripts/coverage-metrics.ts`, which writes every suite's last numbers to
|
||||
`/mnt/fast/appdata/node-exporter/textfile/ipx_coverage.prom`; node-exporter's textfile collector
|
||||
(the monitoring project's compose file) serves them as `ipx_coverage_lines{suite,part,state}`, job
|
||||
`node`, and the iPX dashboard's Coverage row draws them. A coverage run started by hand updates
|
||||
Grafana; nothing runs one on its own.
|
||||
|
||||
Non-trivial logic leaves one runnable check behind. Pure functions (`merge_policy`, `pick`,
|
||||
`matches_keywords`, `parse_interval`) are the easiest place to put it.
|
||||
|
||||
@@ -313,18 +395,20 @@ ms in ipx, the difference being new connections, 80-330 ms each to open (#241).
|
||||
watch the shutdown channel itself; the daemon ignored SIGTERM for exactly this reason.
|
||||
* Only one daemon per socket. Removing the socket file defeats the guard and you get two daemons
|
||||
fighting over the database, with the stale one still holding the port.
|
||||
* **The Grafana dashboard reads the log's fields** (`grafana/dashboard.py`). Production logs JSON
|
||||
(`IPX_LOG_FORMAT=json`); the access log's `method`, `path`, `route`, `status`, `duration_ms` and
|
||||
the events' `ev`, `feed`, `new`, `bytes`, `msg` (`log_event` in ipc.rs) are what the panels query.
|
||||
Rename one and its panels go blank without an error; regenerate the dashboard to match.
|
||||
* **The Grafana dashboard reads the log's fields and the metrics' names** (`grafana/dashboard.py`).
|
||||
Production logs JSON (`IPX_LOG_FORMAT=json`); the events' `ev`, `feed`, `new`, `bytes`, `msg`
|
||||
(`log_event` in ipc.rs) and the metrics in metrics.rs (requests are
|
||||
`ipx_http_request_duration_seconds`, #364) are what the panels query. Rename one and its panels
|
||||
go blank without an error; regenerate the dashboard to match. Routine lines (a request that went
|
||||
well, a scan's end) are debug: the admin page's Log view has them, Loki does not.
|
||||
* `/api/settings` answering `200` does **not** mean the daemon is well — the web server is a
|
||||
different task. `ipx status` checks the control socket and the database; to see the worker
|
||||
different task. `ipxd status` checks the control socket and the database; to see the worker
|
||||
getting through its jobs, watch for `scan complete` in the log.
|
||||
* **The database goes through SeaORM, and the entities in `crates/ipx/src/entity.rs` are the schema.**
|
||||
`Db::open` creates any missing table or index from them (`create_missing`), on every `ipx`
|
||||
command, the healthcheck's `ipx status` included, so it must never write when nothing is
|
||||
command, the healthcheck's `ipxd status` included, so it must never write when nothing is
|
||||
missing: SeaORM's experimental schema sync dropped and remade an index on every open, the
|
||||
write lock that took made `ipx status` time out behind a busy daemon, and it was removed for
|
||||
write lock that took made `ipxd status` time out behind a busy daemon, and it was removed for
|
||||
it. A new column on an existing table needs its own `ALTER`; nothing adds one for you. Look for
|
||||
the column first and alter only when it is missing, as `feeds.description` does.
|
||||
* **SQL written by hand in `db.rs` has to run on SQLite and Postgres both** (issue #18): `$1`
|
||||
@@ -342,7 +426,9 @@ Every change gets one line under `## [Unreleased]` in the changelog of the servi
|
||||
its [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) group: Added, Changed, Deprecated,
|
||||
Removed, Fixed or Security. One changelog a service: [crates/ipx/CHANGELOG.md](crates/ipx/CHANGELOG.md)
|
||||
(the daemon and its API; until 0.12.0 everything), [web/CHANGELOG.md](web/CHANGELOG.md) (the page
|
||||
and ipx-web) and [crates/directory/CHANGELOG.md](crates/directory/CHANGELOG.md) (ipx-directory). A
|
||||
and ipx-web) [crates/directory/CHANGELOG.md](crates/directory/CHANGELOG.md) (ipx-directory),
|
||||
[crates/play/CHANGELOG.md](crates/play/CHANGELOG.md) (ipx-play) and
|
||||
[ios/CHANGELOG.md](ios/CHANGELOG.md) (the iOS app). A
|
||||
change to more than one gets a line in each, each saying what that service does differently. The
|
||||
monitoring (the Grafana dashboard, the prod-check skill) and the repository's own tooling are not
|
||||
a service anyone uses and get no line. Say it the way someone using ipx would notice it. When there is more to
|
||||
@@ -357,6 +443,7 @@ release:
|
||||
|---|---|---|---|
|
||||
| ipx | `crates/ipx/Cargo.toml` | `crates/ipx/CHANGELOG.md` | `vX.Y.Z`, as every release before the split |
|
||||
| ipx-directory | `crates/directory/Cargo.toml` | `crates/directory/CHANGELOG.md` | `directory-vX.Y.Z` |
|
||||
| ipx-play | `crates/play/Cargo.toml` | `crates/play/CHANGELOG.md` | `play-vX.Y.Z` |
|
||||
| ipx-web, the page | `package.json` | `web/CHANGELOG.md` | `web-vX.Y.Z` |
|
||||
|
||||
`crates/feed` is a library the two Rust services build in, not released (`0.0.0`); a change to it
|
||||
|
||||
715
Cargo.lock
generated
715
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -1,5 +1,5 @@
|
||||
[workspace]
|
||||
members = ["crates/ipx", "crates/feed", "crates/directory"]
|
||||
members = ["crates/ipx", "crates/feed", "crates/directory", "crates/tui", "crates/play", "crates/wire"]
|
||||
resolver = "3"
|
||||
|
||||
# Each service has its own version, in its own Cargo.toml (ipx, ipx-directory) or package.json
|
||||
@@ -14,6 +14,8 @@ unsafe_code = "forbid"
|
||||
[workspace.dependencies]
|
||||
ipx-feed = { path = "crates/feed" }
|
||||
ipx-directory = { path = "crates/directory" }
|
||||
ipx-play = { path = "crates/play" }
|
||||
ipx-wire = { path = "crates/wire" }
|
||||
ammonia = "4.1.4"
|
||||
anyhow = "1.0.104"
|
||||
argon2 = "0.6.0"
|
||||
@@ -29,6 +31,11 @@ opentelemetry = { version = "0.33", default-features = false, features = ["trace
|
||||
opentelemetry-otlp = { version = "0.33", default-features = false, features = ["trace", "http-proto", "reqwest-blocking-client", "gzip-http"] }
|
||||
opentelemetry_sdk = { version = "0.33", default-features = false, features = ["trace"] }
|
||||
opml = "1.1.6"
|
||||
prost = "0.14.4"
|
||||
prost-build = "0.14"
|
||||
prost-reflect = { version = "0.16", features = ["serde"] }
|
||||
protox = "0.9"
|
||||
prost-types = "0.14"
|
||||
pulldown-cmark = { version = "0.13.4", default-features = false, features = ["html"] }
|
||||
mimalloc = "0.1.52"
|
||||
http = "1.5.0"
|
||||
|
||||
29
Dockerfile
29
Dockerfile
@@ -7,23 +7,22 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
WORKDIR /src
|
||||
|
||||
# Dependencies first, so editing the source does not rebuild librqbit every time: each crate of
|
||||
# the workspace as an empty stub, its manifest real.
|
||||
COPY Cargo.toml Cargo.lock ./
|
||||
COPY crates/ipx/Cargo.toml crates/ipx/
|
||||
COPY crates/feed/Cargo.toml crates/feed/
|
||||
COPY crates/directory/Cargo.toml crates/directory/
|
||||
RUN mkdir -p crates/ipx/src crates/feed/src crates/directory/src \
|
||||
&& echo 'fn main(){}' > crates/ipx/src/main.rs && touch crates/feed/src/lib.rs \
|
||||
&& echo 'fn main(){}' > crates/directory/src/main.rs && touch crates/directory/src/lib.rs \
|
||||
&& cargo build --release --locked -p ipx \
|
||||
&& rm -rf crates/ipx/src crates/feed/src crates/directory/src
|
||||
|
||||
COPY crates ./crates
|
||||
# ipx builds the directory crate in, whose public pages serve these (#215).
|
||||
COPY web/logo.svg web/logo-dark.svg web/favicon.png ./web/
|
||||
# cargo skips a rebuild if mtimes look untouched; make sure it does not.
|
||||
RUN touch crates/ipx/src/main.rs crates/feed/src/lib.rs && cargo build --release --locked -p ipx
|
||||
# cargo's registry and target/ are BuildKit cache mounts, kept in the builder between builds, so
|
||||
# a Cargo.lock change compiles only the crates that changed (#438): a dependency layer of stub
|
||||
# crates went with any change to it and compiled all 500 again, holding the builder's memory cap
|
||||
# for minutes. A cache mount is not part of the image, so the binary is copied out in the same
|
||||
# step. The workspace's own crates are touched because cargo goes by mtimes and skips a rebuild
|
||||
# if they look untouched.
|
||||
RUN --mount=type=cache,id=ipx-cargo-registry,target=/usr/local/cargo/registry,sharing=locked \
|
||||
--mount=type=cache,id=ipx-target,target=/src/target,sharing=locked \
|
||||
touch crates/ipx/src/main.rs crates/ipx/src/lib.rs crates/feed/src/lib.rs \
|
||||
crates/directory/src/lib.rs crates/play/src/lib.rs crates/wire/src/lib.rs crates/wire/proto/*.proto \
|
||||
&& cargo build --release --locked -p ipx \
|
||||
&& cp target/release/ipxd /usr/local/bin/ipxd
|
||||
|
||||
FROM debian:trixie-slim
|
||||
# ffmpeg is DeadAir's (#171): it finds the silences and copies the frames kept. Debian's adds
|
||||
@@ -31,7 +30,7 @@ FROM debian:trixie-slim
|
||||
RUN apt-get update && apt-get install -y --no-install-recommends \
|
||||
ca-certificates gosu ffmpeg \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
COPY --from=build /src/target/release/ipx /usr/local/bin/ipx
|
||||
COPY --from=build /usr/local/bin/ipxd /usr/local/bin/ipxd
|
||||
COPY docker-entrypoint.sh /usr/local/bin/
|
||||
RUN chmod +x /usr/local/bin/docker-entrypoint.sh
|
||||
|
||||
@@ -46,4 +45,4 @@ VOLUME ["/config", "/data", "/downloads"]
|
||||
EXPOSE 8099/tcp 6881/tcp 6881/udp
|
||||
|
||||
ENTRYPOINT ["docker-entrypoint.sh"]
|
||||
CMD ["ipx", "daemon"]
|
||||
CMD ["ipxd", "daemon"]
|
||||
|
||||
23
Dockerfile.coverage
Normal file
23
Dockerfile.coverage
Normal file
@@ -0,0 +1,23 @@
|
||||
# What scripts/coverage.sh runs the Rust tests in, to see which lines they reach (#390): the
|
||||
# toolchain, LLVM's coverage tools and cargo-llvm-cov. No source: coverage.sh mounts the tree.
|
||||
# Built and pushed like the services, by scripts/build-image.sh coverage, under its caps.
|
||||
#
|
||||
# Ubuntu 24.04, as code-server is (#394): `coverage.sh api` builds an instrumented ipx and
|
||||
# ipx-directory here, under the cap, and the browser suite runs them in code-server, which a
|
||||
# binary linked against Debian trixie's newer glibc (2.41 to Ubuntu's 2.39) would not start in.
|
||||
# The same Rust as the tests are built with elsewhere: a newer one would rebuild everything with
|
||||
# its own flags for nothing.
|
||||
FROM ubuntu:24.04
|
||||
ENV RUSTUP_HOME=/usr/local/rustup CARGO_HOME=/usr/local/cargo PATH=/usr/local/cargo/bin:$PATH
|
||||
# cc for rusqlite's bundled SQLite and librqbit, as Dockerfile's builder; ffmpeg for DeadAir's
|
||||
# tests, as the image ipx runs in; curl and certificates to fetch rustup.
|
||||
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
|
||||
build-essential ca-certificates curl ffmpeg \
|
||||
&& rm -rf /var/lib/apt/lists/* \
|
||||
&& curl -sSf https://sh.rustup.rs | sh -s -- -y --profile minimal --default-toolchain 1.98.1 --no-modify-path \
|
||||
&& rustup component add llvm-tools-preview
|
||||
# cargo-llvm-cov's own build, as its README gives it, rather than compiling it here.
|
||||
ADD https://github.com/taiki-e/cargo-llvm-cov/releases/latest/download/cargo-llvm-cov-x86_64-unknown-linux-gnu.tar.gz /tmp/llvm-cov.tar.gz
|
||||
RUN tar -xzf /tmp/llvm-cov.tar.gz -C /usr/local/cargo/bin && rm /tmp/llvm-cov.tar.gz \
|
||||
&& cargo llvm-cov --version && rustc --version
|
||||
WORKDIR /work
|
||||
@@ -1,23 +1,27 @@
|
||||
# ipx-directory (#204). Built from the repo's root: docker buildx build -f Dockerfile.directory .
|
||||
# No node here: only ipx's own build runs web/build.mjs.
|
||||
# No node here: only ipx's own build runs web/build.mts.
|
||||
FROM rust:1-slim-trixie AS build
|
||||
# SQLite is compiled from source for sqlx, which needs a C toolchain.
|
||||
RUN apt-get update && apt-get install -y --no-install-recommends build-essential \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
WORKDIR /src
|
||||
# Every member of the workspace, since cargo reads them all to build one.
|
||||
# ponytail: no dependency layer, so each build compiles sea-orm again; add the stub trick from
|
||||
# the main Dockerfile if builds get slow enough to matter.
|
||||
COPY Cargo.toml Cargo.lock ./
|
||||
COPY crates ./crates
|
||||
# The logo and icon the public pages serve (#215), compiled into the binary.
|
||||
COPY web/logo.svg web/logo-dark.svg web/favicon.png ./web/
|
||||
RUN cargo build --release --locked -p ipx-directory
|
||||
# Compiled crates kept in the builder between builds, as in the main Dockerfile (#438). A target
|
||||
# of its own: built with ipx's, the two would rebuild shared crates for each other's features.
|
||||
RUN --mount=type=cache,id=ipx-cargo-registry,target=/usr/local/cargo/registry,sharing=locked \
|
||||
--mount=type=cache,id=ipx-directory-target,target=/src/target,sharing=locked \
|
||||
touch crates/directory/src/main.rs crates/directory/src/lib.rs crates/feed/src/lib.rs crates/wire/src/lib.rs crates/wire/proto/*.proto \
|
||||
&& cargo build --release --locked -p ipx-directory \
|
||||
&& cp target/release/ipx-directory /usr/local/bin/ipx-directory
|
||||
|
||||
FROM debian:trixie-slim
|
||||
RUN apt-get update && apt-get install -y --no-install-recommends ca-certificates \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
COPY --from=build /src/target/release/ipx-directory /usr/local/bin/ipx-directory
|
||||
COPY --from=build /usr/local/bin/ipx-directory /usr/local/bin/ipx-directory
|
||||
ENV DIRECTORY_CONFIG=/config/directory.toml
|
||||
# Unraid's nobody:users. It writes nothing to disk: the database is elsewhere.
|
||||
USER 99:100
|
||||
|
||||
28
Dockerfile.play
Normal file
28
Dockerfile.play
Normal file
@@ -0,0 +1,28 @@
|
||||
# ipx-play (#447), the pages shared episodes open. Built from the repo's root:
|
||||
# sh scripts/build-image.sh play
|
||||
FROM rust:1-slim-trixie AS build
|
||||
# SQLite is compiled from source for sqlx, which needs a C toolchain.
|
||||
RUN apt-get update && apt-get install -y --no-install-recommends build-essential \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
WORKDIR /src
|
||||
# Every member of the workspace, since cargo reads them all to build one.
|
||||
COPY Cargo.toml Cargo.lock ./
|
||||
COPY crates ./crates
|
||||
# The logo and icon its pages serve, compiled into the binary.
|
||||
COPY web/logo.svg web/logo-dark.svg web/favicon.png ./web/
|
||||
# Compiled crates kept in the builder between builds (#438), in a target of its own.
|
||||
RUN --mount=type=cache,id=ipx-cargo-registry,target=/usr/local/cargo/registry,sharing=locked \
|
||||
--mount=type=cache,id=ipx-play-target,target=/src/target,sharing=locked \
|
||||
touch crates/play/src/main.rs crates/play/src/lib.rs crates/directory/src/lib.rs crates/feed/src/lib.rs crates/wire/src/lib.rs crates/wire/proto/*.proto \
|
||||
&& cargo build --release --locked -p ipx-play \
|
||||
&& cp target/release/ipx-play /usr/local/bin/ipx-play
|
||||
|
||||
FROM debian:trixie-slim
|
||||
RUN apt-get update && apt-get install -y --no-install-recommends ca-certificates \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
COPY --from=build /usr/local/bin/ipx-play /usr/local/bin/ipx-play
|
||||
ENV PLAY_CONFIG=/config/play.toml
|
||||
# Unraid's nobody:users. It writes nothing to disk: the database is elsewhere.
|
||||
USER 99:100
|
||||
EXPOSE 8102
|
||||
CMD ["ipx-play"]
|
||||
@@ -1,11 +1,13 @@
|
||||
# ipx-web (#203): the frontend, built from web/ and served by Caddy, which passes the API to ipx.
|
||||
# Built from the repo's root: docker buildx build -f Dockerfile.web .
|
||||
FROM node:22-slim AS build
|
||||
FROM node:24-slim AS build
|
||||
WORKDIR /src
|
||||
COPY package.json package-lock.json ./
|
||||
RUN npm ci --omit=dev
|
||||
COPY web ./web
|
||||
RUN node web/build.mjs /dist
|
||||
# The protobuf messages the page reads its answers in (#470).
|
||||
COPY crates/wire/proto ./crates/wire/proto
|
||||
RUN node web/build.mts /dist
|
||||
|
||||
FROM caddy:2
|
||||
# caddy:2's Alpine ships curl, which nothing here runs (no healthcheck since #218): out, so a
|
||||
|
||||
10
README.md
10
README.md
@@ -44,7 +44,7 @@ From source:
|
||||
|
||||
```sh
|
||||
cargo build --release
|
||||
./target/release/ipx daemon
|
||||
./target/release/ipxd daemon
|
||||
```
|
||||
|
||||
The first start creates **admin** with a random password, written to `admin-password` beside
|
||||
@@ -52,7 +52,7 @@ config.toml (readable by ipx's user alone; the log says where, never what it is)
|
||||
`IPX_ADMIN_PASSWORD` to choose it instead. Sign in at `/login`, then change it:
|
||||
|
||||
```sh
|
||||
echo -n 'a good password' | ipx user passwd admin
|
||||
echo -n 'a good password' | ipxd user passwd admin
|
||||
```
|
||||
|
||||
The UI is plain HTTP, so put TLS in front of it if it is reachable from outside your network.
|
||||
@@ -62,7 +62,7 @@ The UI is plain HTTP, so put TLS in front of it if it is reachable from outside
|
||||
| | |
|
||||
|---|---|
|
||||
| [docs/configuration.md](docs/configuration.md) | Every config key, path and environment variable |
|
||||
| [docs/cli.md](docs/cli.md) | Every command, including `ipx user` |
|
||||
| [docs/cli.md](docs/cli.md) | Every command, including `ipxd user` |
|
||||
| [docs/users.md](docs/users.md) | Accounts, and what several people share |
|
||||
| [docs/sso.md](docs/sso.md) | Signing in through Cloudflare Zero Trust or Authentik |
|
||||
| [docs/architecture.md](docs/architecture.md) | How it works: modules, schema, control socket, HTTP API |
|
||||
@@ -73,8 +73,8 @@ The UI is plain HTTP, so put TLS in front of it if it is reachable from outside
|
||||
|
||||
```sh
|
||||
cargo test # the engine: parsing, filters, retention, schedules, SQL, per-user state
|
||||
node tests/page-smoke.js # the page script loads without throwing
|
||||
node tests/native-bridge.js # the page hands playback to a native shell
|
||||
node tests/page-smoke.ts # the page script loads without throwing
|
||||
node tests/native-bridge.ts # the page hands playback to a native shell
|
||||
npx playwright test # a real browser against a real daemon on fixture feeds
|
||||
```
|
||||
|
||||
|
||||
@@ -10,6 +10,26 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
### Added
|
||||
|
||||
- `/v1` answers in protobuf for a client that asks for it (`Accept: application/x-protobuf`) and takes a snapshot sent so; JSON stays the default. The messages are in `crates/wire/proto/directory.proto`, and answers go through structs generated from them (#465, #470).
|
||||
- A `robots.txt` and a sitemap listing the home page, A to Z, every category and every feed, so search engines find the pages, not only the links between them (#380).
|
||||
- `/metrics` counts its requests and their times by route and status, the feeds it checks and those that failed, and the snapshots it takes (#370).
|
||||
- Its log lines inside a trace carry the trace's and span's ids, as ipx's do, so a failed feed check leads to its trace (#375).
|
||||
|
||||
### Changed
|
||||
|
||||
- Its checks of YouTube channels go one at a time, two seconds apart, since YouTube refuses many asked together (#386).
|
||||
- Its JSON log lines no longer carry their span, once on its own and again in a list, which said nothing the line's own fields did not (#363).
|
||||
|
||||
### Fixed
|
||||
|
||||
- A YouTube channel's check no longer fails on the 404 or 500 YouTube answers some feed requests with and does not mean: it is asked again, twice, a second apart (#382).
|
||||
|
||||
### Security
|
||||
|
||||
- Feeds and Markdown items are read with pulldown-cmark 0.13.6, and compressed answers with zstd 0.14.4 and zstd-safe 8.3, each closing published advisories (#467).
|
||||
|
||||
## [1.4.1] - 2026-10-24
|
||||
|
||||
### Fixed
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "ipx-directory"
|
||||
version = "1.4.1"
|
||||
version = "1.4.2-dev"
|
||||
edition.workspace = true
|
||||
publish = false
|
||||
|
||||
@@ -18,6 +18,7 @@ axum.workspace = true
|
||||
clap.workspace = true
|
||||
futures-util.workspace = true
|
||||
ipx-feed.workspace = true
|
||||
ipx-wire.workspace = true
|
||||
opentelemetry.workspace = true
|
||||
opentelemetry-otlp.workspace = true
|
||||
opentelemetry_sdk.workspace = true
|
||||
|
||||
@@ -17,6 +17,18 @@ use tower_http::compression::CompressionLayer;
|
||||
use tower_http::decompression::RequestDecompressionLayer;
|
||||
use tracing::Instrument;
|
||||
|
||||
/// Each route's protobuf messages (#465), beside its JSON.
|
||||
const WIRE: ipx_wire::Routes = &[
|
||||
("GET", "/v1/me", "", "directory.Me"),
|
||||
("GET", "/v1/feeds", "", "directory.Listings"),
|
||||
("GET", "/v1/popular", "", "directory.Listings"),
|
||||
("GET", "/v1/feeds/{id}", "", "directory.FeedPage"),
|
||||
("POST", "/v1/feeds/{id}/refresh", "", "directory.FeedPage"),
|
||||
("POST", "/v1/snapshot", "directory.Snapshot", "directory.SnapshotResult"),
|
||||
("GET", "/v1/blocked", "", "directory.BlockedList"),
|
||||
("GET", "/v1/instances", "", "directory.Instances"),
|
||||
];
|
||||
|
||||
/// Every route: the API under `/v1`, behind a token, and the public pages.
|
||||
pub fn router(dir: Shared) -> Router {
|
||||
let v1 = Router::new()
|
||||
@@ -30,12 +42,14 @@ pub fn router(dir: Shared) -> Router {
|
||||
.route("/v1/blocked/{id}", delete(unblock))
|
||||
.route("/v1/instances", get(instances))
|
||||
.route("/v1/instances/{name}", delete(drop_instance))
|
||||
.route_layer(axum::middleware::from_fn_with_state(WIRE, ipx_wire::negotiate))
|
||||
.layer(axum::middleware::from_fn_with_state(dir.clone(), authenticate));
|
||||
Router::new()
|
||||
.route("/healthz", get(healthz))
|
||||
.merge(v1)
|
||||
// Anyone may browse, no token, read only (#215).
|
||||
.merge(site::router())
|
||||
.route_layer(axum::middleware::from_fn(remember_route))
|
||||
// Brotli, zstd or gzip as the caller takes them: the feed list went to ipx as 603 KB of
|
||||
// JSON, most of a Directory page's time (#253). Inside the access log, which records what
|
||||
// was sent.
|
||||
@@ -65,6 +79,14 @@ impl From<anyhow::Error> for ApiError {
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct Who(String);
|
||||
|
||||
/// Puts the route matched on the response for `access_log`, which runs outside routing: by path,
|
||||
/// each feed's page was a row of its own in the request metric.
|
||||
async fn remember_route(route: axum::extract::MatchedPath, req: Request, next: Next) -> Response {
|
||||
let mut resp = next.run(req).await;
|
||||
resp.extensions_mut().insert(route);
|
||||
resp
|
||||
}
|
||||
|
||||
/// One line a request, with the token's name and never the token: what the Grafana panels read.
|
||||
pub(crate) async fn access_log(req: Request, next: Next) -> Response {
|
||||
let (method, path) = (req.method().clone(), req.uri().path().to_owned());
|
||||
@@ -92,6 +114,15 @@ pub(crate) async fn access_log(req: Request, next: Next) -> Response {
|
||||
let who = resp.extensions().get::<Who>().map_or("", |w| w.0.as_str()).to_owned();
|
||||
let status = resp.status().as_u16();
|
||||
if path != "/healthz" {
|
||||
let route = resp.extensions().get::<axum::extract::MatchedPath>().map_or("", |r| r.as_str());
|
||||
crate::metrics::REQUESTS.observe(
|
||||
&format!(
|
||||
"method=\"{}\",route=\"{}\",status=\"{status}\"",
|
||||
crate::metrics::method(method.as_str()),
|
||||
crate::metrics::label(route)
|
||||
),
|
||||
start.elapsed().as_secs_f64(),
|
||||
);
|
||||
tracing::info!(
|
||||
target: "ipx_directory::http",
|
||||
method = %method, path = %path, status, duration_ms = start.elapsed().as_millis() as u64, token = %who,
|
||||
@@ -174,6 +205,7 @@ pub(crate) async fn snapshot(
|
||||
}
|
||||
let r = dir.store.snapshot(&me.name, &entries).await?;
|
||||
dir.wake.notify_one();
|
||||
crate::metrics::SNAPSHOTS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
|
||||
tracing::info!(
|
||||
instance = %me.name, accepted = r.accepted, refused_private = r.refused_private,
|
||||
refused_blocked = r.refused_blocked, refused_invalid = r.refused_invalid, "snapshot"
|
||||
@@ -362,12 +394,78 @@ mod tests {
|
||||
assert_eq!(get_page(&app, "/category/Nothing").await.0, 404);
|
||||
assert_eq!(get_page(&app, "/search?q=").await.0, 303);
|
||||
assert_eq!(get_page(&app, "/site.js").await.0, 200);
|
||||
// Crawlers are pointed at every page (#380).
|
||||
let (s, robots, _) = get_page(&app, "/robots.txt").await;
|
||||
assert!(s == 200 && robots.contains("Disallow: /v1/") && robots.contains("/sitemap.xml"), "{robots}");
|
||||
let (s, map, _) = get_page(&app, "/sitemap.xml").await;
|
||||
assert_eq!(s, 200);
|
||||
assert!(map.contains(&format!("/feed/{id}</loc>")) && map.contains("/category/Podcasting</loc>"), "{map}");
|
||||
// Reading the pages takes no token; the API still does.
|
||||
assert_eq!(call(&app, "GET", "/v1/feeds", None, None).await.0, 401);
|
||||
assert_eq!(call(&app, "DELETE", &format!("/v1/feeds/{id}"), None, None).await.0, 401);
|
||||
assert_eq!(call(&app, "DELETE", &format!("/feed/{id}"), None, None).await.0, 405);
|
||||
}
|
||||
|
||||
/// What the log layer wrote, kept for the test to read.
|
||||
struct Lines(std::sync::Arc<std::sync::Mutex<Vec<u8>>>);
|
||||
|
||||
impl std::io::Write for Lines {
|
||||
fn write(&mut self, b: &[u8]) -> std::io::Result<usize> {
|
||||
self.0.lock().unwrap().extend_from_slice(b);
|
||||
Ok(b.len())
|
||||
}
|
||||
fn flush(&mut self) -> std::io::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_snapshots_log_line_has_the_fields_grafana_reads() {
|
||||
use tracing_subscriber::layer::SubscriberExt;
|
||||
let buf = std::sync::Arc::new(std::sync::Mutex::new(vec![]));
|
||||
let b = buf.clone();
|
||||
// Global, as the directory's own is; other tests' snapshots land here too, and each of
|
||||
// their lines must carry the same fields.
|
||||
tracing::subscriber::set_global_default(
|
||||
tracing_subscriber::registry().with(crate::otel::json_layer(move || Lines(b.clone()), false)),
|
||||
)
|
||||
.unwrap();
|
||||
let (app, _dir) = app().await;
|
||||
let snap = serde_json::json!([{ "url": "https://x.test/log.xml", "subscribers": 1 }]);
|
||||
assert_eq!(call(&app, "POST", "/v1/snapshot", Some("rw"), Some(snap)).await.0, 200);
|
||||
|
||||
let text = String::from_utf8(buf.lock().unwrap().clone()).unwrap();
|
||||
let lines: Vec<serde_json::Value> = text.lines().map(|l| serde_json::from_str(l).expect(l)).collect();
|
||||
let snaps: Vec<&serde_json::Value> = lines.iter().filter(|l| l["message"] == "snapshot").collect();
|
||||
assert!(!snaps.is_empty(), "no snapshot line in {text}");
|
||||
for l in snaps {
|
||||
assert!(l["instance"].is_string(), "{l}");
|
||||
for n in ["accepted", "refused_private", "refused_blocked", "refused_invalid"] {
|
||||
assert!(l[n].is_u64(), "{n} in {l}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn dropping_a_servers_snapshot_takes_only_the_feeds_nobody_else_lists() {
|
||||
let (app, dir) = app().await;
|
||||
let feed = |url: &str| serde_json::json!({ "url": url, "subscribers": 1 });
|
||||
let mine = serde_json::json!([feed("https://only-rw.test/feed"), feed("https://both.test/feed")]);
|
||||
assert_eq!(call(&app, "POST", "/v1/snapshot", Some("rw"), Some(mine)).await.0, 200);
|
||||
let theirs = serde_json::json!([feed("https://both.test/feed")]);
|
||||
assert_eq!(call(&app, "POST", "/v1/snapshot", Some("rwa"), Some(theirs)).await.0, 200);
|
||||
|
||||
assert_eq!(call(&app, "DELETE", "/v1/instances/rw-token", Some("rw"), None).await.0, 403, "admins only");
|
||||
assert_eq!(call(&app, "DELETE", "/v1/instances/rw-token", Some("rwa"), None).await.0, 204);
|
||||
let left: Vec<(String, i64)> =
|
||||
dir.store.instances().await.unwrap().into_iter().map(|i| (i.name, i.feeds)).collect();
|
||||
assert_eq!(left, [("rwa-token".to_owned(), 1)]);
|
||||
let gone = dir.store.due_one(&store::feed_id("https://only-rw.test/feed")).await.unwrap();
|
||||
assert!(gone.is_none(), "nobody lists it now");
|
||||
let kept = dir.store.due_one(&store::feed_id("https://both.test/feed")).await.unwrap();
|
||||
assert!(kept.is_some(), "another server still does");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn each_access_level_does_what_it_may_and_no_more() {
|
||||
let (app, dir) = app().await;
|
||||
|
||||
@@ -23,8 +23,10 @@ const RETRY: Duration = Duration::from_secs(3600);
|
||||
/// The top this many overall, and of each category: about a thousand shows once they overlap.
|
||||
const TOP: u32 = 100;
|
||||
const TOP_OF_EACH: u32 = 50;
|
||||
/// Apple asks for no more than about twenty calls a minute.
|
||||
const PAUSE: Duration = Duration::from_secs(3);
|
||||
/// Apple asks for no more than about twenty calls a minute. A test's stand-in asks for none.
|
||||
const PAUSE: Duration = if cfg!(test) { Duration::ZERO } else { Duration::from_secs(3) };
|
||||
/// Where Apple's charts and lookup API are.
|
||||
const APPLE: &str = "https://itunes.apple.com";
|
||||
|
||||
/// Syncs when a week has passed since the last, forever.
|
||||
pub async fn run(dir: Shared, country: String) {
|
||||
@@ -46,7 +48,7 @@ pub async fn run(dir: Shared, country: String) {
|
||||
continue;
|
||||
}
|
||||
let span = tracing::info_span!("apple_sync", country = %country);
|
||||
match sync(&dir, &client, &country).instrument(span).await {
|
||||
match sync(&dir, &client, APPLE, &country).instrument(span).await {
|
||||
Ok(()) => {
|
||||
let _ = dir.store.set_synced(INSTANCE, crate::store::now()).await;
|
||||
}
|
||||
@@ -58,8 +60,8 @@ pub async fn run(dir: Shared, country: String) {
|
||||
}
|
||||
}
|
||||
|
||||
async fn sync(dir: &Shared, client: &reqwest::Client, country: &str) -> Result<()> {
|
||||
let genres: Genres = get(client, "https://itunes.apple.com/WebObjects/MZStoreServices.woa/ws/genres?id=26").await?;
|
||||
async fn sync(dir: &Shared, client: &reqwest::Client, apple: &str, country: &str) -> Result<()> {
|
||||
let genres: Genres = get(client, &format!("{apple}/WebObjects/MZStoreServices.woa/ws/genres?id=26")).await?;
|
||||
let theirs = categories(&genres);
|
||||
let (added, gone) = differ(&theirs, ipx_feed::categories());
|
||||
if !added.is_empty() || !gone.is_empty() {
|
||||
@@ -74,8 +76,8 @@ async fn sync(dir: &Shared, client: &reqwest::Client, country: &str) -> Result<(
|
||||
let charts = std::iter::once((None, TOP)).chain(genre_ids(&genres).into_iter().map(|g| (Some(g), TOP_OF_EACH)));
|
||||
for (genre, n) in charts {
|
||||
let url = match &genre {
|
||||
Some(g) => format!("https://itunes.apple.com/{country}/rss/toppodcasts/limit={n}/genre={g}/json"),
|
||||
None => format!("https://itunes.apple.com/{country}/rss/toppodcasts/limit={n}/json"),
|
||||
Some(g) => format!("{apple}/{country}/rss/toppodcasts/limit={n}/genre={g}/json"),
|
||||
None => format!("{apple}/{country}/rss/toppodcasts/limit={n}/json"),
|
||||
};
|
||||
match get::<Chart>(client, &url).await {
|
||||
Ok(chart) => ids.extend(chart_ids(&chart)),
|
||||
@@ -90,12 +92,13 @@ async fn sync(dir: &Shared, client: &reqwest::Client, country: &str) -> Result<(
|
||||
|
||||
let mut entries = Vec::new();
|
||||
for batch in ids.chunks(100) {
|
||||
let url = format!("https://itunes.apple.com/lookup?entity=podcast&id={}", batch.join(","));
|
||||
let url = format!("{apple}/lookup?entity=podcast&id={}", batch.join(","));
|
||||
let found: Lookup = get(client, &url).await?;
|
||||
entries.extend(lookup_entries(&found));
|
||||
tokio::time::sleep(PAUSE).await;
|
||||
}
|
||||
let result = dir.store.snapshot(INSTANCE, &entries).await?;
|
||||
crate::metrics::SNAPSHOTS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
|
||||
tracing::info!(
|
||||
shows = ids.len(),
|
||||
accepted = result.accepted,
|
||||
@@ -232,6 +235,72 @@ fn lookup_entries(l: &Lookup) -> Vec<SnapshotEntry> {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// A stand-in for Apple: one category, Arts (1301); the top chart is shows 1 and 2, Arts' is 2
|
||||
/// and 3; the lookup gives 1 a public feed, 2 one with a key in it, 3 another public one. With
|
||||
/// `lookup_fails`, the lookup is a 503.
|
||||
async fn apple(lookup_fails: bool) -> String {
|
||||
use axum::extract::{Path, Query};
|
||||
let chart = |ids: &[&str]| {
|
||||
let entry: Vec<_> =
|
||||
ids.iter().map(|id| serde_json::json!({ "id": { "attributes": { "im:id": id } } })).collect();
|
||||
serde_json::json!({ "feed": { "entry": entry } })
|
||||
};
|
||||
let (top, arts) = (chart(&["1", "2"]), chart(&["2", "3"]));
|
||||
let app = axum::Router::new()
|
||||
.route(
|
||||
"/WebObjects/MZStoreServices.woa/ws/genres",
|
||||
axum::routing::get(|| async {
|
||||
axum::Json(serde_json::json!({ "26": { "name": "Podcasts", "subgenres": { "1301": { "name": "Arts" } } } }))
|
||||
}),
|
||||
)
|
||||
.route(
|
||||
"/us/rss/toppodcasts/{*chart}",
|
||||
axum::routing::get(move |Path(c): Path<String>| {
|
||||
let got = if c.contains("genre=1301") { arts.clone() } else { top.clone() };
|
||||
async move { axum::Json(got) }
|
||||
}),
|
||||
)
|
||||
.route(
|
||||
"/lookup",
|
||||
axum::routing::get(move |Query(q): Query<BTreeMap<String, String>>| async move {
|
||||
if lookup_fails {
|
||||
return Err(axum::http::StatusCode::SERVICE_UNAVAILABLE);
|
||||
}
|
||||
assert_eq!(q["id"], "1,2,3", "each show once, in chart order");
|
||||
Ok(axum::Json(serde_json::json!({ "results": [
|
||||
{ "feedUrl": "https://one.test/feed", "primaryGenreName": "Arts" },
|
||||
{ "feedUrl": "https://two.test/feed?auth=0123456789abcdef0123456789abcdef", "primaryGenreName": "Arts" },
|
||||
{ "feedUrl": "https://three.test/feed", "primaryGenreName": "Podcasts" },
|
||||
]})))
|
||||
}),
|
||||
);
|
||||
let l = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let at = l.local_addr().unwrap();
|
||||
tokio::spawn(async move { axum::serve(l, app).await });
|
||||
format!("http://{at}")
|
||||
}
|
||||
|
||||
async fn apples(dir: &Shared) -> i64 {
|
||||
dir.store.instances().await.unwrap().iter().find(|i| i.name == INSTANCE).map_or(0, |i| i.feeds)
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_sync_takes_apples_charts_as_a_snapshot_and_a_failed_one_keeps_the_last() {
|
||||
let dir = crate::Directory::new(crate::store::Store::scratch().await, vec![], Default::default()).unwrap();
|
||||
let client = reqwest::Client::new();
|
||||
sync(&dir, &client, &apple(false).await, "us").await.unwrap();
|
||||
assert_eq!(apples(&dir).await, 2, "the feed with a key in it is refused as private");
|
||||
|
||||
// An admin blocks one; the next sync leaves it out.
|
||||
dir.store.block(&crate::store::feed_id("https://three.test/feed")).await.unwrap();
|
||||
sync(&dir, &client, &apple(false).await, "us").await.unwrap();
|
||||
assert_eq!(apples(&dir).await, 1);
|
||||
|
||||
// Apple failing is no reason to forget last week's charts.
|
||||
assert!(sync(&dir, &client, &apple(true).await, "us").await.is_err());
|
||||
assert_eq!(apples(&dir).await, 1, "the last good snapshot stands");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn apples_categories_are_read_and_compared_with_ipxs() {
|
||||
let genres: Genres = serde_json::from_str(
|
||||
|
||||
@@ -112,6 +112,7 @@ impl Directory {
|
||||
Err(e) => Refreshed::Failed(format!("{e:#}")),
|
||||
};
|
||||
if let Refreshed::Failed(e) = &got {
|
||||
crate::metrics::CHECKS_FAILED.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
|
||||
tracing::info!(feed = %due.id, error = %ipx_feed::redact_text(e), "feed check failed");
|
||||
}
|
||||
// ponytail: a feed that moved for good is still asked for at its old address, which
|
||||
@@ -143,6 +144,7 @@ pub async fn refresher(dir: Shared) {
|
||||
.await;
|
||||
// One line a round, for the dashboard's refresher panel (#210); failures have their own.
|
||||
tracing::info!(checked = due.len(), "feeds checked");
|
||||
crate::metrics::CHECKED.fetch_add(due.len() as u64, std::sync::atomic::Ordering::Relaxed);
|
||||
},
|
||||
round,
|
||||
)
|
||||
@@ -215,4 +217,49 @@ mod tests {
|
||||
dir.pages_list.lock().await.as_mut().unwrap().0 -= PAGES_LIST_FOR;
|
||||
assert!(!Arc::ptr_eq(&first, &dir.pages_list().await.unwrap()));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_refresh_lists_a_feed_and_not_one_that_could_not_be_read() {
|
||||
use axum::routing::get;
|
||||
let rss = r#"<?xml version="1.0"?><rss version="2.0"><channel><title>Good Show</title>
|
||||
<item><title>One</title><guid>g1</guid><enclosure url="https://x/1.mp3" type="audio/mpeg" length="1"/></item>
|
||||
</channel></rss>"#;
|
||||
let opml =
|
||||
r#"<?xml version="1.0"?><opml version="2.0"><head/><body><outline xmlUrl="https://x/f"/></body></opml>"#;
|
||||
let app = axum::Router::new()
|
||||
.route("/good.xml", get(move || async move { rss }))
|
||||
.route("/list.opml", get(move || async move { opml }))
|
||||
.route("/garbled.xml", get(|| async { "<rss><channel" }))
|
||||
.route("/gone.xml", get(|| async { axum::http::StatusCode::NOT_FOUND }));
|
||||
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let at = listener.local_addr().unwrap();
|
||||
tokio::spawn(async move { axum::serve(listener, app).await });
|
||||
|
||||
let allowed = ipx_feed::guard::Allowed::parse(&["127.0.0.1".into()]).unwrap();
|
||||
let dir = Directory::new(Store::scratch().await, vec![], allowed).unwrap();
|
||||
let paths = ["/good.xml", "/list.opml", "/garbled.xml", "/gone.xml"];
|
||||
let entries: Vec<crate::SnapshotEntry> = paths
|
||||
.iter()
|
||||
.map(|p| crate::SnapshotEntry { url: format!("http://{at}{p}"), subscribers: 1, category: None })
|
||||
.collect();
|
||||
assert_eq!(dir.store.snapshot("tower", &entries).await.unwrap().accepted, 4);
|
||||
let due = dir.store.due(REFRESH_EVERY, 10).await.unwrap();
|
||||
assert_eq!(due.len(), 4);
|
||||
for d in &due {
|
||||
dir.refresh(d).await.unwrap();
|
||||
}
|
||||
assert!(dir.store.due(REFRESH_EVERY, 10).await.unwrap().is_empty(), "each was checked, failed or not");
|
||||
|
||||
let page = |path: &str| {
|
||||
let d = due.iter().find(|d| d.url.ends_with(path)).unwrap();
|
||||
dir.store.feed(&d.id)
|
||||
};
|
||||
let good = page("/good.xml").await.unwrap().unwrap();
|
||||
assert_eq!(good.listing.title.as_deref(), Some("Good Show"));
|
||||
assert!(good.listing.podcast && good.failing.is_none() && good.items.len() == 1);
|
||||
for bad in ["/list.opml", "/garbled.xml", "/gone.xml"] {
|
||||
// Never read, so never listed: a page of nothing is no use to anyone browsing.
|
||||
assert!(page(bad).await.unwrap().is_none(), "{bad} is not listed");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,15 +83,16 @@ async fn serve(path: &Path) -> Result<()> {
|
||||
let filter = || EnvFilter::try_from_env("DIRECTORY_LOG").unwrap_or_else(|_| "ipx_directory=info".into());
|
||||
// Traces to Tempo when OTEL_EXPORTER_OTLP_ENDPOINT is set, joining ipx's (#213).
|
||||
let otel = ipx_directory::otel::provider("ipx-directory")?;
|
||||
// Each line's own fields say all its span did, and the span said it again (#363); the trace
|
||||
// and span ids lead to the trace instead, as ipx's do (#375). Boxed rather than an Option of
|
||||
// each: Option's Layer does not pass on_register_dispatch on, which the JSON layer needs (#408).
|
||||
let stderr: Box<dyn tracing_subscriber::Layer<tracing_subscriber::Registry> + Send + Sync> = if json {
|
||||
ipx_directory::otel::json_layer(std::io::stderr, false).with_filter(filter()).boxed()
|
||||
} else {
|
||||
tracing_subscriber::fmt::layer().with_writer(std::io::stderr).with_filter(filter()).boxed()
|
||||
};
|
||||
tracing_subscriber::registry()
|
||||
.with((!json).then(|| tracing_subscriber::fmt::layer().with_writer(std::io::stderr).with_filter(filter())))
|
||||
.with(json.then(|| {
|
||||
tracing_subscriber::fmt::layer()
|
||||
.json()
|
||||
.flatten_event(true)
|
||||
.with_writer(std::io::stderr)
|
||||
.with_filter(filter())
|
||||
}))
|
||||
.with(stderr)
|
||||
.with(otel.as_ref().map(|p| {
|
||||
use opentelemetry::trace::TracerProvider;
|
||||
tracing_opentelemetry::layer()
|
||||
|
||||
@@ -7,6 +7,10 @@
|
||||
//! does not come down again is a leak. Not jemalloc, whose upstream was archived in 2025.
|
||||
|
||||
use axum::Router;
|
||||
use std::collections::BTreeMap;
|
||||
use std::fmt::Write as _;
|
||||
use std::sync::Mutex;
|
||||
use std::sync::atomic::AtomicU64;
|
||||
use std::sync::atomic::Ordering::Relaxed;
|
||||
use std::time::Instant;
|
||||
use tokio::net::TcpListener;
|
||||
@@ -27,8 +31,75 @@ pub fn process() -> (Option<f64>, Option<f64>) {
|
||||
(rss, fds)
|
||||
}
|
||||
|
||||
/// The directory's /metrics, in Prometheus's text format: memory, open files, and the feeds it
|
||||
/// has fetched. Nothing from the database, so a scrape costs nothing and answers while it is down.
|
||||
/// Upper bounds for a request's or a query's time, in seconds: most answer in milliseconds, and a
|
||||
/// host stalling takes seconds.
|
||||
pub const SECONDS: &[f64] = &[0.001, 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1.0, 2.5, 5.0, 10.0];
|
||||
|
||||
/// Prometheus's histogram, for ipx and the directory both: for each set of labels, how many
|
||||
/// observations fell under each bound, how many all told, and their sum.
|
||||
pub struct Histograms {
|
||||
bounds: &'static [f64],
|
||||
rows: Mutex<BTreeMap<String, Row>>,
|
||||
}
|
||||
|
||||
/// Under each bound, all told, and the sum.
|
||||
type Row = (Vec<u64>, u64, f64);
|
||||
|
||||
impl Histograms {
|
||||
#[must_use]
|
||||
pub const fn new(bounds: &'static [f64]) -> Self {
|
||||
Self { bounds, rows: Mutex::new(BTreeMap::new()) }
|
||||
}
|
||||
|
||||
/// Counts `v` under `labels`, written as Prometheus reads them (`a="x",b="y"`, or "" for
|
||||
/// none); `label` escapes a value that came from outside.
|
||||
pub fn observe(&self, labels: &str, v: f64) {
|
||||
let mut rows = self.rows.lock().expect("a panic elsewhere poisoned this lock");
|
||||
let (buckets, n, sum) = rows.entry(labels.to_owned()).or_insert_with(|| (vec![0; self.bounds.len()], 0, 0.0));
|
||||
for (b, le) in buckets.iter_mut().zip(self.bounds) {
|
||||
*b += u64::from(v <= *le);
|
||||
}
|
||||
*n += 1;
|
||||
*sum += v;
|
||||
}
|
||||
|
||||
pub fn render(&self, out: &mut String, name: &str, help: &str) {
|
||||
let rows = self.rows.lock().expect("a panic elsewhere poisoned this lock").clone();
|
||||
let _ = writeln!(out, "# HELP {name} {help}\n# TYPE {name} histogram");
|
||||
for (labels, (buckets, n, sum)) in &rows {
|
||||
let sep = if labels.is_empty() { "" } else { "," };
|
||||
for (le, b) in self.bounds.iter().zip(buckets) {
|
||||
let _ = writeln!(out, "{name}_bucket{{{labels}{sep}le=\"{le}\"}} {b}");
|
||||
}
|
||||
let _ = writeln!(out, "{name}_bucket{{{labels}{sep}le=\"+Inf\"}} {n}");
|
||||
let braces = if labels.is_empty() { String::new() } else { format!("{{{labels}}}") };
|
||||
let _ = writeln!(out, "{name}_sum{braces} {sum}\n{name}_count{braces} {n}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A request's method as a label: one outside the usual is "other", so a caller cannot make rows.
|
||||
#[must_use]
|
||||
pub fn method(m: &str) -> &'static str {
|
||||
["GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"].into_iter().find(|x| *x == m).unwrap_or("other")
|
||||
}
|
||||
|
||||
/// A label's value as Prometheus's text format reads it.
|
||||
#[must_use]
|
||||
pub fn label(v: &str) -> String {
|
||||
v.replace('\\', "\\\\").replace('"', "\\\"").replace('\n', "\\n")
|
||||
}
|
||||
|
||||
/// Requests answered, by method, route and status (#370).
|
||||
pub static REQUESTS: Histograms = Histograms::new(SECONDS);
|
||||
/// Feeds the refresher has checked, and of them those that failed (#370).
|
||||
pub static CHECKED: AtomicU64 = AtomicU64::new(0);
|
||||
pub static CHECKS_FAILED: AtomicU64 = AtomicU64::new(0);
|
||||
/// Snapshots taken from ipx servers and the Apple sync (#370).
|
||||
pub static SNAPSHOTS: AtomicU64 = AtomicU64::new(0);
|
||||
|
||||
/// The directory's /metrics, in Prometheus's text format: memory, open files, the feeds it has
|
||||
/// fetched and checked, snapshots and requests. Nothing from the database, so a scrape costs nothing and answers while it is down.
|
||||
fn render(started: Instant) -> String {
|
||||
let mut out = String::new();
|
||||
let mut gauge = |name: &str, kind: &str, help: &str, labels: &str, v: f64| {
|
||||
@@ -69,6 +140,20 @@ fn render(started: Instant) -> String {
|
||||
&format!("{{version=\"{}\"}}", env!("CARGO_PKG_VERSION")),
|
||||
1.0,
|
||||
);
|
||||
gauge("directory_feed_checks_total", "counter", "Feeds the refresher checked.", "", CHECKED.load(Relaxed) as f64);
|
||||
gauge(
|
||||
"directory_feed_checks_failed_total",
|
||||
"counter",
|
||||
"Feed checks that failed: the publisher's error, or a feed that would not parse.",
|
||||
"",
|
||||
CHECKS_FAILED.load(Relaxed) as f64,
|
||||
);
|
||||
gauge("directory_snapshots_total", "counter", "Snapshots taken.", "", SNAPSHOTS.load(Relaxed) as f64);
|
||||
REQUESTS.render(
|
||||
&mut out,
|
||||
"directory_http_request_duration_seconds",
|
||||
"Requests answered and the seconds each took, by method, route and status.",
|
||||
);
|
||||
out
|
||||
}
|
||||
|
||||
@@ -92,6 +177,52 @@ pub async fn serve(listener: TcpListener) {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn a_histogram_is_written_as_prometheus_reads_it() {
|
||||
static H: Histograms = Histograms::new(&[0.1, 1.0]);
|
||||
for v in [0.05, 0.5, 2.0] {
|
||||
H.observe("route=\"/a\"", v);
|
||||
}
|
||||
H.observe("", 0.01);
|
||||
let mut out = String::new();
|
||||
H.render(&mut out, "t_seconds", "Help.");
|
||||
for line in [
|
||||
"# TYPE t_seconds histogram\n",
|
||||
"t_seconds_bucket{route=\"/a\",le=\"0.1\"} 1\n",
|
||||
"t_seconds_bucket{route=\"/a\",le=\"1\"} 2\n",
|
||||
"t_seconds_bucket{route=\"/a\",le=\"+Inf\"} 3\n",
|
||||
"t_seconds_count{route=\"/a\"} 3\n",
|
||||
"t_seconds_bucket{le=\"0.1\"} 1\n",
|
||||
"t_seconds_sum 0.01\n",
|
||||
] {
|
||||
assert!(out.contains(line), "{line} in {out}");
|
||||
}
|
||||
assert_eq!(label("a \"b\"\\"), "a \\\"b\\\"\\\\");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn every_directory_metric_grafana_queries_is_served() {
|
||||
let l = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let at = l.local_addr().unwrap();
|
||||
tokio::spawn(serve(l));
|
||||
let text = reqwest::get(format!("http://{at}/metrics")).await.unwrap().text().await.unwrap();
|
||||
let src = [include_str!("../../../grafana/dashboard.py"), include_str!("../../../grafana/alerts.py")].concat();
|
||||
let names: std::collections::BTreeSet<&str> = src
|
||||
.match_indices("directory_")
|
||||
.filter(|(i, _)| !src[..*i].ends_with(|c: char| c.is_ascii_alphanumeric() || c == '_'))
|
||||
.map(|(i, _)| {
|
||||
let rest = &src[i..];
|
||||
&rest[..rest.find(|c: char| !(c.is_ascii_lowercase() || c == '_')).unwrap_or(rest.len())]
|
||||
})
|
||||
.collect();
|
||||
assert!(names.len() >= 4, "{names:?}");
|
||||
for n in names {
|
||||
assert!(text.contains(&format!("# TYPE {n} ")), "{n} is queried by grafana/ but not served: {text}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn metrics_say_memory_and_feeds() {
|
||||
let text = super::render(std::time::Instant::now());
|
||||
|
||||
@@ -17,6 +17,7 @@ pub fn provider(service: &'static str) -> anyhow::Result<Option<opentelemetry_sd
|
||||
opentelemetry_sdk::trace::SdkTracerProvider::builder()
|
||||
.with_batch_exporter(exporter)
|
||||
.with_resource(resource)
|
||||
.with_id_generator(Ids::default())
|
||||
.build(),
|
||||
))
|
||||
}
|
||||
@@ -58,6 +59,117 @@ pub fn traceparent() -> Option<String> {
|
||||
sc.is_valid().then(|| format!("00-{}-{}-{:02x}", sc.trace_id(), sc.span_id(), sc.trace_flags().to_u8()))
|
||||
}
|
||||
|
||||
/// Random ids, as the SDK's own, but for a span built inside `with_ids`, which takes the ids it is
|
||||
/// given: the page names its tap's span before ipx makes it (#376), and the SDK gives a span
|
||||
/// builder no other way to be told them.
|
||||
#[derive(Debug, Default)]
|
||||
struct Ids(opentelemetry_sdk::trace::RandomIdGenerator);
|
||||
|
||||
thread_local! {
|
||||
static GIVEN: std::cell::Cell<Option<(opentelemetry::trace::TraceId, opentelemetry::trace::SpanId)>> =
|
||||
const { std::cell::Cell::new(None) };
|
||||
}
|
||||
|
||||
impl opentelemetry_sdk::trace::IdGenerator for Ids {
|
||||
fn new_trace_id(&self) -> opentelemetry::trace::TraceId {
|
||||
GIVEN.get().map_or_else(|| self.0.new_trace_id(), |(t, _)| t)
|
||||
}
|
||||
fn new_span_id(&self) -> opentelemetry::trace::SpanId {
|
||||
GIVEN.get().map_or_else(|| self.0.new_span_id(), |(_, s)| s)
|
||||
}
|
||||
}
|
||||
|
||||
/// Runs `f`, which builds one root span, with these ids for it. Synchronous, so nothing else on
|
||||
/// this thread makes a span in between.
|
||||
pub fn with_ids<R>(
|
||||
trace: opentelemetry::trace::TraceId,
|
||||
span: opentelemetry::trace::SpanId,
|
||||
f: impl FnOnce() -> R,
|
||||
) -> R {
|
||||
GIVEN.set(Some((trace, span)));
|
||||
let r = f();
|
||||
GIVEN.set(None);
|
||||
r
|
||||
}
|
||||
|
||||
/// The JSON log line with the trace and span it belongs to (#91), so a line in Loki leads to its
|
||||
/// trace in Tempo. The JSON formatter cannot take a field of its own, so the ids go on the end of
|
||||
/// the object it writes; the directory's lines carry them too (#375). A line outside any traced span, or with no trace exporter, is unchanged.
|
||||
/// One JSON object a line for Loki, each event's fields its own and the trace and span ids beside
|
||||
/// them, for ipx and the directory both: the dashboard and the alert rules read these fields.
|
||||
/// `current_span` adds the span's own fields under `span`, which ipx keeps and the directory
|
||||
/// dropped (#363).
|
||||
pub fn json_layer<S, W>(writer: W, current_span: bool) -> impl tracing_subscriber::Layer<S>
|
||||
where
|
||||
S: tracing::Subscriber + for<'a> tracing_subscriber::registry::LookupSpan<'a>,
|
||||
W: for<'w> tracing_subscriber::fmt::MakeWriter<'w> + Send + Sync + 'static,
|
||||
{
|
||||
use tracing_subscriber::Layer as _;
|
||||
let registered = Registered::default();
|
||||
tracing_subscriber::fmt::layer()
|
||||
.fmt_fields(tracing_subscriber::fmt::format::JsonFields::new())
|
||||
.event_format(WithTrace(
|
||||
tracing_subscriber::fmt::format()
|
||||
.json()
|
||||
.flatten_event(true)
|
||||
.with_current_span(current_span)
|
||||
.with_span_list(false),
|
||||
registered.clone(),
|
||||
))
|
||||
.with_writer(writer)
|
||||
.and_then(registered)
|
||||
}
|
||||
|
||||
/// The subscriber the log layer is part of, as it was handed over on registration. The span's
|
||||
/// OpenTelemetry context is reached through it: `Span::current()` asks the thread's default
|
||||
/// dispatcher, which inside an event under a scoped subscriber (`with_default`) is hidden by
|
||||
/// tracing's re-entrancy guard, and every line went out without its ids (#408).
|
||||
#[derive(Clone, Default)]
|
||||
struct Registered(std::sync::Arc<std::sync::OnceLock<tracing::dispatcher::WeakDispatch>>);
|
||||
|
||||
impl<S: tracing::Subscriber> tracing_subscriber::Layer<S> for Registered {
|
||||
fn on_register_dispatch(&self, subscriber: &tracing::Dispatch) {
|
||||
let _ = self.0.set(subscriber.downgrade());
|
||||
}
|
||||
}
|
||||
|
||||
struct WithTrace<F>(F, Registered);
|
||||
|
||||
impl<S, N, F> tracing_subscriber::fmt::FormatEvent<S, N> for WithTrace<F>
|
||||
where
|
||||
F: tracing_subscriber::fmt::FormatEvent<S, N>,
|
||||
S: tracing::Subscriber + for<'a> tracing_subscriber::registry::LookupSpan<'a>,
|
||||
N: for<'w> tracing_subscriber::fmt::FormatFields<'w> + 'static,
|
||||
{
|
||||
fn format_event(
|
||||
&self,
|
||||
ctx: &tracing_subscriber::fmt::FmtContext<'_, S, N>,
|
||||
mut w: tracing_subscriber::fmt::format::Writer<'_>,
|
||||
ev: &tracing::Event<'_>,
|
||||
) -> std::fmt::Result {
|
||||
use opentelemetry::trace::TraceContextExt;
|
||||
// Without one (a wrapper that does not pass registration on, as Option does not), the
|
||||
// default dispatcher, which a global subscriber's lines still find.
|
||||
let registered = self.1.0.get().and_then(tracing::dispatcher::WeakDispatch::upgrade);
|
||||
let dispatch = registered.unwrap_or_else(|| tracing::dispatcher::get_default(Clone::clone));
|
||||
// The registry's own current span, not the one this layer's filter would show.
|
||||
let sc = ctx
|
||||
.current_span()
|
||||
.id()
|
||||
.and_then(|id| tracing_opentelemetry::get_otel_context(id, &dispatch))
|
||||
.map(|cx| cx.span().span_context().clone());
|
||||
let Some(sc) = sc.filter(|c| c.is_valid()) else {
|
||||
return self.0.format_event(ctx, w, ev);
|
||||
};
|
||||
let mut line = String::new();
|
||||
self.0.format_event(ctx, tracing_subscriber::fmt::format::Writer::new(&mut line), ev)?;
|
||||
match line.trim_end().strip_suffix('}') {
|
||||
Some(body) => writeln!(w, r#"{body},"trace_id":"{}","span_id":"{}"}}"#, sc.trace_id(), sc.span_id()),
|
||||
None => w.write_str(&line),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -92,4 +204,21 @@ mod tests {
|
||||
assert_eq!(q.unwrap().span().span_context().trace_id().to_string(), "4bf92f3577b34da6a3ce929d0e0e4736");
|
||||
assert!(remote_parent(&axum::http::HeaderMap::new(), Some("traceparent=nonsense")).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_span_built_with_given_ids_takes_them_and_the_next_does_not() {
|
||||
use opentelemetry::trace::{Span as _, SpanId, TraceId, Tracer as _, TracerProvider as _};
|
||||
let tracer = opentelemetry_sdk::trace::SdkTracerProvider::builder()
|
||||
.with_id_generator(super::Ids::default())
|
||||
.build()
|
||||
.tracer("t");
|
||||
let (t, s) = (
|
||||
TraceId::from_hex("4bf92f3577b34da6a3ce929d0e0e4736").unwrap(),
|
||||
SpanId::from_hex("00f067aa0ba902b7").unwrap(),
|
||||
);
|
||||
let given = super::with_ids(t, s, || tracer.start("tap"));
|
||||
assert_eq!((given.span_context().trace_id(), given.span_context().span_id()), (t, s));
|
||||
let next = tracer.start("other");
|
||||
assert_ne!(next.span_context().trace_id(), t);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,17 @@
|
||||
// Made from site.ts by `node web/build.mts --directory-script`: edit that, not this.
|
||||
// The one thing the directory's pages script (#215): Copy puts a feed's address on the clipboard.
|
||||
// Without it, the address is in a field of its own to select by hand.
|
||||
document.addEventListener('click', ev => {
|
||||
const b = ev.target.closest('[data-copy]');
|
||||
if (!b || !navigator.clipboard) return;
|
||||
navigator.clipboard.writeText(b.dataset.copy).then(() => {
|
||||
b.textContent = 'Copied';
|
||||
setTimeout(() => { b.textContent = 'Copy'; }, 1500);
|
||||
});
|
||||
//
|
||||
// The source: the directory serves site.js, made from this by `node web/build.mts --directory-script`
|
||||
// (#392), since its build has no Node to make it; tests/directory-script.ts checks the two agree.
|
||||
document.addEventListener('click', (ev)=>{
|
||||
const b = ev.target?.closest('[data-copy]');
|
||||
// Only in a secure context; plain HTTP has no clipboard.
|
||||
if (!b || !navigator.clipboard) return;
|
||||
void navigator.clipboard.writeText(b.dataset.copy ?? '').then(()=>{
|
||||
b.textContent = 'Copied';
|
||||
setTimeout(()=>{
|
||||
b.textContent = 'Copy';
|
||||
}, 1500);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -25,6 +25,8 @@ pub fn router() -> Router<Shared> {
|
||||
.route("/search", get(search))
|
||||
.route("/category/{name}", get(category))
|
||||
.route("/feed/{id}", get(feed))
|
||||
.route("/robots.txt", get(robots))
|
||||
.route("/sitemap.xml", get(sitemap))
|
||||
.route("/site.css", get(|| asset("text/css; charset=utf-8", include_bytes!("site.css"))))
|
||||
.route("/site.js", get(|| asset("text/javascript; charset=utf-8", include_bytes!("site.js"))))
|
||||
.route("/logo.svg", get(|| asset("image/svg+xml", include_bytes!("../../../web/logo.svg"))))
|
||||
@@ -40,6 +42,48 @@ async fn asset(kind: &'static str, body: &'static [u8]) -> Response {
|
||||
([(header::CONTENT_TYPE, kind), (header::CACHE_CONTROL, "public, max-age=86400")], body).into_response()
|
||||
}
|
||||
|
||||
/// The address this was asked for at, which both files must give whole: the public name through
|
||||
/// the tunnel, whose cloudflared says https, or the LAN address. Only a host name's characters are
|
||||
/// kept, since the header is the caller's to write.
|
||||
fn origin(h: &axum::http::HeaderMap) -> String {
|
||||
let get = |k: &str| h.get(k).and_then(|v| v.to_str().ok()).unwrap_or("");
|
||||
let host: String =
|
||||
get("host").chars().filter(|c| c.is_ascii_alphanumeric() || matches!(c, '.' | '-' | ':' | '[' | ']')).collect();
|
||||
let scheme = if get("x-forwarded-proto") == "https" { "https" } else { "http" };
|
||||
format!("{scheme}://{host}")
|
||||
}
|
||||
|
||||
/// Crawlers may read every page but a search, which is any query at all, and the API, which takes
|
||||
/// a token (#380).
|
||||
async fn robots(h: axum::http::HeaderMap) -> Response {
|
||||
let body = format!("User-agent: *\nDisallow: /search\nDisallow: /v1/\n\nSitemap: {}/sitemap.xml\n", origin(&h));
|
||||
([(header::CONTENT_TYPE, "text/plain; charset=utf-8"), (header::CACHE_CONTROL, "public, max-age=86400")], body)
|
||||
.into_response()
|
||||
}
|
||||
|
||||
/// Every page there is to find: the home page, A to Z, each category and each feed (#380).
|
||||
async fn sitemap(State(dir): State<Shared>, h: axum::http::HeaderMap) -> Response {
|
||||
let all = match dir.pages_list().await {
|
||||
Ok(a) => a,
|
||||
Err(e) => return failed(e),
|
||||
};
|
||||
let o = origin(&h);
|
||||
let cats: std::collections::BTreeSet<&str> = all.iter().map(|l| l.category.as_deref().unwrap_or("Other")).collect();
|
||||
let mut b = String::from(
|
||||
r#"<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">"#,
|
||||
);
|
||||
let paths = ["/".to_owned(), "/all".to_owned()]
|
||||
.into_iter()
|
||||
.chain(cats.iter().map(|c| format!("/category/{}", seg(c))))
|
||||
.chain(all.iter().map(|l| format!("/feed/{}", seg(&l.id))));
|
||||
for p in paths {
|
||||
let _ = write!(b, "<url><loc>{}</loc></url>", esc(&format!("{o}{p}")));
|
||||
}
|
||||
b.push_str("</urlset>");
|
||||
([(header::CONTENT_TYPE, "application/xml; charset=utf-8"), (header::CACHE_CONTROL, "public, max-age=3600")], b)
|
||||
.into_response()
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize, Default)]
|
||||
struct Filter {
|
||||
/// `podcasts` or `blogs`; anything else is both.
|
||||
|
||||
14
crates/directory/src/site.ts
Normal file
14
crates/directory/src/site.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
// The one thing the directory's pages script (#215): Copy puts a feed's address on the clipboard.
|
||||
// Without it, the address is in a field of its own to select by hand.
|
||||
//
|
||||
// The source: the directory serves site.js, made from this by `node web/build.mts --directory-script`
|
||||
// (#392), since its build has no Node to make it; tests/directory-script.ts checks the two agree.
|
||||
document.addEventListener('click', (ev) => {
|
||||
const b = (ev.target as Element | null)?.closest<HTMLElement>('[data-copy]');
|
||||
// Only in a secure context; plain HTTP has no clipboard.
|
||||
if (!b || !navigator.clipboard) return;
|
||||
void navigator.clipboard.writeText(b.dataset.copy ?? '').then(() => {
|
||||
b.textContent = 'Copied';
|
||||
setTimeout(() => { b.textContent = 'Copy'; }, 1500);
|
||||
});
|
||||
});
|
||||
@@ -97,3 +97,98 @@ pub struct Blocked {
|
||||
/// The most a snapshot may list: far more than one server's subscriptions, few enough that one
|
||||
/// request cannot hold the database for long.
|
||||
pub const MAX_SNAPSHOT: usize = 20_000;
|
||||
|
||||
// The same in protobuf (#470): ipx reads and writes these through ipx-wire's structs, which
|
||||
// mirror the ones above field for field.
|
||||
use ipx_wire::pb::directory as pb;
|
||||
|
||||
impl From<pb::Listing> for Listing {
|
||||
fn from(l: pb::Listing) -> Self {
|
||||
Listing {
|
||||
id: l.id,
|
||||
url: l.url,
|
||||
title: l.title,
|
||||
image: l.image,
|
||||
category: l.category,
|
||||
subcategory: l.subcategory,
|
||||
podcast: l.podcast,
|
||||
subscribers: l.subscribers,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<pb::Item> for Item {
|
||||
fn from(i: pb::Item) -> Self {
|
||||
Item {
|
||||
guid: i.guid,
|
||||
title: i.title,
|
||||
link: i.link,
|
||||
published: i.published,
|
||||
duration: i.duration,
|
||||
description: i.description,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<pb::FeedPage> for FeedPage {
|
||||
fn from(p: pb::FeedPage) -> Self {
|
||||
FeedPage {
|
||||
listing: Listing {
|
||||
id: p.id,
|
||||
url: p.url,
|
||||
title: p.title,
|
||||
image: p.image,
|
||||
category: p.category,
|
||||
subcategory: p.subcategory,
|
||||
podcast: p.podcast,
|
||||
subscribers: p.subscribers,
|
||||
},
|
||||
description: p.description,
|
||||
failing: p.failing,
|
||||
items: p.items.into_iter().map(Item::from).collect(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<pb::Me> for Me {
|
||||
type Error = String;
|
||||
fn try_from(m: pb::Me) -> Result<Self, String> {
|
||||
let access = match m.access.as_str() {
|
||||
"ro" => Access::Ro,
|
||||
"rw" => Access::Rw,
|
||||
"rwa" => Access::Rwa,
|
||||
other => return Err(format!("an access level not known here: {other:?}")),
|
||||
};
|
||||
Ok(Me { name: m.name, access })
|
||||
}
|
||||
}
|
||||
|
||||
impl From<pb::Instance> for Instance {
|
||||
fn from(i: pb::Instance) -> Self {
|
||||
Instance { name: i.name, feeds: i.feeds, subscribers: i.subscribers }
|
||||
}
|
||||
}
|
||||
|
||||
impl From<pb::Blocked> for Blocked {
|
||||
fn from(b: pb::Blocked) -> Self {
|
||||
Blocked { id: b.id, url: b.url, title: b.title, at: b.at }
|
||||
}
|
||||
}
|
||||
|
||||
impl From<pb::SnapshotResult> for SnapshotResult {
|
||||
fn from(r: pb::SnapshotResult) -> Self {
|
||||
let n = |v: u64| usize::try_from(v).unwrap_or(usize::MAX);
|
||||
SnapshotResult {
|
||||
accepted: n(r.accepted),
|
||||
refused_private: n(r.refused_private),
|
||||
refused_blocked: n(r.refused_blocked),
|
||||
refused_invalid: n(r.refused_invalid),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&SnapshotEntry> for pb::SnapshotEntry {
|
||||
fn from(e: &SnapshotEntry) -> Self {
|
||||
pb::SnapshotEntry { url: e.url.clone(), subscribers: e.subscribers, category: e.category.clone() }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,9 +19,7 @@ quick-xml.workspace = true
|
||||
reqwest.workspace = true
|
||||
rss.workspace = true
|
||||
serde_json.workspace = true
|
||||
tokio.workspace = true
|
||||
tower.workspace = true
|
||||
tracing.workspace = true
|
||||
url.workspace = true
|
||||
|
||||
[dev-dependencies]
|
||||
tokio.workspace = true
|
||||
|
||||
@@ -41,6 +41,41 @@ pub static FETCHED_BYTES: AtomicU64 = AtomicU64::new(0);
|
||||
/// catalogue was 62 MB, and 50 MB here stopped it updating (#357).
|
||||
pub(crate) const MAX_BODY: usize = 200 << 20;
|
||||
|
||||
/// How many times a YouTube feed's spurious failure is asked again, each after its turn.
|
||||
const YOUTUBE_RETRIES: u32 = 2;
|
||||
|
||||
/// The least time between two requests for YouTube feeds from this process. A scan asked for six
|
||||
/// at once, and channels in a row failed together while one asked alone a minute later read
|
||||
/// (#386); asked again a second later, they failed again.
|
||||
const YOUTUBE_GAP: Duration = Duration::from_secs(2);
|
||||
static YOUTUBE_NEXT: tokio::sync::Mutex<Option<tokio::time::Instant>> = tokio::sync::Mutex::const_new(None);
|
||||
|
||||
/// Waits until `gap` after the last request's turn, one caller at a time: tokio's lock is fair,
|
||||
/// so waiting feeds go in the order they asked. shortcut: 39 channels due together add up to 80 s
|
||||
/// to that scan; a scan's other feeds wait behind them only when they come after in the order.
|
||||
#[tracing::instrument(name = "youtube_turn", skip_all)]
|
||||
async fn take_turn(next: &tokio::sync::Mutex<Option<tokio::time::Instant>>, gap: Duration) {
|
||||
let mut next = next.lock().await;
|
||||
if let Some(at) = *next {
|
||||
tokio::time::sleep_until(at).await;
|
||||
}
|
||||
*next = Some(tokio::time::Instant::now() + gap);
|
||||
}
|
||||
|
||||
/// YouTube answers some feed requests with a 404 or 500 it does not mean, in tens of
|
||||
/// milliseconds, while the next request for the same feed reads; checked once a day, a channel
|
||||
/// then missed most days (#382).
|
||||
fn youtube_spurious(url: &reqwest::Url, status: StatusCode) -> bool {
|
||||
is_youtube_feed(url.as_str()) && (status == StatusCode::NOT_FOUND || status.is_server_error())
|
||||
}
|
||||
|
||||
/// A YouTube channel's or playlist's feed, which fails most reads in streaks that outlast
|
||||
/// `fetch`'s retries, so a scheduler asks it again sooner than other feeds (#386).
|
||||
#[must_use]
|
||||
pub fn is_youtube_feed(url: &str) -> bool {
|
||||
reqwest::Url::parse(url).is_ok_and(|u| u.host_str() == Some("www.youtube.com") && u.path() == "/feeds/videos.xml")
|
||||
}
|
||||
|
||||
/// A response's body, up to `MAX_BODY`: past it, an error, and the rest is never read.
|
||||
pub(crate) async fn read_capped(mut resp: reqwest::Response) -> Result<Vec<u8>> {
|
||||
if resp.content_length().is_some_and(|n| n > MAX_BODY as u64) {
|
||||
@@ -68,7 +103,12 @@ pub async fn fetch(
|
||||
let start = reqwest::Url::parse(feed_url).context("the feed's address")?;
|
||||
let mut url = start.clone();
|
||||
let mut permanent = true;
|
||||
for _ in 0..10 {
|
||||
let mut retries = YOUTUBE_RETRIES;
|
||||
// Ten redirects, and the retries beside them.
|
||||
for _ in 0..10 + YOUTUBE_RETRIES {
|
||||
if is_youtube_feed(url.as_str()) {
|
||||
take_turn(&YOUTUBE_NEXT, YOUTUBE_GAP).await;
|
||||
}
|
||||
let mut req = client.get(url.clone()).timeout(FEED_TIMEOUT);
|
||||
if let Some(tag) = etag {
|
||||
req = req.header(IF_NONE_MATCH, tag);
|
||||
@@ -100,6 +140,10 @@ pub async fn fetch(
|
||||
if status == StatusCode::NOT_MODIFIED {
|
||||
return Ok((Fetched::NotModified, moved));
|
||||
}
|
||||
if retries > 0 && youtube_spurious(&url, status) {
|
||||
retries -= 1;
|
||||
continue;
|
||||
}
|
||||
if !status.is_success() {
|
||||
// WordPress.com answers some readers' feed requests with a browser check that only
|
||||
// JavaScript passes, for hours at a time and whatever the User-Agent; its API has the
|
||||
@@ -167,6 +211,38 @@ mod tests {
|
||||
format!("http://{addr}")
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn youtube_turns_are_a_gap_apart_one_at_a_time() {
|
||||
let (next, gap) = (tokio::sync::Mutex::const_new(None), std::time::Duration::from_millis(50));
|
||||
let start = tokio::time::Instant::now();
|
||||
// The first goes at once; three asking together then go one gap after another.
|
||||
super::take_turn(&next, gap).await;
|
||||
assert!(start.elapsed() < gap);
|
||||
let t = || async {
|
||||
super::take_turn(&next, gap).await;
|
||||
start.elapsed()
|
||||
};
|
||||
let (a, b, c) = tokio::join!(t(), t(), t());
|
||||
let mut at = [a, b, c];
|
||||
at.sort();
|
||||
for (i, at) in at.iter().enumerate() {
|
||||
assert!(*at >= gap * (i as u32 + 1), "turn {} at {at:?}", i + 1);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn only_a_youtube_feeds_404_or_5xx_is_asked_again() {
|
||||
use reqwest::StatusCode;
|
||||
let yt = reqwest::Url::parse("https://www.youtube.com/feeds/videos.xml?user=x").unwrap();
|
||||
let other = reqwest::Url::parse("https://example.com/feeds/videos.xml").unwrap();
|
||||
let page = reqwest::Url::parse("https://www.youtube.com/watch?v=x").unwrap();
|
||||
assert!(super::youtube_spurious(&yt, StatusCode::NOT_FOUND));
|
||||
assert!(super::youtube_spurious(&yt, StatusCode::INTERNAL_SERVER_ERROR));
|
||||
assert!(!super::youtube_spurious(&yt, StatusCode::FORBIDDEN));
|
||||
assert!(!super::youtube_spurious(&other, StatusCode::NOT_FOUND));
|
||||
assert!(!super::youtube_spurious(&page, StatusCode::NOT_FOUND));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_feed_that_moved_for_good_says_where_and_one_moved_for_now_does_not() {
|
||||
let base = redirecting_server().await;
|
||||
|
||||
@@ -18,9 +18,75 @@ pub use artwork::{is_image, prefer_https, site_icon};
|
||||
pub use category::{categories, category_path};
|
||||
pub use discover::{expand_input, find_feed};
|
||||
pub use failure::{Failure, explain_failure, failure_kind, is_temporary_lookup_failure};
|
||||
pub use fetch::{FEED_SILENCE, FEED_TIMEOUT, FETCHED_BYTES, FETCHES, Fetched, fetch};
|
||||
pub use fetch::{FEED_SILENCE, FEED_TIMEOUT, FETCHED_BYTES, FETCHES, Fetched, fetch, is_youtube_feed};
|
||||
pub use jev::{JEV_URL, classify};
|
||||
pub use outlines::{collect_outlines, is_opml, opml_title, parse_opml, read_opml};
|
||||
pub use parse::{Enclosure, Entry, ParsedFeed, markdown_html, parse, same_file_key};
|
||||
pub use patreon::{is_patreon_creator, patreon_shows, same_feed};
|
||||
pub use privacy::{redact, redact_text, url_looks_private};
|
||||
|
||||
/// Where another service's API is (WordPress.com's, Patreon's): theirs, or in a test the stand-in
|
||||
/// `test_api` serves.
|
||||
pub(crate) fn api_base(real: &'static str) -> String {
|
||||
#[cfg(test)]
|
||||
{
|
||||
let _ = real;
|
||||
test_api::base()
|
||||
}
|
||||
#[cfg(not(test))]
|
||||
real.to_owned()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) mod test_api {
|
||||
use std::io::{BufRead, BufReader, Write};
|
||||
|
||||
/// A stand-in for WordPress.com's and Patreon's APIs, and for the feeds that send ipx to them,
|
||||
/// on a thread of its own so it outlives any one test's runtime. By path:
|
||||
/// `/challenged` answers as WordPress.com's browser check does, `/wp/v2/sites/<host>/posts`
|
||||
/// with one post, `/creator` as a Patreon creator feed of campaign 12345, and
|
||||
/// `/api/campaigns/12345` with its two shows. Anything else is a 404.
|
||||
pub(crate) fn base() -> String {
|
||||
static AT: std::sync::OnceLock<String> = std::sync::OnceLock::new();
|
||||
AT.get_or_init(|| {
|
||||
let l = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
|
||||
let at = format!("http://{}", l.local_addr().unwrap());
|
||||
std::thread::spawn(move || {
|
||||
for mut sock in l.incoming().flatten() {
|
||||
let mut line = String::new();
|
||||
let _ = BufReader::new(&sock).read_line(&mut line);
|
||||
let path = line.split_whitespace().nth(1).unwrap_or("/").to_owned();
|
||||
let (status, body) = answer(&path);
|
||||
let _ = write!(
|
||||
sock,
|
||||
"HTTP/1.1 {status}\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}",
|
||||
body.len()
|
||||
);
|
||||
}
|
||||
});
|
||||
at
|
||||
})
|
||||
.clone()
|
||||
}
|
||||
|
||||
fn answer(path: &str) -> (&'static str, String) {
|
||||
if path.starts_with("/challenged") {
|
||||
("403 Forbidden", r#"<html><script src="/__challenge/check.js"></script></html>"#.into())
|
||||
} else if path.starts_with("/wp/v2/sites/127.0.0.1/posts") {
|
||||
let post = r#"[{"date_gmt":"2026-10-09T13:34:00","guid":{"rendered":"https://blog.test/?p=1"},
|
||||
"link":"https://blog.test/hello/","title":{"rendered":"Hello"},"content":{"rendered":"<p>Hi</p>"}}]"#;
|
||||
("200 OK", post.into())
|
||||
} else if path.starts_with("/creator") {
|
||||
let feed = r#"<?xml version="1.0"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel>
|
||||
<atom:link href="https://www.patreon.com/rss/12345?auth=x" rel="self"/><title>Creator</title></channel></rss>"#;
|
||||
("200 OK", feed.into())
|
||||
} else if path.starts_with("/api/campaigns/12345?") {
|
||||
let shows = r#"{"data":{"attributes":{"name":"Glass Cannon"},
|
||||
"relationships":{"shows":{"data":[{"id":"7","type":"collection"},{"id":"8","type":"collection"}]}}},
|
||||
"included":[{"id":"7","type":"collection","attributes":{"title":" Main Show "}}]}"#;
|
||||
("200 OK", shows.into())
|
||||
} else {
|
||||
("404 Not Found", String::new())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,13 +22,17 @@ pub fn is_patreon_creator(url: &str) -> bool {
|
||||
matches!(patreon_parts(url), Some((_, None)))
|
||||
}
|
||||
|
||||
/// Whether two URLs are the same feed. One Patreon show has several spellings -- by the
|
||||
/// creator's name, by number, or with no creator at all -- and the token and show are what
|
||||
/// identify it.
|
||||
/// Whether two URLs are the same feed. The same address over http and https is one feed:
|
||||
/// oldvcr's blog was subscribed under both, every post twice (#362). One Patreon show has several
|
||||
/// spellings -- by the creator's name, by number, or with no creator at all -- and the token and
|
||||
/// show are what identify it.
|
||||
#[must_use]
|
||||
pub fn same_feed(a: &str, b: &str) -> bool {
|
||||
// Parsing both is the slow part, and only Patreon's addresses can differ and still match.
|
||||
a == b
|
||||
fn bare(u: &str) -> &str {
|
||||
u.strip_prefix("https://").or_else(|| u.strip_prefix("http://")).unwrap_or(u)
|
||||
}
|
||||
// Parsing both is the slow part, and only Patreon's addresses can differ further and still match.
|
||||
bare(a) == bare(b)
|
||||
|| (a.contains("patreon.com")
|
||||
&& b.contains("patreon.com")
|
||||
&& patreon_parts(a).is_some_and(|p| Some(p) == patreon_parts(b)))
|
||||
@@ -54,8 +58,8 @@ pub async fn patreon_shows(client: &reqwest::Client, url: &str) -> Result<(Optio
|
||||
let campaign = patreon_campaign(&head).ok_or_else(|| anyhow!("the Patreon feed does not say whose it is"))?;
|
||||
|
||||
let api = format!(
|
||||
"https://www.patreon.com/api/campaigns/{campaign}\
|
||||
?include=shows&fields%5Bcampaign%5D=name&fields%5Bcollection%5D=title"
|
||||
"{}/api/campaigns/{campaign}?include=shows&fields%5Bcampaign%5D=name&fields%5Bcollection%5D=title",
|
||||
crate::api_base("https://www.patreon.com")
|
||||
);
|
||||
let resp = client.get(api).send().await.context("asking Patreon for the shows")?;
|
||||
if !resp.status().is_success() {
|
||||
@@ -102,6 +106,21 @@ mod tests {
|
||||
use super::*;
|
||||
use crate::discover;
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_creators_shows_are_asked_of_patreon_by_the_campaign_its_feed_names() {
|
||||
let client = reqwest::Client::new();
|
||||
let creator = format!("{}/creator?auth=x", crate::test_api::base());
|
||||
let (name, shows) = patreon_shows(&client, &creator).await.unwrap();
|
||||
assert_eq!(name.as_deref(), Some("Glass Cannon"));
|
||||
assert_eq!(
|
||||
shows,
|
||||
[("Main Show".to_owned(), format!("{creator}&show=7")), ("Show 8".to_owned(), format!("{creator}&show=8"))],
|
||||
"each show is the creator feed with its own show, on the same credentials"
|
||||
);
|
||||
let nobody = format!("{}/nothing?auth=x", crate::test_api::base());
|
||||
assert!(format!("{:#}", patreon_shows(&client, &nobody).await.unwrap_err()).contains("HTTP 404"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_patreon_creator_is_a_list_of_its_shows() {
|
||||
let tok = "AbCdEfGhIjKlMnOpQrStUvWxYz012_-9";
|
||||
@@ -117,6 +136,13 @@ mod tests {
|
||||
assert!(!is_patreon_creator(&format!("https://www.patreon.com/rss/x?auth={tok}&show=1")), "one show is a feed");
|
||||
assert!(!is_patreon_creator(&format!("https://example.com/rss?auth={tok}")));
|
||||
|
||||
assert!(same_feed(
|
||||
"http://oldvcr.blogspot.com/feeds/posts/default",
|
||||
"https://oldvcr.blogspot.com/feeds/posts/default"
|
||||
));
|
||||
assert!(!same_feed("https://a.example/feed", "https://b.example/feed"));
|
||||
assert!(!same_feed("https://a.example/feed", "https://a.example/feed/comments"));
|
||||
|
||||
// The show you already have by name is the one a bare token would add by number.
|
||||
assert!(same_feed(
|
||||
&format!("https://www.patreon.com/rss/glasscannon?auth={tok}&show=2073588"),
|
||||
|
||||
@@ -15,7 +15,7 @@ pub(crate) fn is_wpcom_challenge(body: &[u8]) -> bool {
|
||||
/// shortcut: the API's default page of ten posts, as many as WordPress puts in a feed; a feed
|
||||
/// set to carry more gets ten here.
|
||||
pub(crate) async fn wpcom_posts(client: &reqwest::Client, host: &str) -> Result<Fetched> {
|
||||
let api = format!("https://public-api.wordpress.com/wp/v2/sites/{host}/posts");
|
||||
let api = format!("{}/wp/v2/sites/{host}/posts", crate::api_base("https://public-api.wordpress.com"));
|
||||
let resp = client
|
||||
.get(&api)
|
||||
.timeout(fetch::FEED_TIMEOUT)
|
||||
@@ -61,6 +61,19 @@ pub(crate) fn from_wp_posts(posts: &[serde_json::Value]) -> ParsedFeed {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_browser_check_in_place_of_the_feed_is_answered_from_wordpress_coms_api() {
|
||||
let client = reqwest::Client::new();
|
||||
let (got, moved) =
|
||||
crate::fetch(&client, &format!("{}/challenged/feed", crate::test_api::base()), None, None, None)
|
||||
.await
|
||||
.unwrap();
|
||||
let crate::Fetched::Body { bytes, .. } = got else { panic!("a body") };
|
||||
let feed = super::parse(&bytes).unwrap();
|
||||
assert_eq!(feed.entries[0].guid, "https://blog.test/?p=1", "the post the site's own feed would have");
|
||||
assert_eq!((feed.entries[0].title.as_deref(), moved), (Some("Hello"), None));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reads_wordpress_posts_as_a_feed() {
|
||||
// As WordPress.com's API sends them, trimmed (#317).
|
||||
|
||||
@@ -12,6 +12,46 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
### Added
|
||||
|
||||
- `GET /api/feeds/{id}/seasons` gives a podcast's seasons with how many episodes and unread each has, and a podcast's episodes can be asked for one season at a time, or for those naming none (`season=none`), and in its own order (`sort=episode`), season by season and episode by episode. `download-latest` takes a `season` to download all of it, first episode first (#482).
|
||||
- Smart playlists: `GET` and `POST /api/playlists`, `PATCH` and `DELETE /api/playlists/{id}`, and `GET /api/playlists/{id}/entries`, the episodes of the podcasts chosen in the playlist's order, those set to play first ahead of the rest (#476).
|
||||
- A feed in `/api/feeds` says whether it has audio or video (`podcast`), for the page's Podcasts shelf (#474).
|
||||
- Up Next, a queue of episodes kept per account so every device shares it: `GET /api/queue` lists it in order, `PUT /api/queue` sets it, and an episode leaves it once it is 90% played (#473).
|
||||
- The API answers in protobuf as well as JSON: a client that sends `Accept: application/x-protobuf` gets protobuf, and one that sends `Content-Type: application/x-protobuf` may send its body so; JSON stays the default for a client that does not ask, and an answer of null stays JSON. The messages are in `crates/wire/proto/ipx.proto`. ipx's own calls to the directory and ipx-play go in protobuf too, so both need their protobuf releases first. Answers and those calls go through structs generated from the `.proto` files, about four times quicker to read than the JSON (#465, #470).
|
||||
- Sharing an episode gives a link to its page on ipx-play, which anyone can open and play, set up in Admin, Directory, Sharing episodes; private and paid feeds still share their own address (#447).
|
||||
- The iOS app's icon count stays right with the app closed: with `[push]` set to an APNs key, ipx sends each registered phone (`POST /api/push/devices`, removed with `DELETE`) its All Subscriptions unread count through Apple's push service, within a minute of a change (#433). The push also wakes the app for a moment, for its widget to show the same count (#428).
|
||||
- A place in an episode saved from DeadAir's shortened version names its file (`deadair` in `POST .../position`), so it is read in that version's clock even when ipx never handed out its playlist, as for a copy kept on the device or after a restart (#412).
|
||||
- `/api/art` also serves the pictures in the text of items iPX has sent, through its artwork cache, so the page can keep them to read offline (#389).
|
||||
- The log has a line for each swipe in the page's reader, saying what came of it and where the reader stood, and a warning when a drag would not scroll the reader, with what lay over it (#384).
|
||||
- `/metrics` says how long statements take and, for hand-written SQL, how long they waited for a connection (#367); counts feed failures by kind, new items, downloads saved and failed, and the silence DeadAir found (#368); and holds the page's own timings, a click to its list, play to sound, a stall (#369).
|
||||
- Traces for what had none: each command the worker runs, from the CLI, the socket or the page, with its job inside (#373); the daemon's start, from opening the database to serving (#374); a list's sync and an OPML upload, step by step (#372); and the tap or key in the page that set a trace's requests off, as its root span, from the tap to its last answer (#376).
|
||||
|
||||
### Changed
|
||||
|
||||
- The server's command is `ipxd`, not `ipx`: `ipxd daemon`, `ipxd status`, `ipxd user add` and the rest, in the container as `docker exec iPX ipxd ...`. `ipx` is now the reader in a terminal (#437).
|
||||
|
||||
- The log no longer has a line for every request that went well, over half its size: `/metrics` counts each request and its time by route and status as `ipx_http_request_duration_seconds`, a request that failed keeps its warning, and the admin page's log view still shows them all (#364).
|
||||
- The log no longer has a line for every scan that finishes, a fifth of its lines; the metrics count scans, and the page's log view still shows them (#363).
|
||||
|
||||
### Fixed
|
||||
|
||||
- A feed an OPML stops listing leaves All Subscriptions with it, rather than its items staying there with no feed name beside them: the scan no longer reads it again after the list drops it, and its subscriptions go with it, as they do when a feed is removed with `ipx rm`. Any left from before are removed when ipx starts (#478).
|
||||
- Searching All Subscriptions answers in a fraction of a second, not five: a search reads each item's title and text without its markup, indexed on Postgres, and still finds part of a word. It no longer matches words inside the HTML's tags, such as a link's address. Text past the first 4,000 characters of a very long post is not searched (#459, #472).
|
||||
- Artwork that cannot be shown is asked for again a day later rather than every hour: one too large is refused on its stated size before it is read, and a refusal from the publisher or one ipx makes (too large, not an image) is remembered for a day; JPEG XL pictures sent without a type are shown (#448).
|
||||
- `ipx reap --dry-run` says "would delete" of each file and of the total, rather than "deleted", word for word a real reap, of files it kept (#461).
|
||||
- Subscribing to a feed someone here already reads puts its recent items to your Signals at once, rather than at the judge's next pass, up to five minutes later (#415).
|
||||
- A download a restart stopped, a deploy's for one, is taken up again as the daemon starts, rather than waiting for its feed's next check, an hour or a day away (#410).
|
||||
- A YouTube channel no longer misses most days' videos: YouTube answers some feed requests with a 404 or 500 it does not mean, and ipx now asks again, twice, a second apart, before calling the feed failing (#382), and then every hour for a day where it would have waited a day, since YouTube's failures run for hours; and its YouTube feeds are read one at a time, two seconds apart, since YouTube refuses many asked together (#386).
|
||||
- Syncing an OPML or Patreon list reads who subscribes to it once, not once for every feed it lists: a list of 922 was thousands of the same queries a sync (#377).
|
||||
- A feed you already have, added again by the same address over http rather than https or the other way round, is the one you have, not a second subscription with every item twice. Changing a feed's address to another feed's over the other scheme is refused the same way (#362).
|
||||
- A podcast that was once in an OPML and is now a feed of its own is no longer hidden from All Subscriptions when that OPML is, so its unread items are listed there as the feed list counts them (#358).
|
||||
|
||||
### Security
|
||||
|
||||
- Show notes are cleaned with ammonia 4.2.5, which no longer lets SVG or MathML in a feed's HTML carry script past it (RUSTSEC-2026-0533); Markdown items are read with pulldown-cmark 0.13.6, and compressed feeds and answers with zstd 0.14.4 and zstd-safe 8.3, each closing published advisories (#467).
|
||||
- Ten wrong passwords from one address in a quarter hour hold that address off sign-in for the rest of it, answered `429` with how long to wait; before, a guesser could try as fast as the server checked. The address is the one a trusted proxy names last in `X-Forwarded-For`, or the peer's (#409).
|
||||
|
||||
## [1.5.1] - 2026-10-24
|
||||
|
||||
### Changed
|
||||
|
||||
@@ -1,15 +1,23 @@
|
||||
[package]
|
||||
name = "ipx"
|
||||
version = "1.5.1"
|
||||
version = "1.5.2-dev"
|
||||
edition.workspace = true
|
||||
publish = false
|
||||
|
||||
# ipxd: `ipx` is the reader in a terminal (crates/tui), which more people type (#437). The
|
||||
# code is the library, still `ipx`, so its log targets keep their names (src/lib.rs).
|
||||
[[bin]]
|
||||
name = "ipxd"
|
||||
path = "src/main.rs"
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
[dependencies]
|
||||
ipx-feed.workspace = true
|
||||
ipx-directory.workspace = true
|
||||
ipx-play.workspace = true
|
||||
ipx-wire.workspace = true
|
||||
ammonia.workspace = true
|
||||
anyhow.workspace = true
|
||||
argon2.workspace = true
|
||||
|
||||
@@ -38,13 +38,14 @@ impl Keys {
|
||||
/// The name the token vouches for, or None: a bad signature, the wrong audience or issuer, an
|
||||
/// expired token, a key that cannot be had, or no email in it (a service token has none).
|
||||
pub async fn verify(&self, client: &reqwest::Client, team: &str, aud: &str, token: &str) -> Option<String> {
|
||||
self.verify_with(team, aud, token, || fetch(client, team)).await
|
||||
let url = certs(team);
|
||||
self.verify_with(team, aud, token, || fetch(client, &url)).await
|
||||
}
|
||||
|
||||
/// Fill the cache before the first request needs it. A failure is only logged: the next
|
||||
/// request tries again, and until one succeeds the proxy sign-in refuses everyone.
|
||||
pub async fn prefetch(&self, client: &reqwest::Client, team: &str) {
|
||||
match fetch(client, team).await {
|
||||
match fetch(client, &certs(team)).await {
|
||||
Ok(set) => self.store(set),
|
||||
Err(e) => tracing::warn!(error = %format!("{e:#}"), "could not fetch Cloudflare Access's signing keys"),
|
||||
}
|
||||
@@ -111,10 +112,14 @@ impl Keys {
|
||||
}
|
||||
}
|
||||
|
||||
async fn fetch(client: &reqwest::Client, team: &str) -> Result<JwkSet> {
|
||||
let url = format!("https://{team}/cdn-cgi/access/certs");
|
||||
/// Where a team publishes its signing keys.
|
||||
fn certs(team: &str) -> String {
|
||||
format!("https://{team}/cdn-cgi/access/certs")
|
||||
}
|
||||
|
||||
async fn fetch(client: &reqwest::Client, url: &str) -> Result<JwkSet> {
|
||||
client
|
||||
.get(&url)
|
||||
.get(url)
|
||||
.timeout(Duration::from_secs(10))
|
||||
.send()
|
||||
.await
|
||||
@@ -126,25 +131,25 @@ async fn fetch(client: &reqwest::Client, team: &str) -> Result<JwkSet> {
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
pub(crate) mod tests {
|
||||
use super::*;
|
||||
use jsonwebtoken::{EncodingKey, Header};
|
||||
|
||||
const TEAM: &str = "team.cloudflareaccess.com";
|
||||
const AUD: &str = "aud-tag";
|
||||
pub(crate) const TEAM: &str = "team.cloudflareaccess.com";
|
||||
pub(crate) const AUD: &str = "aud-tag";
|
||||
|
||||
fn jwks() -> JwkSet {
|
||||
serde_json::from_str(include_str!("../../../tests/data/access-test.jwks.json")).unwrap()
|
||||
}
|
||||
|
||||
fn token(key: &[u8], alg: Algorithm, claims: serde_json::Value) -> String {
|
||||
pub(crate) fn token(key: &[u8], alg: Algorithm, claims: serde_json::Value) -> String {
|
||||
let mut h = Header::new(alg);
|
||||
h.kid = Some("k1".into());
|
||||
let k = if alg == Algorithm::RS256 { EncodingKey::from_rsa_der(key) } else { EncodingKey::from_secret(key) };
|
||||
jsonwebtoken::encode(&h, &claims, &k).unwrap()
|
||||
}
|
||||
|
||||
fn claims(aud: &str, exp_in: i64) -> serde_json::Value {
|
||||
pub(crate) fn claims(aud: &str, exp_in: i64) -> serde_json::Value {
|
||||
let now = jsonwebtoken::get_current_timestamp() as i64;
|
||||
serde_json::json!({
|
||||
"aud": [aud], "iss": format!("https://{TEAM}"), "email": "Rays@SDF1.net",
|
||||
@@ -152,13 +157,45 @@ mod tests {
|
||||
})
|
||||
}
|
||||
|
||||
const SIGNER: &[u8] = include_bytes!("../../../tests/data/access-test.der");
|
||||
pub(crate) const SIGNER: &[u8] = include_bytes!("../../../tests/data/access-test.der");
|
||||
const FORGER: &[u8] = include_bytes!("../../../tests/data/access-forger.der");
|
||||
|
||||
async fn check(keys: &Keys, t: &str) -> Option<String> {
|
||||
keys.verify_with(TEAM, AUD, t, || async { Ok(jwks()) }).await
|
||||
}
|
||||
|
||||
/// Keys holding the test signer's, as if fetched from the team.
|
||||
pub(crate) fn keys() -> Keys {
|
||||
let keys = Keys::default();
|
||||
keys.store(jwks());
|
||||
keys
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn the_keys_are_read_from_the_teams_certs_and_a_failure_is_an_error() {
|
||||
let app = axum::Router::new()
|
||||
.route(
|
||||
"/cdn-cgi/access/certs",
|
||||
axum::routing::get(|| async { include_str!("../../../tests/data/access-test.jwks.json") }),
|
||||
)
|
||||
.route("/down", axum::routing::get(|| async { axum::http::StatusCode::SERVICE_UNAVAILABLE }));
|
||||
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let at = listener.local_addr().unwrap();
|
||||
tokio::spawn(async move { axum::serve(listener, app).await });
|
||||
let client = reqwest::Client::new();
|
||||
|
||||
let set = fetch(&client, &format!("http://{at}/cdn-cgi/access/certs")).await.unwrap();
|
||||
assert_eq!(set.keys.len(), jwks().keys.len());
|
||||
let keys = Keys::default();
|
||||
keys.store(set);
|
||||
let ok = token(SIGNER, Algorithm::RS256, claims(AUD, 300));
|
||||
assert_eq!(check(&keys, &ok).await.as_deref(), Some("rays@sdf1.net"), "the fetched key verifies");
|
||||
|
||||
assert!(fetch(&client, &format!("http://{at}/down")).await.is_err(), "a 503 is not a key set");
|
||||
assert!(fetch(&client, &format!("http://{at}/nothing")).await.is_err(), "nor a 404");
|
||||
assert_eq!(certs(TEAM), "https://team.cloudflareaccess.com/cdn-cgi/access/certs");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn only_a_token_cloudflare_signed_for_this_app_signs_anyone_in() {
|
||||
let keys = Keys::default();
|
||||
|
||||
@@ -1,14 +1,60 @@
|
||||
//! Artwork kept on disk, so the page loads it from iPX: fast after the first time, nothing asked
|
||||
//! of each publisher's server for every visit, and still there when a publisher is not.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::collections::hash_map::DefaultHasher;
|
||||
use std::collections::{HashMap, HashSet, VecDeque};
|
||||
use std::fs::File;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::{LazyLock, Mutex};
|
||||
use std::time::SystemTime;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
/// Pictures in item text ipx has sent, which `/api/art` may then fetch and keep as it does
|
||||
/// artwork, so the page can save them to read offline (#389): only an address some feed's text
|
||||
/// named, as artwork is only one some feed names. The oldest are forgotten past the limit, and
|
||||
/// all of them at a restart, until a list sends them again.
|
||||
static IN_TEXT: LazyLock<Mutex<(HashSet<String>, VecDeque<String>)>> = LazyLock::new(Mutex::default);
|
||||
const IN_TEXT_MAX: usize = 20_000;
|
||||
|
||||
/// Remembers the pictures in a piece of cleaned item text.
|
||||
pub fn name_in_text(html: &str) {
|
||||
let srcs = img_srcs(html);
|
||||
if srcs.is_empty() {
|
||||
return;
|
||||
}
|
||||
let mut known = IN_TEXT.lock().unwrap_or_else(|e| e.into_inner());
|
||||
for src in srcs {
|
||||
if known.0.insert(src.clone()) {
|
||||
known.1.push_back(src);
|
||||
}
|
||||
}
|
||||
while known.1.len() > IN_TEXT_MAX {
|
||||
if let Some(old) = known.1.pop_front() {
|
||||
known.0.remove(&old);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn named_in_text(url: &str) -> bool {
|
||||
IN_TEXT.lock().unwrap_or_else(|e| e.into_inner()).0.contains(url)
|
||||
}
|
||||
|
||||
/// The web addresses of the `<img src>`s in HTML as ammonia writes it: attributes in double
|
||||
/// quotes, `&`, `"` and no-break spaces escaped, and no `srcset`, which it does not let through.
|
||||
fn img_srcs(html: &str) -> Vec<String> {
|
||||
let mut out = Vec::new();
|
||||
for (at, _) in html.match_indices("<img ") {
|
||||
let tag = &html[at..html[at..].find('>').map_or(html.len(), |end| at + end)];
|
||||
let Some(start) = tag.find(" src=\"").map(|i| i + 6) else { continue };
|
||||
let Some(len) = tag[start..].find('"') else { continue };
|
||||
let src = tag[start..start + len].replace(""", "\"").replace(" ", "\u{a0}").replace("&", "&");
|
||||
if src.starts_with("https://") || src.starts_with("http://") {
|
||||
out.push(src);
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
pub fn dir() -> PathBuf {
|
||||
crate::config::data_dir().join("art")
|
||||
}
|
||||
@@ -46,6 +92,10 @@ pub fn write(file: &Path, kind: &str, body: &[u8]) -> std::io::Result<()> {
|
||||
|
||||
/// How long an address that could not be fetched is answered from memory before it is tried again.
|
||||
const RETRY: Duration = Duration::from_secs(60 * 60);
|
||||
/// The same for a failure the publisher will not change by morning: too large, not an image, a
|
||||
/// 4xx. At an hour, five GIFs over the limit were read 20 MB at a time 37 times each in five days,
|
||||
/// about 3 GB for nothing (#448).
|
||||
const RETRY_LASTING: Duration = Duration::from_secs(24 * 60 * 60);
|
||||
|
||||
/// Addresses that could not be fetched lately, and why. Without it a page shown again fetched
|
||||
/// them again: five covers too big to keep were downloaded from Patreon on every load (#169).
|
||||
@@ -53,7 +103,7 @@ const RETRY: Duration = Duration::from_secs(60 * 60);
|
||||
// one per artwork address a feed or item names; move it to disk or prune it on trim if that grows.
|
||||
static FAILED: LazyLock<Mutex<HashMap<String, (Instant, String)>>> = LazyLock::new(Default::default);
|
||||
|
||||
/// Why `url` could not be fetched, if it failed less than an hour ago.
|
||||
/// Why `url` could not be fetched, if it failed lately: an hour, or a day for a lasting failure.
|
||||
pub fn failed(url: &str) -> Option<String> {
|
||||
failed_as_of(url, Instant::now())
|
||||
}
|
||||
@@ -61,7 +111,7 @@ pub fn failed(url: &str) -> Option<String> {
|
||||
fn failed_as_of(url: &str, now: Instant) -> Option<String> {
|
||||
let mut map = FAILED.lock().unwrap_or_else(|e| e.into_inner());
|
||||
match map.get(url) {
|
||||
Some((at, why)) if now.duration_since(*at) < RETRY => Some(why.clone()),
|
||||
Some((until, why)) if now < *until => Some(why.clone()),
|
||||
Some(_) => {
|
||||
map.remove(url);
|
||||
None
|
||||
@@ -70,8 +120,9 @@ fn failed_as_of(url: &str, now: Instant) -> Option<String> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn fail(url: &str, why: &str) {
|
||||
FAILED.lock().unwrap_or_else(|e| e.into_inner()).insert(url.to_owned(), (Instant::now(), why.to_owned()));
|
||||
pub fn fail(url: &str, why: &str, lasting: bool) {
|
||||
let until = Instant::now() + if lasting { RETRY_LASTING } else { RETRY };
|
||||
FAILED.lock().unwrap_or_else(|e| e.into_inner()).insert(url.to_owned(), (until, why.to_owned()));
|
||||
}
|
||||
|
||||
/// Removes the least recently used images until what is kept fits in `limit` bytes; 0 empties it.
|
||||
@@ -107,6 +158,15 @@ pub fn trim(dir: &Path, limit: u64) -> usize {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
fn the_pictures_in_cleaned_text_are_named_and_nothing_else() {
|
||||
let html = r#"<p>a</p><img src="https://img.example/a.jpg?w=1&h=2" alt="x" referrerpolicy="no-referrer"><img alt="no src"><img src="data:image/png;base64,xx"><a href="https://img.example/b.jpg">b</a>"#;
|
||||
assert_eq!(super::img_srcs(html), ["https://img.example/a.jpg?w=1&h=2"]);
|
||||
super::name_in_text(html);
|
||||
assert!(super::named_in_text("https://img.example/a.jpg?w=1&h=2"));
|
||||
assert!(!super::named_in_text("https://img.example/b.jpg"));
|
||||
}
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
@@ -129,13 +189,16 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_failure_is_remembered_for_an_hour() {
|
||||
fn a_failure_is_remembered_for_an_hour_and_a_lasting_one_for_a_day() {
|
||||
let u = "https://example.com/a-failure-is-remembered.png";
|
||||
assert_eq!(failed(u), None);
|
||||
fail(u, "larger than 5 MB");
|
||||
assert_eq!(failed(u).as_deref(), Some("larger than 5 MB"));
|
||||
fail(u, "timed out", false);
|
||||
assert_eq!(failed(u).as_deref(), Some("timed out"));
|
||||
assert_eq!(failed_as_of(u, Instant::now() + RETRY), None);
|
||||
// Asked for after the hour, it is forgotten, not just ignored.
|
||||
assert_eq!(failed(u), None);
|
||||
fail(u, "larger than 20 MB", true);
|
||||
assert_eq!(failed_as_of(u, Instant::now() + RETRY).as_deref(), Some("larger than 20 MB"));
|
||||
assert_eq!(failed_as_of(u, Instant::now() + RETRY_LASTING), None);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@ pub(crate) async fn add(
|
||||
}
|
||||
|
||||
/// Adds the feed at `url` to the catalogue unless it is there already, a feed an OPML lists
|
||||
/// included, so one show is never added twice: its id, and whether it was there. For `ipx add`
|
||||
/// included, so one show is never added twice: its id, and whether it was there. For `ipxd add`
|
||||
/// and the page's Add feed alike, which were the same steps written twice (#161); each finds the
|
||||
/// feed first, to say its own way why an address is not one.
|
||||
pub async fn add_to_catalogue(
|
||||
@@ -136,6 +136,10 @@ pub(crate) async fn rm(ctx: &Ctx, feed: &str) -> Result<()> {
|
||||
return Ok(());
|
||||
};
|
||||
ctx.store_cfg(cfg).await?;
|
||||
// Left subscribed, All Subscriptions listed its items with no feed to name them (#478).
|
||||
for (user, _) in ctx.db.subscribers_of(feed).await? {
|
||||
ctx.db.unsubscribe(user, feed).await?;
|
||||
}
|
||||
// State and files stay: re-adding the feed should not re-download its back catalogue.
|
||||
println!("removed {feed}; downloads and history kept");
|
||||
retire_group(ctx, feed, Some(&parent)).await?;
|
||||
@@ -151,7 +155,7 @@ pub(crate) async fn import(ctx: &Ctx, file: &Path) -> Result<()> {
|
||||
.await?
|
||||
.into_iter()
|
||||
.find(|u| u.is_admin)
|
||||
.ok_or_else(|| anyhow::anyhow!("no admin account to subscribe: ipx user add <name> --admin"))?;
|
||||
.ok_or_else(|| anyhow::anyhow!("no admin account to subscribe: ipxd user add <name> --admin"))?;
|
||||
let doc = feed::read_opml(&text).map_err(|e| anyhow::anyhow!("{} is not OPML: {e}", file.display()))?;
|
||||
let (added, had) = lists::subscribe_opml(ctx, lists::opml_feeds(&doc)?, admin.id).await?;
|
||||
println!("subscribed {} to {added} feed(s); {had} already there", admin.name);
|
||||
@@ -168,7 +172,7 @@ pub(crate) async fn assemble_config(db: &db::Db, mut cfg: config::Config, path:
|
||||
if config::Config::file_holds_stored(path) {
|
||||
tracing::warn!(
|
||||
"config.toml still lists feeds or server settings; they are ignored, since the \
|
||||
database holds them now. Change them in the web UI, or with ipx add and rm."
|
||||
database holds them now. Change them in the web UI, or with ipxd add and rm."
|
||||
);
|
||||
}
|
||||
stored.apply(&mut cfg);
|
||||
@@ -213,13 +217,13 @@ pub(crate) async fn export(ctx: &Ctx, file: &std::path::Path) -> Result<()> {
|
||||
pub(crate) async fn list(ctx: &Ctx) -> Result<()> {
|
||||
let cfg = ctx.cfg();
|
||||
if cfg.feeds.is_empty() {
|
||||
println!("No feeds configured. Add one with `ipx add <url>`, or in the web UI.");
|
||||
println!("No feeds configured. Add one with `ipxd add <url>`, or in the web UI.");
|
||||
return Ok(());
|
||||
}
|
||||
for (id, feed) in &cfg.feeds {
|
||||
let s = ctx.db.feed_summary(id).await?;
|
||||
// The id on a line of its own, labelled: first on the title's line, antirez.com's id
|
||||
// "feed" read as a heading and 'ipx fetch antirez' was tried instead (issue #82).
|
||||
// "feed" read as a heading and 'ipxd fetch antirez' was tried instead (issue #82).
|
||||
println!("{}", s.title.as_deref().filter(|t| !t.is_empty()).unwrap_or(id));
|
||||
println!(" id {id}");
|
||||
println!(" url {}", feed.url);
|
||||
@@ -406,7 +410,7 @@ pub async fn retire_group(ctx: &Ctx, parent_id: &str, parent: Option<&config::Fe
|
||||
if cfg.feeds.contains_key(&m.id) {
|
||||
// Scanned from its config entry and still read. Dropped as derived, its stored
|
||||
// entries would go with it: davewiner's 11 were promoted without being unmanaged.
|
||||
ctx.db.unmanage(&m.id).await?;
|
||||
ctx.db.unmanage(&m.id, cfg.feeds[&m.id].group.as_deref()).await?;
|
||||
} else if promote_kept(ctx, &m, parent).await? {
|
||||
tracing::info!(feed = %m.id, "its list is gone but someone kept it; a feed of its own now");
|
||||
} else if ctx.db.downloaded_count(&m.id).await.unwrap_or(1) > 0 {
|
||||
@@ -450,8 +454,9 @@ pub(crate) async fn promote_kept(ctx: &Ctx, m: &db::Managed, base: Option<&confi
|
||||
// kept show went to the directory (#332). A list no longer known may have been private.
|
||||
private: base.is_none_or(|p| p.private),
|
||||
});
|
||||
let group = cfg.feeds[&m.id].group.clone();
|
||||
ctx.store_cfg(cfg).await?;
|
||||
ctx.db.unmanage(&m.id).await?;
|
||||
ctx.db.unmanage(&m.id, group.as_deref()).await?;
|
||||
ctx.db.set_orphaned(&m.id, false).await?;
|
||||
for (user, kept) in subs {
|
||||
if !kept {
|
||||
@@ -476,6 +481,43 @@ pub(crate) async fn retire_stranded(ctx: &Ctx) -> Result<usize> {
|
||||
Ok(before.len() - ctx.db.managed_feeds().await?.len())
|
||||
}
|
||||
|
||||
/// Unsubscribes everyone from feeds the catalogue no longer has, and returns how many feeds that
|
||||
/// was. Removing a feed once left its subscriptions behind, 41 of them by 2026-11-19, and All
|
||||
/// Subscriptions listed their items with no feed to name them (#478).
|
||||
pub(crate) async fn drop_stray_subscriptions(ctx: &Ctx) -> Result<usize> {
|
||||
let known: std::collections::HashSet<String> = subscriptions(ctx).await?.into_iter().map(|s| s.id).collect();
|
||||
// A catalogue that came back empty is more likely a fault than a server with no feeds, and
|
||||
// taking it at its word would unsubscribe everyone from everything.
|
||||
if known.is_empty() {
|
||||
return Ok(0);
|
||||
}
|
||||
let mut dropped = 0;
|
||||
for feed in ctx.db.subscriber_counts().await?.into_keys().filter(|f| !known.contains(f)) {
|
||||
for (user, _) in ctx.db.subscribers_of(&feed).await? {
|
||||
ctx.db.unsubscribe(user, &feed).await?;
|
||||
}
|
||||
tracing::info!(feed = %feed, "unsubscribed everyone from a feed no longer in the catalogue");
|
||||
dropped += 1;
|
||||
}
|
||||
Ok(dropped)
|
||||
}
|
||||
|
||||
/// Puts each feed no longer derived in the group its catalogue entry says, and returns how many
|
||||
/// moved. Made a feed of its own before #358, a show kept its list's group in the database, and
|
||||
/// hiding the list from All Subscriptions hid it too.
|
||||
pub(crate) async fn regroup_unmanaged(ctx: &Ctx) -> Result<usize> {
|
||||
let cfg = ctx.cfg();
|
||||
let mut moved = 0;
|
||||
for (id, group) in ctx.db.unmanaged_in_groups().await? {
|
||||
let Some(f) = cfg.feeds.get(&id) else { continue };
|
||||
if f.group.as_deref() != Some(group.as_str()) {
|
||||
ctx.db.unmanage(&id, f.group.as_deref()).await?;
|
||||
moved += 1;
|
||||
}
|
||||
}
|
||||
Ok(moved)
|
||||
}
|
||||
|
||||
/// A feed that answered from a new address after permanent redirects moves there in the
|
||||
/// catalogue, so it is read from there and no longer redirected every time. One an OPML lists
|
||||
/// is left alone, since the OPML would only put the old address back; so is a move onto an
|
||||
@@ -499,6 +541,58 @@ mod tests {
|
||||
use super::*;
|
||||
use crate::test_util::{feed, test_ctx};
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_feed_that_moved_for_good_takes_its_new_address_unless_another_feed_has_it() {
|
||||
let mut cfg = config::Config::default();
|
||||
for id in ["moving", "other"] {
|
||||
cfg.feeds.insert(id.into(), config::Feed { url: format!("http://x/{id}.xml"), ..feed() });
|
||||
}
|
||||
let ctx = test_ctx(cfg).await;
|
||||
ctx.db.store_config(&ctx.cfg()).await.unwrap();
|
||||
ctx.db.exec_for_test("INSERT INTO users (id, name, is_admin) VALUES (1, 'dee', false); INSERT INTO subscriptions (user_id, feed_id) VALUES (1, 'moving');").await.unwrap();
|
||||
|
||||
follow_move(&ctx, "moving", "https://x/new.xml").await.unwrap();
|
||||
assert_eq!(ctx.cfg().feeds["moving"].url, "https://x/new.xml");
|
||||
let stored = ctx.db.stored_config().await.unwrap().unwrap().1;
|
||||
assert_eq!(stored["moving"].url, "https://x/new.xml", "kept, not only in memory");
|
||||
// The id stays, so whoever subscribed keeps it, with their read state.
|
||||
assert_eq!(ctx.db.i64s_for_test("SELECT user_id FROM subscriptions WHERE feed_id = 'moving'").await, [1]);
|
||||
|
||||
// Onto an address another feed has: two would be the same feed under two ids.
|
||||
follow_move(&ctx, "moving", "https://x/other.xml").await.unwrap();
|
||||
assert_eq!(ctx.cfg().feeds["moving"].url, "https://x/new.xml", "left where it was");
|
||||
follow_move(&ctx, "from-an-opml", "https://x/elsewhere.xml").await.unwrap();
|
||||
assert!(!ctx.cfg().feeds.contains_key("from-an-opml"), "its OPML would only put it back");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn ipx_add_and_rm_change_the_catalogue() {
|
||||
let base = crate::test_util::empty_or_feed_server().await;
|
||||
let ctx = test_ctx(config::Config::default()).await;
|
||||
let url = format!("{base}/feed");
|
||||
add(&ctx, &url, Some("Shows".into()), vec!["rust".into()], None, false).await.unwrap();
|
||||
let (id, f) = ctx.cfg().feeds.iter().next().map(|(id, f)| (id.clone(), f.clone())).expect("added");
|
||||
assert_eq!(id, "t", "named for its title, T");
|
||||
assert_eq!(
|
||||
(f.url.as_str(), f.folder.as_deref(), f.keywords.as_slice()),
|
||||
(url.as_str(), Some("Shows"), &["rust".to_owned()][..])
|
||||
);
|
||||
assert!(!f.listed);
|
||||
|
||||
let again = add(&ctx, &url, None, vec![], None, false).await.unwrap_err();
|
||||
assert!(format!("{again:#}").contains("already subscribed"), "{again:#}");
|
||||
// Asking again to list it, or with a category, changes only that.
|
||||
add(&ctx, &url, None, vec![], Some("News".into()), true).await.unwrap();
|
||||
let f = &ctx.cfg().feeds[&id];
|
||||
assert!(f.listed && f.category.as_deref() == Some("News") && f.folder.as_deref() == Some("Shows"));
|
||||
assert_eq!(ctx.cfg().feeds.len(), 1);
|
||||
|
||||
rm(&ctx, &id).await.unwrap();
|
||||
assert!(ctx.cfg().feeds.is_empty());
|
||||
let stored = ctx.db.stored_config().await.unwrap().map(|s| s.1).unwrap_or_default();
|
||||
assert!(stored.is_empty(), "gone from the database too");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn the_first_start_moves_the_configuration_in_and_trims_the_file() {
|
||||
let dir = std::env::temp_dir().join(format!("ipx-assemble-{}", std::process::id()));
|
||||
@@ -594,6 +688,70 @@ mod tests {
|
||||
assert!(ctx.db.feed_summary("has-file").await.unwrap().orphaned, "and flagged as orphaned");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_feed_added_again_over_the_other_scheme_is_the_one_already_there() {
|
||||
// oldvcr's blog, subscribed over https and again over http: every post twice (#362).
|
||||
let mut cfg = config::Config::default();
|
||||
let mut f = feed();
|
||||
f.url = "https://oldvcr.blogspot.com/feeds/posts/default".into();
|
||||
cfg.feeds.insert("oldvcr".into(), f);
|
||||
let ctx = test_ctx(cfg).await;
|
||||
let got = add_to_catalogue(&ctx, "http://oldvcr.blogspot.com/feeds/posts/default", None, vec![]).await.unwrap();
|
||||
assert_eq!(got, ("oldvcr".to_owned(), true));
|
||||
assert_eq!(ctx.cfg().feeds.len(), 1);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_show_made_a_feed_of_its_own_is_not_hidden_with_its_list() {
|
||||
// Unsung: promoted out of a Hacker News OPML hidden from All Subscriptions, its unread
|
||||
// counted in the feed list and missing from All Subscriptions (#358).
|
||||
let mut cfg = config::Config::default();
|
||||
cfg.feeds.insert("opml".into(), feed());
|
||||
let ctx = test_ctx(cfg).await;
|
||||
ctx.db
|
||||
.exec_for_test(
|
||||
"INSERT INTO users (id, name, is_admin) VALUES (1,'a',true);
|
||||
INSERT INTO feeds (id, url) VALUES ('opml','u0');
|
||||
INSERT INTO subscriptions (user_id, feed_id, kept) VALUES (1,'opml',false), (1,'show',true), (1,'old',true);
|
||||
INSERT INTO entries (feed_id, guid, first_seen) VALUES ('show','1',1), ('old','2',2);",
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
ctx.db.upsert_managed("show", "http://x/s.xml", "Show", "opml").await.unwrap();
|
||||
ctx.db.upsert_managed("old", "http://x/o.xml", "Old", "opml").await.unwrap();
|
||||
ctx.db.set_hide_from_all(1, "opml", true).await.unwrap();
|
||||
let unread = async || ctx.db.count_of(1, None, db::Filter::Unread, None).await.unwrap();
|
||||
assert_eq!(unread().await, 0, "the list's shows are hidden with it");
|
||||
|
||||
let show = ctx.db.managed_feeds().await.unwrap().into_iter().find(|m| m.id == "show").unwrap();
|
||||
assert!(promote_kept(&ctx, &show, None).await.unwrap());
|
||||
assert_eq!(unread().await, 1, "a feed of its own is in no list");
|
||||
|
||||
// Promoted before the fix: unmanaged, still in the group, and in no group in config.
|
||||
ctx.db.exec_for_test("UPDATE feeds SET managed = false WHERE id = 'old'").await.unwrap();
|
||||
let mut cfg = ctx.edit_cfg().await;
|
||||
cfg.feeds.insert("old".into(), feed());
|
||||
ctx.store_cfg(cfg).await.unwrap();
|
||||
assert_eq!(regroup_unmanaged(&ctx).await.unwrap(), 1);
|
||||
assert_eq!(unread().await, 2);
|
||||
assert_eq!(regroup_unmanaged(&ctx).await.unwrap(), 0, "nothing left to move");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_subscription_to_a_feed_not_in_the_catalogue_is_dropped() {
|
||||
let mut cfg = config::Config::default();
|
||||
cfg.feeds.insert("opml".into(), feed());
|
||||
let ctx = test_ctx(cfg).await;
|
||||
let me = ctx.db.create_user("me", None, false).await.unwrap();
|
||||
ctx.db.upsert_managed("listed", "http://x/l.xml", "Listed", "opml").await.unwrap();
|
||||
for id in ["opml", "listed", "gone"] {
|
||||
ctx.db.subscribe(me, id).await.unwrap();
|
||||
}
|
||||
assert_eq!(drop_stray_subscriptions(&ctx).await.unwrap(), 1);
|
||||
let mine: Vec<String> = ctx.db.subscriptions_for(me).await.unwrap().into_iter().map(|s| s.feed_id).collect();
|
||||
assert!(mine.contains(&"opml".into()) && mine.contains(&"listed".into()) && !mine.contains(&"gone".into()));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_stranded_group_is_retired_but_a_promoted_feed_keeps_its_entries() {
|
||||
// davewiner: the OPML left config before retire_group existed, and 11 of its feeds
|
||||
@@ -613,4 +771,38 @@ mod tests {
|
||||
assert_eq!(managed, ["listed"], "a group still in config is left alone");
|
||||
assert_eq!(ctx.db.feed_summary("promoted").await.unwrap().entries, 1, "its entries survive");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn an_exported_opml_imports_as_the_admins_subscriptions() {
|
||||
let file = std::env::temp_dir().join(format!("ipx-opml-test-{}.opml", std::process::id()));
|
||||
let from = test_ctx(config::Config::default()).await;
|
||||
list(&from).await.unwrap();
|
||||
let mut cfg = from.edit_cfg().await;
|
||||
for (id, url) in [("one", "http://x/one.xml"), ("two", "http://x/two.xml")] {
|
||||
cfg.feeds
|
||||
.insert(id.into(), crate::test_util::add_one_cfg(&mut config::Config::default(), url, None, vec![]));
|
||||
}
|
||||
from.store_cfg(cfg).await.unwrap();
|
||||
list(&from).await.unwrap();
|
||||
export(&from, &file).await.unwrap();
|
||||
let xml = std::fs::read_to_string(&file).unwrap();
|
||||
assert!(xml.contains("http://x/one.xml") && xml.contains("http://x/two.xml"), "{xml}");
|
||||
|
||||
let to = test_ctx(config::Config::default()).await;
|
||||
let none = import(&to, &file).await.unwrap_err();
|
||||
assert!(format!("{none:#}").contains("no admin account"), "the CLI subscribes the admin: {none:#}");
|
||||
let admin = to.db.create_user("admin", None, true).await.unwrap();
|
||||
import(&to, &file).await.unwrap();
|
||||
let urls: std::collections::BTreeSet<String> = to.cfg().feeds.values().map(|f| f.url.clone()).collect();
|
||||
assert_eq!(urls, ["http://x/one.xml".to_owned(), "http://x/two.xml".to_owned()].into());
|
||||
for id in to.cfg().feeds.keys() {
|
||||
assert_eq!(to.db.subscribers_of(id).await.unwrap(), [(admin, false)], "{id} is the admin's");
|
||||
}
|
||||
import(&to, &file).await.unwrap();
|
||||
assert_eq!(to.cfg().feeds.len(), 2, "importing again adds nothing");
|
||||
|
||||
std::fs::write(&file, "not opml").unwrap();
|
||||
assert!(format!("{:#}", import(&to, &file).await.unwrap_err()).contains("is not OPML"));
|
||||
let _ = std::fs::remove_file(&file);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(name = "ipx", version, about = "A headless podcatcher")]
|
||||
#[command(name = "ipxd", version, about = "A headless podcatcher")]
|
||||
pub(crate) struct Cli {
|
||||
/// Config file (default: $XDG_CONFIG_HOME/ipx/config.toml)
|
||||
#[arg(long, global = true)]
|
||||
@@ -81,7 +81,7 @@ pub(crate) enum Command {
|
||||
|
||||
#[derive(Subcommand)]
|
||||
pub(crate) enum UserCmd {
|
||||
/// Create an account. The password is read from stdin: `echo -n hunter2 | ipx user add ray`
|
||||
/// Create an account. The password is read from stdin: `echo -n hunter2 | ipxd user add ray`
|
||||
Add {
|
||||
name: String,
|
||||
/// May manage other accounts, and is who the shared web token signs in as
|
||||
@@ -104,14 +104,13 @@ pub(crate) enum UserCmd {
|
||||
|
||||
/// Accounts. Passwords come in on stdin so they never reach a shell history or a `ps`
|
||||
/// listing.
|
||||
pub(crate) async fn user_cmd(ctx: &Arc<Ctx>, cmd: UserCmd) -> Result<()> {
|
||||
let read_password = || -> Result<String> {
|
||||
use std::io::Read;
|
||||
pub(crate) async fn user_cmd(ctx: &Arc<Ctx>, cmd: UserCmd, mut stdin: impl std::io::Read) -> Result<()> {
|
||||
let mut read_password = || -> Result<String> {
|
||||
let mut buf = String::new();
|
||||
std::io::stdin().read_to_string(&mut buf)?;
|
||||
stdin.read_to_string(&mut buf)?;
|
||||
let pw = buf.trim_end_matches(['\n', '\r']).to_string();
|
||||
if pw.is_empty() {
|
||||
anyhow::bail!("no password on stdin: try `echo -n secret | ipx user ...`");
|
||||
anyhow::bail!("no password on stdin: try `echo -n secret | ipxd user ...`");
|
||||
}
|
||||
Ok(pw)
|
||||
};
|
||||
@@ -139,7 +138,7 @@ pub(crate) async fn user_cmd(ctx: &Arc<Ctx>, cmd: UserCmd) -> Result<()> {
|
||||
UserCmd::List => {
|
||||
let users = ctx.db.users().await?;
|
||||
if users.is_empty() {
|
||||
println!("no accounts yet: ipx user add <name>");
|
||||
println!("no accounts yet: ipxd user add <name>");
|
||||
}
|
||||
for u in users {
|
||||
let added = u
|
||||
@@ -232,3 +231,50 @@ pub(crate) fn duration(secs: u64) -> String {
|
||||
s => format!("{}d", s / 86_400),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[tokio::test]
|
||||
async fn accounts_are_added_renamed_given_a_password_and_removed() {
|
||||
let ctx = Arc::new(crate::test_util::test_ctx(crate::config::Config::default()).await);
|
||||
let user = |cmd, stdin: &'static str| user_cmd(&ctx, cmd, stdin.as_bytes());
|
||||
let add = |name: &str, no_password| UserCmd::Add { name: name.into(), admin: false, no_password };
|
||||
|
||||
user(add(" Ray ", false), "first secret\n").await.unwrap();
|
||||
let ray = ctx.db.user_by_name("ray").await.unwrap().expect("lower-cased and trimmed");
|
||||
assert!(ray.is_admin, "the first account runs the place");
|
||||
assert!(
|
||||
crate::auth::check_password("first secret".into(), ray.pass_hash.clone()).await,
|
||||
"the newline is not part of it"
|
||||
);
|
||||
user(add("sam", true), "").await.unwrap();
|
||||
let sam = ctx.db.user_by_name("sam").await.unwrap().unwrap();
|
||||
assert!(!sam.is_admin && sam.pass_hash.is_none(), "signs in through the proxy");
|
||||
|
||||
assert!(user(add("RAY", true), "").await.is_err(), "already exists");
|
||||
assert!(user(add(" ", true), "").await.is_err(), "a name is required");
|
||||
assert!(user(add("dee", false), "").await.is_err(), "no password on stdin");
|
||||
user(UserCmd::List, "").await.unwrap();
|
||||
|
||||
// A new password signs every browser out.
|
||||
let sid = crate::auth::new_session_token();
|
||||
ctx.db.create_session(ray.id, &sid).await.unwrap();
|
||||
user(UserCmd::Passwd { name: "ray".into() }, "second secret").await.unwrap();
|
||||
let ray = ctx.db.user_by_name("ray").await.unwrap().unwrap();
|
||||
assert!(crate::auth::check_password("second secret".into(), ray.pass_hash).await);
|
||||
assert!(ctx.db.session_user(&sid, 86_400).await.unwrap().is_none(), "signed out");
|
||||
assert!(user(UserCmd::Passwd { name: "nobody".into() }, "x").await.is_err());
|
||||
|
||||
let rename = |name: &str, new_name: &str| UserCmd::Rename { name: name.into(), new_name: new_name.into() };
|
||||
assert!(user(rename("sam", "ray"), "").await.is_err(), "taken");
|
||||
assert!(user(rename("sam", "a,b"), "").await.is_err(), "the proxy could never send it");
|
||||
user(rename("sam", "Sam@X.test"), "").await.unwrap();
|
||||
assert_eq!(ctx.db.user_by_name("sam@x.test").await.unwrap().map(|u| u.id), Some(sam.id), "same account");
|
||||
|
||||
user(UserCmd::Rm { name: "sam@x.test".into() }, "").await.unwrap();
|
||||
assert!(ctx.db.user_by_name("sam@x.test").await.unwrap().is_none());
|
||||
assert!(user(UserCmd::Rm { name: "sam@x.test".into() }, "").await.is_err(), "already gone");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,8 @@ pub struct Config {
|
||||
pub torrent: Torrent,
|
||||
#[serde(default)]
|
||||
pub web: Web,
|
||||
#[serde(default)]
|
||||
pub push: Push,
|
||||
/// Keyed by feed id: the TOML table name, which replaces the old genHash(feedURL).
|
||||
#[serde(default)]
|
||||
pub feeds: BTreeMap<String, Feed>,
|
||||
@@ -52,6 +54,12 @@ pub struct General {
|
||||
pub directory_token: String,
|
||||
/// Send the directory the public feeds people here subscribe to, and how many of them do.
|
||||
pub directory_share: bool,
|
||||
/// ipx-play, which keeps a shared episode and gives back a link to its page (#447), as
|
||||
/// `https://play.ipxreader.app` or, on its own network, `http://ipx-play:8102`. Empty: Share
|
||||
/// hands over the episode's own address.
|
||||
pub play_url: String,
|
||||
/// The token ipx-play gave this server. Never sent to the page.
|
||||
pub play_token: String,
|
||||
/// Signals are judged on the server's TypeSafe key (`TYPESAFE_KEY`), for everyone, rather than
|
||||
/// on each person's own (#271). Without that key it changes nothing.
|
||||
pub signals_on_server: bool,
|
||||
@@ -73,6 +81,33 @@ pub enum Organize {
|
||||
Date,
|
||||
}
|
||||
|
||||
/// Apple's push service (#433), which keeps the iOS app's icon count exact. Nothing is sent
|
||||
/// unless the key, its id and the team are all set.
|
||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||
#[serde(default)]
|
||||
pub struct Push {
|
||||
/// The APNs key Apple gives once as a `.p8` file (developer account, Keys).
|
||||
pub key_file: PathBuf,
|
||||
/// That key's id, ten characters, shown beside it.
|
||||
pub key_id: String,
|
||||
/// The Apple developer team the app is signed by, as `8743L8D729`.
|
||||
pub team_id: String,
|
||||
/// The app's bundle id, which Apple checks each device token against.
|
||||
pub topic: String,
|
||||
}
|
||||
|
||||
impl Default for Push {
|
||||
fn default() -> Self {
|
||||
Push { key_file: PathBuf::new(), key_id: String::new(), team_id: String::new(), topic: "net.sdf1.ipx".into() }
|
||||
}
|
||||
}
|
||||
|
||||
impl Push {
|
||||
pub fn is_set(&self) -> bool {
|
||||
!self.key_file.as_os_str().is_empty() && !self.key_id.is_empty() && !self.team_id.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||
#[serde(default)]
|
||||
pub struct Torrent {
|
||||
@@ -190,7 +225,7 @@ pub struct Feed {
|
||||
/// Preferred over `password`: name of an env var holding the password.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub password_env: Option<String>,
|
||||
/// Put in the Directory by an admin (`ipx add --list`): listed with no subscribers, and kept
|
||||
/// Put in the Directory by an admin (`ipxd add --list`): listed with no subscribers, and kept
|
||||
/// in the catalogue when the last one leaves, where anyone else's feed is dropped.
|
||||
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
|
||||
pub listed: bool,
|
||||
@@ -219,6 +254,8 @@ impl Default for General {
|
||||
directory_url: String::new(),
|
||||
directory_token: String::new(),
|
||||
directory_share: false,
|
||||
play_url: String::new(),
|
||||
play_token: String::new(),
|
||||
signals_on_server: false,
|
||||
signal_days: 7,
|
||||
fetch_internal: vec![],
|
||||
@@ -359,6 +396,10 @@ pub struct Stored {
|
||||
#[serde(default)]
|
||||
pub directory_share: bool,
|
||||
#[serde(default)]
|
||||
pub play_url: String,
|
||||
#[serde(default)]
|
||||
pub play_token: String,
|
||||
#[serde(default)]
|
||||
pub signals_on_server: bool,
|
||||
#[serde(default = "signal_days")]
|
||||
pub signal_days: u64,
|
||||
@@ -373,7 +414,7 @@ fn art_cache_mb() -> u64 {
|
||||
}
|
||||
|
||||
/// `[general]` keys that live in the database once it holds the configuration.
|
||||
const STORED_KEYS: [&str; 11] = [
|
||||
const STORED_KEYS: [&str; 13] = [
|
||||
"schedule",
|
||||
"max_total_gb",
|
||||
"max_age_days",
|
||||
@@ -383,6 +424,8 @@ const STORED_KEYS: [&str; 11] = [
|
||||
"directory_url",
|
||||
"directory_token",
|
||||
"directory_share",
|
||||
"play_url",
|
||||
"play_token",
|
||||
"signals_on_server",
|
||||
"signal_days",
|
||||
];
|
||||
@@ -400,6 +443,8 @@ impl Stored {
|
||||
directory_url: g.directory_url.clone(),
|
||||
directory_token: g.directory_token.clone(),
|
||||
directory_share: g.directory_share,
|
||||
play_url: g.play_url.clone(),
|
||||
play_token: g.play_token.clone(),
|
||||
signals_on_server: g.signals_on_server,
|
||||
signal_days: g.signal_days,
|
||||
}
|
||||
@@ -416,6 +461,8 @@ impl Stored {
|
||||
g.directory_url = self.directory_url;
|
||||
g.directory_token = self.directory_token;
|
||||
g.directory_share = self.directory_share;
|
||||
g.play_url = self.play_url;
|
||||
g.play_token = self.play_token;
|
||||
g.signals_on_server = self.signals_on_server;
|
||||
g.signal_days = self.signal_days;
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ pub struct Ctx {
|
||||
/// Held from taking a copy of the config to storing it (`edit_cfg`). Each change stores the
|
||||
/// whole catalogue from its copy, and two at once stored over each other: a feed added while
|
||||
/// another add spent a minute fetching was gone once that one stored (#153).
|
||||
// ponytail: this process only. `ipx add`, `rm` and `import` write from a process of their
|
||||
// ponytail: this process only. `ipxd add`, `rm` and `import` write from a process of their
|
||||
// own, and a change in flight here can still store over theirs; take the copy from the
|
||||
// database rather than memory if that ever happens.
|
||||
pub cfg_write: tokio::sync::Mutex<()>,
|
||||
|
||||
@@ -12,6 +12,7 @@ use std::time::{Duration, Instant};
|
||||
use tokio::runtime::Handle;
|
||||
use tokio::sync::{broadcast, mpsc, watch};
|
||||
use tokio::task::JoinHandle;
|
||||
use tracing::Instrument;
|
||||
|
||||
/// The catalogue and server settings as the database now has them, in place of the copy in
|
||||
/// memory, for a daemon told another ipx changed them (#120); config.toml, read again, for where
|
||||
@@ -23,7 +24,7 @@ pub(crate) async fn reload(ctx: &Ctx) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The counts `ipx status` prints, and /api/status serves. A running daemon's socket answers with
|
||||
/// The counts `ipxd status` prints, and /api/status serves. A running daemon's socket answers with
|
||||
/// this directly rather than through the job queue.
|
||||
pub(crate) async fn status(ctx: &Ctx) -> Event {
|
||||
match tracing::Instrument::instrument(ctx.db.counts(), db::step("counts")).await {
|
||||
@@ -49,54 +50,87 @@ pub(crate) async fn daemon(
|
||||
config_path: PathBuf,
|
||||
web_addr: Option<String>,
|
||||
events: broadcast::Sender<Event>,
|
||||
start: tracing::Span,
|
||||
) -> Result<()> {
|
||||
let socket = ctx.cfg().general.socket.clone();
|
||||
if ipc::daemon_is_live(&socket).await {
|
||||
anyhow::bail!("a daemon is already listening on {}", socket.display());
|
||||
}
|
||||
|
||||
// A database with nobody in it cannot be signed into.
|
||||
if ctx.db.users().await?.is_empty() {
|
||||
let dir = ctx.config_path.parent().unwrap_or(Path::new("."));
|
||||
let (password, told) = auth::first_password(dir, std::env::var("IPX_ADMIN_PASSWORD").ok())?;
|
||||
ctx.db.create_user("admin", Some(&crate::auth::hash_password(&password)?), /* admin */ true).await?;
|
||||
tracing::warn!(
|
||||
"no accounts yet: created 'admin' {told}. Change it with `echo -n <password> | ipx user passwd admin`"
|
||||
);
|
||||
}
|
||||
|
||||
// Unread lists start from here, and a reap may be a day off (#347). Slower without, never wrong.
|
||||
if let Err(e) = ctx.db.advance_unread_from().await {
|
||||
tracing::warn!(error = %format!("{e:#}"), "could not work out where unread items start");
|
||||
}
|
||||
|
||||
if let Some(admin) = ctx.db.users().await?.into_iter().find(|u| u.is_admin) {
|
||||
let catalogue: Vec<String> = ctx.cfg().feeds.keys().cloned().collect();
|
||||
match ctx.db.adopt_catalogue(admin.id, &catalogue).await {
|
||||
Ok(0) => {}
|
||||
Ok(n) => tracing::info!(user = %admin.name, feeds = n, "subscribed the first admin to the catalogue"),
|
||||
Err(e) => tracing::error!(error = %e, "could not subscribe the first admin to the catalogue"),
|
||||
// The rest of start-up, in the trace main began (#374): how long a deploy took to answer, and
|
||||
// what held it.
|
||||
let interrupted = async {
|
||||
// A database with nobody in it cannot be signed into.
|
||||
if ctx.db.users().instrument(db::step("users")).await?.is_empty() {
|
||||
let dir = ctx.config_path.parent().unwrap_or(Path::new("."));
|
||||
let (password, told) = auth::first_password(dir, std::env::var("IPX_ADMIN_PASSWORD").ok())?;
|
||||
ctx.db.create_user("admin", Some(&crate::auth::hash_password(&password)?), /* admin */ true).await?;
|
||||
tracing::warn!(
|
||||
"no accounts yet: created 'admin' {told}. Change it with `echo -n <password> | ipxd user passwd admin`"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
match ctx.db.requeue_interrupted().await {
|
||||
Ok(n) if n > 0 => tracing::info!(count = n, "requeued downloads interrupted by a restart"),
|
||||
Ok(_) => {}
|
||||
Err(e) => tracing::warn!(error = %format!("{e:#}"), "could not requeue interrupted downloads"),
|
||||
}
|
||||
// Unread lists start from here, and a reap may be a day off (#347). Slower without, never wrong.
|
||||
if let Err(e) = ctx.db.advance_unread_from().instrument(db::step("advance_unread_from")).await {
|
||||
tracing::warn!(error = %format!("{e:#}"), "could not work out where unread items start");
|
||||
}
|
||||
|
||||
match catalogue::retire_stranded(&ctx).await {
|
||||
Ok(0) => {}
|
||||
Ok(n) => tracing::info!(feeds = n, "retired feeds whose OPML is no longer in config"),
|
||||
Err(e) => tracing::warn!(error = %format!("{e:#}"), "could not retire feeds whose OPML is no longer in config"),
|
||||
if let Some(admin) = ctx.db.users().await?.into_iter().find(|u| u.is_admin) {
|
||||
let catalogue: Vec<String> = ctx.cfg().feeds.keys().cloned().collect();
|
||||
match ctx.db.adopt_catalogue(admin.id, &catalogue).instrument(db::step("adopt_catalogue")).await {
|
||||
Ok(0) => {}
|
||||
Ok(n) => tracing::info!(user = %admin.name, feeds = n, "subscribed the first admin to the catalogue"),
|
||||
Err(e) => tracing::error!(error = %e, "could not subscribe the first admin to the catalogue"),
|
||||
}
|
||||
}
|
||||
|
||||
let interrupted = match ctx.db.requeue_interrupted().instrument(db::step("requeue_interrupted")).await {
|
||||
Ok(ids) => ids,
|
||||
Err(e) => {
|
||||
tracing::warn!(error = %format!("{e:#}"), "could not requeue interrupted downloads");
|
||||
vec![]
|
||||
}
|
||||
};
|
||||
|
||||
match catalogue::retire_stranded(&ctx).instrument(db::step("retire_stranded")).await {
|
||||
Ok(0) => {}
|
||||
Ok(n) => tracing::info!(feeds = n, "retired feeds whose OPML is no longer in config"),
|
||||
Err(e) => tracing::warn!(error = %format!("{e:#}"), "could not retire feeds whose OPML is no longer in config"),
|
||||
}
|
||||
// After retire_stranded, which drops feeds whose subscriptions this then finds.
|
||||
match catalogue::drop_stray_subscriptions(&ctx).instrument(db::step("drop_stray_subscriptions")).await {
|
||||
Ok(0) => {}
|
||||
Ok(n) => tracing::info!(feeds = n, "unsubscribed everyone from feeds no longer in the catalogue"),
|
||||
Err(e) => tracing::warn!(error = %format!("{e:#}"), "could not drop subscriptions to feeds no longer in the catalogue"),
|
||||
}
|
||||
match catalogue::regroup_unmanaged(&ctx).instrument(db::step("regroup_unmanaged")).await {
|
||||
Ok(0) => {}
|
||||
Ok(n) => tracing::info!(feeds = n, "took feeds of their own out of their old list's group"),
|
||||
Err(e) => {
|
||||
tracing::warn!(error = %format!("{e:#}"), "could not take feeds of their own out of their old list's group")
|
||||
}
|
||||
}
|
||||
anyhow::Ok(interrupted)
|
||||
}
|
||||
.instrument(start.clone())
|
||||
.await?;
|
||||
|
||||
// Room for any one request's commands: a web request waits inside itself for a free place,
|
||||
// and Download latest sends up to 100. With 64 the rest waited on downloads an hour long, and
|
||||
// every other request that queues work waited behind them (#154).
|
||||
let (tx_cmd, mut rx_cmd) = mpsc::channel::<Cmd>(1024);
|
||||
|
||||
let web = start_web(&ctx, &config_path, web_addr, &tx_cmd, &events).await?;
|
||||
// Taken again first thing: left for their feeds' next check, a download a deploy stopped
|
||||
// waited an hour, or a day for a daily feed (#410).
|
||||
if !interrupted.is_empty() {
|
||||
tracing::info!(count = interrupted.len(), "taking up downloads interrupted by a restart");
|
||||
}
|
||||
for enclosure in interrupted {
|
||||
if tx_cmd.try_send(Cmd::Download { enclosure }).is_err() {
|
||||
break; // The queue is full; the rest wait for their feeds' next check, as before.
|
||||
}
|
||||
}
|
||||
let web = start_web(&ctx, &config_path, web_addr, &tx_cmd, &events).instrument(start.clone()).await?;
|
||||
// status and reload are answered by the socket itself; everything else waits its turn in the
|
||||
// queue.
|
||||
let answer: ipc::StatusFn = {
|
||||
@@ -116,7 +150,9 @@ pub(crate) async fn daemon(
|
||||
let server = tokio::spawn(ipc::serve(socket.clone(), events.clone(), tx_cmd, answer));
|
||||
|
||||
// One command at a time: the queue is what keeps two scans from overlapping.
|
||||
tracing::info!(feeds = catalogue::subscriptions(&ctx).await.map(|s| s.len()).unwrap_or(0), "daemon started");
|
||||
let feeds = catalogue::subscriptions(&ctx).await.map(|s| s.len()).unwrap_or(0);
|
||||
start.in_scope(|| tracing::info!(feeds, "daemon started"));
|
||||
drop(start);
|
||||
|
||||
// A signal has to be able to interrupt work in progress, not just the wait between
|
||||
// jobs. Racing `shutdown()` in the outer select only cancels branch selection: once
|
||||
@@ -131,6 +167,8 @@ pub(crate) async fn daemon(
|
||||
tokio::spawn(deadair::run(ctx.clone(), rx_stop.clone()));
|
||||
tokio::spawn(signals::run(ctx.clone(), rx_stop.clone()));
|
||||
tokio::spawn(directory::run(ctx.clone(), rx_stop.clone()));
|
||||
tokio::spawn(crate::push::run(ctx.clone(), rx_stop.clone()));
|
||||
tokio::spawn(crate::search::run(ctx.clone(), rx_stop.clone()));
|
||||
// The library's counts for /metrics. The Docker healthcheck asked for them every 30 s until
|
||||
// it was removed: Docker runs it with exec, which failed on Tower whenever root was logged
|
||||
// out (#218).
|
||||
@@ -184,7 +222,10 @@ pub(crate) async fn daemon(
|
||||
"-> {}",
|
||||
serde_json::to_string(&cmd).unwrap_or_else(|_| format!("{cmd:?}"))
|
||||
);
|
||||
if !until_stopped(&ctx, &rx_stop, cli::run(&ctx, cmd)).await {
|
||||
// A trace a command, its job inside it (#373): from the CLI or the page alike.
|
||||
let kind = serde_json::to_value(&cmd).ok().and_then(|v| v["cmd"].as_str().map(str::to_owned));
|
||||
let span = tracing::info_span!("command", cmd = kind.as_deref().unwrap_or("?"));
|
||||
if !until_stopped(&ctx, &rx_stop, cli::run(&ctx, cmd).instrument(span)).await {
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -277,7 +318,14 @@ pub(crate) async fn start_web(
|
||||
let at = metrics::spawn(&cfg.web.metrics_bind, sources)?;
|
||||
tracing::info!(%at, "serving metrics for Prometheus at /metrics");
|
||||
}
|
||||
let state = web::WebState { ctx: ctx.clone(), cmds: cmds.clone(), events: events.clone(), access, playing };
|
||||
let state = web::WebState {
|
||||
ctx: ctx.clone(),
|
||||
cmds: cmds.clone(),
|
||||
events: events.clone(),
|
||||
access,
|
||||
playing,
|
||||
wrong: Default::default(),
|
||||
};
|
||||
Ok(Some(tokio::spawn(async move {
|
||||
if let Err(e) = web::serve(state, &bind).await {
|
||||
tracing::error!(error = %format!("{e:#}"), "web ui stopped");
|
||||
@@ -301,7 +349,7 @@ pub(crate) async fn shutdown() {
|
||||
/// How long the daemon may sleep before a feed is due: until the earliest one, at least 30 s and
|
||||
/// at most 10 minutes. It ticked every minute and ran a scan pass each time, 80% of them finding
|
||||
/// nothing due (#114). The floor keeps a feed that never gets a check time from spinning it; the
|
||||
/// ceiling picks up within ten minutes what no command announces, such as `ipx add` or a
|
||||
/// ceiling picks up within ten minutes what no command announces, such as `ipxd add` or a
|
||||
/// shorter schedule. A command, a refresh or a feed added on the page, wakes it at once anyway.
|
||||
pub(crate) async fn until_next_scan(ctx: &Ctx) -> std::time::Duration {
|
||||
let next = async {
|
||||
@@ -337,6 +385,16 @@ mod tests {
|
||||
use super::*;
|
||||
use crate::test_util::{feed, test_ctx};
|
||||
|
||||
#[tokio::test]
|
||||
async fn the_daemon_sleeps_at_least_30_s_and_at_most_10_minutes() {
|
||||
let ctx = test_ctx(config::Config::default()).await;
|
||||
assert_eq!(until_next_scan(&ctx).await, Duration::from_secs(600), "nothing to check: the ceiling");
|
||||
let mut cfg = config::Config::default();
|
||||
cfg.feeds.insert("never-checked".into(), feed());
|
||||
let ctx = test_ctx(cfg).await;
|
||||
assert_eq!(until_next_scan(&ctx).await, Duration::from_secs(30), "due now: the floor, not a spin");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_reload_takes_the_catalogue_another_ipx_wrote() {
|
||||
let with = |id: &str| {
|
||||
@@ -347,7 +405,7 @@ mod tests {
|
||||
let held = with("kept-in-memory");
|
||||
let ctx = test_ctx(held.clone()).await;
|
||||
ctx.db.store_config(&held).await.unwrap();
|
||||
// `ipx add` and `ipx rm`, from a process of their own (#120).
|
||||
// `ipxd add` and `ipxd rm`, from a process of their own (#120).
|
||||
ctx.db.store_config(&with("added-by-the-cli")).await.unwrap();
|
||||
reload(&ctx).await.unwrap();
|
||||
let urls: Vec<String> = ctx.cfg().feeds.values().map(|f| f.url.clone()).collect();
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -5,6 +5,8 @@
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
use ipx_directory::{FeedPage, Listing, Me, SnapshotEntry, SnapshotResult};
|
||||
use ipx_wire::Message as _;
|
||||
use ipx_wire::pb::directory as pb;
|
||||
use serde::de::DeserializeOwned;
|
||||
|
||||
use crate::catalogue::Sub;
|
||||
@@ -65,6 +67,13 @@ pub fn configured(cfg: &config::Config) -> bool {
|
||||
target(cfg).is_some()
|
||||
}
|
||||
|
||||
/// How an answer in protobuf becomes what the caller wants (#470).
|
||||
type Decode<T> = fn(&[u8]) -> Result<T, String>;
|
||||
|
||||
fn listings(b: &[u8]) -> Result<Vec<Listing>, String> {
|
||||
Ok(pb::Listings::decode(b).map_err(|e| e.to_string())?.items.into_iter().map(Listing::from).collect())
|
||||
}
|
||||
|
||||
/// A span the whole call, its answer read and parsed included: the directory's own span ends when it
|
||||
/// has answered, so a slow read of a large answer showed under no span (#250).
|
||||
#[tracing::instrument(name = "directory_call", skip_all, fields(http.request.method = %method, url.path = %path.split('?').next().unwrap_or(path), http.response.body.size))]
|
||||
@@ -73,6 +82,7 @@ async fn call<T: DeserializeOwned>(
|
||||
method: reqwest::Method,
|
||||
path: &str,
|
||||
body: Option<&[SnapshotEntry]>,
|
||||
answer: Option<Decode<T>>,
|
||||
) -> Result<T, DirError> {
|
||||
let (base, token) = target(&ctx.cfg()).ok_or(DirError::NotConfigured)?;
|
||||
let mut req =
|
||||
@@ -81,15 +91,19 @@ async fn call<T: DeserializeOwned>(
|
||||
if let Some(tp) = ipx_directory::otel::traceparent() {
|
||||
req = req.header("traceparent", tp);
|
||||
}
|
||||
// A snapshot is about 100 KB of JSON, sent gzipped; the directory unpacks it (#253).
|
||||
// Protobuf both ways (#465), where the answer has a message: a smaller body to read.
|
||||
if answer.is_some() {
|
||||
req = req.header(reqwest::header::ACCEPT, ipx_wire::PROTOBUF);
|
||||
}
|
||||
// A snapshot is about 100 KB, sent gzipped; the directory unpacks it (#253).
|
||||
if let Some(b) = body {
|
||||
use std::io::Write as _;
|
||||
let json = serde_json::to_vec(b).map_err(|e| DirError::Unreachable(format!("{e:#}")))?;
|
||||
let pb = pb::Snapshot { items: b.iter().map(pb::SnapshotEntry::from).collect() }.encode_to_vec();
|
||||
let mut gz = flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::default());
|
||||
gz.write_all(&json).map_err(|e| DirError::Unreachable(format!("{e:#}")))?;
|
||||
gz.write_all(&pb).map_err(|e| DirError::Unreachable(format!("{e:#}")))?;
|
||||
let packed = gz.finish().map_err(|e| DirError::Unreachable(format!("{e:#}")))?;
|
||||
req = req
|
||||
.header(reqwest::header::CONTENT_TYPE, "application/json")
|
||||
.header(reqwest::header::CONTENT_TYPE, ipx_wire::PROTOBUF)
|
||||
.header(reqwest::header::CONTENT_ENCODING, "gzip")
|
||||
.body(packed);
|
||||
}
|
||||
@@ -104,9 +118,16 @@ async fn call<T: DeserializeOwned>(
|
||||
.unwrap_or_default();
|
||||
return Err(DirError::Refused(status.as_u16(), msg));
|
||||
}
|
||||
let protobuf = resp
|
||||
.headers()
|
||||
.get(reqwest::header::CONTENT_TYPE)
|
||||
.is_some_and(|v| v.as_bytes().starts_with(ipx_wire::PROTOBUF.as_bytes()));
|
||||
// A removal or an unblock answers 204, with nothing to read: that reads as null, which is ().
|
||||
let body = resp.bytes().await.map_err(|e| DirError::Unreachable(format!("{e:#}")))?;
|
||||
tracing::Span::current().record("http.response.body.size", body.len());
|
||||
if let Some(decode) = answer.filter(|_| protobuf) {
|
||||
return decode(&body).map_err(|e| DirError::Unreachable(format!("its answer did not read: {e}")));
|
||||
}
|
||||
serde_json::from_slice(if body.is_empty() { b"null" } else { &body })
|
||||
.map_err(|e| DirError::Unreachable(format!("its answer did not read: {e:#}")))
|
||||
}
|
||||
@@ -116,23 +137,31 @@ fn seg(s: &str) -> percent_encoding::PercentEncode<'_> {
|
||||
}
|
||||
|
||||
pub async fn me(ctx: &Ctx) -> Result<Me, DirError> {
|
||||
call(ctx, reqwest::Method::GET, "/v1/me", None).await
|
||||
call(ctx, reqwest::Method::GET, "/v1/me", None, Some(|b| pb::Me::decode(b).map_err(|e| e.to_string())?.try_into()))
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn feeds(ctx: &Ctx) -> Result<Vec<Listing>, DirError> {
|
||||
let got: Vec<Listing> = call(ctx, reqwest::Method::GET, "/v1/feeds", None).await?;
|
||||
let got: Vec<Listing> = call(ctx, reqwest::Method::GET, "/v1/feeds", None, Some(listings)).await?;
|
||||
note_art(got.iter());
|
||||
Ok(got)
|
||||
}
|
||||
|
||||
pub async fn popular(ctx: &Ctx) -> Result<Vec<Listing>, DirError> {
|
||||
let got: Vec<Listing> = call(ctx, reqwest::Method::GET, "/v1/popular?limit=10", None).await?;
|
||||
let got: Vec<Listing> = call(ctx, reqwest::Method::GET, "/v1/popular?limit=10", None, Some(listings)).await?;
|
||||
note_art(got.iter());
|
||||
Ok(got)
|
||||
}
|
||||
|
||||
pub async fn page(ctx: &Ctx, id: &str) -> Result<FeedPage, DirError> {
|
||||
let got: FeedPage = call(ctx, reqwest::Method::GET, &format!("/v1/feeds/{}", seg(id)), None).await?;
|
||||
let got: FeedPage = call(
|
||||
ctx,
|
||||
reqwest::Method::GET,
|
||||
&format!("/v1/feeds/{}", seg(id)),
|
||||
None,
|
||||
Some(|b| pb::FeedPage::decode(b).map(FeedPage::from).map_err(|e| e.to_string())),
|
||||
)
|
||||
.await?;
|
||||
note_art(std::iter::once(&got.listing));
|
||||
Ok(got)
|
||||
}
|
||||
@@ -157,29 +186,45 @@ pub fn listed_art(url: &str) -> bool {
|
||||
|
||||
/// Removes a feed and blocks it, so no snapshot brings it back.
|
||||
pub async fn remove(ctx: &Ctx, id: &str) -> Result<(), DirError> {
|
||||
call(ctx, reqwest::Method::DELETE, &format!("/v1/feeds/{}", seg(id)), None).await
|
||||
call(ctx, reqwest::Method::DELETE, &format!("/v1/feeds/{}", seg(id)), None, None).await
|
||||
}
|
||||
|
||||
pub async fn refresh(ctx: &Ctx, id: &str) -> Result<serde::de::IgnoredAny, DirError> {
|
||||
call(ctx, reqwest::Method::POST, &format!("/v1/feeds/{}/refresh", seg(id)), None).await
|
||||
call(ctx, reqwest::Method::POST, &format!("/v1/feeds/{}/refresh", seg(id)), None, None).await
|
||||
}
|
||||
|
||||
pub async fn blocked(ctx: &Ctx) -> Result<Vec<ipx_directory::Blocked>, DirError> {
|
||||
call(ctx, reqwest::Method::GET, "/v1/blocked", None).await
|
||||
call(
|
||||
ctx,
|
||||
reqwest::Method::GET,
|
||||
"/v1/blocked",
|
||||
None,
|
||||
Some(|b| {
|
||||
Ok(pb::BlockedList::decode(b).map_err(|e| e.to_string())?.items.into_iter().map(Into::into).collect())
|
||||
}),
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn unblock(ctx: &Ctx, id: &str) -> Result<(), DirError> {
|
||||
call(ctx, reqwest::Method::DELETE, &format!("/v1/blocked/{}", seg(id)), None).await
|
||||
call(ctx, reqwest::Method::DELETE, &format!("/v1/blocked/{}", seg(id)), None, None).await
|
||||
}
|
||||
|
||||
pub async fn instances(ctx: &Ctx) -> Result<Vec<ipx_directory::Instance>, DirError> {
|
||||
call(ctx, reqwest::Method::GET, "/v1/instances", None).await
|
||||
call(
|
||||
ctx,
|
||||
reqwest::Method::GET,
|
||||
"/v1/instances",
|
||||
None,
|
||||
Some(|b| Ok(pb::Instances::decode(b).map_err(|e| e.to_string())?.items.into_iter().map(Into::into).collect())),
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
/// Drops one server's snapshot, and its counts with it. It comes back with that server's next
|
||||
/// one: to keep a server out, revoke its token.
|
||||
pub async fn drop_instance(ctx: &Ctx, name: &str) -> Result<(), DirError> {
|
||||
call(ctx, reqwest::Method::DELETE, &format!("/v1/instances/{}", seg(name)), None).await
|
||||
call(ctx, reqwest::Method::DELETE, &format!("/v1/instances/{}", seg(name)), None, None).await
|
||||
}
|
||||
|
||||
/// Has the snapshot sent again now, changed or not.
|
||||
@@ -234,7 +279,14 @@ async fn snapshot(ctx: &Ctx) -> Result<Vec<SnapshotEntry>, DirError> {
|
||||
}
|
||||
|
||||
pub async fn share(ctx: &Ctx, entries: &[SnapshotEntry]) -> Result<SnapshotResult, DirError> {
|
||||
let r: SnapshotResult = call(ctx, reqwest::Method::POST, "/v1/snapshot", Some(entries)).await?;
|
||||
let r: SnapshotResult = call(
|
||||
ctx,
|
||||
reqwest::Method::POST,
|
||||
"/v1/snapshot",
|
||||
Some(entries),
|
||||
Some(|b| pb::SnapshotResult::decode(b).map(Into::into).map_err(|e| e.to_string())),
|
||||
)
|
||||
.await?;
|
||||
if r.refused_private > 0 {
|
||||
// The directory caught what this side should have: worth knowing which check differs.
|
||||
tracing::warn!(refused = r.refused_private, "the directory refused feeds as private that ipx sent");
|
||||
|
||||
@@ -457,4 +457,51 @@ mod tests {
|
||||
assert!(!blocked(&[" ".to_string()], &["anything"]), "a blank word blocks nothing");
|
||||
assert!(!blocked(&[], &["anything"]));
|
||||
}
|
||||
#[tokio::test]
|
||||
async fn a_download_is_sniffed_for_what_it_is_and_placed_without_clobbering() {
|
||||
use axum::routing::get;
|
||||
let app = axum::Router::new()
|
||||
.route("/ep.mp3", get(|| async { "ID3 an episode" }))
|
||||
.route("/wall.mp3", get(|| async { "\n <!DOCTYPE html><html>Sign in</html>" }))
|
||||
.route("/show.torrent", get(|| async { "d8:announce35:udp://tracker.example:80/announcee" }))
|
||||
.route("/gone.mp3", get(|| async { axum::http::StatusCode::NOT_FOUND }));
|
||||
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let at = listener.local_addr().unwrap();
|
||||
tokio::spawn(async move { axum::serve(listener, app).await });
|
||||
|
||||
let d = std::env::temp_dir().join(format!("ipx-download-test-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&d);
|
||||
let mut cfg = Config::default();
|
||||
cfg.general.download_dir = d.clone();
|
||||
let feed = crate::test_util::feed();
|
||||
let client = reqwest::Client::new();
|
||||
let (c, g, f) = (&client, &cfg, &feed);
|
||||
let get = |path: &str| {
|
||||
let url = format!("http://{at}{path}");
|
||||
async move { download(c, g, f, &url, |_, _| {}).await }
|
||||
};
|
||||
|
||||
let mut seen = vec![];
|
||||
let ep =
|
||||
download(&client, &cfg, &feed, &format!("http://{at}/ep.mp3"), |done, _| seen.push(done)).await.unwrap();
|
||||
assert!(matches!(ep.kind, Kind::File));
|
||||
assert_eq!((ep.name.as_str(), ep.bytes), ("ep.mp3", 14));
|
||||
assert_eq!(seen.last(), Some(&14), "progress reaches the whole file");
|
||||
let first = place(&ep, &d.join("Show")).await.unwrap();
|
||||
assert_eq!(first, d.join("Show/ep.mp3"));
|
||||
assert_eq!(std::fs::read(&first).unwrap(), b"ID3 an episode");
|
||||
|
||||
// The same name again, from another enclosure: beside the first, not over it.
|
||||
let again = get("/ep.mp3").await.unwrap();
|
||||
assert_eq!(place(&again, &d.join("Show")).await.unwrap(), d.join("Show/ep-2.mp3"));
|
||||
assert!(first.exists());
|
||||
|
||||
let wall = get("/wall.mp3").await.err().unwrap();
|
||||
assert!(format!("{wall:#}").contains("HTML page"), "{wall:#}");
|
||||
assert!(!d.join(".ipx-incomplete/wall.mp3").exists(), "a login page is not kept");
|
||||
|
||||
assert!(matches!(get("/show.torrent").await.unwrap().kind, Kind::Torrent));
|
||||
assert!(format!("{:#}", get("/gone.mp3").await.err().unwrap()).contains("404"));
|
||||
let _ = std::fs::remove_dir_all(&d);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,6 +86,10 @@ pub mod entries {
|
||||
pub duration: Option<i64>,
|
||||
pub episode: Option<i64>,
|
||||
pub season: Option<i64>,
|
||||
/// The title and the text without markup, lowercased and cut short, which search reads
|
||||
/// (#459); none until search.rs makes it.
|
||||
#[sea_orm(column_type = "Text", nullable)]
|
||||
pub search_text: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
@@ -266,6 +270,53 @@ pub mod entry_state {
|
||||
owned_by_user!();
|
||||
}
|
||||
|
||||
/// Up Next (#473): the episodes someone has lined up to play, in order. Per person, and on the
|
||||
/// server so the phone and the browser play through the same one.
|
||||
pub mod queue {
|
||||
use sea_orm::entity::prelude::*;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
|
||||
#[sea_orm(table_name = "queue")]
|
||||
pub struct Model {
|
||||
#[sea_orm(primary_key, auto_increment = false)]
|
||||
pub user_id: i64,
|
||||
#[sea_orm(primary_key, auto_increment = false, column_type = "Text")]
|
||||
pub feed_id: String,
|
||||
#[sea_orm(primary_key, auto_increment = false, column_type = "Text")]
|
||||
pub guid: String,
|
||||
pub pos: i64,
|
||||
}
|
||||
|
||||
owned_by_user!();
|
||||
}
|
||||
|
||||
/// Smart playlists (#476): episodes of the podcasts chosen, kept up to date by themselves. The
|
||||
/// priority podcasts' episodes come first.
|
||||
pub mod playlists {
|
||||
use sea_orm::entity::prelude::*;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
|
||||
#[sea_orm(table_name = "playlists")]
|
||||
pub struct Model {
|
||||
#[sea_orm(primary_key)]
|
||||
pub id: i64,
|
||||
pub user_id: i64,
|
||||
#[sea_orm(column_type = "Text")]
|
||||
pub name: String,
|
||||
/// JSON array of feed ids.
|
||||
#[sea_orm(column_type = "Text")]
|
||||
pub feeds: String,
|
||||
/// JSON array of feed ids, of those in `feeds`.
|
||||
#[sea_orm(column_type = "Text")]
|
||||
pub priority: String,
|
||||
pub only_unplayed: bool,
|
||||
pub oldest_first: bool,
|
||||
pub created: i64,
|
||||
}
|
||||
|
||||
owned_by_user!();
|
||||
}
|
||||
|
||||
/// Words someone never wants to see: an item whose title or text has one is hidden from them and
|
||||
/// not downloaded on their account (issue #47). `feed_id` is empty for the list that applies to
|
||||
/// every feed they read.
|
||||
@@ -386,6 +437,27 @@ pub mod sessions {
|
||||
owned_by_user!();
|
||||
}
|
||||
|
||||
/// A phone with the iOS app, signed in as someone, to send the icon's unread count to (#433).
|
||||
pub mod push_devices {
|
||||
use sea_orm::entity::prelude::*;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
|
||||
#[sea_orm(table_name = "push_devices")]
|
||||
pub struct Model {
|
||||
/// The token Apple gave the app on that phone, in hex.
|
||||
#[sea_orm(primary_key, auto_increment = false, column_type = "Text")]
|
||||
pub token: String,
|
||||
pub user_id: i64,
|
||||
/// From a debug build, which Apple's sandbox serves rather than production.
|
||||
pub sandbox: bool,
|
||||
pub created: i64,
|
||||
/// The count last sent, so a minute in which it did not change sends nothing.
|
||||
pub badge: Option<i64>,
|
||||
}
|
||||
|
||||
owned_by_user!();
|
||||
}
|
||||
|
||||
/// A token a script or agent sends as `Authorization: Bearer`, acting as the person who made
|
||||
/// it (#123). Kept as its SHA-256, so the table is no use to anyone who reads it.
|
||||
pub mod api_tokens {
|
||||
|
||||
@@ -83,6 +83,9 @@ pub enum Event {
|
||||
Reaped {
|
||||
path: String,
|
||||
bytes: u64,
|
||||
/// Only what would go: `reap --dry-run` said "deleted" of files it kept (#461).
|
||||
#[serde(default)]
|
||||
dry_run: bool,
|
||||
},
|
||||
/// Terminal: a client that asked for work stops reading here.
|
||||
ScanDone {
|
||||
@@ -97,6 +100,8 @@ pub enum Event {
|
||||
ReapDone {
|
||||
files: usize,
|
||||
bytes: u64,
|
||||
#[serde(default)]
|
||||
dry_run: bool,
|
||||
},
|
||||
Status {
|
||||
feeds: usize,
|
||||
@@ -165,11 +170,11 @@ impl Event {
|
||||
},
|
||||
Event::DownloadDone { path, .. } => format!(" saved {path}"),
|
||||
Event::DownloadError { url, msg, .. } => format!(" failed {url}: {msg}"),
|
||||
Event::Reaped { path, bytes } => {
|
||||
format!("deleted {path} ({:.1} MB)", *bytes as f64 / 1_048_576.0)
|
||||
Event::Reaped { path, bytes, dry_run } => {
|
||||
format!("{} {path} ({:.1} MB)", deleted(*dry_run), *bytes as f64 / 1_048_576.0)
|
||||
}
|
||||
Event::ReapDone { files, bytes } => {
|
||||
format!("deleted {files} old file(s), {:.1} MB", *bytes as f64 / 1_048_576.0)
|
||||
Event::ReapDone { files, bytes, dry_run } => {
|
||||
format!("{} {files} old file(s), {:.1} MB", deleted(*dry_run), *bytes as f64 / 1_048_576.0)
|
||||
}
|
||||
Event::Status { feeds, pending, downloaded } => {
|
||||
format!("{feeds} feeds, {pending} queued to download, {downloaded} downloaded")
|
||||
@@ -212,7 +217,7 @@ pub enum Command {
|
||||
enclosure: i64,
|
||||
},
|
||||
Status,
|
||||
/// Read the catalogue and server settings again from the database, which `ipx add`, `rm`
|
||||
/// Read the catalogue and server settings again from the database, which `ipxd add`, `rm`
|
||||
/// and `import` change in a process of their own. A daemon keeps them in memory, and with
|
||||
/// its old copy wrote a removed feed back, and dropped an added one, at its next change of
|
||||
/// its own (#120). Answered, as status is, with the counts after it.
|
||||
@@ -245,6 +250,7 @@ impl Emitter {
|
||||
// line per feed per tick for "not due yet" would push everything worth reading
|
||||
// out of the buffer within a few minutes.
|
||||
log_event(&e, self.tx.is_some());
|
||||
crate::metrics::STATS.event(&e);
|
||||
// A feed not due changes nothing a listener shows, and a scan has one for nearly every
|
||||
// feed: 1,500 at once overflowed the channel's 1,024 for a page slow to read (#197).
|
||||
let routine = matches!(&e, Event::FeedSkip { reason, .. } if reason.starts_with("not due"));
|
||||
@@ -287,6 +293,10 @@ impl Event {
|
||||
}
|
||||
}
|
||||
|
||||
const fn deleted(dry_run: bool) -> &'static str {
|
||||
if dry_run { "would delete" } else { "deleted" }
|
||||
}
|
||||
|
||||
/// An event, logged once (#91): its words as the message, and `ev`, `feed`, `new` and the rest as
|
||||
/// fields, so Loki reads them without parsing the message. A failure also gets `error.type` and,
|
||||
/// from an HTTP error, `http.response.status_code`, so failures group by kind without a regex.
|
||||
@@ -307,7 +317,9 @@ fn log_event(e: &Event, wire: bool) {
|
||||
_ => (None, None),
|
||||
};
|
||||
let routine = match e {
|
||||
Event::Progress { .. } | Event::FeedSkip { .. } | Event::FeedStart { .. } => true,
|
||||
// A scan's end: ipx_jobs_total, ipx_feed_fetches_total and ipx_last_scan_age_seconds hold it,
|
||||
// and since scans became one or two feeds at a time it was a fifth of the log (#363).
|
||||
Event::Progress { .. } | Event::FeedSkip { .. } | Event::FeedStart { .. } | Event::ScanDone { .. } => true,
|
||||
Event::FeedDone { new, downloaded, failed, torrents, .. } => {
|
||||
*new == 0 && *downloaded == 0 && *failed == 0 && *torrents == 0
|
||||
}
|
||||
@@ -326,7 +338,7 @@ fn log_event(e: &Event, wire: bool) {
|
||||
)
|
||||
};
|
||||
}
|
||||
// `ipx status`'s answer: a reply on the socket rather than work done, so it
|
||||
// `ipxd status`'s answer: a reply on the socket rather than work done, so it
|
||||
// stays with the rest of the conversation, and the Scans tab stays about scans.
|
||||
if matches!(e, Event::Status { .. }) {
|
||||
return line!(info, "ipx::io", format!("<- {json}"));
|
||||
@@ -353,7 +365,7 @@ pub async fn daemon_is_live(path: &Path) -> bool {
|
||||
/// time, and a healthcheck left waiting behind a scan or a long download timed out and called a
|
||||
/// busy daemon dead; a reload left waiting would let the web UI write the old catalogue back in
|
||||
/// the meantime. The answer goes to the client that asked and no one else: broadcast, it ended
|
||||
/// any `ipx fetch` that was watching a scan, since `status` is a terminal event.
|
||||
/// any `ipxd fetch` that was watching a scan, since `status` is a terminal event.
|
||||
/// A future, since reading the counts is a database query.
|
||||
pub type StatusFn = Arc<dyn Fn(Command) -> Pin<Box<dyn Future<Output = Event> + Send>> + Send + Sync>;
|
||||
|
||||
@@ -513,7 +525,9 @@ mod tests {
|
||||
assert!(Event::ScanDone { feeds: 3, only: None, forced: false }.is_for(&me, &mine));
|
||||
assert!(Event::SignalHits { user: 1, hits: 2 }.is_for(&me, &mine));
|
||||
assert!(!Event::SignalHits { user: 3, hits: 2 }.is_for(&me, &mine));
|
||||
assert!(!Event::Reaped { path: "/downloads/Theirs/ep.mp3".into(), bytes: 1 }.is_for(&me, &mine));
|
||||
assert!(
|
||||
!Event::Reaped { path: "/downloads/Theirs/ep.mp3".into(), bytes: 1, dry_run: false }.is_for(&me, &mine)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -624,7 +638,7 @@ mod tests {
|
||||
#[test]
|
||||
fn only_completion_events_end_a_client_session() {
|
||||
assert!(Event::ScanDone { feeds: 1, only: None, forced: false }.is_terminal());
|
||||
assert!(Event::ReapDone { files: 0, bytes: 0 }.is_terminal());
|
||||
assert!(Event::ReapDone { files: 0, bytes: 0, dry_run: false }.is_terminal());
|
||||
assert!(!Event::FeedDone { feed: "a".into(), new: 0, downloaded: 0, failed: 0, torrents: 0 }.is_terminal());
|
||||
}
|
||||
|
||||
|
||||
132
crates/ipx/src/lib.rs
Normal file
132
crates/ipx/src/lib.rs
Normal file
@@ -0,0 +1,132 @@
|
||||
//! ipx, the daemon and its command line. The code is in the modules below; `main` starts it.
|
||||
//! A library under the binary `ipxd` (src/main.rs) so that its modules, and so its log targets
|
||||
//! (`ipx::scan`, `ipx::http`), the filters (`ipx=info`) and the dashboard's queries on them, kept
|
||||
//! their names when the command was renamed (#437).
|
||||
|
||||
mod access;
|
||||
mod art;
|
||||
mod auth;
|
||||
mod catalogue;
|
||||
mod cli;
|
||||
mod config;
|
||||
mod ctx;
|
||||
mod daemon;
|
||||
mod db;
|
||||
mod deadair;
|
||||
mod directory;
|
||||
mod download;
|
||||
mod entity;
|
||||
// The crate root's import, so `crate::feed` reaches it from every module as before.
|
||||
use ipx_feed as feed;
|
||||
mod ipc;
|
||||
mod lists;
|
||||
mod logbuf;
|
||||
mod logging;
|
||||
mod metrics;
|
||||
mod play;
|
||||
mod push;
|
||||
mod retention;
|
||||
mod scan;
|
||||
mod search;
|
||||
mod signals;
|
||||
#[cfg(test)]
|
||||
mod test_util;
|
||||
mod torrent;
|
||||
mod web;
|
||||
|
||||
use crate::cli::{Cli, Command, UserCmd};
|
||||
use crate::ctx::Ctx;
|
||||
use crate::ipc::Command as Cmd;
|
||||
use anyhow::Result;
|
||||
use clap::Parser;
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::broadcast;
|
||||
use tracing::Instrument;
|
||||
|
||||
#[tokio::main]
|
||||
pub async fn main() -> Result<()> {
|
||||
let cli = Cli::parse();
|
||||
// The daemon only: a trace for every `ipxd status` a script runs would bury the ones worth
|
||||
// looking at.
|
||||
let otel = match cli.command {
|
||||
Command::Daemon { .. } => ipx_directory::otel::provider("ipx")?,
|
||||
_ => None,
|
||||
};
|
||||
logging::init(otel.as_ref());
|
||||
// For the spans the page reports of itself (#376), which ipx builds with the page's own ids.
|
||||
if let Some(p) = &otel {
|
||||
opentelemetry::global::set_tracer_provider(p.clone());
|
||||
}
|
||||
|
||||
let config_path = cli.config.clone().unwrap_or_else(config::config_path);
|
||||
let cfg = config::Config::load(&config_path)?;
|
||||
// The daemon holds its connections open; a one-off command opens what it uses.
|
||||
let keep_open = matches!(cli.command, Command::Daemon { .. });
|
||||
// The daemon's start-up is a trace (#374), from opening the database to serving; daemon()
|
||||
// carries it on.
|
||||
let start = if keep_open { tracing::info_span!("start") } else { tracing::Span::none() };
|
||||
let db = async { db::Db::open(&db::location(), keep_open).instrument(db::step("open_database")).await }
|
||||
.instrument(start.clone())
|
||||
.await?;
|
||||
|
||||
// A daemon owns the state; don't have two processes downloading the same thing.
|
||||
let wire_cmd = match &cli.command {
|
||||
Command::Fetch { feed, force } => Some(Cmd::Fetch { feed: feed.clone(), force: *force, feeds: vec![] }),
|
||||
Command::Reap { dry_run } => Some(Cmd::Reap { dry_run: *dry_run }),
|
||||
Command::Status => Some(Cmd::Status),
|
||||
Command::List
|
||||
| Command::Daemon { .. }
|
||||
| Command::User { .. }
|
||||
| Command::Add { .. }
|
||||
| Command::Rm { .. }
|
||||
| Command::Import { .. }
|
||||
| Command::Export { .. } => None,
|
||||
};
|
||||
if let Some(cmd) = &wire_cmd
|
||||
&& !cli.local
|
||||
&& ipc::daemon_is_live(&cfg.general.socket).await
|
||||
{
|
||||
return ipc::proxy(&cfg.general.socket, cmd).await;
|
||||
}
|
||||
|
||||
let socket = cfg.general.socket.clone();
|
||||
let cfg =
|
||||
async { catalogue::assemble_config(&db, cfg, &config_path).instrument(db::step("assemble_config")).await }
|
||||
.instrument(start.clone())
|
||||
.await?;
|
||||
|
||||
let is_daemon = matches!(cli.command, Command::Daemon { .. });
|
||||
// These write the catalogue from a process of their own, which a running daemon, holding its
|
||||
// own copy, would write over at its next change (#120).
|
||||
let changes_catalogue = matches!(
|
||||
cli.command,
|
||||
Command::Add { .. } | Command::Rm { .. } | Command::Import { .. } | Command::User { cmd: UserCmd::Rm { .. } }
|
||||
);
|
||||
let (events, _) = broadcast::channel(1024);
|
||||
let ctx = Arc::new(Ctx::new(cfg, db, config_path.clone(), is_daemon, &events)?);
|
||||
|
||||
let result = match cli.command {
|
||||
Command::List => catalogue::list(&ctx).await,
|
||||
Command::Daemon { web } => daemon::daemon(ctx, config_path, web, events, start).await,
|
||||
Command::Add { url, folder, keywords, category, list } => {
|
||||
catalogue::add(&ctx, &url, folder, keywords, category, list).await
|
||||
}
|
||||
Command::Rm { feed } => catalogue::rm(&ctx, &feed).await,
|
||||
Command::User { cmd } => cli::user_cmd(&ctx, cmd, std::io::stdin()).await,
|
||||
Command::Import { file } => catalogue::import(&ctx, &file).await,
|
||||
Command::Export { file } => catalogue::export(&ctx, &file).await,
|
||||
_ => cli::run(&ctx, wire_cmd.expect("only List and Daemon have no wire form")).await,
|
||||
};
|
||||
if result.is_ok()
|
||||
&& changes_catalogue
|
||||
&& ipc::daemon_is_live(&socket).await
|
||||
&& let Err(e) = ipc::reload(&socket).await
|
||||
{
|
||||
eprintln!("the running daemon did not take the change ({e:#}); restart it, or it may undo it");
|
||||
}
|
||||
// The batch exporter holds the last few seconds of spans; without this they are lost.
|
||||
if let Some(p) = otel {
|
||||
let _ = p.shutdown();
|
||||
}
|
||||
result
|
||||
}
|
||||
@@ -2,10 +2,11 @@
|
||||
|
||||
use crate::ctx::Ctx;
|
||||
use crate::scan::Outcome;
|
||||
use crate::{catalogue, config, feed};
|
||||
use crate::{catalogue, config, db, feed};
|
||||
use anyhow::Result;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::sync::Arc;
|
||||
use tracing::Instrument;
|
||||
|
||||
/// The feeds an OPML document lists, up to as many as a subscribed list may bring in: each becomes
|
||||
/// a feed in the catalogue, scanned for everyone, and one upload of tens of thousands held the
|
||||
@@ -31,6 +32,7 @@ pub fn opml_feeds(doc: &opml::OPML) -> Result<Vec<(String, String)>> {
|
||||
///
|
||||
/// The caller parses the document and lists its feeds (`opml_feeds`), so each refuses a file in
|
||||
/// its own terms, before anything is touched: a 400 from the web, a message from the CLI.
|
||||
#[tracing::instrument(name = "opml_import", skip_all, fields(feeds = found.len(), user = user_id))]
|
||||
pub async fn subscribe_opml(ctx: &Ctx, found: Vec<(String, String)>, user_id: i64) -> Result<(usize, usize)> {
|
||||
let mut cfg = ctx.edit_cfg().await;
|
||||
// By address, once: each feed looked through every subscription and the whole catalogue, and
|
||||
@@ -110,6 +112,8 @@ pub(crate) const MAX_LISTED: usize = 2000;
|
||||
/// the list is unsubscribed *only if nothing was ever downloaded for it* -- otherwise it
|
||||
/// is kept and flagged, because dropping it would orphan files on disk with nothing in
|
||||
/// the UI to explain them.
|
||||
/// A trace of its own steps (#372): one long `feed` span said nothing of where a slow sync went.
|
||||
#[tracing::instrument(name = "list_sync", skip_all, fields(list = %parent_id, listed = listed.len()))]
|
||||
pub(crate) async fn sync_group(ctx: &Arc<Ctx>, parent_id: &str, listed: &[(String, String)]) -> Result<Outcome> {
|
||||
// Each is a feed scanned for everyone subscribed to the list, and the list is its author's to
|
||||
// write: one of tens of thousands held the scan for hours (#337). davewiner's had 922.
|
||||
@@ -119,74 +123,94 @@ pub(crate) async fn sync_group(ctx: &Arc<Ctx>, parent_id: &str, listed: &[(Strin
|
||||
listed.len()
|
||||
);
|
||||
let cfg = ctx.cfg();
|
||||
let existing = ctx.db.managed_feeds().await?;
|
||||
let existing = ctx.db.managed_feeds().instrument(db::step("managed_feeds")).await?;
|
||||
let mut added = vec![];
|
||||
// Read once, not once a new feed: each read is the whole feeds table.
|
||||
let known = ctx.db.feed_urls().await?;
|
||||
let known = ctx.db.feed_urls().instrument(db::step("feed_urls")).await?;
|
||||
let fixed: HashSet<&str> = cfg.feeds.keys().chain(existing.iter().map(|m| &m.id)).map(String::as_str).collect();
|
||||
let known_ids: HashSet<&str> = known.iter().map(|(id, _)| id.as_str()).collect();
|
||||
|
||||
for (title, url) in listed {
|
||||
// Already known, whether derived or promoted into the config.
|
||||
if let Some(m) = existing.iter().find(|m| &m.url == url) {
|
||||
ctx.db.upsert_managed(&m.id, url, title, parent_id).await?;
|
||||
continue;
|
||||
async {
|
||||
for (title, url) in listed {
|
||||
// Already known, whether derived or promoted into the config.
|
||||
if let Some(m) = existing.iter().find(|m| &m.url == url) {
|
||||
ctx.db.upsert_managed(&m.id, url, title, parent_id).await?;
|
||||
continue;
|
||||
}
|
||||
// A Patreon show you added by hand may be spelled differently from the one listed.
|
||||
if cfg.feeds.values().any(|f| feed::same_feed(&f.url, url)) {
|
||||
continue;
|
||||
}
|
||||
// A removed feed keeps its rows, so its id is only free again for the same feed.
|
||||
let its_own: Vec<&str> =
|
||||
known.iter().filter(|(_, u)| feed::same_feed(u, url)).map(|(id, _)| id.as_str()).collect();
|
||||
let id = config::unique_slug(title, |s| {
|
||||
fixed.contains(s) || added.iter().any(|a| a == s) || (known_ids.contains(s) && !its_own.contains(&s))
|
||||
});
|
||||
ctx.db.upsert_managed(&id, url, title, parent_id).await?;
|
||||
added.push(id);
|
||||
}
|
||||
// A Patreon show you added by hand may be spelled differently from the one listed.
|
||||
if cfg.feeds.values().any(|f| feed::same_feed(&f.url, url)) {
|
||||
continue;
|
||||
}
|
||||
// A removed feed keeps its rows, so its id is only free again for the same feed.
|
||||
let its_own: Vec<&str> =
|
||||
known.iter().filter(|(_, u)| feed::same_feed(u, url)).map(|(id, _)| id.as_str()).collect();
|
||||
let id = config::unique_slug(title, |s| {
|
||||
fixed.contains(s) || added.iter().any(|a| a == s) || (known_ids.contains(s) && !its_own.contains(&s))
|
||||
});
|
||||
ctx.db.upsert_managed(&id, url, title, parent_id).await?;
|
||||
added.push(id);
|
||||
anyhow::Ok(())
|
||||
}
|
||||
.instrument(db::step("upsert_listed"))
|
||||
.await?;
|
||||
|
||||
// Whoever subscribes to the OPML subscribes to what it lists: that is what taking a
|
||||
// subscription means. Their own feeds are untouched.
|
||||
let managed = ctx.db.managed_feeds().await?;
|
||||
let list_and_feeds = managed
|
||||
.iter()
|
||||
.filter(|m| m.group_id == parent_id)
|
||||
.map(|m| m.id.clone())
|
||||
.chain(std::iter::once(parent_id.to_string()));
|
||||
for id in list_and_feeds {
|
||||
// subscription means. Their own feeds are untouched. Who reads the list is asked once: asked
|
||||
// for each listed feed, a list of 922 was thousands of the same queries a sync (#377).
|
||||
async {
|
||||
let mut readers = vec![];
|
||||
for user in ctx.db.users().await? {
|
||||
if ctx.db.subscription(user.id, parent_id).await?.is_some() {
|
||||
ctx.db.subscribe(user.id, &id).await?;
|
||||
readers.push(user.id);
|
||||
}
|
||||
}
|
||||
let managed = ctx.db.managed_feeds().await?;
|
||||
let list_and_feeds = managed
|
||||
.iter()
|
||||
.filter(|m| m.group_id == parent_id)
|
||||
.map(|m| m.id.clone())
|
||||
.chain(std::iter::once(parent_id.to_string()));
|
||||
for id in list_and_feeds {
|
||||
for &user in &readers {
|
||||
ctx.db.subscribe(user, &id).await?;
|
||||
}
|
||||
}
|
||||
anyhow::Ok(())
|
||||
}
|
||||
.instrument(db::step("subscribe_readers"))
|
||||
.await?;
|
||||
|
||||
// Anything in this group the OPML no longer lists.
|
||||
let mut removed = 0;
|
||||
let mut removed = vec![];
|
||||
let mut kept = 0;
|
||||
let parent = ctx.cfg().feeds.get(parent_id).cloned();
|
||||
let in_group = existing.iter().filter(|m| m.group_id == parent_id);
|
||||
for m in in_group {
|
||||
if listed.iter().any(|(_, u)| u == &m.url) {
|
||||
continue;
|
||||
}
|
||||
if catalogue::promote_kept(ctx, m, parent.as_ref()).await? {
|
||||
kept += 1;
|
||||
tracing::info!(feed = %m.id, "dropped from the OPML but someone kept it; a feed of its own now");
|
||||
continue;
|
||||
}
|
||||
if ctx.db.downloaded_count(&m.id).await.unwrap_or(1) > 0 {
|
||||
// Never orphan a downloaded file: keep the feed and say why in the UI.
|
||||
ctx.db.set_orphaned(&m.id, true).await?;
|
||||
kept += 1;
|
||||
tracing::info!(feed = %m.id, "dropped from the OPML but has downloads; keeping it");
|
||||
} else {
|
||||
ctx.db.drop_managed(&m.id).await?;
|
||||
removed += 1;
|
||||
tracing::info!(feed = %m.id, "dropped from the OPML with nothing downloaded; removed");
|
||||
async {
|
||||
let in_group = existing.iter().filter(|m| m.group_id == parent_id);
|
||||
for m in in_group {
|
||||
if listed.iter().any(|(_, u)| u == &m.url) {
|
||||
continue;
|
||||
}
|
||||
if catalogue::promote_kept(ctx, m, parent.as_ref()).await? {
|
||||
kept += 1;
|
||||
tracing::info!(feed = %m.id, "dropped from the OPML but someone kept it; a feed of its own now");
|
||||
continue;
|
||||
}
|
||||
if ctx.db.downloaded_count(&m.id).await.unwrap_or(1) > 0 {
|
||||
// Never orphan a downloaded file: keep the feed and say why in the UI.
|
||||
ctx.db.set_orphaned(&m.id, true).await?;
|
||||
kept += 1;
|
||||
tracing::info!(feed = %m.id, "dropped from the OPML but has downloads; keeping it");
|
||||
} else {
|
||||
ctx.db.drop_managed(&m.id).await?;
|
||||
removed.push(m.id.clone());
|
||||
tracing::info!(feed = %m.id, "dropped from the OPML with nothing downloaded; removed");
|
||||
}
|
||||
}
|
||||
anyhow::Ok(())
|
||||
}
|
||||
.instrument(db::step("drop_unlisted"))
|
||||
.await?;
|
||||
|
||||
Ok(Outcome::Opml { added, removed, kept, total: listed.len() })
|
||||
}
|
||||
@@ -267,4 +291,21 @@ mod tests {
|
||||
assert!(ctx.db.subscription(me, "b").await.unwrap().is_some());
|
||||
assert!(ctx.db.subscription(you, "b").await.unwrap().is_none());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_feed_the_list_drops_takes_its_subscriptions_with_it() {
|
||||
let mut cfg = config::Config::default();
|
||||
cfg.feeds.insert("opml".into(), feed());
|
||||
let ctx = Arc::new(test_ctx(cfg).await);
|
||||
let me = ctx.db.create_user("me", None, false).await.unwrap();
|
||||
ctx.db.upsert_managed("a", "http://x/a.xml", "a", "opml").await.unwrap();
|
||||
ctx.db.subscribe(me, "opml").await.unwrap();
|
||||
ctx.db.subscribe(me, "a").await.unwrap();
|
||||
|
||||
// Left subscribed, All Subscriptions listed a's items with no feed to name them (#478).
|
||||
let Outcome::Opml { removed, .. } = sync_group(&ctx, "opml", &[]).await.unwrap() else { panic!("a list") };
|
||||
assert_eq!(removed, ["a"], "the scan is told, so it does not read a again");
|
||||
assert!(ctx.db.subscription(me, "a").await.unwrap().is_none());
|
||||
assert!(ctx.db.subscription(me, "opml").await.unwrap().is_some());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,89 +1,120 @@
|
||||
//! Log lines as JSON for Loki, carrying the trace and span they belong to.
|
||||
|
||||
use crate::logbuf;
|
||||
use ipx_directory::otel::json_layer;
|
||||
use std::io::IsTerminal;
|
||||
use tracing::Subscriber;
|
||||
use tracing_subscriber::EnvFilter;
|
||||
use tracing_subscriber::fmt::format::JsonFields;
|
||||
use tracing_subscriber::fmt::{FmtContext, FormatEvent, FormatFields};
|
||||
use tracing_subscriber::registry::LookupSpan;
|
||||
|
||||
/// The JSON log line with the trace and span it belongs to (#91), so a line in Loki leads to its
|
||||
/// trace in Tempo. The JSON formatter cannot take a field of its own, so the ids go on the end of
|
||||
/// the object it writes. A line outside any traced span, or with no trace exporter, is unchanged.
|
||||
pub(crate) struct WithTrace<F>(F);
|
||||
|
||||
impl<S, N, F> FormatEvent<S, N> for WithTrace<F>
|
||||
where
|
||||
F: FormatEvent<S, N>,
|
||||
S: Subscriber + for<'a> LookupSpan<'a>,
|
||||
N: for<'w> FormatFields<'w> + 'static,
|
||||
{
|
||||
fn format_event(
|
||||
&self,
|
||||
ctx: &FmtContext<'_, S, N>,
|
||||
mut w: tracing_subscriber::fmt::format::Writer<'_>,
|
||||
ev: &tracing::Event<'_>,
|
||||
) -> std::fmt::Result {
|
||||
use opentelemetry::trace::TraceContextExt;
|
||||
use tracing_opentelemetry::OpenTelemetrySpanExt;
|
||||
let current = tracing::Span::current();
|
||||
let sc = if current.is_none() { None } else { Some(current.context().span().span_context().clone()) };
|
||||
let Some(sc) = sc.filter(|c| c.is_valid()) else {
|
||||
return self.0.format_event(ctx, w, ev);
|
||||
};
|
||||
let mut line = String::new();
|
||||
self.0.format_event(ctx, tracing_subscriber::fmt::format::Writer::new(&mut line), ev)?;
|
||||
match line.trim_end().strip_suffix('}') {
|
||||
Some(body) => writeln!(w, r#"{body},"trace_id":"{}","span_id":"{}"}}"#, sc.trace_id(), sc.span_id()),
|
||||
None => w.write_str(&line),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Sets up logging: to stderr, as text or as JSON (`IPX_LOG_FORMAT`), into the ring the page's log
|
||||
/// reads, and to the trace exporter when there is one.
|
||||
pub(crate) fn init(otel: Option<&opentelemetry_sdk::trace::SdkTracerProvider>) {
|
||||
use tracing_subscriber::util::SubscriberInitExt;
|
||||
let json = std::env::var("IPX_LOG_FORMAT").is_ok_and(|f| f.eq_ignore_ascii_case("json"));
|
||||
subscriber(json, otel, std::io::stderr).init();
|
||||
}
|
||||
|
||||
/// The subscriber `init` installs, writing to `out`: the test's lines are production's.
|
||||
fn subscriber<W>(
|
||||
json: bool,
|
||||
otel: Option<&opentelemetry_sdk::trace::SdkTracerProvider>,
|
||||
out: W,
|
||||
) -> impl tracing::Subscriber + Send + Sync
|
||||
where
|
||||
W: for<'w> tracing_subscriber::fmt::MakeWriter<'w> + Send + Sync + 'static,
|
||||
{
|
||||
// Everything goes to stderr as before, and is mirrored into a ring the UI can read.
|
||||
use tracing_subscriber::Layer;
|
||||
use tracing_subscriber::layer::SubscriberExt;
|
||||
use tracing_subscriber::util::SubscriberInitExt;
|
||||
// Two filters, deliberately different. stderr follows IPX_LOG; the in-app buffer
|
||||
// keeps debug as well, so the log view can show protocol traffic and routine
|
||||
// skips that would be noise on a terminal. IPX_UI_LOG overrides it.
|
||||
let stderr_filter = || EnvFilter::try_from_env("IPX_LOG").unwrap_or_else(|_| "ipx=info".into());
|
||||
// One JSON object a line for Loki (#91), with each event's fields as its own; text
|
||||
// otherwise, for someone reading a terminal.
|
||||
let json = std::env::var("IPX_LOG_FORMAT").is_ok_and(|f| f.eq_ignore_ascii_case("json"));
|
||||
let ui_filter = EnvFilter::try_from_env("IPX_UI_LOG").unwrap_or_else(|_| "ipx=debug".into());
|
||||
tracing_subscriber::registry()
|
||||
.with((!json).then(|| {
|
||||
tracing_subscriber::fmt::layer()
|
||||
.with_writer(std::io::stderr)
|
||||
// Colour for a terminal only: in docker logs and Loki the escapes are noise
|
||||
// every query has to strip (#88).
|
||||
.with_ansi(IsTerminal::is_terminal(&std::io::stderr()))
|
||||
.with_filter(stderr_filter())
|
||||
}))
|
||||
.with(json.then(|| {
|
||||
tracing_subscriber::fmt::layer()
|
||||
.fmt_fields(JsonFields::new())
|
||||
.event_format(WithTrace(
|
||||
tracing_subscriber::fmt::format()
|
||||
.json()
|
||||
.flatten_event(true)
|
||||
.with_current_span(true)
|
||||
.with_span_list(false),
|
||||
))
|
||||
.with_writer(std::io::stderr)
|
||||
.with_filter(stderr_filter())
|
||||
}))
|
||||
.with(logbuf::RingLayer.with_filter(ui_filter))
|
||||
.with(otel.as_ref().map(|p| {
|
||||
// Boxed rather than an Option of each: Option's Layer does not pass on_register_dispatch on,
|
||||
// which the JSON layer finds each line's trace by (#408).
|
||||
let stderr: Box<dyn Layer<tracing_subscriber::Registry> + Send + Sync> = if json {
|
||||
json_layer(out, true).with_filter(stderr_filter()).boxed()
|
||||
} else {
|
||||
tracing_subscriber::fmt::layer()
|
||||
.with_writer(out)
|
||||
// Colour for a terminal only: in docker logs and Loki the escapes are noise
|
||||
// every query has to strip (#88).
|
||||
.with_ansi(IsTerminal::is_terminal(&std::io::stderr()))
|
||||
.with_filter(stderr_filter())
|
||||
.boxed()
|
||||
};
|
||||
tracing_subscriber::registry().with(stderr).with(logbuf::RingLayer.with_filter(ui_filter)).with(otel.as_ref().map(
|
||||
|p| {
|
||||
use opentelemetry::trace::TracerProvider;
|
||||
tracing_opentelemetry::layer()
|
||||
.with_tracer(p.tracer("ipx"))
|
||||
.with_filter(EnvFilter::new("ipx=info,ipx_feed=info"))
|
||||
}))
|
||||
.init();
|
||||
},
|
||||
))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::ipc::{Emitter, Event};
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
/// What the layer wrote, kept for the test to read.
|
||||
struct Lines(Arc<Mutex<Vec<u8>>>);
|
||||
|
||||
impl std::io::Write for Lines {
|
||||
fn write(&mut self, b: &[u8]) -> std::io::Result<usize> {
|
||||
self.0.lock().unwrap().extend_from_slice(b);
|
||||
Ok(b.len())
|
||||
}
|
||||
fn flush(&mut self) -> std::io::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_scans_events_are_json_lines_with_the_fields_grafana_reads() {
|
||||
let buf = Arc::new(Mutex::new(vec![]));
|
||||
let b = buf.clone();
|
||||
let provider = opentelemetry_sdk::trace::SdkTracerProvider::builder().build();
|
||||
let sub = subscriber(true, Some(&provider), move || Lines(b.clone()));
|
||||
// Scoped, where the formatter once found no dispatcher and no line named its trace (#408).
|
||||
tracing::subscriber::with_default(sub, || {
|
||||
let out = Emitter::terminal();
|
||||
out.emit(Event::FeedDone { feed: "log-test-f".into(), new: 2, downloaded: 1, failed: 0, torrents: 0 });
|
||||
let scan = tracing::info_span!("scan");
|
||||
let _in = scan.enter();
|
||||
out.emit(Event::DownloadDone {
|
||||
feed: "log-test-f".into(),
|
||||
enclosure: 7,
|
||||
url: "https://x.test/a.mp3".into(),
|
||||
path: "/downloads/a.mp3".into(),
|
||||
bytes: 1234,
|
||||
});
|
||||
out.emit(Event::FeedError { feed: "log-test-g".into(), msg: "HTTP 404 Not Found".into() });
|
||||
});
|
||||
let text = String::from_utf8(buf.lock().unwrap().clone()).unwrap();
|
||||
let lines: Vec<serde_json::Value> = text.lines().map(|l| serde_json::from_str(l).expect(l)).collect();
|
||||
let find = |ev: &str| {
|
||||
lines
|
||||
.iter()
|
||||
.find(|l| l["ev"] == ev && l["feed"].as_str().is_some_and(|f| f.starts_with("log-test-")))
|
||||
.unwrap_or_else(|| panic!("no {ev} in {text}"))
|
||||
};
|
||||
|
||||
let done = find("feed_done");
|
||||
assert_eq!((done["feed"].as_str(), done["new"].as_u64()), (Some("log-test-f"), Some(2)), "{done}");
|
||||
assert!(done.get("trace_id").is_none(), "outside a span there is no trace to name: {done}");
|
||||
let saved = find("download_done");
|
||||
assert_eq!(saved["bytes"].as_u64(), Some(1234), "{saved}");
|
||||
let failed = find("feed_error");
|
||||
assert_eq!((failed["feed"].as_str(), failed["msg"].as_str()), (Some("log-test-g"), Some("HTTP 404 Not Found")));
|
||||
assert!(failed["error.type"].is_string(), "the failure's kind: {failed}");
|
||||
for l in [saved, failed] {
|
||||
let (t, s) = (l["trace_id"].as_str().unwrap_or(""), l["span_id"].as_str().unwrap_or(""));
|
||||
assert!(t.len() == 32 && s.len() == 16, "inside a span a line names its trace: {l}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,113 +1,5 @@
|
||||
//! ipx, the daemon and its command line. The code is in the modules below; this only starts it.
|
||||
//! `ipxd`, the daemon and its command line; everything is in the library (lib.rs).
|
||||
|
||||
mod access;
|
||||
mod art;
|
||||
mod auth;
|
||||
mod catalogue;
|
||||
mod cli;
|
||||
mod config;
|
||||
mod ctx;
|
||||
mod daemon;
|
||||
mod db;
|
||||
mod deadair;
|
||||
mod directory;
|
||||
mod download;
|
||||
mod entity;
|
||||
// The crate root's import, so `crate::feed` reaches it from every module as before.
|
||||
use ipx_feed as feed;
|
||||
mod ipc;
|
||||
mod lists;
|
||||
mod logbuf;
|
||||
mod logging;
|
||||
mod metrics;
|
||||
mod retention;
|
||||
mod scan;
|
||||
mod signals;
|
||||
#[cfg(test)]
|
||||
mod test_util;
|
||||
mod torrent;
|
||||
mod web;
|
||||
|
||||
use crate::cli::{Cli, Command, UserCmd};
|
||||
use crate::ctx::Ctx;
|
||||
use crate::ipc::Command as Cmd;
|
||||
use anyhow::Result;
|
||||
use clap::Parser;
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::broadcast;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<()> {
|
||||
let cli = Cli::parse();
|
||||
// The daemon only: a trace for every `ipx status` a script runs would bury the ones worth
|
||||
// looking at.
|
||||
let otel = match cli.command {
|
||||
Command::Daemon { .. } => ipx_directory::otel::provider("ipx")?,
|
||||
_ => None,
|
||||
};
|
||||
logging::init(otel.as_ref());
|
||||
|
||||
let config_path = cli.config.clone().unwrap_or_else(config::config_path);
|
||||
let cfg = config::Config::load(&config_path)?;
|
||||
// The daemon holds its connections open; a one-off command opens what it uses.
|
||||
let keep_open = matches!(cli.command, Command::Daemon { .. });
|
||||
let db = db::Db::open(&db::location(), keep_open).await?;
|
||||
|
||||
// A daemon owns the state; don't have two processes downloading the same thing.
|
||||
let wire_cmd = match &cli.command {
|
||||
Command::Fetch { feed, force } => Some(Cmd::Fetch { feed: feed.clone(), force: *force, feeds: vec![] }),
|
||||
Command::Reap { dry_run } => Some(Cmd::Reap { dry_run: *dry_run }),
|
||||
Command::Status => Some(Cmd::Status),
|
||||
Command::List
|
||||
| Command::Daemon { .. }
|
||||
| Command::User { .. }
|
||||
| Command::Add { .. }
|
||||
| Command::Rm { .. }
|
||||
| Command::Import { .. }
|
||||
| Command::Export { .. } => None,
|
||||
};
|
||||
if let Some(cmd) = &wire_cmd
|
||||
&& !cli.local
|
||||
&& ipc::daemon_is_live(&cfg.general.socket).await
|
||||
{
|
||||
return ipc::proxy(&cfg.general.socket, cmd).await;
|
||||
}
|
||||
|
||||
let socket = cfg.general.socket.clone();
|
||||
let cfg = catalogue::assemble_config(&db, cfg, &config_path).await?;
|
||||
|
||||
let is_daemon = matches!(cli.command, Command::Daemon { .. });
|
||||
// These write the catalogue from a process of their own, which a running daemon, holding its
|
||||
// own copy, would write over at its next change (#120).
|
||||
let changes_catalogue = matches!(
|
||||
cli.command,
|
||||
Command::Add { .. } | Command::Rm { .. } | Command::Import { .. } | Command::User { cmd: UserCmd::Rm { .. } }
|
||||
);
|
||||
let (events, _) = broadcast::channel(1024);
|
||||
let ctx = Arc::new(Ctx::new(cfg, db, config_path.clone(), is_daemon, &events)?);
|
||||
|
||||
let result = match cli.command {
|
||||
Command::List => catalogue::list(&ctx).await,
|
||||
Command::Daemon { web } => daemon::daemon(ctx, config_path, web, events).await,
|
||||
Command::Add { url, folder, keywords, category, list } => {
|
||||
catalogue::add(&ctx, &url, folder, keywords, category, list).await
|
||||
}
|
||||
Command::Rm { feed } => catalogue::rm(&ctx, &feed).await,
|
||||
Command::User { cmd } => cli::user_cmd(&ctx, cmd).await,
|
||||
Command::Import { file } => catalogue::import(&ctx, &file).await,
|
||||
Command::Export { file } => catalogue::export(&ctx, &file).await,
|
||||
_ => cli::run(&ctx, wire_cmd.expect("only List and Daemon have no wire form")).await,
|
||||
};
|
||||
if result.is_ok()
|
||||
&& changes_catalogue
|
||||
&& ipc::daemon_is_live(&socket).await
|
||||
&& let Err(e) = ipc::reload(&socket).await
|
||||
{
|
||||
eprintln!("the running daemon did not take the change ({e:#}); restart it, or it may undo it");
|
||||
}
|
||||
// The batch exporter holds the last few seconds of spans; without this they are lost.
|
||||
if let Some(p) = otel {
|
||||
let _ = p.shutdown();
|
||||
}
|
||||
result
|
||||
fn main() -> anyhow::Result<()> {
|
||||
ipx::main()
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
//! the text format is a few lines, and a client library would be a dependency for them.
|
||||
|
||||
use axum::Router;
|
||||
use ipx_directory::metrics::{Histograms, SECONDS, label, method};
|
||||
use std::collections::BTreeMap;
|
||||
use std::fmt::Write as _;
|
||||
use std::net::SocketAddr;
|
||||
@@ -62,8 +63,23 @@ pub struct Stats {
|
||||
job: std::sync::Mutex<Option<(&'static str, Instant)>>,
|
||||
/// Jobs finished, by kind: how many, and their seconds all told.
|
||||
jobs: std::sync::Mutex<BTreeMap<&'static str, (u64, f64)>>,
|
||||
/// Feed failures by kind, `error.type` (#368).
|
||||
feed_failures: std::sync::Mutex<BTreeMap<String, u64>>,
|
||||
pub new_items: AtomicU64,
|
||||
pub downloads_done: AtomicU64,
|
||||
pub downloads_failed: AtomicU64,
|
||||
pub deadair_saved_seconds: AtomicU64,
|
||||
}
|
||||
|
||||
/// Requests answered, by method, route and status (#364).
|
||||
pub static REQUESTS: Histograms = Histograms::new(SECONDS);
|
||||
/// Each statement's time once it has a connection, by its first word (#367).
|
||||
pub static QUERIES: Histograms = Histograms::new(SECONDS);
|
||||
/// The wait for a connection, as hand-written SQL in db.rs sees it (#367).
|
||||
pub static DB_WAIT: Histograms = Histograms::new(SECONDS);
|
||||
/// The page's timings, by `ev`: a click to its list drawn, play to sound, a stall (#369).
|
||||
pub static PAGE: Histograms = Histograms::new(&[0.05, 0.1, 0.25, 0.5, 1.0, 2.0, 5.0, 10.0, 30.0]);
|
||||
|
||||
impl Stats {
|
||||
pub const fn new() -> Self {
|
||||
Self {
|
||||
@@ -89,6 +105,46 @@ impl Stats {
|
||||
people: std::sync::Mutex::new(BTreeMap::new()),
|
||||
job: std::sync::Mutex::new(None),
|
||||
jobs: std::sync::Mutex::new(BTreeMap::new()),
|
||||
feed_failures: std::sync::Mutex::new(BTreeMap::new()),
|
||||
new_items: AtomicU64::new(0),
|
||||
downloads_done: AtomicU64::new(0),
|
||||
downloads_failed: AtomicU64::new(0),
|
||||
deadair_saved_seconds: AtomicU64::new(0),
|
||||
}
|
||||
}
|
||||
|
||||
/// Counts a request answered. `route` is the pattern matched, "" for none, so a scanner's
|
||||
/// made-up paths are one row.
|
||||
pub fn request(&self, m: &str, route: &str, status: u16, secs: f64) {
|
||||
REQUESTS.observe(&format!("method=\"{}\",route=\"{}\",status=\"{status}\"", method(m), label(route)), secs);
|
||||
}
|
||||
|
||||
/// Counts what an event says happened (#368): one place, as every event passes `emit`.
|
||||
pub fn event(&self, e: &crate::ipc::Event) {
|
||||
use crate::ipc::Event;
|
||||
match e {
|
||||
Event::FeedDone { new, .. } => {
|
||||
self.new_items.fetch_add(*new as u64, Relaxed);
|
||||
}
|
||||
Event::FeedError { msg, .. } => {
|
||||
let kind = crate::feed::failure_kind(msg).0;
|
||||
*self
|
||||
.feed_failures
|
||||
.lock()
|
||||
.expect("a panic elsewhere poisoned this lock")
|
||||
.entry(kind.to_string())
|
||||
.or_default() += 1;
|
||||
}
|
||||
Event::DownloadDone { .. } => {
|
||||
self.downloads_done.fetch_add(1, Relaxed);
|
||||
}
|
||||
Event::DownloadError { .. } => {
|
||||
self.downloads_failed.fetch_add(1, Relaxed);
|
||||
}
|
||||
Event::DeadairDone { saved_s, .. } => {
|
||||
self.deadair_saved_seconds.fetch_add((*saved_s).max(0) as u64, Relaxed);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -102,6 +158,12 @@ impl Stats {
|
||||
}
|
||||
}
|
||||
|
||||
/// What `who` has been billed on `key`: items judged, Signals fit, input and output tokens.
|
||||
#[cfg(test)]
|
||||
pub fn billed(&self, who: &str, key: &'static str) -> [f64; 4] {
|
||||
self.people.lock().unwrap().get(&(who.to_owned(), key)).copied().unwrap_or_default()
|
||||
}
|
||||
|
||||
/// Marks the worker as on a job of `kind` until what it returns is dropped, however the job
|
||||
/// ends: finished, failed or stopped.
|
||||
pub fn job(&'static self, kind: &'static str) -> Job {
|
||||
@@ -308,6 +370,42 @@ fn work(out: &mut String, st: &Stats, event_queue: usize, retries: usize, art_li
|
||||
"Web requests being answered.",
|
||||
&[("", st.http_in_flight.load(Relaxed) as f64)],
|
||||
);
|
||||
REQUESTS.render(
|
||||
out,
|
||||
"ipx_http_request_duration_seconds",
|
||||
"Web requests answered and the seconds each took, by method, route and status.",
|
||||
);
|
||||
QUERIES.render(out, "ipx_db_query_seconds", "Statements' time once they had a connection, by their first word.");
|
||||
DB_WAIT.render(out, "ipx_db_wait_seconds", "The wait for a database connection, as hand-written SQL saw it.");
|
||||
PAGE.render(
|
||||
out,
|
||||
"ipx_page_seconds",
|
||||
"What the page timed: a click to its list drawn, play to sound, a stall's wait.",
|
||||
);
|
||||
let failures = st.feed_failures.lock().expect("a panic elsewhere poisoned this lock").clone();
|
||||
let labels: Vec<String> = failures.keys().map(|k| format!("error_type=\"{}\"", label(k))).collect();
|
||||
counter(
|
||||
out,
|
||||
"ipx_feed_failures_total",
|
||||
"Feeds that failed to fetch or parse, by kind: an HTTP status, dns, timeout, tls, parse...",
|
||||
&labels.iter().zip(failures.values()).map(|(l, v)| (l.as_str(), *v as f64)).collect::<Vec<_>>(),
|
||||
);
|
||||
counter(out, "ipx_new_items_total", "New items scans found.", &[("", st.new_items.load(Relaxed) as f64)]);
|
||||
counter(
|
||||
out,
|
||||
"ipx_downloads_total",
|
||||
"Downloads finished, saved or failed.",
|
||||
&[
|
||||
("result=\"saved\"", st.downloads_done.load(Relaxed) as f64),
|
||||
("result=\"failed\"", st.downloads_failed.load(Relaxed) as f64),
|
||||
],
|
||||
);
|
||||
counter(
|
||||
out,
|
||||
"ipx_deadair_saved_seconds_total",
|
||||
"Seconds of silence DeadAir found in the files it looked at.",
|
||||
&[("", st.deadair_saved_seconds.load(Relaxed) as f64)],
|
||||
);
|
||||
gauge(
|
||||
out,
|
||||
"ipx_scan_feeds_fetching",
|
||||
@@ -429,11 +527,6 @@ fn work(out: &mut String, st: &Stats, event_queue: usize, retries: usize, art_li
|
||||
}
|
||||
}
|
||||
|
||||
/// A label's value as Prometheus's text format reads it.
|
||||
fn label(v: &str) -> String {
|
||||
v.replace('\\', "\\\\").replace('"', "\\\"").replace('\n', "\\n")
|
||||
}
|
||||
|
||||
/// A counter, which only goes up while the daemon runs, in Prometheus's text format.
|
||||
pub fn counter(out: &mut String, name: &str, help: &str, samples: &[(&str, f64)]) {
|
||||
write(out, "counter", name, help, samples);
|
||||
@@ -457,6 +550,54 @@ fn write(out: &mut String, kind: &str, name: &str, help: &str, samples: &[(&str,
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// The names the dashboard and the alert rules query, read out of what generates them.
|
||||
fn queried() -> std::collections::BTreeSet<String> {
|
||||
let src = [include_str!("../../../grafana/dashboard.py"), include_str!("../../../grafana/alerts.py")].concat();
|
||||
src.match_indices("ipx_")
|
||||
.filter(|(i, _)| !src[..*i].ends_with(|c: char| c.is_ascii_alphanumeric() || c == '_'))
|
||||
.map(|(i, _)| {
|
||||
let rest = &src[i..];
|
||||
let end = rest.find(|c: char| !(c.is_ascii_lowercase() || c == '_')).unwrap_or(rest.len());
|
||||
(&rest[..end], rest[end..].starts_with("::"))
|
||||
})
|
||||
// A log target (ipx_directory::http) is not a metric; the coverage numbers are
|
||||
// node-exporter's, written by scripts/coverage-metrics.ts.
|
||||
.filter(|(n, target)| !target && !n.starts_with("ipx_coverage_"))
|
||||
.map(|(n, _)| n.to_string())
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn every_metric_grafana_queries_is_served() {
|
||||
let ctx = Arc::new(crate::test_util::test_ctx(crate::config::Config::default()).await);
|
||||
let s = Sources {
|
||||
ctx,
|
||||
cmds: tokio::sync::mpsc::channel(1).0,
|
||||
events: tokio::sync::broadcast::channel(1).0,
|
||||
playing: Default::default(),
|
||||
runtime: Handle::current(),
|
||||
started: Instant::now(),
|
||||
};
|
||||
// Written only once known (a count taken, a scan finished, a job under way): know them.
|
||||
for v in [&STATS.feeds, &STATS.pending, &STATS.downloaded, &STATS.last_scan_done, &STATS.signals_last_pass] {
|
||||
v.fetch_max(1, Relaxed);
|
||||
}
|
||||
let _job = STATS.job("scan");
|
||||
let out = render(&s);
|
||||
let served: std::collections::BTreeSet<&str> =
|
||||
out.lines().filter_map(|l| l.strip_prefix("# TYPE ")).filter_map(|l| l.split(' ').next()).collect();
|
||||
let names = queried();
|
||||
assert!(names.len() > 30, "the dashboard's names were not found: {names:?}");
|
||||
let missing: Vec<&String> = names
|
||||
.iter()
|
||||
.filter(|n| {
|
||||
let base = ["_bucket", "_count", "_sum"].iter().find_map(|x| n.strip_suffix(x)).unwrap_or(n);
|
||||
!served.contains(n.as_str()) && !served.contains(base)
|
||||
})
|
||||
.collect();
|
||||
assert!(missing.is_empty(), "queried by grafana/ but not served by /metrics: {missing:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gauges_are_written_as_prometheus_reads_them() {
|
||||
let mut out = String::new();
|
||||
@@ -512,6 +653,28 @@ mod tests {
|
||||
assert!(out.contains("ipx_jev_tokens_total{kind=\"output\",user=\"a \\\"b\\\"\",key=\"own\"} 1\n"), "{out}");
|
||||
// The library's counts are left out until the daemon has counted it.
|
||||
assert!(!out.contains("ipx_feeds "));
|
||||
|
||||
for secs in [0.03, 0.04, 2.0] {
|
||||
T.request("GET", "/api/feeds", 200, secs);
|
||||
}
|
||||
T.request("PROPFIND", "", 404, 0.001);
|
||||
T.event(&crate::ipc::Event::FeedError { feed: "f".into(), msg: "connecting: timed out".into() });
|
||||
T.event(&crate::ipc::Event::FeedDone { feed: "f".into(), new: 3, downloaded: 0, failed: 0, torrents: 0 });
|
||||
let mut out = String::new();
|
||||
work(&mut out, &T, 0, 0, 0);
|
||||
let l = "method=\"GET\",route=\"/api/feeds\",status=\"200\"";
|
||||
for line in [
|
||||
format!("ipx_http_request_duration_seconds_bucket{{{l},le=\"0.025\"}} 0\n"),
|
||||
format!("ipx_http_request_duration_seconds_bucket{{{l},le=\"0.05\"}} 2\n"),
|
||||
format!("ipx_http_request_duration_seconds_bucket{{{l},le=\"2.5\"}} 3\n"),
|
||||
format!("ipx_http_request_duration_seconds_bucket{{{l},le=\"+Inf\"}} 3\n"),
|
||||
format!("ipx_http_request_duration_seconds_count{{{l}}} 3\n"),
|
||||
"ipx_http_request_duration_seconds_count{method=\"other\",route=\"\",status=\"404\"} 1\n".into(),
|
||||
"ipx_new_items_total 3\n".into(),
|
||||
"# TYPE ipx_feed_failures_total counter\n".into(),
|
||||
] {
|
||||
assert!(out.contains(&line), "{line} in {out}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
143
crates/ipx/src/play.rs
Normal file
143
crates/ipx/src/play.rs
Normal file
@@ -0,0 +1,143 @@
|
||||
//! Sharing an episode as a page (#447): ipx-play keeps the episode and hands back a short link,
|
||||
//! `play.ipxreader.app/e/<id>`, which anyone can open, as Overcast's links are. Only an episode of
|
||||
//! a public feed goes: the same rule as what the directory is sent, so a private or paid feed's
|
||||
//! file address, which carries someone's access, never leaves this server.
|
||||
|
||||
use crate::ctx::Ctx;
|
||||
use ipx_wire::Message as _;
|
||||
use std::collections::HashMap;
|
||||
use std::time::Duration;
|
||||
|
||||
/// Why there is no link, which the page answers by sharing the episode's own address as it did
|
||||
/// before.
|
||||
#[derive(Debug)]
|
||||
pub enum NoLink {
|
||||
/// No ipx-play address or token in Admin, Sharing.
|
||||
NotSetUp,
|
||||
/// A private or paid feed, or one in a private OPML.
|
||||
Private,
|
||||
/// Nothing to play: a post, or a feed's picture.
|
||||
NoFile,
|
||||
NotFound,
|
||||
/// ipx-play did not answer, or refused.
|
||||
Failed(String),
|
||||
}
|
||||
|
||||
fn target(ctx: &Ctx) -> Option<(String, String)> {
|
||||
let cfg = ctx.cfg();
|
||||
let url = cfg.general.play_url.trim().trim_end_matches('/');
|
||||
let token = cfg.general.play_token.trim();
|
||||
(!url.is_empty() && !token.is_empty()).then(|| (url.to_owned(), token.to_owned()))
|
||||
}
|
||||
|
||||
/// The file to share: the one asked for, or the first that is sound, since a page plays audio.
|
||||
fn pick(encs: &[crate::db::EncRow], asked: Option<i64>) -> Option<&crate::db::EncRow> {
|
||||
match asked {
|
||||
Some(id) => encs.iter().find(|e| e.id == id),
|
||||
None => encs.iter().find(|e| e.mime.as_deref().is_some_and(|m| m.starts_with("audio/"))),
|
||||
}
|
||||
}
|
||||
|
||||
#[tracing::instrument(name = "share_episode", skip(ctx))]
|
||||
pub async fn share(
|
||||
ctx: &Ctx,
|
||||
user_id: i64,
|
||||
feed_id: &str,
|
||||
guid: &str,
|
||||
enc: Option<i64>,
|
||||
) -> Result<ipx_play::Shared, NoLink> {
|
||||
let (base, token) = target(ctx).ok_or(NoLink::NotSetUp)?;
|
||||
let failed = |e: anyhow::Error| NoLink::Failed(format!("{e:#}"));
|
||||
let row = ctx.db.entry(user_id, feed_id, guid).await.map_err(failed)?.ok_or(NoLink::NotFound)?;
|
||||
// Public by the directory's own rule, so the two cannot disagree on what is private.
|
||||
let catalogue = crate::catalogue::subscriptions(ctx).await.map_err(failed)?;
|
||||
let sub = catalogue.iter().find(|s| s.id == feed_id).ok_or(NoLink::NotFound)?;
|
||||
let public = crate::directory::snapshot_entries(&catalogue, &HashMap::new());
|
||||
if !public.iter().any(|p| p.url == sub.cfg.url) {
|
||||
return Err(NoLink::Private);
|
||||
}
|
||||
let file = pick(&row.enclosures, enc).ok_or(NoLink::NoFile)?;
|
||||
let feed = ctx.db.feed_summary(feed_id).await.map_err(failed)?;
|
||||
let episode = ipx_play::Episode {
|
||||
feed_url: sub.cfg.url.clone(),
|
||||
guid: guid.to_owned(),
|
||||
feed_title: feed.title,
|
||||
feed_image: row.image.clone().or(feed.image),
|
||||
title: row.title.clone(),
|
||||
// As the feed sent it: ipx-play sanitizes it for its page.
|
||||
description: row.description.clone(),
|
||||
link: row.link.clone(),
|
||||
published: row.published,
|
||||
duration: row.duration,
|
||||
audio_url: file.url.clone(),
|
||||
mime: file.mime.clone(),
|
||||
};
|
||||
let mut req = ctx
|
||||
.local_client
|
||||
.post(format!("{base}/v1/shares"))
|
||||
.bearer_auth(token)
|
||||
.timeout(Duration::from_secs(15))
|
||||
// Protobuf both ways (#465).
|
||||
.header(reqwest::header::CONTENT_TYPE, ipx_wire::PROTOBUF)
|
||||
.header(reqwest::header::ACCEPT, ipx_wire::PROTOBUF)
|
||||
.body(
|
||||
ipx_wire::pb::play::Episode {
|
||||
feed_url: episode.feed_url,
|
||||
guid: episode.guid,
|
||||
feed_title: episode.feed_title,
|
||||
feed_image: episode.feed_image,
|
||||
title: episode.title,
|
||||
description: episode.description,
|
||||
link: episode.link,
|
||||
published: episode.published,
|
||||
duration: episode.duration,
|
||||
audio_url: episode.audio_url,
|
||||
mime: episode.mime,
|
||||
}
|
||||
.encode_to_vec(),
|
||||
);
|
||||
// Its spans join this request's trace.
|
||||
if let Some(tp) = ipx_directory::otel::traceparent() {
|
||||
req = req.header("traceparent", tp);
|
||||
}
|
||||
let resp = req.send().await.map_err(|e| NoLink::Failed(format!("reaching ipx-play: {e:#}")))?;
|
||||
let status = resp.status();
|
||||
if !status.is_success() {
|
||||
let msg = resp.json::<serde_json::Value>().await.ok().and_then(|v| v["error"].as_str().map(str::to_owned));
|
||||
return Err(NoLink::Failed(format!("ipx-play answered {status}: {}", msg.unwrap_or_default())));
|
||||
}
|
||||
let body = resp.bytes().await.map_err(|e| NoLink::Failed(format!("reading ipx-play's answer: {e:#}")))?;
|
||||
let shared = ipx_wire::pb::play::Shared::decode(body)
|
||||
.map_err(|e| NoLink::Failed(format!("ipx-play's answer did not read: {e}")))?;
|
||||
Ok(ipx_play::Shared { id: shared.id, url: shared.url })
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn enc(id: i64, mime: Option<&str>) -> crate::db::EncRow {
|
||||
crate::db::EncRow {
|
||||
id,
|
||||
deadair: None,
|
||||
deadair_pending: false,
|
||||
feed_id: "f".into(),
|
||||
guid: "g".into(),
|
||||
url: format!("https://x/{id}"),
|
||||
mime: mime.map(Into::into),
|
||||
length: None,
|
||||
path: None,
|
||||
state: "done".into(),
|
||||
last_error: None,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_file_shared_is_the_one_asked_for_or_the_first_sound() {
|
||||
let encs = [enc(1, Some("image/jpeg")), enc(2, Some("video/mp4")), enc(3, Some("audio/mpeg"))];
|
||||
assert_eq!(pick(&encs, None).map(|e| e.id), Some(3));
|
||||
assert_eq!(pick(&encs, Some(2)).map(|e| e.id), Some(2));
|
||||
assert!(pick(&encs, Some(9)).is_none());
|
||||
assert!(pick(&encs[..2], None).is_none(), "a picture and a video are not a page's to play");
|
||||
}
|
||||
}
|
||||
322
crates/ipx/src/push.rs
Normal file
322
crates/ipx/src/push.rs
Normal file
@@ -0,0 +1,322 @@
|
||||
//! The iOS app's icon count, through Apple's push service (#433).
|
||||
//!
|
||||
//! The app shows All Subscriptions' unread count on its icon. While it is open the page keeps it
|
||||
//! right; while it is closed only iOS's background refresh could, which runs when iOS chooses and
|
||||
//! for this app had been rarely. So ipx sends it: once a minute, to each phone whose person's count
|
||||
//! has changed since the last one sent, a push carrying the badge and nothing else, which shows no
|
||||
//! alert and makes no sound.
|
||||
//!
|
||||
//! The app registers its phone through the page (`POST /api/push/devices`), signed in as the page
|
||||
//! is: the address the app's player uses lets through only the player's own paths.
|
||||
|
||||
use crate::ctx::Ctx;
|
||||
use crate::entity::push_devices;
|
||||
use anyhow::{Context, Result};
|
||||
use jsonwebtoken::{Algorithm, EncodingKey, Header};
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
use std::time::{Duration, Instant};
|
||||
use tracing::Instrument;
|
||||
|
||||
/// How often counts are compared. A minute behind is near enough for a number on an icon.
|
||||
// ponytail: one count query per person with a phone, every minute; fine for a household, move to
|
||||
// sending from where read state and new items are written if the people with phones grow many.
|
||||
const EVERY: Duration = Duration::from_secs(60);
|
||||
|
||||
/// Apple refuses a signing token older than an hour, and one made more often than every twenty
|
||||
/// minutes; so each is used for forty.
|
||||
const TOKEN_LIFE: Duration = Duration::from_secs(40 * 60);
|
||||
|
||||
pub async fn run(ctx: Arc<Ctx>, mut stop: tokio::sync::watch::Receiver<bool>) {
|
||||
let mut signer = Signer::default();
|
||||
loop {
|
||||
if ctx.cfg().push.is_set() {
|
||||
let pass = pass(&ctx, &mut signer, None).instrument(tracing::info_span!("push"));
|
||||
tokio::select! {
|
||||
_ = stop.changed() => return,
|
||||
done = pass => if let Err(e) = done {
|
||||
tracing::warn!(error = %format!("{e:#}"), "push: the icon counts could not be sent");
|
||||
},
|
||||
}
|
||||
}
|
||||
tokio::select! {
|
||||
_ = stop.changed() => return,
|
||||
_ = tokio::time::sleep(EVERY) => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Sends each phone its person's count, if it changed. `apns` stands in for Apple's address in tests.
|
||||
async fn pass(ctx: &Ctx, signer: &mut Signer, apns: Option<&str>) -> Result<()> {
|
||||
let devices = ctx.db.push_devices().instrument(crate::db::step("push_devices")).await?;
|
||||
let mut counts: HashMap<i64, i64> = HashMap::new();
|
||||
for d in devices {
|
||||
let n = match counts.get(&d.user_id) {
|
||||
Some(n) => *n,
|
||||
None => {
|
||||
let n = ctx
|
||||
.db
|
||||
.count_of(d.user_id, None, crate::db::Filter::Unread, None)
|
||||
.instrument(crate::db::step("unread_in_all"))
|
||||
.await?;
|
||||
counts.insert(d.user_id, n);
|
||||
n
|
||||
}
|
||||
};
|
||||
if d.badge == Some(n) {
|
||||
continue;
|
||||
}
|
||||
match send(ctx, signer, &d, n, apns).await? {
|
||||
Sent::Yes => ctx.db.set_push_badge(&d.token, n).await?,
|
||||
Sent::Gone => {
|
||||
tracing::info!(user = d.user_id, "push: a phone's app is gone, so its token is forgotten");
|
||||
ctx.db.forget_push_device(&d.token).await?;
|
||||
}
|
||||
// Tried again next minute: the badge kept is still the old one.
|
||||
Sent::NotNow => {}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
enum Sent {
|
||||
Yes,
|
||||
/// Apple says the token is not the app's: uninstalled, or for another app.
|
||||
Gone,
|
||||
NotNow,
|
||||
}
|
||||
|
||||
#[tracing::instrument(name = "push.send", skip_all, fields(user = d.user_id, badge = n, status))]
|
||||
async fn send(ctx: &Ctx, signer: &mut Signer, d: &push_devices::Model, n: i64, apns: Option<&str>) -> Result<Sent> {
|
||||
let cfg = ctx.cfg();
|
||||
let host = if d.sandbox { "https://api.sandbox.push.apple.com" } else { "https://api.push.apple.com" };
|
||||
let r = ctx
|
||||
.client
|
||||
.post(format!("{}/3/device/{}", apns.unwrap_or(host), d.token))
|
||||
.bearer_auth(signer.token(&cfg.push)?)
|
||||
.header("apns-topic", &cfg.push.topic)
|
||||
// A badge alone is still an alert to Apple: background pushes cannot set one.
|
||||
.header("apns-push-type", "alert")
|
||||
// Not urgent, so iOS may hold it to save the battery.
|
||||
.header("apns-priority", "5")
|
||||
// content-available wakes the app for a moment, which the badge alone does not, so its
|
||||
// widget's count follows the icon's (#428). iOS rations those wakes; the badge is set
|
||||
// whether or not one comes.
|
||||
.json(&serde_json::json!({ "aps": { "badge": n, "content-available": 1 } }))
|
||||
.send()
|
||||
.await
|
||||
.context("reaching Apple's push service")?;
|
||||
let status = r.status().as_u16();
|
||||
tracing::Span::current().record("status", status);
|
||||
if status == 200 {
|
||||
return Ok(Sent::Yes);
|
||||
}
|
||||
let reason = r.json::<serde_json::Value>().await.ok().and_then(|b| b["reason"].as_str().map(str::to_owned));
|
||||
let reason = reason.unwrap_or_default();
|
||||
Ok(match (status, reason.as_str()) {
|
||||
(410, _) | (400, "BadDeviceToken" | "DeviceTokenNotForTopic") => Sent::Gone,
|
||||
(403, "ExpiredProviderToken" | "InvalidProviderToken") => {
|
||||
signer.forget();
|
||||
tracing::warn!(status, reason, "push: Apple refused the signing token; check [push] in config.toml");
|
||||
Sent::NotNow
|
||||
}
|
||||
_ => {
|
||||
tracing::warn!(status, reason, "push: Apple did not take an icon count");
|
||||
Sent::NotNow
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// The token each request is signed with: the `.p8` key's ES256 signature over the team and the
|
||||
/// time, made again as Apple's limits ask.
|
||||
#[derive(Default)]
|
||||
struct Signer {
|
||||
key: Option<EncodingKey>,
|
||||
made: Option<(Instant, String)>,
|
||||
}
|
||||
|
||||
impl Signer {
|
||||
fn token(&mut self, cfg: &crate::config::Push) -> Result<String> {
|
||||
if let Some((at, t)) = &self.made
|
||||
&& at.elapsed() < TOKEN_LIFE
|
||||
{
|
||||
return Ok(t.clone());
|
||||
}
|
||||
if self.key.is_none() {
|
||||
let pem = std::fs::read(&cfg.key_file)
|
||||
.with_context(|| format!("reading the APNs key {}", cfg.key_file.display()))?;
|
||||
self.key = Some(p8_key(&pem)?);
|
||||
}
|
||||
let token = sign(self.key.as_ref().context("loaded above")?, &cfg.key_id, &cfg.team_id, crate::db::now())?;
|
||||
self.made = Some((Instant::now(), token.clone()));
|
||||
Ok(token)
|
||||
}
|
||||
|
||||
/// Made again, key and all, on the next send: the key file or its id may have changed.
|
||||
fn forget(&mut self) {
|
||||
*self = Signer::default();
|
||||
}
|
||||
}
|
||||
|
||||
/// The key in a `.p8` file: PKCS#8 in PEM, which this build of jsonwebtoken does not read, so
|
||||
/// the base64 between its two lines is decoded here rather than a crate added for one file.
|
||||
fn p8_key(pem: &[u8]) -> Result<EncodingKey> {
|
||||
let text = std::str::from_utf8(pem).context("the APNs key is not a .p8 key")?;
|
||||
let body: String = text.lines().filter(|l| !l.starts_with("-----")).collect::<String>();
|
||||
let der = base64_decode(body.trim()).context("the APNs key is not a .p8 key")?;
|
||||
Ok(EncodingKey::from_ec_der(&der))
|
||||
}
|
||||
|
||||
fn base64_decode(s: &str) -> Option<Vec<u8>> {
|
||||
let value = |c: u8| match c {
|
||||
b'A'..=b'Z' => Some(c - b'A'),
|
||||
b'a'..=b'z' => Some(c - b'a' + 26),
|
||||
b'0'..=b'9' => Some(c - b'0' + 52),
|
||||
b'+' => Some(62),
|
||||
b'/' => Some(63),
|
||||
_ => None,
|
||||
};
|
||||
let (mut out, mut acc, mut bits) = (Vec::new(), 0u32, 0);
|
||||
for c in s.bytes().filter(|c| !c.is_ascii_whitespace() && *c != b'=') {
|
||||
acc = (acc << 6) | u32::from(value(c)?);
|
||||
bits += 6;
|
||||
if bits >= 8 {
|
||||
bits -= 8;
|
||||
out.push((acc >> bits) as u8);
|
||||
}
|
||||
}
|
||||
Some(out)
|
||||
}
|
||||
|
||||
fn sign(key: &EncodingKey, key_id: &str, team_id: &str, now: i64) -> Result<String> {
|
||||
let mut h = Header::new(Algorithm::ES256);
|
||||
h.kid = Some(key_id.to_owned());
|
||||
Ok(jsonwebtoken::encode(&h, &serde_json::json!({ "iss": team_id, "iat": now }), key)?)
|
||||
}
|
||||
|
||||
/// A device token as the app sends it: Apple's bytes in hex. Checked before it goes into a URL.
|
||||
pub fn valid_token(t: &str) -> bool {
|
||||
(64..=200).contains(&t.len()) && t.bytes().all(|b| b.is_ascii_hexdigit())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use jsonwebtoken::{DecodingKey, Validation};
|
||||
|
||||
// A P-256 key made for this test alone, in the PKCS#8 form Apple's .p8 files are.
|
||||
const KEY: &str = "-----BEGIN PRIVATE KEY-----
|
||||
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQg9x/aoouMrdbCsEAr
|
||||
hLPm+4G3w8qNIHc9V7CMphVZL6WhRANCAATTwL52tAoDzAH+RcFS4cd9azD7xvGT
|
||||
MnDzi5bfjGbabd5hc7sDHiTremhTWJzPs1TdGgtqd9RDURqCHr8jfP+B
|
||||
-----END PRIVATE KEY-----";
|
||||
// Its public half, as the point a JWK names.
|
||||
const PUB_X: &str = "08C-drQKA8wB_kXBUuHHfWsw-8bxkzJw84uW34xm2m0";
|
||||
const PUB_Y: &str = "3mFzuwMeJOt6aFNYnM-zVN0aC2p31ENRGoIevyN8_4E";
|
||||
|
||||
#[test]
|
||||
fn the_signing_token_is_what_apple_reads() {
|
||||
let key = p8_key(KEY.as_bytes()).unwrap();
|
||||
let t = sign(&key, "ABC123DEFG", "8743L8D729", 1_800_000_000).unwrap();
|
||||
let h = jsonwebtoken::decode_header(&t).unwrap();
|
||||
assert_eq!(h.alg, Algorithm::ES256);
|
||||
assert_eq!(h.kid.as_deref(), Some("ABC123DEFG"));
|
||||
let mut v = Validation::new(Algorithm::ES256);
|
||||
v.validate_exp = false;
|
||||
v.required_spec_claims.clear();
|
||||
let c =
|
||||
jsonwebtoken::decode::<serde_json::Value>(&t, &DecodingKey::from_ec_components(PUB_X, PUB_Y).unwrap(), &v)
|
||||
.unwrap()
|
||||
.claims;
|
||||
assert_eq!(c, serde_json::json!({ "iss": "8743L8D729", "iat": 1_800_000_000 }));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn base64_as_pem_has_it() {
|
||||
assert_eq!(base64_decode("aXB4").unwrap(), b"ipx");
|
||||
assert_eq!(base64_decode("aXB4IQ==").unwrap(), b"ipx!");
|
||||
assert!(base64_decode("a!b").is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn only_hex_of_a_tokens_length_is_a_device() {
|
||||
assert!(valid_token(&"ab12".repeat(16)));
|
||||
assert!(!valid_token("ab12"));
|
||||
assert!(!valid_token(&format!("{}/../x", "a".repeat(64))));
|
||||
assert!(!valid_token(&"g".repeat(64)));
|
||||
}
|
||||
|
||||
/// A stand-in for Apple's push service: each device token's first two characters say how it
|
||||
/// answers. Every request is recorded as (token, badge, signed).
|
||||
async fn apns() -> (String, Arc<std::sync::Mutex<Vec<(String, i64, bool)>>>) {
|
||||
use axum::http::{HeaderMap, StatusCode};
|
||||
let seen: Arc<std::sync::Mutex<Vec<(String, i64, bool)>>> = Default::default();
|
||||
let log = seen.clone();
|
||||
let app = axum::Router::new().route(
|
||||
"/3/device/{token}",
|
||||
axum::routing::post(
|
||||
move |axum::extract::Path(token): axum::extract::Path<String>,
|
||||
headers: HeaderMap,
|
||||
axum::Json(body): axum::Json<serde_json::Value>| {
|
||||
let signed = headers.get("authorization").is_some_and(|v| v.as_bytes().starts_with(b"Bearer e"))
|
||||
&& headers.get("apns-topic").is_some_and(|v| v == "net.sdf1.ipx");
|
||||
log.lock().unwrap().push((token.clone(), body["aps"]["badge"].as_i64().unwrap(), signed));
|
||||
let (status, reason) = match &token[..2] {
|
||||
"aa" => (StatusCode::OK, ""),
|
||||
"bb" => (StatusCode::GONE, "Unregistered"),
|
||||
"cc" => (StatusCode::BAD_REQUEST, "BadDeviceToken"),
|
||||
"dd" => (StatusCode::FORBIDDEN, "ExpiredProviderToken"),
|
||||
_ => (StatusCode::INTERNAL_SERVER_ERROR, "InternalServerError"),
|
||||
};
|
||||
async move { (status, axum::Json(serde_json::json!({ "reason": reason }))) }
|
||||
},
|
||||
),
|
||||
);
|
||||
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let at = listener.local_addr().unwrap();
|
||||
tokio::spawn(async move { axum::serve(listener, app).await });
|
||||
(format!("http://{at}"), seen)
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_pass_sends_changed_counts_and_forgets_phones_apple_says_are_gone() {
|
||||
let key = std::env::temp_dir().join(format!("ipx-push-test-{}.p8", std::process::id()));
|
||||
std::fs::write(&key, KEY).unwrap();
|
||||
let cfg = crate::config::Config {
|
||||
push: crate::config::Push {
|
||||
key_file: key.clone(),
|
||||
key_id: "ABC123DEFG".into(),
|
||||
team_id: "8743L8D729".into(),
|
||||
..Default::default()
|
||||
},
|
||||
..Default::default()
|
||||
};
|
||||
let ctx = crate::test_util::test_ctx(cfg).await;
|
||||
let me = ctx.db.create_user("me", None, false).await.unwrap();
|
||||
for t in ["aa", "bb", "cc", "dd", "ee"] {
|
||||
ctx.db.add_push_device(me, &t.repeat(32), false).await.unwrap();
|
||||
}
|
||||
let (at, seen) = apns().await;
|
||||
let mut signer = Signer::default();
|
||||
|
||||
pass(&ctx, &mut signer, Some(&at)).await.unwrap();
|
||||
let first = seen.lock().unwrap().clone();
|
||||
assert_eq!(first.len(), 5, "every phone gets its first count");
|
||||
assert!(first.iter().all(|(_, badge, signed)| *badge == 0 && *signed), "{first:?}");
|
||||
|
||||
let left: std::collections::BTreeMap<String, Option<i64>> =
|
||||
ctx.db.push_devices().await.unwrap().into_iter().map(|d| (d.token[..2].to_owned(), d.badge)).collect();
|
||||
assert_eq!(
|
||||
left,
|
||||
[("aa".into(), Some(0)), ("dd".into(), None), ("ee".into(), None)].into(),
|
||||
"a 410 or a bad token forgets the phone; a refusal keeps the old badge to try again"
|
||||
);
|
||||
|
||||
seen.lock().unwrap().clear();
|
||||
pass(&ctx, &mut signer, Some(&at)).await.unwrap();
|
||||
let mut again: Vec<String> = seen.lock().unwrap().iter().map(|(t, ..)| t[..2].to_owned()).collect();
|
||||
again.sort();
|
||||
assert_eq!(again, ["dd", "ee"], "a count Apple took is not sent again until it changes");
|
||||
let _ = std::fs::remove_file(key);
|
||||
}
|
||||
}
|
||||
@@ -142,10 +142,10 @@ pub(crate) async fn reap(ctx: &Ctx, dry_run: bool, standalone: bool) -> Result<(
|
||||
}
|
||||
let removed = r.aged_out.iter().chain(r.over_quota.iter());
|
||||
for c in removed {
|
||||
ctx.out.emit(Event::Reaped { path: c.path.clone(), bytes: c.bytes.max(0) as u64 });
|
||||
ctx.out.emit(Event::Reaped { path: c.path.clone(), bytes: c.bytes.max(0) as u64, dry_run });
|
||||
}
|
||||
if standalone {
|
||||
ctx.out.emit(Event::ReapDone { files: r.aged_out.len() + r.over_quota.len(), bytes: r.bytes_freed });
|
||||
ctx.out.emit(Event::ReapDone { files: r.aged_out.len() + r.over_quota.len(), bytes: r.bytes_freed, dry_run });
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -267,6 +267,142 @@ mod tests {
|
||||
assert_eq!(run(&cfg, &db, false).await.unwrap().reconciled, 1, "the folder is there and the file is not");
|
||||
}
|
||||
|
||||
/// A scratch downloads folder of its own, so tests running at once never share files.
|
||||
fn scratch(name: &str) -> std::path::PathBuf {
|
||||
let dir = std::env::temp_dir().join(format!("ipx-reap-{}-{name}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
dir
|
||||
}
|
||||
|
||||
fn file(dir: &Path, name: &str, len: usize) -> String {
|
||||
let p = dir.join(name);
|
||||
std::fs::write(&p, vec![0u8; len]).unwrap();
|
||||
p.display().to_string()
|
||||
}
|
||||
|
||||
async fn state(db: &Db, id: i64) -> (String, Option<String>) {
|
||||
let s = db
|
||||
.strings_for_test(&format!("SELECT state || '|' || coalesce(path, '') FROM enclosures WHERE id = {id}"))
|
||||
.await;
|
||||
let (state, path) = s[0].split_once('|').unwrap();
|
||||
(state.into(), Some(path.to_string()).filter(|p| !p.is_empty()))
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn the_age_pass_deletes_old_files_and_a_dry_run_only_counts_them() {
|
||||
let dir = scratch("age");
|
||||
let (old, new) = (file(&dir, "old.mp3", 100), file(&dir, "new.mp3", 200));
|
||||
let (day, now) = (86_400, db::now());
|
||||
let db = Db::memory().await.unwrap();
|
||||
db.exec_for_test(&format!(
|
||||
"INSERT INTO entries (feed_id, guid, first_seen) VALUES
|
||||
('f', 'old', {old_seen}), ('f', 'new', {now}), ('f', 'stale', {old_seen}), ('f', 'held', {old_seen});
|
||||
INSERT INTO enclosures (id, feed_id, guid, url, path, bytes_done, state, downloaded_at) VALUES
|
||||
(1, 'f', 'old', 'u1', '{old}', 100, 'done', {old_at}),
|
||||
(2, 'f', 'new', 'u2', '{new}', 200, 'done', {new_at}),
|
||||
(3, 'f', 'held', 'u3', NULL, 0, 'held', {old_at});",
|
||||
old_seen = now - 40 * day,
|
||||
old_at = now - 10 * day,
|
||||
new_at = now - day,
|
||||
))
|
||||
.await
|
||||
.unwrap();
|
||||
let mut cfg = Config::default();
|
||||
cfg.general.download_dir = dir.clone();
|
||||
cfg.general.max_age_days = 7;
|
||||
|
||||
let dry = run(&cfg, &db, true).await.unwrap();
|
||||
assert_eq!(dry.aged_out.iter().map(|c| c.id).collect::<Vec<_>>(), vec![1], "a held item has no file to take");
|
||||
assert_eq!((dry.bytes_freed, dry.entries_pruned), (100, 0), "counted, and nothing pruned");
|
||||
assert!(Path::new(&old).exists(), "a dry run deletes nothing");
|
||||
assert_eq!(state(&db, 1).await.0, "done");
|
||||
|
||||
let r = run(&cfg, &db, false).await.unwrap();
|
||||
assert_eq!(r.aged_out.iter().map(|c| c.id).collect::<Vec<_>>(), vec![1]);
|
||||
assert_eq!(r.bytes_freed, 100);
|
||||
assert!(!Path::new(&old).exists() && Path::new(&new).exists(), "only the old file goes");
|
||||
assert_eq!(state(&db, 1).await, ("reaped".into(), None), "the row stays, so it is not fetched again");
|
||||
assert_eq!(state(&db, 2).await, ("done".into(), Some(new)));
|
||||
assert_eq!(state(&db, 3).await.0, "held");
|
||||
// The reaped one's entry and the one that never had a file; not held, whose row has no
|
||||
// path, nor new.
|
||||
assert_eq!(r.entries_pruned, 3, "old, stale and held are past the cutoff and have no file");
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn over_quota_the_oldest_go_and_the_age_pass_is_not_counted_twice() {
|
||||
let dir = scratch("quota");
|
||||
let paths: Vec<String> = (1..=4).map(|i| file(&dir, &format!("{i}.mp3"), 10 * i)).collect();
|
||||
let (day, now) = (86_400, db::now());
|
||||
let db = Db::memory().await.unwrap();
|
||||
// Recorded sizes, not the files' own: a dry run counts these. 1 is past the age limit.
|
||||
db.exec_for_test(&format!(
|
||||
"INSERT INTO enclosures (id, feed_id, guid, url, path, bytes_done, state, downloaded_at) VALUES
|
||||
(1, 'f', 'a', 'u1', '{}', 2000000, 'done', {}),
|
||||
(2, 'f', 'b', 'u2', '{}', 1000000, 'done', {}),
|
||||
(3, 'f', 'c', 'u3', '{}', 1000000, 'done', {}),
|
||||
(4, 'f', 'd', 'u4', '{}', 1000000, 'done', {});",
|
||||
paths[0],
|
||||
now - 40 * day,
|
||||
paths[1],
|
||||
now - 5 * day,
|
||||
paths[2],
|
||||
now - 3 * day,
|
||||
paths[3],
|
||||
now - day,
|
||||
))
|
||||
.await
|
||||
.unwrap();
|
||||
let mut cfg = Config::default();
|
||||
cfg.general.download_dir = dir.clone();
|
||||
cfg.general.max_age_days = 30;
|
||||
// 53.7 MB less the 50 MB pad leaves about 1.26 MB. Without 1, the other three are 3 MB:
|
||||
// two must go. Counting 1 again would make it 5 MB and take all three.
|
||||
cfg.general.max_total_gb = 0.05;
|
||||
|
||||
let dry = run(&cfg, &db, true).await.unwrap();
|
||||
assert_eq!(dry.aged_out.iter().map(|c| c.id).collect::<Vec<_>>(), vec![1]);
|
||||
assert_eq!(dry.over_quota.iter().map(|c| c.id).collect::<Vec<_>>(), vec![2, 3], "oldest first");
|
||||
assert_eq!(dry.bytes_freed, 4_000_000);
|
||||
assert!(paths.iter().all(|p| Path::new(p).exists()), "a dry run deletes nothing");
|
||||
|
||||
let r = run(&cfg, &db, false).await.unwrap();
|
||||
assert_eq!(r.over_quota.iter().map(|c| c.id).collect::<Vec<_>>(), vec![2, 3]);
|
||||
assert_eq!(r.bytes_freed, 10 + 20 + 30, "what was on disk, not what was recorded");
|
||||
assert_eq!(paths.iter().map(|p| Path::new(p).exists()).collect::<Vec<_>>(), [false, false, false, true]);
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_file_already_gone_is_reaped_and_one_that_will_not_delete_is_left() {
|
||||
let dir = scratch("stuck");
|
||||
let now = db::now();
|
||||
let db = Db::memory().await.unwrap();
|
||||
// procfs refuses an unlink even from root, and the file is there, so it is a candidate.
|
||||
db.exec_for_test(&format!(
|
||||
"INSERT INTO enclosures (id, feed_id, guid, url, path, bytes_done, state, downloaded_at) VALUES
|
||||
(1, 'f', 'gone', 'u1', '{}', 100, 'done', 1),
|
||||
(2, 'f', 'stuck', 'u2', '/proc/version', 100, 'done', 1);",
|
||||
dir.join("gone.mp3").display(),
|
||||
))
|
||||
.await
|
||||
.unwrap();
|
||||
let mut cfg = Config::default();
|
||||
cfg.general.download_dir = dir.clone();
|
||||
cfg.general.max_age_days = 1;
|
||||
assert!(now > 86_400);
|
||||
|
||||
let r = run(&cfg, &db, false).await.unwrap();
|
||||
assert_eq!(r.reconciled, 1, "the missing file is noticed");
|
||||
assert_eq!(state(&db, 1).await, ("reaped".into(), None));
|
||||
assert_eq!(r.aged_out.iter().map(|c| c.id).collect::<Vec<_>>(), vec![2]);
|
||||
assert_eq!(r.bytes_freed, 0, "nothing was freed");
|
||||
assert_eq!(state(&db, 2).await, ("done".into(), Some("/proc/version".into())), "still claims its file");
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn prune_keeps_entries_that_still_have_a_file() {
|
||||
let db = Db::memory().await.unwrap();
|
||||
|
||||
@@ -5,6 +5,7 @@ use crate::ctx::Ctx;
|
||||
use crate::ipc::Event;
|
||||
use crate::{catalogue, cli, config, db, download, feed, lists, metrics, signals, torrent};
|
||||
use anyhow::Result;
|
||||
use std::collections::HashSet;
|
||||
use std::path::Path;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
@@ -23,6 +24,8 @@ pub(crate) async fn fetch(ctx: &Arc<Ctx>, only: Option<&str>, force: bool, scope
|
||||
let subscribed = ctx.db.subscriber_counts().await?;
|
||||
let mut scanned = 0;
|
||||
let mut fresh: Vec<String> = vec![];
|
||||
// Feeds a list read earlier in this run dropped: still in `due`, scanned they came back (#478).
|
||||
let mut dropped: HashSet<String> = HashSet::new();
|
||||
let in_scope =
|
||||
|s: &Sub| scope.is_empty() || scope.contains(&s.id) || s.cfg.group.as_ref().is_some_and(|g| scope.contains(g));
|
||||
let mut states = ctx.db.http_states().await?;
|
||||
@@ -95,6 +98,9 @@ pub(crate) async fn fetch(ctx: &Arc<Ctx>, only: Option<&str>, force: bool, scope
|
||||
let (id, feed_cfg) = (&sub.id, &sub.cfg);
|
||||
// A task that panicked has no body; scan_one then fetches it itself.
|
||||
let fetched = futures_util::StreamExt::next(&mut bodies).await.and_then(|j| j.ok()).flatten();
|
||||
if dropped.contains(id) {
|
||||
continue;
|
||||
}
|
||||
scanned += 1;
|
||||
ctx.out.emit(Event::FeedStart { feed: id.clone() });
|
||||
match scan_one(ctx, id, feed_cfg, state, force, fetched).await {
|
||||
@@ -113,10 +119,12 @@ pub(crate) async fn fetch(ctx: &Arc<Ctx>, only: Option<&str>, force: bool, scope
|
||||
ctx.out.emit(Event::FeedSkip {
|
||||
feed: id.clone(),
|
||||
reason: format!(
|
||||
"{total} feed(s) listed, {} added, {removed} unsubscribed, {kept} kept without a listing",
|
||||
added.len()
|
||||
"{total} feed(s) listed, {} added, {} unsubscribed, {kept} kept without a listing",
|
||||
added.len(),
|
||||
removed.len()
|
||||
),
|
||||
});
|
||||
dropped.extend(removed);
|
||||
// Read them in the same pass, as the original did, rather than making
|
||||
// the user wait a whole interval for a newly listed show.
|
||||
fresh.extend(added);
|
||||
@@ -171,7 +179,13 @@ pub(crate) async fn fetch(ctx: &Arc<Ctx>, only: Option<&str>, force: bool, scope
|
||||
pub fn due_at(cfg: &config::Config, feed: &config::Feed, state: &db::HttpState, subscribed: bool) -> Option<i64> {
|
||||
let last = state.last_checked?;
|
||||
let floor = if subscribed { 0 } else { 86_400 };
|
||||
Some(last + due_after(cfg, feed, state.ttl_mins, state.error_since, state.same_since, last).max(floor) as i64)
|
||||
let wait = due_after(cfg, feed, state.ttl_mins, state.error_since, state.same_since, last).max(floor);
|
||||
// YouTube fails about 70% of reads from Tower, in streaks longer than fetch's retries; read
|
||||
// once a day, its 39 catalogue channels missed 5 days in 7 (#386). Hourly for the first day of
|
||||
// a run of failures finds a moment it answers, and a channel gone for good costs 24 requests.
|
||||
let youtube_again =
|
||||
state.error_since.is_some_and(|since| last - since < 86_400) && feed::is_youtube_feed(&feed.url);
|
||||
Some(last + if youtube_again { wait.min(3600) } else { wait } as i64)
|
||||
}
|
||||
|
||||
/// A per-feed schedule is an explicit instruction and wins outright. Without one, the
|
||||
@@ -241,7 +255,7 @@ pub(crate) enum Outcome {
|
||||
/// The URL is a list of feeds rather than a feed: an OPML, or a Patreon creator's shows.
|
||||
Opml {
|
||||
added: Vec<String>,
|
||||
removed: usize,
|
||||
removed: Vec<String>,
|
||||
kept: usize,
|
||||
total: usize,
|
||||
},
|
||||
@@ -711,6 +725,9 @@ pub(crate) async fn fetch_one(
|
||||
let mut last_pct = -1i64;
|
||||
let name = download::filename_for(url, None);
|
||||
let cfg = ctx.cfg();
|
||||
// Said while it runs, as a torrent's is: a stop mid-file leaves it 'downloading', which the
|
||||
// next start takes up at once (#410). Every way out records 'done' or 'error'.
|
||||
ctx.db.mark_enclosure(url, "downloading", None).await?;
|
||||
let got = download::download(&ctx.client, &cfg, feed_cfg, url, |done, total| {
|
||||
if let Some(t) = total.filter(|t| *t > 0) {
|
||||
let pct = (done * 100 / t) as i64;
|
||||
@@ -907,6 +924,20 @@ mod tests {
|
||||
assert_eq!(at(Some(1000), false), Some(1000 + 86_400)); // listed, nobody subscribed
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_failing_youtube_feed_is_asked_again_hourly_for_a_day() {
|
||||
let cfg = config::Config::default();
|
||||
let yt = config::Feed { url: "https://www.youtube.com/feeds/videos.xml?channel_id=UCx".into(), ..feed() };
|
||||
let at = |f: &config::Feed, since: Option<i64>, last: i64| {
|
||||
let state = db::HttpState { last_checked: Some(last), error_since: since, ..Default::default() };
|
||||
due_at(&cfg, f, &state, false).map(|t| t - last)
|
||||
};
|
||||
assert_eq!(at(&yt, None, 1000), Some(86_400), "well, it waits its day like any listed feed");
|
||||
assert_eq!(at(&yt, Some(1000), 1000 + 7200), Some(3600), "failing two hours: in an hour");
|
||||
assert_eq!(at(&yt, Some(1000), 1000 + 90_000), Some(86_400), "failing over a day: back off");
|
||||
assert_eq!(at(&feed(), Some(1000), 1000 + 7200), Some(86_400), "another host's failure is not hurried");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_feed_long_the_same_is_checked_less_and_a_schedule_set_wins() {
|
||||
let (hour, day) = (3600u64, 86_400u64);
|
||||
@@ -1040,6 +1071,39 @@ mod tests {
|
||||
assert_eq!(ctx.db.feed_summary("iconic").await.unwrap().image, Some(icon));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_torrent_enclosure_goes_to_the_torrent_worker_not_the_file_downloader() {
|
||||
let body = b"<?xml version=\"1.0\"?><rss version=\"2.0\"><channel><title>T</title><item><title>One</title><guid>g1</guid>\
|
||||
<enclosure url=\"http://127.0.0.1:9/ep.torrent\" type=\"application/x-bittorrent\" length=\"10\"/></item></channel></rss>";
|
||||
let scan = |ctx: Arc<Ctx>| async move {
|
||||
let got = feed::Fetched::Body { bytes: body.to_vec(), etag: None, last_modified: None };
|
||||
let cfg = config::Feed { url: "http://127.0.0.1:9/feed".into(), ..feed() };
|
||||
match scan_one(&ctx, "t", &cfg, &db::HttpState::default(), true, Some((Ok((got, None)), None)))
|
||||
.await
|
||||
.unwrap()
|
||||
{
|
||||
Outcome::Feed(s) => (s.downloaded, s.torrents),
|
||||
_ => panic!("a feed"),
|
||||
}
|
||||
};
|
||||
let state = |ctx: Arc<Ctx>| async move { ctx.db.strings_for_test("SELECT state FROM enclosures").await };
|
||||
|
||||
// Detached, as the daemon runs them: no slot free, so it waits its turn as 'downloading'.
|
||||
let mut ctx = test_ctx(config::Config::default()).await;
|
||||
ctx.detach_torrents = true;
|
||||
ctx.torrent_slots = Arc::new(tokio::sync::Semaphore::new(0));
|
||||
let ctx = Arc::new(ctx);
|
||||
assert_eq!(scan(ctx.clone()).await, (0, 1), "counted as a torrent, not a download");
|
||||
assert_eq!(state(ctx).await, ["downloading"], "so the next scan does not queue it again");
|
||||
|
||||
// Turned off, it is skipped and still counted.
|
||||
let mut cfg = config::Config::default();
|
||||
cfg.torrent.enabled = false;
|
||||
let ctx = Arc::new(test_ctx(cfg).await);
|
||||
assert_eq!(scan(ctx.clone()).await, (0, 1));
|
||||
assert_eq!(state(ctx).await, ["skipped"]);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn nothing_from_a_feed_that_never_posted_is_an_error_and_from_one_that_has_is_not() {
|
||||
let base = empty_or_feed_server().await;
|
||||
|
||||
88
crates/ipx/src/search.rs
Normal file
88
crates/ipx/src/search.rs
Normal file
@@ -0,0 +1,88 @@
|
||||
//! What a search reads (#459): each item's title and text, the markup taken out, lowercased and cut
|
||||
//! short, kept in `entries.search_text` beside the HTML. Matching the HTML itself took 5 s on All
|
||||
//! Subscriptions, most of it reading 4 GB of descriptions; the plain text is a fraction of that,
|
||||
//! and Postgres indexes it with pg_trgm, so a part of a word still finds the word, as before.
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
use tracing::Instrument;
|
||||
|
||||
use crate::ctx::Ctx;
|
||||
|
||||
/// Enough of a post for its subject to be in it; a word only past this in a very long post is not
|
||||
/// found. It keeps the index a size the shared Postgres carries.
|
||||
pub const MAX_CHARS: usize = 4000;
|
||||
/// Items made at a time, and how long between looks for more once none are left.
|
||||
const BATCH: i64 = 500;
|
||||
const EVERY: Duration = Duration::from_secs(5 * 60);
|
||||
|
||||
/// The title, then the text with its markup taken out (an image's alt and title kept, as Signals
|
||||
/// read them), lowercased, its spaces made single, and at most `MAX_CHARS` long.
|
||||
pub fn search_text(title: Option<&str>, description: Option<&str>) -> String {
|
||||
let text = crate::signals::text_of(description.unwrap_or(""));
|
||||
let all = format!("{} {}", title.unwrap_or(""), text).to_lowercase();
|
||||
all.split_whitespace().collect::<Vec<_>>().join(" ").chars().take(MAX_CHARS).collect()
|
||||
}
|
||||
|
||||
/// Makes every item's search text that is missing, then the index, then looks again now and then:
|
||||
/// a scan that changes an item's title or text leaves its search text to be made again here, and
|
||||
/// a search reads that item the old way until it is.
|
||||
pub async fn run(ctx: Arc<Ctx>, mut stop: tokio::sync::watch::Receiver<bool>) {
|
||||
let mut indexed = false;
|
||||
loop {
|
||||
let pass = async {
|
||||
let made = fill(&ctx).await?;
|
||||
if !indexed {
|
||||
indexed = ctx.db.search_index().instrument(crate::db::step("search_index")).await?;
|
||||
}
|
||||
anyhow::Ok(made)
|
||||
};
|
||||
tokio::select! {
|
||||
_ = stop.changed() => return,
|
||||
done = pass.instrument(tracing::info_span!("search_text")) => match done {
|
||||
Ok(n) if n > 0 => tracing::info!(items = n, "search: made items' search text"),
|
||||
Ok(_) => {}
|
||||
Err(e) => tracing::warn!(error = %format!("{e:#}"), "search: items' search text could not be made"),
|
||||
},
|
||||
}
|
||||
tokio::select! {
|
||||
_ = stop.changed() => return,
|
||||
_ = tokio::time::sleep(EVERY) => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Every item without search text, given one, a batch at a time: how many.
|
||||
async fn fill(ctx: &Ctx) -> anyhow::Result<usize> {
|
||||
let mut made = 0;
|
||||
loop {
|
||||
let batch = ctx.db.unsearched(BATCH).await?;
|
||||
if batch.is_empty() {
|
||||
return Ok(made);
|
||||
}
|
||||
for (feed, guid, title, description) in &batch {
|
||||
ctx.db.set_search_text(feed, guid, &search_text(title.as_deref(), description.as_deref())).await?;
|
||||
}
|
||||
made += batch.len();
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn the_text_is_the_words_not_the_markup() {
|
||||
let t = search_text(
|
||||
Some("Apple's NEW Thing"),
|
||||
Some(r#"<p>Read <a href="https://x.example/app">more</a></p><img alt="A Chart" src="c.png">"#),
|
||||
);
|
||||
assert_eq!(t, "apple's new thing read more a chart");
|
||||
assert!(!t.contains("href") && !t.contains("x.example"), "an address in a tag is not a word of the post");
|
||||
assert_eq!(search_text(None, None), "");
|
||||
let long = "word ".repeat(2000);
|
||||
assert_eq!(search_text(Some("Title"), Some(&long)).chars().count(), MAX_CHARS);
|
||||
assert!(search_text(Some("Title"), Some(&long)).starts_with("title word"), "the title is always in it");
|
||||
}
|
||||
}
|
||||
@@ -405,6 +405,9 @@ pub async fn check_key(ctx: &Ctx, who: &str, key: &str) -> Result<()> {
|
||||
/// Puts `body` to Jev on `key` and gives its answer, or what TypeSafe refused it with. Carries
|
||||
/// this trace's `traceparent`, as every call out of ipx does.
|
||||
async fn ask(ctx: &Ctx, key: &str, body: &serde_json::Value) -> Result<serde_json::Value> {
|
||||
#[cfg(test)]
|
||||
let url = tests::jev();
|
||||
#[cfg(not(test))]
|
||||
let url = std::env::var("IPX_TYPESAFE_URL").unwrap_or_else(|_| feed::JEV_URL.to_owned());
|
||||
let mut req = ctx.client.post(url).bearer_auth(key).json(body).timeout(feed::FEED_TIMEOUT);
|
||||
if let Some(tp) = ipx_directory::otel::traceparent() {
|
||||
@@ -494,7 +497,7 @@ fn chosen(answer: &serde_json::Value, candidates: &[(String, String)]) -> Option
|
||||
/// Show notes are HTML: Jev is given their words, and an image's alt and title text, which is all
|
||||
/// the words some posts have. xkcd's is a comic and its hover text; dropping the tag whole sent Jev
|
||||
/// "Ground Effect" alone, an F1 aerodynamics term, and the comic went into Formula 1 (#267).
|
||||
fn text_of(html: &str) -> String {
|
||||
pub(crate) fn text_of(html: &str) -> String {
|
||||
let mut out = String::with_capacity(html.len());
|
||||
let mut tag: Option<String> = None;
|
||||
for c in html.chars() {
|
||||
@@ -535,8 +538,172 @@ fn attr<'a>(tag: &'a str, name: &str) -> Option<&'a str> {
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
pub(crate) mod tests {
|
||||
use super::*;
|
||||
use std::sync::{Mutex, OnceLock};
|
||||
|
||||
/// Calls the stand-in has had, by the key they came with.
|
||||
static CALLS: Mutex<BTreeMap<String, usize>> = Mutex::new(BTreeMap::new());
|
||||
|
||||
/// A stand-in for TypeSafe's Jev, as the browser suite's fixture server is: on a thread and
|
||||
/// runtime of its own, so it outlives any one test's. It answers by the key: `refused-` as a
|
||||
/// key it does not know, `slow-` as over its limits, `malformed-` as a request it cannot read,
|
||||
/// `offlist-` with an icon it was not offered; anything else judges an item to fit when its
|
||||
/// text says "fits", and picks the icon labelled robot.
|
||||
pub(crate) fn jev() -> String {
|
||||
static AT: OnceLock<String> = OnceLock::new();
|
||||
AT.get_or_init(|| {
|
||||
let l = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
|
||||
l.set_nonblocking(true).unwrap();
|
||||
let at = l.local_addr().unwrap();
|
||||
std::thread::spawn(move || {
|
||||
let rt = tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap();
|
||||
rt.block_on(async move {
|
||||
let app = axum::Router::new().route("/", axum::routing::post(stand_in));
|
||||
axum::serve(tokio::net::TcpListener::from_std(l).unwrap(), app).await.unwrap();
|
||||
});
|
||||
});
|
||||
format!("http://{at}/")
|
||||
})
|
||||
.clone()
|
||||
}
|
||||
|
||||
async fn stand_in(
|
||||
headers: axum::http::HeaderMap,
|
||||
axum::Json(body): axum::Json<serde_json::Value>,
|
||||
) -> axum::response::Response {
|
||||
use axum::response::IntoResponse;
|
||||
let key =
|
||||
headers.get("authorization").and_then(|v| v.to_str().ok()).unwrap_or("").trim_start_matches("Bearer ");
|
||||
*CALLS.lock().unwrap().entry(key.to_owned()).or_default() += 1;
|
||||
let refuse = |status: u16, detail: &str| {
|
||||
(
|
||||
axum::http::StatusCode::from_u16(status).unwrap(),
|
||||
[("retry-after", "1")],
|
||||
format!(r#"{{"detail":"{detail}"}}"#),
|
||||
)
|
||||
.into_response()
|
||||
};
|
||||
if key.starts_with("refused-") {
|
||||
return refuse(401, "invalid api key");
|
||||
} else if key.starts_with("slow-") {
|
||||
return refuse(429, "rate limited");
|
||||
} else if key.starts_with("malformed-") {
|
||||
return refuse(422, "criteria should be an object");
|
||||
}
|
||||
let fits = body["state"].as_str().is_some_and(|s| s.contains("fits"));
|
||||
let mut answers = serde_json::Map::new();
|
||||
for (id, q) in body["questions"].as_object().unwrap() {
|
||||
let a = if q["type"] == "choice" {
|
||||
let robot = q["criteria"].as_object().unwrap().iter().find(|(_, l)| l.as_str() == Some("robot"));
|
||||
let pick = if key.starts_with("offlist-") { "solid/nope" } else { robot.map_or("", |(k, _)| k) };
|
||||
serde_json::json!({ "choice": pick })
|
||||
} else {
|
||||
serde_json::json!({ "noul": if fits { 0.9 } else { 0.1 } })
|
||||
};
|
||||
answers.insert(id.clone(), a);
|
||||
}
|
||||
axum::Json(serde_json::json!({ "answers": answers, "usage": { "input_tokens": 100, "output_tokens": 3 } }))
|
||||
.into_response()
|
||||
}
|
||||
|
||||
fn calls_with(key: &str) -> usize {
|
||||
CALLS.lock().unwrap().get(key).copied().unwrap_or(0)
|
||||
}
|
||||
|
||||
/// Someone with a key, a Signal and a feed of their own holding these items; their Signal's id.
|
||||
async fn person(ctx: &Ctx, name: &str, key: &str, items: &[(&str, &str)]) -> i64 {
|
||||
let id = ctx.db.create_user(name, None, false).await.unwrap();
|
||||
ctx.db.set_typesafe_key(id, Some(key)).await.unwrap();
|
||||
let feed = format!("feed-{name}");
|
||||
let mut sql = format!("INSERT INTO subscriptions (user_id, feed_id) VALUES ({id}, '{feed}');");
|
||||
for (guid, title) in items {
|
||||
sql += &format!(
|
||||
"INSERT INTO entries (feed_id, guid, title, first_seen) VALUES ('{feed}', '{guid}', '{title}', {});",
|
||||
db::now()
|
||||
);
|
||||
}
|
||||
ctx.db.exec_for_test(&sql).await.unwrap();
|
||||
ctx.db.add_signal(id, "Rust", &["rust".into()], None).await.unwrap()
|
||||
}
|
||||
|
||||
async fn verdicts(ctx: &Ctx, signal: i64) -> Vec<String> {
|
||||
ctx.db
|
||||
.strings_for_test(&format!(
|
||||
"SELECT guid || CASE WHEN hit THEN ' fits' ELSE ' not' END FROM signal_verdicts
|
||||
WHERE signal_id = {signal} ORDER BY guid"
|
||||
))
|
||||
.await
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_pass_judges_each_new_item_once_keeps_its_verdicts_and_bills_its_owner() {
|
||||
let ctx = crate::test_util::test_ctx(crate::config::Config::default()).await;
|
||||
let signal = person(&ctx, "sig-ann", "good-ann", &[("a", "this one fits"), ("b", "about gardening")]).await;
|
||||
assert!(!pass(&ctx).await.unwrap(), "two items is not a full batch");
|
||||
assert_eq!(verdicts(&ctx, signal).await, ["a fits", "b not"]);
|
||||
assert_eq!(calls_with("good-ann"), 2, "a call an item");
|
||||
assert!(!pass(&ctx).await.unwrap());
|
||||
assert_eq!(calls_with("good-ann"), 2, "nothing is asked twice");
|
||||
// Items judged, Signals fit, input and output tokens, on the person's own key (#278).
|
||||
assert_eq!(STATS.billed("sig-ann", "own"), [2.0, 1.0, 200.0, 6.0]);
|
||||
assert_eq!(STATS.billed("sig-ann", "server"), [0.0; 4]);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_refused_key_stops_its_owners_judging_and_nobody_elses() {
|
||||
let ctx = crate::test_util::test_ctx(crate::config::Config::default()).await;
|
||||
let ann = person(&ctx, "sig-ann2", "good-ann2", &[("a", "fits")]).await;
|
||||
let bob = person(&ctx, "sig-bob", "refused-bob", &[("b", "fits")]).await;
|
||||
let refused_before = STATS.jev_calls[1].load(Relaxed);
|
||||
assert!(pass(&ctx).await.unwrap(), "goes round again for whoever is left");
|
||||
assert_eq!(verdicts(&ctx, ann).await, ["a fits"]);
|
||||
assert!(verdicts(&ctx, bob).await.is_empty(), "left unjudged, not judged a miss");
|
||||
let bob_id = ctx.db.user_by_name("sig-bob").await.unwrap().unwrap().id;
|
||||
assert!(ctx.signals_refused.lock().unwrap().contains(&bob_id));
|
||||
assert!(STATS.jev_calls[1].load(Relaxed) > refused_before, "counted as key_refused");
|
||||
assert!(!pass(&ctx).await.unwrap());
|
||||
assert_eq!(calls_with("refused-bob"), 1, "not sent again until a new key is saved");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn over_typesafes_limits_the_pass_stops_and_the_next_call_waits_its_turn() {
|
||||
let ctx = crate::test_util::test_ctx(crate::config::Config::default()).await;
|
||||
let signal = person(&ctx, "sig-cy", "slow-cy", &[("c", "fits")]).await;
|
||||
let limited_before = STATS.jev_calls[3].load(Relaxed);
|
||||
assert!(!pass(&ctx).await.unwrap(), "stops rather than going straight round");
|
||||
assert!(verdicts(&ctx, signal).await.is_empty());
|
||||
assert!(STATS.jev_calls[3].load(Relaxed) > limited_before, "counted as rate_limited");
|
||||
// Retry-After said a second: the next call to Jev, anyone's, waits for it.
|
||||
let started = std::time::Instant::now();
|
||||
check_key(&ctx, "sig-cy", "good-cy").await.unwrap();
|
||||
assert!(started.elapsed() >= Duration::from_millis(900), "{:?}", started.elapsed());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_request_typesafe_cannot_read_stops_the_pass_and_is_counted() {
|
||||
let ctx = crate::test_util::test_ctx(crate::config::Config::default()).await;
|
||||
let signal = person(&ctx, "sig-dee", "malformed-dee", &[("d", "fits"), ("e", "fits")]).await;
|
||||
let malformed_before = STATS.jev_calls[2].load(Relaxed);
|
||||
assert!(!pass(&ctx).await.unwrap(), "every other item's would fail the same way (#257)");
|
||||
assert!(verdicts(&ctx, signal).await.is_empty());
|
||||
assert!(STATS.jev_calls[2].load(Relaxed) > malformed_before);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_key_is_checked_and_an_icon_is_one_jev_was_offered() {
|
||||
let ctx = crate::test_util::test_ctx(crate::config::Config::default()).await;
|
||||
check_key(&ctx, "sig-eve", "good-eve").await.unwrap();
|
||||
assert!(refuses_key(&check_key(&ctx, "sig-eve", "refused-eve").await.unwrap_err()));
|
||||
assert_eq!(STATS.billed("sig-eve", "own")[2], 100.0, "a check costs tokens too");
|
||||
|
||||
let offered = [("solid/robot".to_owned(), "robot".to_owned()), ("solid/leaf".to_owned(), "leaf".to_owned())];
|
||||
let words = ["ai".to_owned()];
|
||||
let icon = pick_icon(&ctx, "sig-eve", "good-eve", "AI", &words, None, &offered).await.unwrap();
|
||||
assert_eq!(icon, "solid/robot");
|
||||
assert!(pick_icon(&ctx, "sig-eve", "offlist-eve", "AI", &words, None, &offered).await.is_err(), "not offered");
|
||||
assert_eq!(STATS.billed("sig-eve", "server")[2], 200.0, "icons are on the server's key");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_noul_of_a_half_or_more_is_a_fit() {
|
||||
|
||||
@@ -8,7 +8,7 @@ use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
|
||||
pub(crate) fn feed() -> config::Feed {
|
||||
// Whatever `ipx add` would write, which is the shape every code path sees.
|
||||
// Whatever `ipxd add` would write, which is the shape every code path sees.
|
||||
let mut cfg = config::Config::default();
|
||||
|
||||
add_one_cfg(&mut cfg, "http://x/f.xml", None, vec![])
|
||||
|
||||
@@ -29,6 +29,9 @@ impl Torrents {
|
||||
let opts = SessionOptions {
|
||||
listen: Some(ListenerOptions { listen_addr, ..Default::default() }),
|
||||
client_name_and_version: Some(concat!("ipx ", env!("CARGO_PKG_VERSION")).into()),
|
||||
// A test's swarm is two sessions and a tracker on 127.0.0.1, not the public DHT or the LAN.
|
||||
dht: if cfg!(test) { None } else { Some(Default::default()) },
|
||||
disable_local_service_discovery: cfg!(test),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
@@ -184,6 +187,136 @@ pub fn ratio(uploaded: u64, total: u64) -> f64 {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn free_port() -> u16 {
|
||||
std::net::TcpListener::bind("127.0.0.1:0").unwrap().local_addr().unwrap().port()
|
||||
}
|
||||
|
||||
/// A swarm on 127.0.0.1: a seeding session holding `file`, and a web server answering for the
|
||||
/// .torrent (`/ep.torrent`) and as its tracker, naming the seed as the one peer. The server's
|
||||
/// address, and the seeding session, which stops seeding when dropped.
|
||||
async fn swarm(file: &Path) -> (SocketAddr, Arc<Session>) {
|
||||
let web = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let at = web.local_addr().unwrap();
|
||||
let seed_port = free_port();
|
||||
let made = librqbit::create_torrent(
|
||||
file,
|
||||
librqbit::CreateTorrentOptions {
|
||||
name: None,
|
||||
trackers: vec![format!("http://{at}/announce")],
|
||||
piece_length: Some(16 * 1024),
|
||||
},
|
||||
&librqbit::spawn_utils::BlockingSpawner::new(2),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
let torrent = made.as_bytes().unwrap();
|
||||
// Compact peers: the address's four bytes, then the port's two.
|
||||
let mut peers = b"d8:intervali60e5:peers6:".to_vec();
|
||||
peers.extend_from_slice(&[127, 0, 0, 1]);
|
||||
peers.extend_from_slice(&seed_port.to_be_bytes());
|
||||
peers.push(b'e');
|
||||
let app = axum::Router::new()
|
||||
.route("/ep.torrent", axum::routing::get(move || std::future::ready(torrent.clone())))
|
||||
.route("/announce", axum::routing::get(move || std::future::ready(peers.clone())));
|
||||
tokio::spawn(async move { axum::serve(web, app).await });
|
||||
|
||||
let seed = Session::new_with_opts(
|
||||
file.parent().unwrap().to_owned(),
|
||||
SessionOptions {
|
||||
dht: None,
|
||||
disable_local_service_discovery: true,
|
||||
listen: Some(ListenerOptions {
|
||||
listen_addr: format!("127.0.0.1:{seed_port}").parse().unwrap(),
|
||||
..Default::default()
|
||||
}),
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
seed.add_torrent(
|
||||
AddTorrent::from_bytes(made.as_bytes().unwrap()),
|
||||
Some(AddTorrentOptions {
|
||||
output_folder: Some(file.parent().unwrap().display().to_string()),
|
||||
overwrite: true,
|
||||
..Default::default()
|
||||
}),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
(at, seed)
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn a_detached_torrent_lands_in_its_own_folder_is_recorded_and_frees_its_slot() {
|
||||
let root = std::env::temp_dir().join(format!("ipx-torrent-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&root);
|
||||
let (seeded, downloads) = (root.join("seed"), root.join("downloads"));
|
||||
std::fs::create_dir_all(&seeded).unwrap();
|
||||
let episode: Vec<u8> = (0..200_000u32).map(|i| (i * 7 % 251) as u8).collect();
|
||||
std::fs::write(seeded.join("episode.mp3"), &episode).unwrap();
|
||||
let (at, _seed) = swarm(&seeded.join("episode.mp3")).await;
|
||||
|
||||
let mut cfg = Config::default();
|
||||
cfg.general.download_dir = downloads.clone();
|
||||
cfg.torrent.enabled = true;
|
||||
let port = free_port();
|
||||
cfg.torrent.port_range = format!("{port}-{port}");
|
||||
cfg.torrent.seed_ratio = 0.0;
|
||||
let mut ctx = crate::test_util::test_ctx(cfg.clone()).await;
|
||||
ctx.detach_torrents = true;
|
||||
let ctx = Arc::new(ctx);
|
||||
let url = format!("http://{at}/ep.torrent");
|
||||
ctx.db
|
||||
.exec_for_test(&format!(
|
||||
"INSERT INTO enclosures (id, feed_id, guid, url, state) VALUES (9, 'f', 'g', '{url}', 'downloading')"
|
||||
))
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
crate::scan::spawn_torrent(&ctx, "f".into(), 9, url.clone(), downloads.join("Show"));
|
||||
let row = tokio::time::timeout(Duration::from_secs(90), async {
|
||||
loop {
|
||||
let row = ctx.db.enclosure(9).await.unwrap().unwrap();
|
||||
if row.state != "downloading" {
|
||||
return row;
|
||||
}
|
||||
tokio::time::sleep(Duration::from_millis(200)).await;
|
||||
}
|
||||
})
|
||||
.await
|
||||
.expect("the torrent finished within 90 s");
|
||||
let folder = own_folder(&downloads.join("Show"), 9);
|
||||
assert_eq!((row.state.as_str(), row.last_error.as_deref()), ("done", None));
|
||||
assert_eq!(row.path.as_deref(), Some(folder.display().to_string().as_str()), "the folder is ours (#318)");
|
||||
assert_eq!(std::fs::read(folder.join("episode.mp3")).unwrap(), episode);
|
||||
// The slot is let go once the row is written; give the task its moment.
|
||||
tokio::time::sleep(Duration::from_millis(200)).await;
|
||||
assert_eq!(ctx.torrent_slots.available_permits(), 2, "the slot is free again");
|
||||
let _ = std::fs::remove_dir_all(&root);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_torrent_file_from_an_internal_address_is_refused_unless_allowed() {
|
||||
let app = axum::Router::new().route("/ep.torrent", axum::routing::get(|| async { "d4:infode" }));
|
||||
let web = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let at = web.local_addr().unwrap();
|
||||
tokio::spawn(async move { axum::serve(web, app).await });
|
||||
let guarded = |allowed: &[&str]| {
|
||||
let allowed =
|
||||
crate::feed::guard::Allowed::parse(&allowed.iter().map(|s| s.to_string()).collect::<Vec<_>>()).unwrap();
|
||||
reqwest::Client::builder().connector_layer(crate::feed::guard::Guard::new(allowed)).build().unwrap()
|
||||
};
|
||||
let url = format!("http://{at}/ep.torrent");
|
||||
let err = torrent_file(&guarded(&[]), &url).await.unwrap_err();
|
||||
assert!(format!("{err:#}").contains("fetching the .torrent"), "{err:#}");
|
||||
assert_eq!(
|
||||
torrent_file(&guarded(&["127.0.0.1"]), &url).await.unwrap(),
|
||||
b"d4:infode",
|
||||
"fetch_internal allows it"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ratio_handles_the_empty_torrent() {
|
||||
assert_eq!(ratio(0, 100), 0.0);
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
236
crates/ipx/tests/daemon.rs
Normal file
236
crates/ipx/tests/daemon.rs
Normal file
@@ -0,0 +1,236 @@
|
||||
//! The daemon as production runs it: the built binary, a scratch config and SQLite database, a
|
||||
//! feed served from this process, and SIGTERM sent as Docker sends it (#400). A process of its
|
||||
//! own, because a signal sent to the test binary would reach every test in it.
|
||||
|
||||
use std::io::{BufRead, BufReader, Read, Write};
|
||||
use std::net::{TcpListener, TcpStream};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::process::{Child, Command, Output, Stdio};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
const EPISODE_BYTES: u64 = 50_000_000;
|
||||
/// Times the episode has been asked for.
|
||||
static EPISODE_GETS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
|
||||
|
||||
fn episode_gets() -> usize {
|
||||
EPISODE_GETS.load(std::sync::atomic::Ordering::SeqCst)
|
||||
}
|
||||
|
||||
/// A feed of one episode, and the episode, sent 4 KB every 50 ms: a download that takes minutes.
|
||||
fn serve_feed() -> u16 {
|
||||
let l = TcpListener::bind("127.0.0.1:0").unwrap();
|
||||
let port = l.local_addr().unwrap().port();
|
||||
std::thread::spawn(move || {
|
||||
for sock in l.incoming().flatten() {
|
||||
std::thread::spawn(move || answer(sock, port));
|
||||
}
|
||||
});
|
||||
port
|
||||
}
|
||||
|
||||
fn answer(mut sock: TcpStream, port: u16) {
|
||||
let mut line = String::new();
|
||||
if BufReader::new(&sock).read_line(&mut line).is_err() {
|
||||
return;
|
||||
}
|
||||
if line.contains("/ep.mp3") {
|
||||
EPISODE_GETS.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
|
||||
let head = format!("HTTP/1.1 200 OK\r\nContent-Type: audio/mpeg\r\nContent-Length: {EPISODE_BYTES}\r\n\r\n");
|
||||
// An MP3's frame sync first, so the file is taken for audio.
|
||||
let mut chunk = vec![0u8; 4096];
|
||||
chunk[..2].copy_from_slice(&[0xFF, 0xFB]);
|
||||
if sock.write_all(head.as_bytes()).is_err() {
|
||||
return;
|
||||
}
|
||||
while sock.write_all(&chunk).is_ok() {
|
||||
chunk[..2].fill(0);
|
||||
std::thread::sleep(Duration::from_millis(50));
|
||||
}
|
||||
return;
|
||||
}
|
||||
let body = format!(
|
||||
r#"<?xml version="1.0"?><rss version="2.0"><channel><title>Slow</title><item><title>One</title><guid>g1</guid><enclosure url="http://127.0.0.1:{port}/ep.mp3" type="audio/mpeg" length="{EPISODE_BYTES}"/></item></channel></rss>"#
|
||||
);
|
||||
let _ = sock.write_all(format!("HTTP/1.1 200 OK\r\nContent-Length: {}\r\n\r\n{body}", body.len()).as_bytes());
|
||||
}
|
||||
|
||||
struct Scratch {
|
||||
root: PathBuf,
|
||||
web: u16,
|
||||
}
|
||||
|
||||
impl Scratch {
|
||||
fn new(feed_port: u16) -> Self {
|
||||
let root = std::env::temp_dir().join(format!("ipx-daemon-test-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&root);
|
||||
std::fs::create_dir_all(root.join("data")).unwrap();
|
||||
let web = TcpListener::bind("127.0.0.1:0").unwrap().local_addr().unwrap().port();
|
||||
std::fs::write(
|
||||
root.join("config.toml"),
|
||||
format!(
|
||||
r#"[general]
|
||||
download_dir = "{root}/downloads"
|
||||
socket = "{root}/ipx.sock"
|
||||
schedule = "every 60m"
|
||||
fetch_internal = ["127.0.0.1"]
|
||||
|
||||
[torrent]
|
||||
enabled = false
|
||||
|
||||
[web]
|
||||
enabled = true
|
||||
bind = "127.0.0.1:{web}"
|
||||
token = ""
|
||||
|
||||
[feeds.slow]
|
||||
url = "http://127.0.0.1:{feed_port}/feed.xml"
|
||||
"#,
|
||||
root = root.display()
|
||||
),
|
||||
)
|
||||
.unwrap();
|
||||
Self { root, web }
|
||||
}
|
||||
|
||||
fn ipx(&self, args: &[&str]) -> Command {
|
||||
let mut c = Command::new(env!("CARGO_BIN_EXE_ipxd"));
|
||||
c.args(args)
|
||||
.env("IPX_CONFIG", self.root.join("config.toml"))
|
||||
.env("IPX_DATA_DIR", self.root.join("data"))
|
||||
.env("IPX_ADMIN_PASSWORD", "a test password")
|
||||
.env("IPX_LOG", "ipx=info")
|
||||
.stdin(Stdio::null());
|
||||
// Nothing of the shell's: no Postgres, no traces, no JSON log, no TypeSafe key.
|
||||
for v in
|
||||
["IPX_DATABASE_URL", "OTEL_EXPORTER_OTLP_ENDPOINT", "IPX_LOG_FORMAT", "TYPESAFE_KEY", "IPX_TYPESAFE_URL"]
|
||||
{
|
||||
c.env_remove(v);
|
||||
}
|
||||
c
|
||||
}
|
||||
|
||||
fn run(&self, args: &[&str]) -> Output {
|
||||
self.ipx(args).output().unwrap()
|
||||
}
|
||||
|
||||
/// A daemon, its log going to `log`.
|
||||
fn daemon(&self, log: &str) -> Daemon {
|
||||
let log = std::fs::File::create(self.root.join(log)).unwrap();
|
||||
Daemon(self.ipx(&["daemon"]).stdout(Stdio::null()).stderr(log).spawn().unwrap())
|
||||
}
|
||||
|
||||
fn log(&self, name: &str) -> String {
|
||||
std::fs::read_to_string(self.root.join(name)).unwrap_or_default()
|
||||
}
|
||||
|
||||
fn socket(&self) -> PathBuf {
|
||||
self.root.join("ipx.sock")
|
||||
}
|
||||
|
||||
/// Bytes of the download under way, in the downloads folder's incomplete files.
|
||||
fn downloading(&self) -> u64 {
|
||||
let dir = self.root.join("downloads/.ipx-incomplete");
|
||||
std::fs::read_dir(dir).map_or(0, |d| d.flatten().filter_map(|e| e.metadata().ok()).map(|m| m.len()).sum())
|
||||
}
|
||||
}
|
||||
|
||||
fn until(what: &str, limit: Duration, mut ok: impl FnMut() -> bool) {
|
||||
let started = Instant::now();
|
||||
while !ok() {
|
||||
assert!(started.elapsed() < limit, "{what}: not after {limit:?}");
|
||||
std::thread::sleep(Duration::from_millis(100));
|
||||
}
|
||||
}
|
||||
|
||||
fn text(o: &Output) -> String {
|
||||
format!("{}{}", String::from_utf8_lossy(&o.stdout), String::from_utf8_lossy(&o.stderr))
|
||||
}
|
||||
|
||||
/// SIGTERM, as `docker stop` sends; how long it took to exit.
|
||||
/// A daemon this test started, killed when dropped: a failed assertion left two running for an
|
||||
/// hour, holding their scratch sockets and ports.
|
||||
struct Daemon(Child);
|
||||
|
||||
impl Drop for Daemon {
|
||||
fn drop(&mut self) {
|
||||
let _ = self.0.kill();
|
||||
let _ = self.0.wait();
|
||||
}
|
||||
}
|
||||
|
||||
fn terminate(Daemon(child): &mut Daemon) -> Duration {
|
||||
let started = Instant::now();
|
||||
assert!(Command::new("kill").args(["-TERM", &child.id().to_string()]).status().unwrap().success());
|
||||
loop {
|
||||
if let Some(status) = child.try_wait().unwrap() {
|
||||
assert!(status.success(), "the daemon exited {status}");
|
||||
return started.elapsed();
|
||||
}
|
||||
if started.elapsed() > Duration::from_secs(10) {
|
||||
let _ = child.kill();
|
||||
panic!("the daemon ignored SIGTERM for 10 s");
|
||||
}
|
||||
std::thread::sleep(Duration::from_millis(50));
|
||||
}
|
||||
}
|
||||
|
||||
fn get(port: u16, path: &str) -> String {
|
||||
let mut s = TcpStream::connect(("127.0.0.1", port)).unwrap();
|
||||
write!(s, "GET {path} HTTP/1.1\r\nHost: 127.0.0.1\r\nConnection: close\r\n\r\n").unwrap();
|
||||
let mut out = String::new();
|
||||
let _ = s.read_to_string(&mut out);
|
||||
out
|
||||
}
|
||||
|
||||
fn token_in(config: &Path) -> String {
|
||||
let text = std::fs::read_to_string(config).unwrap();
|
||||
let line = text.lines().find(|l| l.trim_start().starts_with("token")).expect("a token line");
|
||||
line.split('"').nth(1).unwrap_or("").to_owned()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_daemon_starts_answers_refuses_a_second_and_stops_on_sigterm_mid_download() {
|
||||
let s = Scratch::new(serve_feed());
|
||||
let mut first = s.daemon("first.log");
|
||||
until("the control socket", Duration::from_secs(60), || s.socket().exists() && s.run(&["status"]).status.success());
|
||||
|
||||
// Through the socket: the daemon's own answer, in words.
|
||||
let status = s.run(&["status"]);
|
||||
assert!(text(&status).contains("1 feeds,"), "{}", text(&status));
|
||||
let local = s.run(&["--local", "status"]);
|
||||
assert!(local.status.success() && text(&local).contains("1 feeds,"), "{}", text(&local));
|
||||
|
||||
// A first start with no token writes one, and it signs in.
|
||||
let token = token_in(&s.root.join("config.toml"));
|
||||
assert_eq!(token.len(), 64, "a session-strength token");
|
||||
assert!(!s.log("first.log").contains(&token), "the log says where the token is, never what it is");
|
||||
let me = get(s.web, &format!("/api/me?token={token}"));
|
||||
assert!(me.starts_with("HTTP/1.1 200"), "{me}");
|
||||
assert!(get(s.web, "/api/me").starts_with("HTTP/1.1 401"));
|
||||
|
||||
// Two daemons on one socket would fight over the database.
|
||||
let second = s.ipx(&["daemon"]).output().unwrap();
|
||||
assert!(!second.status.success());
|
||||
assert!(text(&second).contains("a daemon is already listening"), "{}", text(&second));
|
||||
assert!(s.run(&["status"]).status.success(), "the first is still serving");
|
||||
|
||||
// The first pass starts at once and takes the episode, which takes minutes. SIGTERM stops it.
|
||||
until("the download to start", Duration::from_secs(60), || s.downloading() > 0);
|
||||
let took = terminate(&mut first);
|
||||
assert!(took < Duration::from_secs(5), "stopped in {took:?}");
|
||||
assert!(!s.socket().exists(), "the socket is removed");
|
||||
assert!(s.log("first.log").contains("abandoning the job in progress"), "{}", s.log("first.log"));
|
||||
|
||||
// The next start takes the abandoned download up again at once, not at its feed's next check,
|
||||
// which is an hour away (#410).
|
||||
assert_eq!(episode_gets(), 1);
|
||||
let mut again = s.daemon("again.log");
|
||||
until("the episode asked for again", Duration::from_secs(30), || episode_gets() == 2);
|
||||
assert!(s.log("again.log").contains("taking up downloads interrupted by a restart"), "{}", s.log("again.log"));
|
||||
terminate(&mut again);
|
||||
|
||||
// No daemon: the command does the work itself.
|
||||
let alone = s.run(&["status"]);
|
||||
assert!(alone.status.success() && text(&alone).contains("1 feeds,"), "{}", text(&alone));
|
||||
let _ = std::fs::remove_dir_all(&s.root);
|
||||
}
|
||||
25
crates/play/CHANGELOG.md
Normal file
25
crates/play/CHANGELOG.md
Normal file
@@ -0,0 +1,25 @@
|
||||
# Changelog: ipx-play
|
||||
|
||||
The pages shared episodes open, at `play.ipxreader.app` (docs/play.md). ipx's changelog is
|
||||
[crates/ipx/CHANGELOG.md](../ipx/CHANGELOG.md), the page's [web/CHANGELOG.md](../../web/CHANGELOG.md).
|
||||
|
||||
Its version is in `crates/play/Cargo.toml`, and a release is tagged `play-vX.Y.Z`.
|
||||
|
||||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
||||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
### Added
|
||||
|
||||
- `POST /v1/shares` takes and answers protobuf (`application/x-protobuf`) as well as JSON, chosen by `Content-Type` and `Accept`. The messages are in `crates/wire/proto/play.proto`, and answers go through structs generated from them (#465, #470).
|
||||
- A page for each shared episode, at `/e/<id>`: its show, title, date, length and notes, a player that plays the publisher's own file from the time it was shared at (`?t=754` or `?t=12:34`), buttons to subscribe in Apple Podcasts, Overcast, Pocket Casts or Castro, the feed's address, and a preview where the link is pasted (#447).
|
||||
- `POST /v1/shares`, for an ipx server with a token from `ipx-play token <name>`: an episode in, its short link out, the same link however often and from whichever server it is shared. An address that looks like a private or paid feed's is refused (#447).
|
||||
- Metrics for Prometheus on a port of its own, `metrics_bind`: requests by route and status and their time, episodes shared, memory (#458).
|
||||
|
||||
[unreleased]: https://git.sdf1.net/rays/ipx/commits/main/crates/play
|
||||
|
||||
### Security
|
||||
|
||||
- An episode's notes are cleaned with ammonia 4.2.5, which no longer lets SVG or MathML in a feed's HTML carry script past it (RUSTSEC-2026-0533) (#467).
|
||||
|
||||
40
crates/play/Cargo.toml
Normal file
40
crates/play/Cargo.toml
Normal file
@@ -0,0 +1,40 @@
|
||||
[package]
|
||||
name = "ipx-play"
|
||||
version = "0.1.0-dev"
|
||||
edition.workspace = true
|
||||
publish = false
|
||||
|
||||
[[bin]]
|
||||
name = "ipx-play"
|
||||
path = "src/main.rs"
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
[dependencies]
|
||||
ammonia.workspace = true
|
||||
anyhow.workspace = true
|
||||
axum.workspace = true
|
||||
chrono.workspace = true
|
||||
clap.workspace = true
|
||||
# Its tracing setup and token helpers, shared rather than copied: the same traceparent and JSON
|
||||
# log lines as ipx and the directory.
|
||||
ipx-directory.workspace = true
|
||||
ipx-feed.workspace = true
|
||||
ipx-wire.workspace = true
|
||||
opentelemetry.workspace = true
|
||||
percent-encoding.workspace = true
|
||||
reqwest.workspace = true
|
||||
sea-orm.workspace = true
|
||||
serde.workspace = true
|
||||
serde_json.workspace = true
|
||||
sha2.workspace = true
|
||||
tokio.workspace = true
|
||||
toml.workspace = true
|
||||
tracing.workspace = true
|
||||
tracing-opentelemetry.workspace = true
|
||||
tracing-subscriber.workspace = true
|
||||
url.workspace = true
|
||||
|
||||
[dev-dependencies]
|
||||
tower.workspace = true
|
||||
119
crates/play/src/config.rs
Normal file
119
crates/play/src/config.rs
Normal file
@@ -0,0 +1,119 @@
|
||||
//! `play.toml`: where ipx-play listens, its database, its public address, and the ipx servers
|
||||
//! that may make a link. Tokens are changed only here, as the directory's are.
|
||||
|
||||
use anyhow::{Context, Result, bail};
|
||||
use serde::Deserialize;
|
||||
use std::collections::HashSet;
|
||||
use std::path::Path;
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct Config {
|
||||
/// Where the API and the episode pages listen.
|
||||
#[serde(default = "default_bind")]
|
||||
pub bind: String,
|
||||
/// The environment variable holding the database URL, so the file holds no secret.
|
||||
#[serde(default = "default_database_url_env")]
|
||||
pub database_url_env: String,
|
||||
/// Where to serve /metrics for Prometheus (#458), apart from `bind`, which the tunnel
|
||||
/// publishes: `0.0.0.0:9191`. Empty serves none.
|
||||
#[serde(default)]
|
||||
pub metrics_bind: String,
|
||||
/// The address people open, which the links ipx is handed begin with.
|
||||
#[serde(default = "default_public_url")]
|
||||
pub public_url: String,
|
||||
#[serde(default)]
|
||||
pub tokens: Vec<TokenCfg>,
|
||||
}
|
||||
|
||||
fn default_bind() -> String {
|
||||
"0.0.0.0:8102".into()
|
||||
}
|
||||
|
||||
fn default_database_url_env() -> String {
|
||||
"PLAY_DATABASE_URL".into()
|
||||
}
|
||||
|
||||
fn default_public_url() -> String {
|
||||
"https://play.ipxreader.app".into()
|
||||
}
|
||||
|
||||
/// An ipx server allowed to make links: its name, for the log and the shares it made, and only
|
||||
/// its token's hash.
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct TokenCfg {
|
||||
pub name: String,
|
||||
/// What `ipx-play token new` prints.
|
||||
pub token_sha256: String,
|
||||
}
|
||||
|
||||
/// A token as the server checks it.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Token {
|
||||
pub name: String,
|
||||
pub sha256: String,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub fn load(path: &Path) -> Result<Self> {
|
||||
let text = std::fs::read_to_string(path).with_context(|| format!("reading {}", path.display()))?;
|
||||
Self::parse(&text).with_context(|| format!("in {}", path.display()))
|
||||
}
|
||||
|
||||
pub fn parse(text: &str) -> Result<Self> {
|
||||
let cfg: Config = toml::from_str(text)?;
|
||||
let u = url::Url::parse(&cfg.public_url).context("public_url is not an address")?;
|
||||
if !matches!(u.scheme(), "http" | "https") {
|
||||
bail!("public_url is not an http(s) address");
|
||||
}
|
||||
Ok(cfg)
|
||||
}
|
||||
|
||||
/// Refuses an empty or repeated name, and a hash that is not one or is listed twice: which
|
||||
/// server made a link would be ambiguous.
|
||||
pub fn tokens(&self) -> Result<Vec<Token>> {
|
||||
let (mut names, mut hashes) = (HashSet::new(), HashSet::new());
|
||||
self.tokens
|
||||
.iter()
|
||||
.map(|t| {
|
||||
if t.name.trim().is_empty() || !names.insert(t.name.as_str()) {
|
||||
bail!("token {:?}: each token needs a name of its own", t.name);
|
||||
}
|
||||
let sha256 = t.token_sha256.trim().to_ascii_lowercase();
|
||||
if sha256.len() != 64 || !sha256.bytes().all(|b| b.is_ascii_hexdigit()) {
|
||||
bail!("token {:?}: token_sha256 is not a SHA-256 in hex", t.name);
|
||||
}
|
||||
if !hashes.insert(sha256.clone()) {
|
||||
bail!("token {:?} is the same token as another", t.name);
|
||||
}
|
||||
Ok(Token { name: t.name.clone(), sha256 })
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// The public address without a trailing slash, for links.
|
||||
#[must_use]
|
||||
pub fn base(&self) -> &str {
|
||||
self.public_url.trim_end_matches('/')
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn a_config_reads_with_its_defaults_and_refuses_ambiguous_tokens() {
|
||||
let h = "a".repeat(64);
|
||||
let cfg = Config::parse(&format!("[[tokens]]\nname = \"tower\"\ntoken_sha256 = \"{h}\"")).unwrap();
|
||||
assert_eq!((cfg.bind.as_str(), cfg.base()), ("0.0.0.0:8102", "https://play.ipxreader.app"));
|
||||
assert_eq!(cfg.tokens().unwrap()[0].name, "tower");
|
||||
let two = format!(
|
||||
"[[tokens]]\nname = \"a\"\ntoken_sha256 = \"{h}\"\n[[tokens]]\nname = \"b\"\ntoken_sha256 = \"{h}\""
|
||||
);
|
||||
assert!(Config::parse(&two).unwrap().tokens().is_err(), "one token under two names");
|
||||
assert!(Config::parse("[[tokens]]\nname = \"a\"\ntoken_sha256 = \"xyz\"").unwrap().tokens().is_err());
|
||||
assert!(Config::parse("public_url = \"ftp://x\"").is_err());
|
||||
}
|
||||
}
|
||||
345
crates/play/src/lib.rs
Normal file
345
crates/play/src/lib.rs
Normal file
@@ -0,0 +1,345 @@
|
||||
//! `ipx-play` (#447): the page a shared episode opens, at `play.ipxreader.app/e/<id>`, as
|
||||
//! Overcast's links do. When someone shares an episode, their ipx server sends it here with its
|
||||
//! token and gets back a short link; anyone with the link sees the episode, its show and its
|
||||
//! notes, and plays it from the publisher's own file, from the time it was shared at if it was.
|
||||
//!
|
||||
//! Only public episodes come here: ipx sends none from a private or paid feed, and this refuses
|
||||
//! an address that looks like one (`ipx_feed::url_looks_private`), which would hand out the
|
||||
//! sharer's subscription. The files stay the publisher's; this keeps what an episode is and where
|
||||
//! its file is, nothing more.
|
||||
|
||||
pub mod config;
|
||||
pub mod metrics;
|
||||
mod page;
|
||||
pub mod store;
|
||||
|
||||
use axum::extract::{MatchedPath, Path, Query, Request, State};
|
||||
use axum::http::{StatusCode, header};
|
||||
use axum::middleware::Next;
|
||||
use axum::response::{IntoResponse, Response};
|
||||
use axum::routing::{get, post};
|
||||
use axum::{Extension, Json, Router};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::sync::Arc;
|
||||
use std::time::Instant;
|
||||
use tracing::Instrument;
|
||||
|
||||
/// An episode as ipx sends it to be shared.
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct Episode {
|
||||
pub feed_url: String,
|
||||
pub guid: String,
|
||||
pub feed_title: Option<String>,
|
||||
pub feed_image: Option<String>,
|
||||
pub title: Option<String>,
|
||||
/// The feed's own HTML for it, sanitized here when the page is drawn.
|
||||
pub description: Option<String>,
|
||||
pub link: Option<String>,
|
||||
/// Unix seconds.
|
||||
pub published: Option<i64>,
|
||||
/// Seconds.
|
||||
pub duration: Option<i64>,
|
||||
/// The publisher's file, which the page plays.
|
||||
pub audio_url: String,
|
||||
pub mime: Option<String>,
|
||||
}
|
||||
|
||||
/// What ipx gets back: the link to hand on, to which it adds `?t=` for a time.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct Shared {
|
||||
pub id: String,
|
||||
pub url: String,
|
||||
}
|
||||
|
||||
/// An episode kept, by its id.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Share {
|
||||
pub id: String,
|
||||
pub episode: Episode,
|
||||
}
|
||||
|
||||
/// Longer than any real one, short enough that nobody keeps a book here.
|
||||
const MAX_TEXT: usize = 100_000;
|
||||
const MAX_SHORT: usize = 2_000;
|
||||
|
||||
impl Episode {
|
||||
/// What makes an episode unfit to keep, in words for whoever sent it; None when it is fine.
|
||||
/// Over-long text is cut to fit rather than refused: a show's notes can be long.
|
||||
fn check(&mut self) -> Option<&'static str> {
|
||||
let web = |u: &str| url::Url::parse(u).is_ok_and(|u| matches!(u.scheme(), "http" | "https"));
|
||||
if !web(&self.feed_url) || !web(&self.audio_url) {
|
||||
return Some("the feed and its file need http(s) addresses");
|
||||
}
|
||||
if ipx_feed::url_looks_private(&self.feed_url) || ipx_feed::url_looks_private(&self.audio_url) {
|
||||
return Some("that looks like a private or paid feed, whose addresses carry someone's access");
|
||||
}
|
||||
if self.guid.trim().is_empty() || self.guid.len() > MAX_SHORT {
|
||||
return Some("an episode needs its guid");
|
||||
}
|
||||
if self.feed_image.as_deref().is_some_and(|u| !web(u)) {
|
||||
self.feed_image = None;
|
||||
}
|
||||
if self.link.as_deref().is_some_and(|u| !web(u)) {
|
||||
self.link = None;
|
||||
}
|
||||
for (s, max) in
|
||||
[(&mut self.title, MAX_SHORT), (&mut self.feed_title, MAX_SHORT), (&mut self.description, MAX_TEXT)]
|
||||
{
|
||||
if let Some(t) = s
|
||||
&& t.len() > max
|
||||
{
|
||||
let mut end = max;
|
||||
while !t.is_char_boundary(end) {
|
||||
end -= 1;
|
||||
}
|
||||
t.truncate(end);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// What every request shares.
|
||||
pub struct Play {
|
||||
pub store: store::Store,
|
||||
pub tokens: Vec<config::Token>,
|
||||
pub base: String,
|
||||
}
|
||||
|
||||
type St = Arc<Play>;
|
||||
|
||||
/// Each route's protobuf messages (#465), beside its JSON.
|
||||
const WIRE: ipx_wire::Routes = &[("POST", "/v1/shares", "play.Episode", "play.Shared")];
|
||||
|
||||
pub fn router(play: Play) -> Router {
|
||||
let st = Arc::new(play);
|
||||
let api = Router::new()
|
||||
.route("/v1/shares", post(share))
|
||||
.route_layer(axum::middleware::from_fn_with_state(WIRE, ipx_wire::negotiate))
|
||||
.layer(axum::middleware::from_fn_with_state(st.clone(), authenticate));
|
||||
Router::new()
|
||||
.route("/healthz", get(healthz))
|
||||
.route("/e/{id}", get(episode_page))
|
||||
.route("/play.css", get(|| async { page::asset("text/css; charset=utf-8", include_bytes!("play.css")) }))
|
||||
.route("/logo.svg", get(|| async { page::asset("image/svg+xml", include_bytes!("../../../web/logo.svg")) }))
|
||||
.route("/favicon.png", get(|| async { page::asset("image/png", include_bytes!("../../../web/favicon.png")) }))
|
||||
.merge(api)
|
||||
.fallback(|| async { page::not_found() })
|
||||
.layer(axum::middleware::from_fn(access_log))
|
||||
.with_state(st)
|
||||
}
|
||||
|
||||
/// Which server made the request, for the log and the share, set by `authenticate`.
|
||||
#[derive(Clone)]
|
||||
struct Who(String);
|
||||
|
||||
async fn authenticate(State(st): State<St>, mut req: Request, next: Next) -> Response {
|
||||
let presented = req
|
||||
.headers()
|
||||
.get(header::AUTHORIZATION)
|
||||
.and_then(|v| v.to_str().ok())
|
||||
.and_then(|v| v.strip_prefix("Bearer "))
|
||||
.map(str::trim)
|
||||
.filter(|t| !t.is_empty());
|
||||
let who = presented.and_then(|t| {
|
||||
let h = ipx_directory::config::hash(t);
|
||||
st.tokens.iter().find(|k| ipx_directory::config::same_secret(&k.sha256, &h)).map(|k| k.name.clone())
|
||||
});
|
||||
let Some(who) = who else {
|
||||
return error(StatusCode::UNAUTHORIZED, "a valid token is needed: Authorization: Bearer <token>");
|
||||
};
|
||||
req.extensions_mut().insert(Who(who.clone()));
|
||||
let mut resp = next.run(req).await;
|
||||
resp.extensions_mut().insert(Who(who));
|
||||
resp
|
||||
}
|
||||
|
||||
fn error(status: StatusCode, msg: &str) -> Response {
|
||||
(status, Json(serde_json::json!({ "error": msg }))).into_response()
|
||||
}
|
||||
|
||||
#[tracing::instrument(name = "share", skip_all, fields(server = %who.0))]
|
||||
async fn share(State(st): State<St>, Extension(who): Extension<Who>, Json(mut e): Json<Episode>) -> Response {
|
||||
if let Some(why) = e.check() {
|
||||
return error(StatusCode::UNPROCESSABLE_ENTITY, why);
|
||||
}
|
||||
match st.store.put(&who.0, &e).await {
|
||||
Ok(id) => {
|
||||
metrics::SHARES.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
|
||||
tracing::info!(server = %who.0, id = %id, "an episode was shared");
|
||||
Json(Shared { url: format!("{}/e/{id}", st.base), id }).into_response()
|
||||
}
|
||||
Err(err) => {
|
||||
tracing::error!(error = %format!("{err:#}"), "keeping a share failed");
|
||||
error(StatusCode::INTERNAL_SERVER_ERROR, "it could not be kept; ipx-play's log says why")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct At {
|
||||
/// Where to start: seconds, or as written, `12:34` or `1:02:03`.
|
||||
t: Option<String>,
|
||||
}
|
||||
|
||||
async fn episode_page(State(st): State<St>, Path(id): Path<String>, Query(at): Query<At>) -> Response {
|
||||
match st.store.get(&id).instrument(tracing::info_span!("step", of = "get_share")).await {
|
||||
Ok(Some(s)) => page::episode(&s, at.t.as_deref().and_then(page::parse_time), &st.base),
|
||||
Ok(None) => page::not_found(),
|
||||
Err(err) => {
|
||||
tracing::error!(error = %format!("{err:#}"), "reading a share failed");
|
||||
page::failed()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn healthz(State(st): State<St>) -> Response {
|
||||
match st.store.ping().await {
|
||||
Ok(()) => (StatusCode::OK, "ok").into_response(),
|
||||
Err(e) => (StatusCode::SERVICE_UNAVAILABLE, format!("database: {e:#}")).into_response(),
|
||||
}
|
||||
}
|
||||
|
||||
/// A span a request, joined to the caller's trace when it sent a traceparent, as ipx does on its
|
||||
/// calls here; and a line, with the server's name and never its token.
|
||||
async fn access_log(req: Request, next: Next) -> Response {
|
||||
let (method, path) = (req.method().clone(), req.uri().path().to_owned());
|
||||
// By route, not path: by path each shared episode would be a row of its own. A path no route
|
||||
// has is "", which someone trying addresses cannot multiply.
|
||||
let route = req.extensions().get::<MatchedPath>().map_or("", |r| r.as_str()).to_owned();
|
||||
let start = Instant::now();
|
||||
let span = if path == "/healthz" {
|
||||
tracing::Span::none()
|
||||
} else {
|
||||
tracing::info_span!(
|
||||
"http",
|
||||
otel.name = %format!("{method} {}", if path.starts_with("/e/") { "/e/{id}" } else { path.as_str() }),
|
||||
http.request.method = %method,
|
||||
url.path = %path,
|
||||
http.response.status_code = tracing::field::Empty,
|
||||
)
|
||||
};
|
||||
if let Some(parent) = ipx_directory::otel::remote_parent(req.headers(), None) {
|
||||
use tracing_opentelemetry::OpenTelemetrySpanExt;
|
||||
let _ = span.set_parent(parent);
|
||||
}
|
||||
let resp = next.run(req).instrument(span.clone()).await;
|
||||
let status = resp.status().as_u16();
|
||||
span.record("http.response.status_code", status);
|
||||
if path != "/healthz" {
|
||||
metrics::REQUESTS.observe(
|
||||
&format!(
|
||||
"method=\"{}\",route=\"{}\",status=\"{status}\"",
|
||||
ipx_directory::metrics::method(method.as_str()),
|
||||
ipx_directory::metrics::label(&route)
|
||||
),
|
||||
start.elapsed().as_secs_f64(),
|
||||
);
|
||||
let _in = span.enter();
|
||||
let who = resp.extensions().get::<Who>().map_or("", |w| w.0.as_str()).to_owned();
|
||||
tracing::info!(
|
||||
target: "ipx_play::http",
|
||||
method = %method, path = %path, status, duration_ms = start.elapsed().as_millis() as u64, token = %who,
|
||||
"{method} {path} -> {status}"
|
||||
);
|
||||
}
|
||||
resp
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use axum::body::Body;
|
||||
use tower::ServiceExt;
|
||||
|
||||
const TOKEN: &str = "ipxd_test";
|
||||
|
||||
async fn app() -> Router {
|
||||
router(Play {
|
||||
store: store::Store::scratch().await,
|
||||
tokens: vec![config::Token { name: "tower".into(), sha256: ipx_directory::config::hash(TOKEN) }],
|
||||
base: "https://play.test".into(),
|
||||
})
|
||||
}
|
||||
|
||||
fn episode() -> Episode {
|
||||
Episode {
|
||||
feed_url: "https://example.com/feed.xml".into(),
|
||||
guid: "ep-1".into(),
|
||||
feed_title: Some("A Show".into()),
|
||||
feed_image: Some("https://example.com/art.jpg".into()),
|
||||
title: Some("Episode <One>".into()),
|
||||
description: Some("<p>Notes</p><script>alert(1)</script>".into()),
|
||||
link: Some("javascript:alert(1)".into()),
|
||||
published: Some(1_800_000_000),
|
||||
duration: Some(3600),
|
||||
audio_url: "https://example.com/ep1.mp3".into(),
|
||||
mime: Some("audio/mpeg".into()),
|
||||
}
|
||||
}
|
||||
|
||||
async fn post(app: &Router, token: Option<&str>, e: &Episode) -> (StatusCode, serde_json::Value) {
|
||||
let mut req = axum::http::Request::post("/v1/shares").header("content-type", "application/json");
|
||||
if let Some(t) = token {
|
||||
req = req.header("authorization", format!("Bearer {t}"));
|
||||
}
|
||||
let resp = app.clone().oneshot(req.body(Body::from(serde_json::to_vec(e).unwrap())).unwrap()).await.unwrap();
|
||||
let status = resp.status();
|
||||
let body = axum::body::to_bytes(resp.into_body(), 1 << 20).await.unwrap();
|
||||
(status, serde_json::from_slice(&body).unwrap_or_default())
|
||||
}
|
||||
|
||||
async fn get(app: &Router, path: &str) -> (StatusCode, String) {
|
||||
let resp = app.clone().oneshot(axum::http::Request::get(path).body(Body::empty()).unwrap()).await.unwrap();
|
||||
let status = resp.status();
|
||||
let body = axum::body::to_bytes(resp.into_body(), 1 << 20).await.unwrap();
|
||||
(status, String::from_utf8_lossy(&body).into_owned())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_server_with_a_token_makes_a_link_and_anyone_opens_it() {
|
||||
let app = app().await;
|
||||
assert_eq!(post(&app, None, &episode()).await.0, StatusCode::UNAUTHORIZED);
|
||||
assert_eq!(post(&app, Some("wrong"), &episode()).await.0, StatusCode::UNAUTHORIZED);
|
||||
|
||||
let (status, body) = post(&app, Some(TOKEN), &episode()).await;
|
||||
assert_eq!(status, StatusCode::OK);
|
||||
let url = body["url"].as_str().unwrap();
|
||||
assert!(url.starts_with("https://play.test/e/"), "{url}");
|
||||
|
||||
let path = url.trim_start_matches("https://play.test");
|
||||
let (status, html) = get(&app, &format!("{path}?t=12:34")).await;
|
||||
assert_eq!(status, StatusCode::OK);
|
||||
assert!(html.contains("Episode <One>"), "the title, escaped");
|
||||
assert!(html.contains("ep1.mp3#t=754"), "the player starts at the time shared");
|
||||
assert!(html.contains("Starts at 12:34"));
|
||||
assert!(html.contains(r#"property="og:title""#), "a link preview");
|
||||
assert!(!html.contains("<script>alert"), "the notes are sanitized");
|
||||
assert!(!html.contains("javascript:"), "a link that is not a web address is dropped");
|
||||
|
||||
assert_eq!(get(&app, "/e/nothere").await.0, StatusCode::NOT_FOUND);
|
||||
get(&app, "/no/such/page").await;
|
||||
|
||||
let mut out = String::new();
|
||||
metrics::REQUESTS.render(&mut out, "r", "");
|
||||
for row in [
|
||||
r#"r_count{method="GET",route="/e/{id}",status="200"}"#,
|
||||
r#"r_count{method="POST",route="/v1/shares",status="200"}"#,
|
||||
r#"r_count{method="GET",route="",status="404"}"#,
|
||||
] {
|
||||
assert!(out.contains(row), "requests are counted by route, not path: {row} in {out}");
|
||||
}
|
||||
assert!(!out.contains("nothere") && !out.contains("/no/such"), "{out}");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_private_feed_is_refused() {
|
||||
let app = app().await;
|
||||
let mut e = episode();
|
||||
e.feed_url = "https://www.patreon.com/rss/show?auth=secret".into();
|
||||
assert_eq!(post(&app, Some(TOKEN), &e).await.0, StatusCode::UNPROCESSABLE_ENTITY);
|
||||
let mut e = episode();
|
||||
e.audio_url = "file:///etc/passwd".into();
|
||||
assert_eq!(post(&app, Some(TOKEN), &e).await.0, StatusCode::UNPROCESSABLE_ENTITY);
|
||||
}
|
||||
}
|
||||
124
crates/play/src/main.rs
Normal file
124
crates/play/src/main.rs
Normal file
@@ -0,0 +1,124 @@
|
||||
//! `ipx-play`: serve the shared-episode pages, make a token for an ipx server, or check that a
|
||||
//! running one answers.
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
use clap::{Parser, Subcommand};
|
||||
use ipx_play::{Play, config, store::Store};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::time::Duration;
|
||||
use tokio::net::TcpListener;
|
||||
use tokio::signal::unix::SignalKind;
|
||||
use tracing_subscriber::EnvFilter;
|
||||
use tracing_subscriber::prelude::*;
|
||||
|
||||
// mimalloc comes with ipx-directory, whose library sets it as the allocator.
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(version, about = "The pages ipx's shared episodes open")]
|
||||
struct Cli {
|
||||
/// The config file. Otherwise PLAY_CONFIG, or else /config/play.toml.
|
||||
#[arg(long)]
|
||||
config: Option<PathBuf>,
|
||||
#[command(subcommand)]
|
||||
cmd: Option<Cmd>,
|
||||
}
|
||||
|
||||
#[derive(Subcommand)]
|
||||
enum Cmd {
|
||||
/// Serve the pages and the API (the default).
|
||||
Serve,
|
||||
/// Make a token for an ipx server, and print the lines that let it in.
|
||||
Token {
|
||||
/// Who it is for, as the log will name them.
|
||||
name: String,
|
||||
},
|
||||
/// Check that ipx-play on this machine answers and can read its database.
|
||||
Health,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<()> {
|
||||
let cli = Cli::parse();
|
||||
let path = cli
|
||||
.config
|
||||
.or_else(|| std::env::var_os("PLAY_CONFIG").map(Into::into))
|
||||
.unwrap_or_else(|| "/config/play.toml".into());
|
||||
match cli.cmd.unwrap_or(Cmd::Serve) {
|
||||
Cmd::Serve => serve(&path).await,
|
||||
Cmd::Token { name } => {
|
||||
let token = ipx_directory::config::new_token();
|
||||
println!("A token for {name}, to enter in its Admin, Sharing; it is not kept anywhere:\n\n {token}\n");
|
||||
println!("To let it in, add this to {} and restart ipx-play:\n", path.display());
|
||||
println!("[[tokens]]\nname = {name:?}\ntoken_sha256 = {:?}", ipx_directory::config::hash(&token));
|
||||
Ok(())
|
||||
}
|
||||
Cmd::Health => {
|
||||
let cfg = config::Config::load(&path)?;
|
||||
let port = cfg.bind.rsplit(':').next().unwrap_or("8102");
|
||||
let resp = reqwest::Client::new()
|
||||
.get(format!("http://127.0.0.1:{port}/healthz"))
|
||||
.timeout(Duration::from_secs(4))
|
||||
.send()
|
||||
.await
|
||||
.context("ipx-play does not answer")?;
|
||||
anyhow::ensure!(resp.status().is_success(), "ipx-play answers {}", resp.status());
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn serve(path: &Path) -> Result<()> {
|
||||
// One JSON object a line for Loki when PLAY_LOG_FORMAT=json, as ipx and the directory do.
|
||||
let json = std::env::var("PLAY_LOG_FORMAT").is_ok_and(|f| f.eq_ignore_ascii_case("json"));
|
||||
let filter = || EnvFilter::try_from_env("PLAY_LOG").unwrap_or_else(|_| "ipx_play=info".into());
|
||||
// Traces to Tempo when OTEL_EXPORTER_OTLP_ENDPOINT is set, joining ipx's.
|
||||
let otel = ipx_directory::otel::provider("ipx-play")?;
|
||||
let stderr: Box<dyn tracing_subscriber::Layer<tracing_subscriber::Registry> + Send + Sync> = if json {
|
||||
ipx_directory::otel::json_layer(std::io::stderr, false).with_filter(filter()).boxed()
|
||||
} else {
|
||||
tracing_subscriber::fmt::layer().with_writer(std::io::stderr).with_filter(filter()).boxed()
|
||||
};
|
||||
tracing_subscriber::registry()
|
||||
.with(stderr)
|
||||
.with(otel.as_ref().map(|p| {
|
||||
use opentelemetry::trace::TracerProvider;
|
||||
tracing_opentelemetry::layer()
|
||||
.with_tracer(p.tracer("ipx-play"))
|
||||
.with_filter(EnvFilter::new("ipx_play=info"))
|
||||
}))
|
||||
.init();
|
||||
|
||||
let cfg = config::Config::load(path)?;
|
||||
// ponytail: tokens are read at start; a new server's token takes a restart. Reload the file
|
||||
// on a timer, as the directory does, if servers come and go often.
|
||||
let tokens = cfg.tokens()?;
|
||||
let url = std::env::var(&cfg.database_url_env)
|
||||
.ok()
|
||||
.filter(|u| !u.trim().is_empty())
|
||||
.with_context(|| format!("{} is not set: ipx-play needs a database URL", cfg.database_url_env))?;
|
||||
let store = Store::open(&url, true).await.context("opening the database")?;
|
||||
let listener = TcpListener::bind(&cfg.bind).await.with_context(|| format!("listening on {}", cfg.bind))?;
|
||||
if !cfg.metrics_bind.is_empty() {
|
||||
let metrics = TcpListener::bind(&cfg.metrics_bind)
|
||||
.await
|
||||
.with_context(|| format!("listening for metrics on {}", cfg.metrics_bind))?;
|
||||
tracing::info!(bind = %cfg.metrics_bind, "serving metrics for Prometheus at /metrics");
|
||||
tokio::spawn(ipx_play::metrics::serve(metrics));
|
||||
}
|
||||
tracing::info!(version = env!("CARGO_PKG_VERSION"), bind = %cfg.bind, tokens = tokens.len(), "ipx-play started");
|
||||
let app = ipx_play::router(Play { store, tokens, base: cfg.base().to_owned() });
|
||||
axum::serve(listener, app)
|
||||
.with_graceful_shutdown(async {
|
||||
let mut term = tokio::signal::unix::signal(SignalKind::terminate()).expect("SIGTERM handler");
|
||||
tokio::select! {
|
||||
_ = term.recv() => {}
|
||||
_ = tokio::signal::ctrl_c() => {}
|
||||
}
|
||||
})
|
||||
.await?;
|
||||
// The batch exporter holds the last few seconds of spans; without this they are lost.
|
||||
if let Some(p) = otel {
|
||||
let _ = p.shutdown();
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
99
crates/play/src/metrics.rs
Normal file
99
crates/play/src/metrics.rs
Normal file
@@ -0,0 +1,99 @@
|
||||
//! ipx-play's /metrics (#458), in Prometheus's text format: memory, open files, the requests it
|
||||
//! answered and the episodes it was given to share. The histogram and /proc reading are the
|
||||
//! directory's, so the two read alike on the dashboard.
|
||||
|
||||
use axum::Router;
|
||||
use ipx_directory::metrics::{Histograms, SECONDS, process};
|
||||
use std::sync::atomic::AtomicU64;
|
||||
use std::sync::atomic::Ordering::Relaxed;
|
||||
use std::time::Instant;
|
||||
use tokio::net::TcpListener;
|
||||
|
||||
/// Requests answered, by method, route and status: a page opened is `route="/e/{id}"`.
|
||||
pub static REQUESTS: Histograms = Histograms::new(SECONDS);
|
||||
/// Episodes an ipx server sent to be shared, the same one again included.
|
||||
pub static SHARES: AtomicU64 = AtomicU64::new(0);
|
||||
|
||||
/// Nothing from the database, so a scrape costs nothing and answers while it is down.
|
||||
fn render(started: Instant) -> String {
|
||||
let mut out = String::new();
|
||||
let mut gauge = |name: &str, kind: &str, help: &str, labels: &str, v: f64| {
|
||||
out.push_str(&format!("# HELP {name} {help}\n# TYPE {name} {kind}\n{name}{labels} {v}\n"));
|
||||
};
|
||||
let (rss, fds) = process();
|
||||
if let Some(v) = rss {
|
||||
gauge("play_process_resident_bytes", "gauge", "Memory the process holds.", "", v);
|
||||
}
|
||||
if let Some(v) = fds {
|
||||
gauge("play_process_open_fds", "gauge", "Files and sockets the process has open.", "", v);
|
||||
}
|
||||
gauge("play_uptime_seconds", "gauge", "Seconds since ipx-play started.", "", started.elapsed().as_secs() as f64);
|
||||
gauge(
|
||||
"play_build_info",
|
||||
"gauge",
|
||||
"The version running, as a label.",
|
||||
&format!("{{version=\"{}\"}}", env!("CARGO_PKG_VERSION")),
|
||||
1.0,
|
||||
);
|
||||
gauge("play_shares_total", "counter", "Episodes ipx servers sent to be shared.", "", SHARES.load(Relaxed) as f64);
|
||||
REQUESTS.render(
|
||||
&mut out,
|
||||
"play_http_request_duration_seconds",
|
||||
"Requests answered and the seconds each took, by method, route and status.",
|
||||
);
|
||||
out
|
||||
}
|
||||
|
||||
/// Serves /metrics on a listener of its own: the public port is behind the tunnel with no Access
|
||||
/// in front, and these are for the LAN.
|
||||
// ponytail: no gzip, unlike the directory's (#253): a few dozen lines a scrape. Add tower-http's
|
||||
// CompressionLayer if the routes grow.
|
||||
pub async fn serve(listener: TcpListener) {
|
||||
let started = Instant::now();
|
||||
let app = Router::new().route(
|
||||
"/metrics",
|
||||
axum::routing::get(move || async move {
|
||||
([(axum::http::header::CONTENT_TYPE, "text/plain; version=0.0.4")], render(started))
|
||||
}),
|
||||
);
|
||||
if let Err(e) = axum::serve(listener, app).await {
|
||||
tracing::error!(error = %e, "metrics stopped");
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[tokio::test]
|
||||
async fn every_play_metric_grafana_queries_is_served() {
|
||||
// A row for each name, so a histogram's lines are there to find.
|
||||
REQUESTS.observe("method=\"GET\",route=\"/e/{id}\",status=\"200\"", 0.01);
|
||||
let l = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let at = l.local_addr().unwrap();
|
||||
tokio::spawn(serve(l));
|
||||
let text = reqwest::get(format!("http://{at}/metrics")).await.unwrap().text().await.unwrap();
|
||||
let src = [include_str!("../../../grafana/dashboard.py"), include_str!("../../../grafana/alerts.py")].concat();
|
||||
let names: std::collections::BTreeSet<&str> = src
|
||||
.match_indices("play_")
|
||||
.filter(|(i, _)| !src[..*i].ends_with(|c: char| c.is_ascii_alphanumeric() || c == '_'))
|
||||
.filter_map(|(i, _)| {
|
||||
let rest = &src[i..];
|
||||
let end = rest.find(|c: char| !(c.is_ascii_lowercase() || c == '_')).unwrap_or(rest.len());
|
||||
// A query names one with its labels, or the dashboard keeps it in a string; ipx's
|
||||
// events, such as play_start in a panel's description, are neither.
|
||||
rest[end..].starts_with(['{', '"']).then(|| &rest[..end])
|
||||
})
|
||||
// A histogram is queried by its series' names and served under its own.
|
||||
.map(|n| n.trim_end_matches("_bucket").trim_end_matches("_count").trim_end_matches("_sum"))
|
||||
.collect();
|
||||
assert!(names.len() >= 3, "{names:?}");
|
||||
for n in names {
|
||||
// Memory comes from /proc, which a Mac has not.
|
||||
if n.starts_with("play_process_") && process().0.is_none() {
|
||||
continue;
|
||||
}
|
||||
assert!(text.contains(&format!("# TYPE {n} ")), "{n} is queried by grafana/ but not served: {text}");
|
||||
}
|
||||
}
|
||||
}
|
||||
235
crates/play/src/page.rs
Normal file
235
crates/play/src/page.rs
Normal file
@@ -0,0 +1,235 @@
|
||||
//! The episode page: what it is, a player that starts at the time shared, its notes, and how to
|
||||
//! subscribe to the show in whatever app the reader has. No script: the start time is the media
|
||||
//! fragment `#t=`, which every browser's player honours.
|
||||
|
||||
use crate::Share;
|
||||
use axum::http::{HeaderValue, StatusCode, header};
|
||||
use axum::response::{IntoResponse, Response};
|
||||
use std::fmt::Write as _;
|
||||
|
||||
/// Nothing runs on the page, nothing loads but the art and the file, and no other site may frame
|
||||
/// it. Images and audio from anywhere: they are the publisher's.
|
||||
const CSP: &str = "default-src 'none'; img-src https: http: data:; media-src https: http:; style-src 'self'; \
|
||||
base-uri 'none'; form-action 'none'; frame-ancestors 'none'";
|
||||
|
||||
pub fn asset(content_type: &'static str, body: &'static [u8]) -> Response {
|
||||
([(header::CONTENT_TYPE, content_type), (header::CACHE_CONTROL, "public, max-age=86400")], body).into_response()
|
||||
}
|
||||
|
||||
fn esc(s: &str) -> String {
|
||||
s.replace('&', "&").replace('<', "<").replace('>', ">").replace('"', """).replace('\'', "'")
|
||||
}
|
||||
|
||||
/// `?t=`: seconds (`754`), or as a clock reads (`12:34`, `1:02:03`). None for anything else.
|
||||
#[must_use]
|
||||
pub fn parse_time(t: &str) -> Option<u64> {
|
||||
let parts: Vec<&str> = t.trim().split(':').collect();
|
||||
if parts.is_empty()
|
||||
|| parts.len() > 3
|
||||
|| parts.iter().any(|p| p.is_empty() || !p.bytes().all(|b| b.is_ascii_digit()))
|
||||
{
|
||||
return None;
|
||||
}
|
||||
parts.iter().try_fold(0u64, |n, p| n.checked_mul(60)?.checked_add(p.parse().ok()?)).filter(|&s| s > 0)
|
||||
}
|
||||
|
||||
/// `12:34`, or `1:02:03` past an hour, as the player shows it.
|
||||
#[must_use]
|
||||
pub fn clock(secs: u64) -> String {
|
||||
let (h, m, s) = (secs / 3600, secs / 60 % 60, secs % 60);
|
||||
if h > 0 { format!("{h}:{m:02}:{s:02}") } else { format!("{m}:{s:02}") }
|
||||
}
|
||||
|
||||
/// `1 hr 2 min`, `45 min`.
|
||||
fn length(secs: i64) -> String {
|
||||
let m = (secs + 30) / 60;
|
||||
match (m / 60, m % 60) {
|
||||
(0, m) => format!("{} min", m.max(1)),
|
||||
(h, 0) => format!("{h} hr"),
|
||||
(h, m) => format!("{h} hr {m} min"),
|
||||
}
|
||||
}
|
||||
|
||||
/// The notes as words, for a link preview: their tags gone, cut at a word near `max` characters.
|
||||
fn plain(html: &str, max: usize) -> String {
|
||||
// A space where each tag was, or two paragraphs read as one word ("Jerrytalk"). A preview
|
||||
// can afford the odd space before a bold word.
|
||||
let text = ammonia::Builder::empty().clean(&html.replace('<', " <")).to_string();
|
||||
let text = text
|
||||
.replace(" ", " ")
|
||||
.replace("<", "<")
|
||||
.replace(">", ">")
|
||||
.replace(""", "\"")
|
||||
.replace("'", "'")
|
||||
.replace("&", "&");
|
||||
let words = text.split_whitespace().collect::<Vec<_>>().join(" ");
|
||||
if words.chars().count() <= max {
|
||||
return words;
|
||||
}
|
||||
let cut: String = words.chars().take(max).collect();
|
||||
format!("{}…", cut.rsplit_once(' ').map_or(cut.as_str(), |(a, _)| a))
|
||||
}
|
||||
|
||||
/// Where a podcast app takes a feed to subscribe to, by the address schemes each one answers.
|
||||
/// On a phone without the app the link does nothing, as Overcast's own page's do.
|
||||
fn apps(feed: &str) -> Vec<(&'static str, String)> {
|
||||
let bare = feed.split_once("://").map_or(feed, |(_, rest)| rest);
|
||||
let enc = percent_encoding::utf8_percent_encode(feed, percent_encoding::NON_ALPHANUMERIC).to_string();
|
||||
vec![
|
||||
("Apple Podcasts", format!("podcast://{bare}")),
|
||||
("Overcast", format!("overcast://x-callback-url/add?url={enc}")),
|
||||
("Pocket Casts", format!("pktc://subscribe/{bare}")),
|
||||
("Castro", format!("castros://subscribe/{bare}")),
|
||||
]
|
||||
}
|
||||
|
||||
fn html(status: StatusCode, body: String) -> Response {
|
||||
let mut resp = (status, [(header::CONTENT_TYPE, "text/html; charset=utf-8")], body).into_response();
|
||||
let h = resp.headers_mut();
|
||||
h.insert(header::CONTENT_SECURITY_POLICY, HeaderValue::from_static(CSP));
|
||||
h.insert(header::CACHE_CONTROL, HeaderValue::from_static("public, max-age=300"));
|
||||
h.insert("referrer-policy", HeaderValue::from_static("no-referrer"));
|
||||
resp
|
||||
}
|
||||
|
||||
fn frame(title: &str, head: &str, body: &str) -> String {
|
||||
format!(
|
||||
"<!doctype html><html lang=en><head><meta charset=utf-8>\
|
||||
<meta name=viewport content=\"width=device-width, initial-scale=1\">\
|
||||
<meta name=color-scheme content=\"light dark\"><title>{}</title>\
|
||||
<link rel=icon href=/favicon.png><link rel=stylesheet href=/play.css>{head}</head>\
|
||||
<body><main><a class=brand href=\"https://ipxreader.app\"><img src=/logo.svg alt=\"\" width=28 height=28>iPX</a>\
|
||||
{body}<footer>Shared from <a href=\"https://ipxreader.app\">iPX</a>, a podcast and feed reader you run yourself.</footer>\
|
||||
</main></body></html>",
|
||||
esc(title)
|
||||
)
|
||||
}
|
||||
|
||||
pub fn not_found() -> Response {
|
||||
html(
|
||||
StatusCode::NOT_FOUND,
|
||||
frame("Not here", "", "<h1>Not here</h1><p class=meta>That link is not one iPX made, or it was mistyped.</p>"),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn failed() -> Response {
|
||||
html(
|
||||
StatusCode::SERVICE_UNAVAILABLE,
|
||||
frame("Try again", "", "<h1>Try again</h1><p class=meta>The episode could not be read just now.</p>"),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn episode(s: &Share, at: Option<u64>, base: &str) -> Response {
|
||||
let e = &s.episode;
|
||||
let show = e.feed_title.as_deref().map(str::trim).filter(|t| !t.is_empty()).unwrap_or("A podcast");
|
||||
let title = e.title.as_deref().map(str::trim).filter(|t| !t.is_empty()).unwrap_or("Untitled episode");
|
||||
let notes = e.description.as_deref().map(ammonia::clean).filter(|d| !d.trim().is_empty());
|
||||
let summary = e.description.as_deref().map(|d| plain(d, 200)).filter(|d| !d.is_empty());
|
||||
let url = format!("{base}/e/{}", s.id);
|
||||
|
||||
let mut head = String::new();
|
||||
let _ = write!(
|
||||
head,
|
||||
"<meta property=\"og:type\" content=\"website\"><meta property=\"og:site_name\" content=\"iPX\">\
|
||||
<meta property=\"og:title\" content=\"{}\"><meta property=\"og:url\" content=\"{}\">\
|
||||
<meta property=\"og:audio\" content=\"{}\"><meta name=\"twitter:card\" content=\"summary\">",
|
||||
esc(title),
|
||||
esc(&url),
|
||||
esc(&e.audio_url)
|
||||
);
|
||||
let desc = match &summary {
|
||||
Some(sum) => format!("{show}: {sum}"),
|
||||
None => show.to_owned(),
|
||||
};
|
||||
let _ = write!(
|
||||
head,
|
||||
"<meta property=\"og:description\" content=\"{0}\"><meta name=description content=\"{0}\">",
|
||||
esc(&desc)
|
||||
);
|
||||
if let Some(img) = &e.feed_image {
|
||||
let _ = write!(head, "<meta property=\"og:image\" content=\"{}\">", esc(img));
|
||||
}
|
||||
if let Some(m) = &e.mime {
|
||||
let _ = write!(head, "<meta property=\"og:audio:type\" content=\"{}\">", esc(m));
|
||||
}
|
||||
|
||||
let mut b = String::new();
|
||||
b.push_str("<article class=ep><div class=head>");
|
||||
match &e.feed_image {
|
||||
Some(img) => {
|
||||
let _ = write!(b, "<img class=art src=\"{}\" alt=\"\" width=160 height=160>", esc(img));
|
||||
}
|
||||
None => {
|
||||
let initials: String = show.split_whitespace().filter_map(|w| w.chars().next()).take(2).collect();
|
||||
let _ = write!(b, "<div class=\"art tile\" aria-hidden=true>{}</div>", esc(&initials.to_uppercase()));
|
||||
}
|
||||
}
|
||||
let mut meta = Vec::new();
|
||||
if let Some(p) = e.published.and_then(|p| chrono::DateTime::from_timestamp(p, 0)) {
|
||||
meta.push(p.format("%B %-d, %Y").to_string());
|
||||
}
|
||||
if let Some(d) = e.duration.filter(|d| *d > 0) {
|
||||
meta.push(length(d));
|
||||
}
|
||||
let _ = write!(
|
||||
b,
|
||||
"<div><p class=show>{}</p><h1>{}</h1><p class=meta>{}</p></div></div>",
|
||||
esc(show),
|
||||
esc(title),
|
||||
esc(&meta.join(" · "))
|
||||
);
|
||||
// The file as the publisher serves it: a show that inserts ads by listener can move the time
|
||||
// by the length of an ad, as Overcast's links do.
|
||||
let src = match at {
|
||||
Some(t) => format!("{}#t={t}", e.audio_url),
|
||||
None => e.audio_url.clone(),
|
||||
};
|
||||
if let Some(t) = at {
|
||||
let _ = write!(b, "<p class=at>Starts at {}</p>", clock(t));
|
||||
}
|
||||
let _ = write!(b, "<audio controls preload=metadata src=\"{}\"></audio>", esc(&src));
|
||||
if let Some(link) = &e.link {
|
||||
let _ = write!(
|
||||
b,
|
||||
"<p class=more><a href=\"{}\" rel=\"noopener nofollow\">The episode's own page</a></p>",
|
||||
esc(link)
|
||||
);
|
||||
}
|
||||
if let Some(n) = notes {
|
||||
let _ = write!(b, "<section class=notes><h2>Notes</h2>{n}</section>");
|
||||
}
|
||||
let _ = write!(b, "<section class=sub><h2>Subscribe to {}</h2><p class=apps>", esc(show));
|
||||
for (name, href) in apps(&e.feed_url) {
|
||||
let _ = write!(b, "<a class=app href=\"{}\">{name}</a>", esc(&href));
|
||||
}
|
||||
let _ = write!(
|
||||
b,
|
||||
"</p><label for=feed>Or paste the feed's address into any podcast app</label>\
|
||||
<input id=feed readonly value=\"{}\" spellcheck=false></section></article>",
|
||||
esc(&e.feed_url)
|
||||
);
|
||||
html(StatusCode::OK, frame(&format!("{title} · {show}"), &head, &b))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn a_time_reads_as_seconds_or_a_clock() {
|
||||
assert_eq!(parse_time("754"), Some(754));
|
||||
assert_eq!(parse_time("12:34"), Some(754));
|
||||
assert_eq!(parse_time("1:02:03"), Some(3723));
|
||||
for bad in ["", "0", "-5", "1:2:3:4", "12:", "abc", "99999999999999999999999"] {
|
||||
assert_eq!(parse_time(bad), None, "{bad:?}");
|
||||
}
|
||||
assert_eq!((clock(754), clock(3723)), ("12:34".to_owned(), "1:02:03".to_owned()));
|
||||
assert_eq!((length(2700), length(3600), length(3720)), ("45 min".into(), "1 hr".into(), "1 hr 2 min".into()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_preview_has_the_notes_as_words() {
|
||||
assert_eq!(plain("<p>Tom & Jerry</p><p>talk</p>", 200), "Tom & Jerry talk");
|
||||
assert_eq!(plain("one two three four", 9), "one two…");
|
||||
}
|
||||
}
|
||||
70
crates/play/src/play.css
Normal file
70
crates/play/src/play.css
Normal file
@@ -0,0 +1,70 @@
|
||||
:root {
|
||||
--bg: #f6f7f9;
|
||||
--card: #ffffff;
|
||||
--ink: #111827;
|
||||
--muted: #5b6472;
|
||||
--line: #e2e6ec;
|
||||
--accent: #e8590c;
|
||||
--tile: #dbe3ee;
|
||||
color-scheme: light dark;
|
||||
}
|
||||
@media (prefers-color-scheme: dark) {
|
||||
:root {
|
||||
--bg: #0b1424;
|
||||
--card: #111d31;
|
||||
--ink: #e8edf4;
|
||||
--muted: #9aa7b8;
|
||||
--line: #22324a;
|
||||
--accent: #ff7a2f;
|
||||
--tile: #1d2c44;
|
||||
}
|
||||
}
|
||||
* { box-sizing: border-box; }
|
||||
body {
|
||||
margin: 0;
|
||||
background: var(--bg);
|
||||
color: var(--ink);
|
||||
font: 16px/1.55 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
|
||||
}
|
||||
main { max-width: 680px; margin: 0 auto; padding: 20px 16px 48px; }
|
||||
a { color: var(--accent); }
|
||||
.brand {
|
||||
display: inline-flex; align-items: center; gap: 8px;
|
||||
color: var(--ink); font-weight: 700; text-decoration: none; margin-bottom: 20px;
|
||||
}
|
||||
.ep { background: var(--card); border: 1px solid var(--line); border-radius: 16px; padding: 20px; }
|
||||
.head { display: flex; gap: 18px; align-items: center; }
|
||||
.art { width: 120px; height: 120px; border-radius: 12px; object-fit: cover; flex: none; }
|
||||
.tile {
|
||||
display: grid; place-items: center; background: var(--tile);
|
||||
font-size: 36px; font-weight: 700; color: var(--muted);
|
||||
}
|
||||
.show { margin: 0; color: var(--accent); font-weight: 600; font-size: 15px; }
|
||||
h1 { margin: 2px 0 4px; font-size: 22px; line-height: 1.25; overflow-wrap: anywhere; }
|
||||
h2 { font-size: 15px; margin: 0 0 8px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
|
||||
.meta { margin: 0; color: var(--muted); font-size: 14px; }
|
||||
.at {
|
||||
display: inline-block; margin: 18px 0 0; padding: 4px 10px; border-radius: 999px;
|
||||
background: var(--tile); font-size: 14px; font-weight: 600;
|
||||
}
|
||||
audio { display: block; width: 100%; margin-top: 14px; }
|
||||
.more { margin: 12px 0 0; font-size: 15px; }
|
||||
.notes, .sub { margin-top: 22px; padding-top: 18px; border-top: 1px solid var(--line); }
|
||||
.notes { overflow-wrap: anywhere; }
|
||||
.notes img { max-width: 100%; height: auto; }
|
||||
.apps { display: flex; flex-wrap: wrap; gap: 8px; margin: 0 0 14px; }
|
||||
.app {
|
||||
padding: 8px 14px; border-radius: 999px; border: 1px solid var(--line);
|
||||
color: var(--ink); text-decoration: none; font-size: 15px; font-weight: 600;
|
||||
}
|
||||
.app:hover { border-color: var(--accent); }
|
||||
label { display: block; color: var(--muted); font-size: 14px; margin-bottom: 6px; }
|
||||
input {
|
||||
width: 100%; padding: 9px 12px; border-radius: 10px; border: 1px solid var(--line);
|
||||
background: var(--bg); color: var(--ink); font: 14px ui-monospace, Menlo, monospace;
|
||||
}
|
||||
footer { margin-top: 24px; color: var(--muted); font-size: 14px; text-align: center; }
|
||||
@media (max-width: 480px) {
|
||||
.head { flex-direction: column; align-items: flex-start; }
|
||||
.art { width: 100%; height: auto; aspect-ratio: 1; max-width: 240px; }
|
||||
}
|
||||
200
crates/play/src/store.rs
Normal file
200
crates/play/src/store.rs
Normal file
@@ -0,0 +1,200 @@
|
||||
//! The episodes people have shared. SQL written by hand, as the directory's is, so it runs on
|
||||
//! SQLite (tests) and Postgres (production) both.
|
||||
|
||||
use crate::{Episode, Share};
|
||||
use anyhow::Result;
|
||||
use sea_orm::{ConnectOptions, ConnectionTrait, Database, DatabaseConnection, QueryResult, Statement, Value};
|
||||
use std::time::SystemTime;
|
||||
|
||||
const SCHEMA: &[&str] = &["CREATE TABLE IF NOT EXISTS shares (
|
||||
id TEXT PRIMARY KEY,
|
||||
feed_url TEXT NOT NULL,
|
||||
guid TEXT NOT NULL,
|
||||
feed_title TEXT,
|
||||
feed_image TEXT,
|
||||
title TEXT,
|
||||
description TEXT,
|
||||
link TEXT,
|
||||
published BIGINT,
|
||||
duration BIGINT,
|
||||
audio_url TEXT NOT NULL,
|
||||
mime TEXT,
|
||||
instance TEXT NOT NULL,
|
||||
created BIGINT NOT NULL,
|
||||
updated BIGINT NOT NULL
|
||||
)"];
|
||||
|
||||
pub struct Store {
|
||||
db: DatabaseConnection,
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn now() -> i64 {
|
||||
SystemTime::now().duration_since(std::time::UNIX_EPOCH).map_or(0, |d| d.as_secs() as i64)
|
||||
}
|
||||
|
||||
/// An episode's id: ten letters and digits from the SHA-256 of its feed's address (written as
|
||||
/// the directory writes it) and its guid, so sharing it again, from any server, gives the same
|
||||
/// link, and nothing needs looking up to make one. 50 bits: a collision is not expected before
|
||||
/// some billions of episodes.
|
||||
#[must_use]
|
||||
pub fn share_id(feed_url: &str, guid: &str) -> String {
|
||||
use sha2::Digest;
|
||||
const ALPHABET: &[u8] = b"abcdefghijklmnopqrstuvwxyz234567";
|
||||
let feed = ipx_directory::store::normalise(feed_url).unwrap_or_else(|| feed_url.trim().to_owned());
|
||||
let digest = sha2::Sha256::digest(format!("{feed}\n{guid}").as_bytes());
|
||||
let bits = digest[..8].iter().fold(0u64, |n, b| (n << 8) | u64::from(*b));
|
||||
(0..10).map(|i| ALPHABET[((bits >> (59 - 5 * i)) & 31) as usize] as char).collect()
|
||||
}
|
||||
|
||||
impl Store {
|
||||
pub async fn open(url: &str, keep_open: bool) -> Result<Self> {
|
||||
let mut url = url.to_owned();
|
||||
if url.starts_with("postgres") && !url.contains("options=") {
|
||||
// A notice for every CREATE ... IF NOT EXISTS on something there, otherwise.
|
||||
let sep = if url.contains('?') { '&' } else { '?' };
|
||||
url = format!("{url}{sep}options=-c%20client_min_messages%3Dwarning");
|
||||
}
|
||||
let mut opts = ConnectOptions::new(url.clone());
|
||||
opts.sqlx_logging(false);
|
||||
// An in-memory SQLite database is one per connection.
|
||||
if url.contains(":memory:") {
|
||||
opts.max_connections(1);
|
||||
}
|
||||
// Kept open: a Postgres connection takes 80-330 ms to open here (#241).
|
||||
if keep_open && url.starts_with("postgres") {
|
||||
opts.max_connections(4).min_connections(2);
|
||||
}
|
||||
let db = Database::connect(opts).await?;
|
||||
for sql in SCHEMA {
|
||||
db.execute_unprepared(sql).await?;
|
||||
}
|
||||
Ok(Self { db })
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub async fn scratch() -> Self {
|
||||
Self::open("sqlite::memory:", false).await.unwrap()
|
||||
}
|
||||
|
||||
fn stmt(&self, sql: &str, values: Vec<Value>) -> Statement {
|
||||
Statement::from_sql_and_values(self.db.get_database_backend(), sql, values)
|
||||
}
|
||||
|
||||
async fn rows(&self, sql: &str, values: Vec<Value>) -> Result<Vec<QueryResult>> {
|
||||
Ok(self.db.query_all_raw(self.stmt(sql, values)).await?)
|
||||
}
|
||||
|
||||
pub async fn ping(&self) -> Result<()> {
|
||||
self.rows("SELECT 1", vec![]).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Keeps an episode, or what it says now if it was shared before, and gives its id. The
|
||||
/// server that first shared it stays its sharer.
|
||||
pub async fn put(&self, instance: &str, e: &Episode) -> Result<String> {
|
||||
let id = share_id(&e.feed_url, &e.guid);
|
||||
let t = now();
|
||||
self.db
|
||||
.execute_raw(self.stmt(
|
||||
"INSERT INTO shares (id, feed_url, guid, feed_title, feed_image, title, description, link,
|
||||
published, duration, audio_url, mime, instance, created, updated)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $14)
|
||||
ON CONFLICT (id) DO UPDATE SET feed_title = excluded.feed_title, feed_image = excluded.feed_image,
|
||||
title = excluded.title, description = excluded.description, link = excluded.link,
|
||||
published = excluded.published, duration = excluded.duration, audio_url = excluded.audio_url,
|
||||
mime = excluded.mime, updated = excluded.updated",
|
||||
vec![
|
||||
id.clone().into(),
|
||||
e.feed_url.clone().into(),
|
||||
e.guid.clone().into(),
|
||||
e.feed_title.clone().into(),
|
||||
e.feed_image.clone().into(),
|
||||
e.title.clone().into(),
|
||||
e.description.clone().into(),
|
||||
e.link.clone().into(),
|
||||
e.published.into(),
|
||||
e.duration.into(),
|
||||
e.audio_url.clone().into(),
|
||||
e.mime.clone().into(),
|
||||
instance.into(),
|
||||
t.into(),
|
||||
],
|
||||
))
|
||||
.await?;
|
||||
Ok(id)
|
||||
}
|
||||
|
||||
pub async fn get(&self, id: &str) -> Result<Option<Share>> {
|
||||
let Some(r) = self
|
||||
.rows(
|
||||
"SELECT feed_url, guid, feed_title, feed_image, title, description, link, published, duration,
|
||||
audio_url, mime FROM shares WHERE id = $1",
|
||||
vec![id.into()],
|
||||
)
|
||||
.await?
|
||||
.into_iter()
|
||||
.next()
|
||||
else {
|
||||
return Ok(None);
|
||||
};
|
||||
Ok(Some(Share {
|
||||
id: id.to_owned(),
|
||||
episode: Episode {
|
||||
feed_url: r.try_get("", "feed_url")?,
|
||||
guid: r.try_get("", "guid")?,
|
||||
feed_title: r.try_get("", "feed_title")?,
|
||||
feed_image: r.try_get("", "feed_image")?,
|
||||
title: r.try_get("", "title")?,
|
||||
description: r.try_get("", "description")?,
|
||||
link: r.try_get("", "link")?,
|
||||
published: r.try_get("", "published")?,
|
||||
duration: r.try_get("", "duration")?,
|
||||
audio_url: r.try_get("", "audio_url")?,
|
||||
mime: r.try_get("", "mime")?,
|
||||
},
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
pub(crate) fn episode() -> Episode {
|
||||
Episode {
|
||||
feed_url: "https://example.com/feed.xml".into(),
|
||||
guid: "ep-1".into(),
|
||||
feed_title: Some("A Show".into()),
|
||||
feed_image: Some("https://example.com/art.jpg".into()),
|
||||
title: Some("Episode One".into()),
|
||||
description: Some("<p>Notes</p>".into()),
|
||||
link: None,
|
||||
published: Some(1_800_000_000),
|
||||
duration: Some(3600),
|
||||
audio_url: "https://example.com/ep1.mp3".into(),
|
||||
mime: Some("audio/mpeg".into()),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_episode_has_one_short_id_whoever_shares_it() {
|
||||
let a = share_id("https://example.com/feed.xml", "ep-1");
|
||||
assert_eq!(a.len(), 10);
|
||||
assert!(a.bytes().all(|b| b.is_ascii_lowercase() || (b'2'..=b'7').contains(&b)));
|
||||
assert_eq!(a, share_id("HTTPS://Example.com/feed.xml", "ep-1"), "the address as every server writes it");
|
||||
assert_ne!(a, share_id("https://example.com/feed.xml", "ep-2"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn sharing_again_keeps_the_link_and_takes_what_the_episode_says_now() {
|
||||
let s = Store::scratch().await;
|
||||
let mut e = episode();
|
||||
let id = s.put("tower", &e).await.unwrap();
|
||||
e.title = Some("Episode One, fixed".into());
|
||||
assert_eq!(s.put("elsewhere", &e).await.unwrap(), id);
|
||||
let got = s.get(&id).await.unwrap().unwrap();
|
||||
assert_eq!(got.episode, e);
|
||||
assert!(s.get("nothere").await.unwrap().is_none());
|
||||
}
|
||||
}
|
||||
17
crates/tui/CHANGELOG.md
Normal file
17
crates/tui/CHANGELOG.md
Normal file
@@ -0,0 +1,17 @@
|
||||
# Changelog: ipx-tui
|
||||
|
||||
The reader in a terminal (README.md). ipx's changelog is [crates/ipx/CHANGELOG.md](../ipx/CHANGELOG.md).
|
||||
|
||||
Its version is in `crates/tui/Cargo.toml`.
|
||||
|
||||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
||||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
### Added
|
||||
|
||||
- `ipx`, iPX's reader in a terminal: your feeds, a feed's items and an item's text, read and flagged as on the page, signed in with an API token (#437).
|
||||
- `p` plays the item's file in mpv (#439).
|
||||
- Moving onto an item with `j` or `k` marks it read, as on the page (#441).
|
||||
- Feeds are listed as on the page: a subscribed OPML is a folder `Space` opens, and pinned feeds are at the top; `P` pins and unpins (#440).
|
||||
21
crates/tui/Cargo.toml
Normal file
21
crates/tui/Cargo.toml
Normal file
@@ -0,0 +1,21 @@
|
||||
[package]
|
||||
name = "ipx-tui"
|
||||
version = "0.1.0-dev"
|
||||
edition.workspace = true
|
||||
publish = false
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
[[bin]]
|
||||
name = "ipx"
|
||||
path = "src/main.rs"
|
||||
|
||||
[dependencies]
|
||||
anyhow.workspace = true
|
||||
chrono.workspace = true
|
||||
clap = { workspace = true, features = ["env"] }
|
||||
ratatui = { version = "0.30.2", default-features = false, features = ["crossterm"] }
|
||||
reqwest = { workspace = true, features = ["blocking"] }
|
||||
serde.workspace = true
|
||||
serde_json.workspace = true
|
||||
32
crates/tui/README.md
Normal file
32
crates/tui/README.md
Normal file
@@ -0,0 +1,32 @@
|
||||
# ipx-tui
|
||||
|
||||
The `ipx` command. The server, which had that name, is `ipxd`.
|
||||
|
||||
iPX's reader in a terminal: your feeds on the left, a feed's items above, the item's text below.
|
||||
It talks to iPX's API as the page does, as you, with an API token made in the page's Settings.
|
||||
|
||||
```sh
|
||||
cargo build --release -p ipx-tui
|
||||
IPX_URL=http://192.168.1.130:8099 IPX_API_TOKEN=... target/release/ipx
|
||||
```
|
||||
|
||||
`--url` and `--token` do the same as the two variables.
|
||||
|
||||
| key | does |
|
||||
|---|---|
|
||||
| `j` `k`, arrows | move, or scroll the item's text |
|
||||
| `Enter`, `l`, `Tab` | open the feed or the item |
|
||||
| `h`, `Esc` | back a pane; `Esc` on the feeds quits |
|
||||
| `Space`, `PgUp` `PgDn` | page through the item's text |
|
||||
| `Space` | on the feeds, open or close a folder: a subscribed OPML, its feeds inside |
|
||||
| `P` | pin the feed or folder to the top of the list, or unpin it |
|
||||
| `p` | play the item's file in [mpv](https://mpv.io), in a window of its own |
|
||||
| `m` | mark the item read or unread |
|
||||
| `f` | flag or unflag it |
|
||||
| `u` | unread items only, or every item (unread only to start) |
|
||||
| `r` | reload |
|
||||
| `q` | quit |
|
||||
|
||||
An item is marked read when you move onto it or open it, as on the page. It shows a feed's newest
|
||||
200 items. Playing needs `mpv` on the path (`brew install mpv` on a Mac),
|
||||
and where you stop is not saved to iPX as it is on the page.
|
||||
641
crates/tui/src/main.rs
Normal file
641
crates/tui/src/main.rs
Normal file
@@ -0,0 +1,641 @@
|
||||
//! A terminal reader for ipx (#437): your feeds, a feed's items and an item's text, through the
|
||||
//! same API the page uses, as the person whose API token it is given.
|
||||
|
||||
use anyhow::{Context, Result, bail};
|
||||
use clap::Parser;
|
||||
use ratatui::crossterm::event::{self, Event, KeyCode, KeyEventKind};
|
||||
use ratatui::layout::{Constraint, Layout};
|
||||
use ratatui::style::{Modifier, Style, Stylize};
|
||||
use ratatui::text::Line;
|
||||
use ratatui::widgets::{Block, List, ListItem, ListState, Paragraph, Wrap};
|
||||
use ratatui::{DefaultTerminal, Frame};
|
||||
use reqwest::Url;
|
||||
use serde::Deserialize;
|
||||
use std::collections::HashSet;
|
||||
use std::time::Duration;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(name = "ipx", version, about = "Read iPX in a terminal")]
|
||||
struct Args {
|
||||
/// Where iPX is, as a browser reaches it.
|
||||
#[arg(long, env = "IPX_URL", default_value = "http://localhost:8099")]
|
||||
url: Url,
|
||||
/// An API token, made in the page's Settings.
|
||||
#[arg(long, env = "IPX_API_TOKEN", hide_env_values = true)]
|
||||
token: String,
|
||||
}
|
||||
|
||||
/// The part of web.rs's FeedRow this needs.
|
||||
#[derive(Deserialize)]
|
||||
struct Feed {
|
||||
id: String,
|
||||
url: String,
|
||||
title: Option<String>,
|
||||
unread: i64,
|
||||
/// The OPML it came from.
|
||||
group: Option<String>,
|
||||
hide_from_all: bool,
|
||||
pinned: bool,
|
||||
}
|
||||
|
||||
/// The feed list as the page draws it (feeds.ts renderFeeds), as indexes into `feeds` and
|
||||
/// whether each is inside a folder: pinned rows first, a subscribed OPML a folder with its feeds
|
||||
/// under it when open (those with unread first), and a pinned feed lifted out of its folder.
|
||||
fn order(feeds: &[Feed], open: &HashSet<String>) -> Vec<(usize, bool)> {
|
||||
let listed = |id: &str| feeds.iter().any(|f| f.id == id);
|
||||
let mut tops: Vec<usize> =
|
||||
(0..feeds.len()).filter(|&i| feeds[i].pinned || !feeds[i].group.as_deref().is_some_and(listed)).collect();
|
||||
tops.sort_by_key(|&i| !feeds[i].pinned);
|
||||
let mut rows = vec![];
|
||||
for t in tops {
|
||||
rows.push((t, false));
|
||||
if open.contains(&feeds[t].id) {
|
||||
let mut inside: Vec<usize> = (0..feeds.len())
|
||||
.filter(|&i| !feeds[i].pinned && feeds[i].group.as_deref() == Some(feeds[t].id.as_str()))
|
||||
.collect();
|
||||
inside.sort_by_key(|&i| feeds[i].unread == 0);
|
||||
rows.extend(inside.into_iter().map(|i| (i, true)));
|
||||
}
|
||||
}
|
||||
rows
|
||||
}
|
||||
|
||||
/// The part of db.rs's EntryRow this needs.
|
||||
#[derive(Deserialize)]
|
||||
struct Entry {
|
||||
guid: String,
|
||||
feed_id: String,
|
||||
title: Option<String>,
|
||||
link: Option<String>,
|
||||
published: Option<i64>,
|
||||
description: Option<String>,
|
||||
read: bool,
|
||||
flagged: bool,
|
||||
enclosures: Vec<Enclosure>,
|
||||
}
|
||||
|
||||
/// The part of db.rs's EncRow this needs: its id is its address, `/media/{id}`, which is there
|
||||
/// only once it is downloaded (`path`); one skipped or still to come answers 404.
|
||||
#[derive(Deserialize)]
|
||||
struct Enclosure {
|
||||
id: i64,
|
||||
path: Option<String>,
|
||||
}
|
||||
|
||||
/// What `p` plays: the item's first file iPX has.
|
||||
fn playable(e: &Entry) -> Option<&Enclosure> {
|
||||
e.enclosures.iter().find(|x| x.path.is_some())
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct EntryPage {
|
||||
total: i64,
|
||||
entries: Vec<Entry>,
|
||||
}
|
||||
|
||||
struct Api {
|
||||
http: reqwest::blocking::Client,
|
||||
base: Url,
|
||||
token: String,
|
||||
}
|
||||
|
||||
impl Api {
|
||||
/// The API's address for these path segments, each escaped: a guid is often a URL itself.
|
||||
fn url(&self, segments: &[&str]) -> Url {
|
||||
let mut url = self.base.clone();
|
||||
url.path_segments_mut().expect("an http URL has a path").pop_if_empty().extend(segments);
|
||||
url
|
||||
}
|
||||
|
||||
fn send(&self, req: reqwest::blocking::RequestBuilder) -> Result<reqwest::blocking::Response> {
|
||||
let res = req.bearer_auth(&self.token).send()?;
|
||||
if !res.status().is_success() {
|
||||
bail!("{} {}", res.status(), res.text().unwrap_or_default());
|
||||
}
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
/// Plays a file in mpv, in a window of its own so the terminal stays the reader's, with the
|
||||
/// token in a header as every other request has it.
|
||||
// shortcut: the token is on mpv's command line, which `ps` shows anyone on this machine;
|
||||
// pass it through a private mpv config file if the reader is used on a shared one.
|
||||
fn play(&self, enc: &Enclosure) -> Result<()> {
|
||||
let url = self.url(&["media", &enc.id.to_string()]);
|
||||
std::process::Command::new("mpv")
|
||||
.arg("--force-window=yes")
|
||||
.arg(format!("--http-header-fields=Authorization: Bearer {}", self.token))
|
||||
.arg(url.as_str())
|
||||
.stdin(std::process::Stdio::null())
|
||||
.stdout(std::process::Stdio::null())
|
||||
.stderr(std::process::Stdio::null())
|
||||
.spawn()
|
||||
.context("could not start mpv; is it installed?")?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn set_pinned(&self, feed: &str, on: bool) -> Result<()> {
|
||||
let req = self.http.patch(self.url(&["api", "feeds", feed])).json(&serde_json::json!({ "pinned": on }));
|
||||
self.send(req).map(drop)
|
||||
}
|
||||
|
||||
fn feeds(&self) -> Result<Vec<Feed>> {
|
||||
Ok(self.send(self.http.get(self.url(&["api", "feeds"])))?.json()?)
|
||||
}
|
||||
|
||||
/// The newest items of one feed, or of all of them when `feed` is None.
|
||||
// shortcut: the newest 200 only, no paging; page through `offset` if someone wants older.
|
||||
fn entries(&self, feed: Option<&str>, unread_only: bool) -> Result<EntryPage> {
|
||||
let mut url = match feed {
|
||||
Some(id) => self.url(&["api", "feeds", id, "entries"]),
|
||||
None => self.url(&["api", "entries"]),
|
||||
};
|
||||
let filter = if unread_only { "unread" } else { "all" };
|
||||
url.query_pairs_mut().append_pair("limit", "200").append_pair("filter", filter);
|
||||
Ok(self.send(self.http.get(url))?.json()?)
|
||||
}
|
||||
|
||||
/// Sets an item's read or flagged, and answers with its feed's row, for the new unread count.
|
||||
fn set_flag(&self, e: &Entry, flag: &str, on: bool) -> Result<Option<Feed>> {
|
||||
let url = self.url(&["api", "entries", &e.feed_id, &e.guid, "flags"]);
|
||||
Ok(self.send(self.http.post(url).json(&serde_json::json!({ flag: on })))?.json()?)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Clone, Copy)]
|
||||
enum Pane {
|
||||
Feeds,
|
||||
Items,
|
||||
Text,
|
||||
}
|
||||
|
||||
struct App {
|
||||
api: Api,
|
||||
feeds: Vec<Feed>,
|
||||
/// What the feed list shows, after All Subscriptions, which is index 0 of `feed_list`.
|
||||
rows: Vec<(usize, bool)>,
|
||||
/// The folders open; they start closed, as on the page, since one can hold dozens of feeds.
|
||||
open: HashSet<String>,
|
||||
feed_list: ListState,
|
||||
entries: Vec<Entry>,
|
||||
total: i64,
|
||||
entry_list: ListState,
|
||||
pane: Pane,
|
||||
scroll: u16,
|
||||
unread_only: bool,
|
||||
status: String,
|
||||
}
|
||||
|
||||
impl App {
|
||||
fn feed(&self) -> Option<&Feed> {
|
||||
let i = self.feed_list.selected()?;
|
||||
self.rows.get(i.checked_sub(1)?).map(|&(f, _)| &self.feeds[f])
|
||||
}
|
||||
|
||||
fn feed_id(&self) -> Option<&str> {
|
||||
self.feed().map(|f| f.id.as_str())
|
||||
}
|
||||
|
||||
/// The feed list again after the feeds or the open folders change, on the same feed, or on
|
||||
/// its folder when that has just closed over it.
|
||||
fn arrange(&mut self) {
|
||||
let was = self.feed().map(|f| (f.id.clone(), f.group.clone()));
|
||||
self.rows = order(&self.feeds, &self.open);
|
||||
let at = |id: &str| self.rows.iter().position(|&(f, _)| self.feeds[f].id == id).map(|i| i + 1);
|
||||
let row = was.and_then(|(id, group)| at(&id).or_else(|| group.and_then(|g| at(&g))));
|
||||
self.feed_list.select(Some(row.unwrap_or(0)));
|
||||
}
|
||||
|
||||
/// The unread count a row shows: a folder holds no items itself, so it is what is inside.
|
||||
fn unread(&self, f: &Feed) -> i64 {
|
||||
let inside: Vec<_> = self.feeds.iter().filter(|c| c.group.as_deref() == Some(f.id.as_str())).collect();
|
||||
if inside.is_empty() { f.unread } else { inside.iter().map(|c| c.unread).sum() }
|
||||
}
|
||||
|
||||
fn entry(&self) -> Option<&Entry> {
|
||||
self.entries.get(self.entry_list.selected()?)
|
||||
}
|
||||
|
||||
/// Runs an API call, putting what went wrong in the status line instead of ending the program.
|
||||
fn attempt<T>(&mut self, what: Result<T>) -> Option<T> {
|
||||
what.map_err(|e| self.status = format!("{e:#}")).ok()
|
||||
}
|
||||
|
||||
fn load_feeds(&mut self) {
|
||||
if let Some(feeds) = self.attempt(self.api.feeds()) {
|
||||
self.feeds = feeds;
|
||||
self.arrange();
|
||||
self.status.clear();
|
||||
}
|
||||
}
|
||||
|
||||
fn load_entries(&mut self) {
|
||||
let page = self.api.entries(self.feed_id(), self.unread_only);
|
||||
if let Some(page) = self.attempt(page) {
|
||||
self.entries = page.entries;
|
||||
self.total = page.total;
|
||||
self.entry_list.select(if self.entries.is_empty() { None } else { Some(0) });
|
||||
self.scroll = 0;
|
||||
self.status.clear();
|
||||
}
|
||||
}
|
||||
|
||||
/// Sets the selected item's read or flagged, as the page does.
|
||||
fn flag(&mut self, flag: &str, on: impl Fn(&Entry) -> bool) {
|
||||
let Some(e) = self.entry() else { return };
|
||||
let on = on(e);
|
||||
let result = self.api.set_flag(e, flag, on);
|
||||
let Some(row) = self.attempt(result) else { return };
|
||||
if let Some(i) = self.entry_list.selected() {
|
||||
let e = &mut self.entries[i];
|
||||
if flag == "read" { e.read = on } else { e.flagged = on }
|
||||
}
|
||||
if let Some(row) = row
|
||||
&& let Some(f) = self.feeds.iter_mut().find(|f| f.id == row.id)
|
||||
{
|
||||
f.unread = row.unread;
|
||||
}
|
||||
}
|
||||
|
||||
/// Selecting an item is reading it, as on the page (items.ts markRead), j and k included.
|
||||
fn mark_read(&mut self) {
|
||||
if self.entry().is_some_and(|e| !e.read) {
|
||||
self.flag("read", |_| true);
|
||||
}
|
||||
}
|
||||
|
||||
/// Opens the selected item, which marks it read, as opening one on the page does.
|
||||
fn open(&mut self) {
|
||||
self.scroll = 0;
|
||||
self.pane = Pane::Text;
|
||||
self.mark_read();
|
||||
}
|
||||
|
||||
fn step(&mut self, down: bool) {
|
||||
let list = match self.pane {
|
||||
Pane::Feeds => &mut self.feed_list,
|
||||
Pane::Items => &mut self.entry_list,
|
||||
Pane::Text => {
|
||||
self.scroll = if down { self.scroll.saturating_add(1) } else { self.scroll.saturating_sub(1) };
|
||||
return;
|
||||
}
|
||||
};
|
||||
if down {
|
||||
list.select_next()
|
||||
} else {
|
||||
list.select_previous()
|
||||
}
|
||||
if self.pane == Pane::Items {
|
||||
self.scroll = 0;
|
||||
self.mark_read();
|
||||
}
|
||||
}
|
||||
|
||||
/// Handles a key, answering false when it is time to quit.
|
||||
fn key(&mut self, code: KeyCode) -> bool {
|
||||
match code {
|
||||
KeyCode::Char('q') => return false,
|
||||
KeyCode::Esc if self.pane == Pane::Feeds => return false,
|
||||
KeyCode::Char('j') | KeyCode::Down => self.step(true),
|
||||
KeyCode::Char('k') | KeyCode::Up => self.step(false),
|
||||
KeyCode::PageDown | KeyCode::Char(' ') if self.pane == Pane::Text => {
|
||||
self.scroll = self.scroll.saturating_add(10)
|
||||
}
|
||||
KeyCode::Char(' ') if self.pane == Pane::Feeds => {
|
||||
if let Some(id) = self.feed_id().map(str::to_owned) {
|
||||
if !self.open.remove(&id) {
|
||||
self.open.insert(id);
|
||||
}
|
||||
self.arrange();
|
||||
}
|
||||
}
|
||||
KeyCode::Char('P') => {
|
||||
let Some(f) = self.feed() else { return true };
|
||||
let (id, on) = (f.id.clone(), !f.pinned);
|
||||
if self.attempt(self.api.set_pinned(&id, on)).is_some() {
|
||||
self.load_feeds();
|
||||
self.status = if on { "pinned to the top" } else { "unpinned" }.into();
|
||||
}
|
||||
}
|
||||
KeyCode::PageUp if self.pane == Pane::Text => self.scroll = self.scroll.saturating_sub(10),
|
||||
KeyCode::Enter | KeyCode::Char('l') | KeyCode::Right | KeyCode::Tab => match self.pane {
|
||||
Pane::Feeds => {
|
||||
self.load_entries();
|
||||
self.pane = Pane::Items;
|
||||
}
|
||||
Pane::Items => self.open(),
|
||||
Pane::Text => {}
|
||||
},
|
||||
KeyCode::Esc | KeyCode::Char('h') | KeyCode::Left | KeyCode::BackTab => {
|
||||
self.pane = match self.pane {
|
||||
Pane::Text => Pane::Items,
|
||||
_ => Pane::Feeds,
|
||||
}
|
||||
}
|
||||
KeyCode::Char('p') => {
|
||||
let Some(e) = self.entry() else { return true };
|
||||
match playable(e).map(|enc| self.api.play(enc)) {
|
||||
Some(r) => {
|
||||
if self.attempt(r).is_some() {
|
||||
self.status = "playing in mpv".into();
|
||||
}
|
||||
}
|
||||
None if e.enclosures.is_empty() => self.status = "this item has no file to play".into(),
|
||||
None => self.status = "iPX has not downloaded this item's file".into(),
|
||||
}
|
||||
}
|
||||
KeyCode::Char('m') => self.flag("read", |e| !e.read),
|
||||
KeyCode::Char('f') => self.flag("flagged", |e| !e.flagged),
|
||||
KeyCode::Char('u') => {
|
||||
self.unread_only = !self.unread_only;
|
||||
self.load_entries();
|
||||
}
|
||||
KeyCode::Char('r') => {
|
||||
self.load_feeds();
|
||||
self.load_entries();
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
fn draw(&mut self, frame: &mut Frame) {
|
||||
let [main, foot] = Layout::vertical([Constraint::Fill(1), Constraint::Length(1)]).areas(frame.area());
|
||||
let [left, right] = Layout::horizontal([Constraint::Percentage(28), Constraint::Fill(1)]).areas(main);
|
||||
let [top, bottom] = Layout::vertical([Constraint::Percentage(40), Constraint::Fill(1)]).areas(right);
|
||||
let block = |title: String, pane: Pane| {
|
||||
let b = Block::bordered().title(title);
|
||||
if self.pane == pane { b.border_style(Style::new().bold()) } else { b.dim() }
|
||||
};
|
||||
let picked = Style::new().add_modifier(Modifier::REVERSED);
|
||||
|
||||
// What the page counts (feeds.ts inAll): not a feed hidden from All Subscriptions, or
|
||||
// one from an OPML that is (#288).
|
||||
let hidden = |id: &str| self.feeds.iter().any(|g| g.id == id && g.hide_from_all);
|
||||
let all: i64 = self
|
||||
.feeds
|
||||
.iter()
|
||||
.filter(|f| !f.hide_from_all && !f.group.as_deref().is_some_and(hidden))
|
||||
.map(|f| f.unread)
|
||||
.sum();
|
||||
let feeds =
|
||||
std::iter::once(format!("All Subscriptions ({all})")).chain(self.rows.iter().map(|&(i, inside)| {
|
||||
let f = &self.feeds[i];
|
||||
let folder = self.feeds.iter().any(|c| c.group.as_deref() == Some(f.id.as_str()));
|
||||
let lead = match (inside, folder, self.open.contains(&f.id)) {
|
||||
(true, ..) => " ",
|
||||
(_, true, true) => "▾ ",
|
||||
(_, true, false) => "▸ ",
|
||||
_ => " ",
|
||||
};
|
||||
let pin = if f.pinned && !inside { "↑ " } else { "" };
|
||||
let name = f.title.as_deref().unwrap_or(&f.url);
|
||||
match self.unread(f) {
|
||||
0 => format!("{lead}{pin}{name}"),
|
||||
n => format!("{lead}{pin}{name} ({n})"),
|
||||
}
|
||||
}));
|
||||
let feeds = List::new(feeds).block(block("Feeds".into(), Pane::Feeds)).highlight_style(picked);
|
||||
frame.render_stateful_widget(feeds, left, &mut self.feed_list);
|
||||
|
||||
let items = self.entries.iter().map(|e| {
|
||||
let mark = format!("{}{}", if e.read { ' ' } else { '•' }, if e.flagged { '★' } else { ' ' });
|
||||
let line = format!("{mark} {:<10} {}", date(e.published), e.title.as_deref().unwrap_or("(untitled)"));
|
||||
if e.read { ListItem::new(line) } else { ListItem::new(line).bold() }
|
||||
});
|
||||
let shown = if self.unread_only { "unread" } else { "items" };
|
||||
let title = format!("{} of {} {shown}", self.entries.len(), self.total);
|
||||
let items = List::new(items).block(block(title, Pane::Items)).highlight_style(picked);
|
||||
frame.render_stateful_widget(items, top, &mut self.entry_list);
|
||||
|
||||
let text = match self.entry() {
|
||||
Some(e) => {
|
||||
let mut lines = vec![Line::from(e.title.clone().unwrap_or_default()).bold()];
|
||||
if let Some(link) = &e.link {
|
||||
lines.push(Line::from(link.clone()).dim());
|
||||
}
|
||||
if playable(e).is_some() {
|
||||
lines.push(Line::from("▶ p to play").dim());
|
||||
}
|
||||
lines.push(Line::default());
|
||||
lines
|
||||
.extend(text_of(e.description.as_deref().unwrap_or("")).lines().map(|l| Line::from(l.to_string())));
|
||||
lines
|
||||
}
|
||||
None => vec![],
|
||||
};
|
||||
let text = Paragraph::new(text).wrap(Wrap { trim: false }).scroll((self.scroll, 0));
|
||||
frame.render_widget(text.block(block("Item".into(), Pane::Text)), bottom);
|
||||
|
||||
let help = "j/k move enter open h back space folder P pin p play m read f flag u unread only r reload q quit";
|
||||
let foot_text = if self.status.is_empty() { help } else { &self.status };
|
||||
frame.render_widget(Paragraph::new(foot_text).dim(), foot);
|
||||
}
|
||||
}
|
||||
|
||||
fn date(t: Option<i64>) -> String {
|
||||
t.and_then(|t| chrono::DateTime::from_timestamp(t, 0)).map(|d| d.format("%Y-%m-%d").to_string()).unwrap_or_default()
|
||||
}
|
||||
|
||||
/// An item's HTML as plain text for a terminal: tags dropped, blocks on lines of their own,
|
||||
/// entities decoded. ipx has already cleaned it, so there is no script or style to skip.
|
||||
fn text_of(html: &str) -> String {
|
||||
let mut out = String::new();
|
||||
let mut rest = html;
|
||||
loop {
|
||||
let (text, tag) = match rest.find('<') {
|
||||
Some(i) => (&rest[..i], Some(&rest[i + 1..])),
|
||||
None => (rest, None),
|
||||
};
|
||||
// HTML's own whitespace is not layout; the tags say where lines break.
|
||||
if text.starts_with(char::is_whitespace) && !out.is_empty() && !out.ends_with(['\n', ' ']) {
|
||||
out.push(' ');
|
||||
}
|
||||
out.push_str(&decode(&text.split_whitespace().collect::<Vec<_>>().join(" ")));
|
||||
if text.ends_with(char::is_whitespace) && !text.trim().is_empty() {
|
||||
out.push(' ');
|
||||
}
|
||||
let Some(tag) = tag else { break };
|
||||
let Some(end) = tag.find('>') else { break };
|
||||
let (inner, closing) = match tag[..end].strip_prefix('/') {
|
||||
Some(t) => (t, true),
|
||||
None => (&tag[..end], false),
|
||||
};
|
||||
let name = inner.split(|c: char| c.is_whitespace() || c == '/').next().unwrap_or("").to_ascii_lowercase();
|
||||
match name.as_str() {
|
||||
"br" => out.push('\n'),
|
||||
"li" => {
|
||||
if !out.is_empty() && !out.ends_with('\n') {
|
||||
out.push('\n');
|
||||
}
|
||||
if !closing {
|
||||
out.push_str("• ");
|
||||
}
|
||||
}
|
||||
"p" | "div" | "ul" | "ol" | "blockquote" | "pre" | "tr" | "h1" | "h2" | "h3" | "h4" | "h5" | "h6"
|
||||
if !out.is_empty() && !out.ends_with("\n\n") =>
|
||||
{
|
||||
out.push_str(if out.ends_with('\n') { "\n" } else { "\n\n" });
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
rest = &tag[end + 1..];
|
||||
}
|
||||
out.lines().map(str::trim_end).collect::<Vec<_>>().join("\n").trim().to_string()
|
||||
}
|
||||
|
||||
/// HTML's entities, the named ones feeds use and every numbered one.
|
||||
fn decode(s: &str) -> String {
|
||||
let mut out = String::with_capacity(s.len());
|
||||
let mut rest = s;
|
||||
while let Some(i) = rest.find('&') {
|
||||
out.push_str(&rest[..i]);
|
||||
rest = &rest[i..];
|
||||
let end = rest[..rest.len().min(12)].find(';');
|
||||
let ch = end.and_then(|end| match &rest[1..end] {
|
||||
"amp" => Some('&'),
|
||||
"lt" => Some('<'),
|
||||
"gt" => Some('>'),
|
||||
"quot" => Some('"'),
|
||||
"apos" => Some('\''),
|
||||
"nbsp" => Some(' '),
|
||||
"hellip" => Some('…'),
|
||||
"mdash" => Some('—'),
|
||||
"ndash" => Some('–'),
|
||||
"rsquo" => Some('’'),
|
||||
"lsquo" => Some('‘'),
|
||||
"rdquo" => Some('”'),
|
||||
"ldquo" => Some('“'),
|
||||
n => {
|
||||
let n = n.strip_prefix('#')?;
|
||||
let code = match n.strip_prefix(['x', 'X']) {
|
||||
Some(hex) => u32::from_str_radix(hex, 16).ok()?,
|
||||
None => n.parse().ok()?,
|
||||
};
|
||||
char::from_u32(code)
|
||||
}
|
||||
});
|
||||
match (ch, end) {
|
||||
(Some(c), Some(end)) => {
|
||||
out.push(c);
|
||||
rest = &rest[end + 1..];
|
||||
}
|
||||
_ => {
|
||||
out.push('&');
|
||||
rest = &rest[1..];
|
||||
}
|
||||
}
|
||||
}
|
||||
out.push_str(rest);
|
||||
out
|
||||
}
|
||||
|
||||
fn run(terminal: &mut DefaultTerminal, app: &mut App) -> Result<()> {
|
||||
loop {
|
||||
terminal.draw(|f| app.draw(f))?;
|
||||
// A poll, not a blocking read, so a resize redraws without waiting for a key.
|
||||
if !event::poll(Duration::from_millis(500))? {
|
||||
continue;
|
||||
}
|
||||
if let Event::Key(k) = event::read()?
|
||||
&& k.kind == KeyEventKind::Press
|
||||
&& !app.key(k.code)
|
||||
{
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let args = Args::parse();
|
||||
let http = reqwest::blocking::Client::builder().timeout(Duration::from_secs(30)).build()?;
|
||||
let api = Api { http, base: args.url, token: args.token };
|
||||
// Before the terminal is taken over, so a wrong address or token is said plainly.
|
||||
let feeds = api.feeds().context("could not read your feeds")?;
|
||||
let mut app = App {
|
||||
api,
|
||||
feeds,
|
||||
rows: vec![],
|
||||
open: HashSet::new(),
|
||||
feed_list: ListState::default().with_selected(Some(0)),
|
||||
entries: vec![],
|
||||
total: 0,
|
||||
entry_list: ListState::default(),
|
||||
pane: Pane::Feeds,
|
||||
scroll: 0,
|
||||
unread_only: true,
|
||||
status: String::new(),
|
||||
};
|
||||
app.arrange();
|
||||
app.load_entries();
|
||||
let mut terminal = ratatui::init();
|
||||
let result = run(&mut terminal, &mut app);
|
||||
ratatui::restore();
|
||||
result
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn text_of_html() {
|
||||
let html = "<p>One & two,\n <b>bold</b>.</p><p>Next line<br>broken — ’s &bogus; &</p>\
|
||||
<ul><li>a</li><li>b</li></ul>";
|
||||
assert_eq!(text_of(html), "One & two, bold.\n\nNext line\nbroken — ’s &bogus; &\n\n• a\n• b");
|
||||
assert_eq!(text_of("plain"), "plain");
|
||||
assert_eq!(text_of("a <i>b</i> c"), "a b c");
|
||||
assert_eq!(text_of("<b>x</b> <i>y</i>"), "x y");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn feeds_in_the_pages_order() {
|
||||
let feed = |id: &str, group: Option<&str>, pinned: bool, unread: i64| Feed {
|
||||
id: id.into(),
|
||||
url: id.into(),
|
||||
title: None,
|
||||
unread,
|
||||
group: group.map(Into::into),
|
||||
hide_from_all: false,
|
||||
pinned,
|
||||
};
|
||||
let feeds = [
|
||||
feed("a", None, false, 0),
|
||||
feed("opml", None, false, 0),
|
||||
feed("in-read", Some("opml"), false, 0),
|
||||
feed("in-unread", Some("opml"), false, 3),
|
||||
feed("in-pinned", Some("opml"), true, 0),
|
||||
feed("b", None, true, 0),
|
||||
feed("lost", Some("gone"), false, 0),
|
||||
];
|
||||
let ids = |open: &[&str]| -> Vec<(&str, bool)> {
|
||||
let open = open.iter().map(|s| s.to_string()).collect();
|
||||
order(&feeds, &open).into_iter().map(|(i, inside)| (feeds[i].id.as_str(), inside)).collect()
|
||||
};
|
||||
// Closed, a folder hides its feeds; a pinned one is out of it, at the top; a feed whose
|
||||
// folder is not listed stands on its own.
|
||||
assert_eq!(ids(&[]), [("in-pinned", false), ("b", false), ("a", false), ("opml", false), ("lost", false)]);
|
||||
assert_eq!(
|
||||
ids(&["opml"]),
|
||||
[
|
||||
("in-pinned", false),
|
||||
("b", false),
|
||||
("a", false),
|
||||
("opml", false),
|
||||
("in-unread", true),
|
||||
("in-read", true),
|
||||
("lost", false),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn plays_the_first_downloaded_file() {
|
||||
let e: Entry = serde_json::from_value(serde_json::json!({
|
||||
"guid": "g", "feed_id": "f", "title": null, "link": null, "published": null,
|
||||
"description": null, "read": false, "flagged": false,
|
||||
"enclosures": [{"id": 1, "path": null}, {"id": 2, "path": "a.mp3"}, {"id": 3, "path": "b.mp3"}],
|
||||
}))
|
||||
.unwrap();
|
||||
assert_eq!(playable(&e).map(|x| x.id), Some(2));
|
||||
}
|
||||
}
|
||||
27
crates/wire/Cargo.toml
Normal file
27
crates/wire/Cargo.toml
Normal file
@@ -0,0 +1,27 @@
|
||||
[package]
|
||||
name = "ipx-wire"
|
||||
version = "0.0.0"
|
||||
publish = false
|
||||
edition.workspace = true
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
[dependencies]
|
||||
axum.workspace = true
|
||||
prost.workspace = true
|
||||
prost-reflect.workspace = true
|
||||
prost-types.workspace = true
|
||||
serde.workspace = true
|
||||
serde_json.workspace = true
|
||||
tracing.workspace = true
|
||||
|
||||
[build-dependencies]
|
||||
prost.workspace = true
|
||||
prost-build.workspace = true
|
||||
prost-types.workspace = true
|
||||
protox.workspace = true
|
||||
|
||||
[dev-dependencies]
|
||||
tokio.workspace = true
|
||||
tower.workspace = true
|
||||
47
crates/wire/build.rs
Normal file
47
crates/wire/build.rs
Normal file
@@ -0,0 +1,47 @@
|
||||
//! Compiles the `.proto` files into one descriptor set the crate reads at run time, and into Rust
|
||||
//! structs (prost) that read the handlers' JSON straight into protobuf (#470). protox is a
|
||||
//! compiler in Rust, so building needs no `protoc` on the host or in the image.
|
||||
|
||||
use prost::Message as _;
|
||||
use std::fmt::Write as _;
|
||||
|
||||
fn main() {
|
||||
let files = ["proto/ipx.proto", "proto/directory.proto", "proto/play.proto"];
|
||||
for f in files {
|
||||
println!("cargo:rerun-if-changed={f}");
|
||||
}
|
||||
let set = protox::compile(files, ["proto"]).unwrap_or_else(|e| panic!("{e:?}"));
|
||||
let out = std::path::PathBuf::from(std::env::var("OUT_DIR").expect("cargo sets OUT_DIR"));
|
||||
std::fs::write(out.join("wire.bin"), set.encode_to_vec()).expect("writing the descriptor set");
|
||||
|
||||
// Every message that reads from JSON with serde: all but the bodies holding Google's Value or
|
||||
// ListValue, whose JSON serde cannot read; those go the slow way, through prost-reflect.
|
||||
let mut config = prost_build::Config::new();
|
||||
let mut fast = String::from(
|
||||
"/// An answer's JSON as message `name` in protobuf, when `name` has a struct here.\n\
|
||||
pub fn json_to_proto(name: &str, json: &[u8]) -> Option<Result<Vec<u8>, String>> {\n match name {\n",
|
||||
);
|
||||
for file in set.file.iter().filter(|f| f.package() != "google.protobuf") {
|
||||
for m in &file.message_type {
|
||||
if m.field.iter().any(|f| f.type_name().starts_with(".google.")) {
|
||||
continue;
|
||||
}
|
||||
let path = format!(".{}.{}", file.package(), m.name());
|
||||
// A list message is the JSON array itself.
|
||||
let list = matches!(&m.field[..], [only] if only.name() == "items"
|
||||
&& only.label() == prost_types::field_descriptor_proto::Label::Repeated);
|
||||
let attrs = if list {
|
||||
"#[derive(serde::Deserialize)] #[serde(transparent)]"
|
||||
} else {
|
||||
// Debug builds refuse a field the message lacks, as the slow way does.
|
||||
"#[derive(serde::Deserialize)] #[serde(default)] #[cfg_attr(debug_assertions, serde(deny_unknown_fields))]"
|
||||
};
|
||||
config.type_attribute(&path, attrs);
|
||||
writeln!(fast, " {:?} => Some(encode::<pb::{}::{}>(json)),", &path[1..], file.package(), m.name())
|
||||
.expect("writing to a String");
|
||||
}
|
||||
}
|
||||
fast.push_str(" _ => None,\n }\n}\n");
|
||||
std::fs::write(out.join("fast.rs"), fast).expect("writing fast.rs");
|
||||
config.out_dir(&out).compile_fds(set).unwrap_or_else(|e| panic!("{e:?}"));
|
||||
}
|
||||
94
crates/wire/proto/directory.proto
Normal file
94
crates/wire/proto/directory.proto
Normal file
@@ -0,0 +1,94 @@
|
||||
// ipx-directory's API under /v1 (crates/directory/src/wire.rs), each message named after the Rust
|
||||
// struct it mirrors. A field added there goes here too; debug builds refuse an answer with a field
|
||||
// missing here. A message whose only field is `repeated items` is a JSON array.
|
||||
syntax = "proto3";
|
||||
package directory;
|
||||
|
||||
// GET /v1/me.
|
||||
message Me {
|
||||
string name = 1;
|
||||
// "ro", "rw" or "rwa".
|
||||
string access = 2;
|
||||
}
|
||||
|
||||
message Listing {
|
||||
string id = 1;
|
||||
string url = 2;
|
||||
optional string title = 3;
|
||||
optional string image = 4;
|
||||
optional string category = 5;
|
||||
optional string subcategory = 6;
|
||||
bool podcast = 7;
|
||||
int64 subscribers = 8;
|
||||
}
|
||||
|
||||
// GET /v1/feeds, GET /v1/popular.
|
||||
message Listings {
|
||||
repeated Listing items = 1;
|
||||
}
|
||||
|
||||
message Item {
|
||||
string guid = 1;
|
||||
optional string title = 2;
|
||||
optional string link = 3;
|
||||
optional int64 published = 4;
|
||||
optional int64 duration = 5;
|
||||
optional string description = 6;
|
||||
}
|
||||
|
||||
// GET /v1/feeds/{id}, POST /v1/feeds/{id}/refresh: a Listing's fields and these.
|
||||
message FeedPage {
|
||||
string id = 1;
|
||||
string url = 2;
|
||||
optional string title = 3;
|
||||
optional string image = 4;
|
||||
optional string category = 5;
|
||||
optional string subcategory = 6;
|
||||
bool podcast = 7;
|
||||
int64 subscribers = 8;
|
||||
optional string description = 9;
|
||||
optional string failing = 10;
|
||||
repeated Item items = 11;
|
||||
}
|
||||
|
||||
message SnapshotEntry {
|
||||
string url = 1;
|
||||
int64 subscribers = 2;
|
||||
optional string category = 3;
|
||||
}
|
||||
|
||||
// POST /v1/snapshot, the body.
|
||||
message Snapshot {
|
||||
repeated SnapshotEntry items = 1;
|
||||
}
|
||||
|
||||
// POST /v1/snapshot, the answer.
|
||||
message SnapshotResult {
|
||||
uint64 accepted = 1;
|
||||
uint64 refused_private = 2;
|
||||
uint64 refused_blocked = 3;
|
||||
uint64 refused_invalid = 4;
|
||||
}
|
||||
|
||||
message Instance {
|
||||
string name = 1;
|
||||
int64 feeds = 2;
|
||||
int64 subscribers = 3;
|
||||
}
|
||||
|
||||
// GET /v1/instances.
|
||||
message Instances {
|
||||
repeated Instance items = 1;
|
||||
}
|
||||
|
||||
message Blocked {
|
||||
string id = 1;
|
||||
optional string url = 2;
|
||||
optional string title = 3;
|
||||
int64 at = 4;
|
||||
}
|
||||
|
||||
// GET /v1/blocked.
|
||||
message BlockedList {
|
||||
repeated Blocked items = 1;
|
||||
}
|
||||
550
crates/wire/proto/ipx.proto
Normal file
550
crates/wire/proto/ipx.proto
Normal file
@@ -0,0 +1,550 @@
|
||||
// ipx's API under /api (crates/ipx/src/web.rs), the same answers its JSON gives, each message named
|
||||
// after the Rust struct it mirrors where there is one (web/src/types.ts mirrors the same). A field
|
||||
// added in Rust goes here too: debug builds refuse an answer with a field missing here.
|
||||
//
|
||||
// How the JSON maps (crates/wire/src/lib.rs):
|
||||
// * A field name is the JSON key.
|
||||
// * A Rust Option is `optional`, or a message; so is a field with a serde default, which an absent
|
||||
// field must leave to Rust.
|
||||
// * A message whose only field is `repeated ... items` is a JSON array, top-level or as a field.
|
||||
// An Option<Vec> in a body is one (Words), so leaving a list alone and emptying it differ.
|
||||
// * A body field that takes null to clear it is a google.protobuf.Value: absent leaves it, null
|
||||
// clears it, a value sets it.
|
||||
// * An answer of JSON null stays JSON null: the empty message would be a row of defaults.
|
||||
syntax = "proto3";
|
||||
package ipx;
|
||||
|
||||
import "google/protobuf/struct.proto";
|
||||
import "directory.proto";
|
||||
|
||||
// A list of words, as an Option<Vec<String>> in a body: absent, or this.
|
||||
message Words {
|
||||
repeated string items = 1;
|
||||
}
|
||||
|
||||
// ---- signing in, and you ----
|
||||
|
||||
// POST /api/login, the body.
|
||||
message Credentials {
|
||||
string name = 1;
|
||||
string password = 2;
|
||||
}
|
||||
|
||||
// POST /api/login, the answer.
|
||||
message SignedIn {
|
||||
string name = 1;
|
||||
bool admin = 2;
|
||||
}
|
||||
|
||||
message Theme {
|
||||
string name = 1;
|
||||
string mode = 2;
|
||||
}
|
||||
|
||||
// GET /api/me.
|
||||
message Me {
|
||||
string name = 1;
|
||||
bool admin = 2;
|
||||
optional string sign_out = 3;
|
||||
repeated string blocked = 4;
|
||||
int64 deadair_saved = 5;
|
||||
optional Theme theme = 6;
|
||||
string version = 7;
|
||||
bool typesafe_key_set = 8;
|
||||
// "server" or "own".
|
||||
string signals = 9;
|
||||
uint64 signal_days = 10;
|
||||
}
|
||||
|
||||
// PATCH /api/me, the body.
|
||||
message MePatch {
|
||||
Words blocked = 1;
|
||||
google.protobuf.Value typesafe_key = 2;
|
||||
}
|
||||
|
||||
message ApiToken {
|
||||
int64 id = 1;
|
||||
string name = 2;
|
||||
int64 created = 3;
|
||||
optional int64 last_used = 4;
|
||||
}
|
||||
|
||||
// GET /api/tokens.
|
||||
message ApiTokens {
|
||||
repeated ApiToken items = 1;
|
||||
}
|
||||
|
||||
// POST /api/tokens, the body.
|
||||
message NewToken {
|
||||
string name = 1;
|
||||
}
|
||||
|
||||
// POST /api/tokens, the answer: the only time the token is shown.
|
||||
message MadeToken {
|
||||
int64 id = 1;
|
||||
string name = 2;
|
||||
string token = 3;
|
||||
}
|
||||
|
||||
// POST /api/push/devices, the body.
|
||||
message PushDevice {
|
||||
string token = 1;
|
||||
optional bool sandbox = 2;
|
||||
}
|
||||
|
||||
// ---- feeds ----
|
||||
|
||||
message FailingRow {
|
||||
string reason = 1;
|
||||
optional string new_url = 2;
|
||||
}
|
||||
|
||||
message FeedRow {
|
||||
string id = 1;
|
||||
string url = 2;
|
||||
optional string title = 3;
|
||||
optional string image = 4;
|
||||
optional string folder = 5;
|
||||
optional string category = 6;
|
||||
optional string feed_category = 7;
|
||||
repeated string keywords = 8;
|
||||
repeated string blocked = 9;
|
||||
bool deadair = 10;
|
||||
int64 deadair_pause = 11;
|
||||
bool allow_explicit = 12;
|
||||
bool auto_download = 13;
|
||||
optional uint64 max_new_per_check = 14;
|
||||
optional string group = 15;
|
||||
bool orphaned = 16;
|
||||
bool managed = 17;
|
||||
optional string schedule = 18;
|
||||
optional uint64 schedule_mins = 19;
|
||||
uint64 every_mins = 20;
|
||||
optional int64 last_checked = 21;
|
||||
optional int64 next_check = 22;
|
||||
optional string last_error = 23;
|
||||
optional FailingRow failing = 24;
|
||||
int64 entries = 25;
|
||||
int64 downloaded = 26;
|
||||
int64 unread = 27;
|
||||
int64 subscribers = 28;
|
||||
bool pinned = 29;
|
||||
bool kept = 30;
|
||||
bool hide_from_all = 31;
|
||||
bool hide_from_signals = 32;
|
||||
bool private = 33;
|
||||
bool private_by_address = 34;
|
||||
// Has an audio or video file: on the Podcasts shelf (#474).
|
||||
bool podcast = 35;
|
||||
}
|
||||
|
||||
// GET /api/feeds.
|
||||
message FeedRows {
|
||||
repeated FeedRow items = 1;
|
||||
}
|
||||
|
||||
// POST /api/feeds, the body.
|
||||
message NewFeed {
|
||||
string url = 1;
|
||||
optional string folder = 2;
|
||||
repeated string keywords = 3;
|
||||
optional bool allow_explicit = 4;
|
||||
}
|
||||
|
||||
// POST /api/feeds, the answer.
|
||||
message AddedFeed {
|
||||
string id = 1;
|
||||
bool existing = 2;
|
||||
}
|
||||
|
||||
// PATCH /api/feeds/{id}, the body: what is there changes, what is not is left.
|
||||
message FeedPatch {
|
||||
optional string url = 1;
|
||||
google.protobuf.Value schedule = 2;
|
||||
google.protobuf.Value folder = 3;
|
||||
google.protobuf.Value category = 4;
|
||||
Words keywords = 5;
|
||||
Words blocked = 6;
|
||||
optional bool allow_explicit = 7;
|
||||
optional bool auto_download = 8;
|
||||
google.protobuf.Value max_new_per_check = 9;
|
||||
optional bool pinned = 10;
|
||||
optional bool kept = 11;
|
||||
optional bool hide_from_all = 12;
|
||||
optional bool hide_from_signals = 13;
|
||||
google.protobuf.Value deadair = 14;
|
||||
optional int64 deadair_pause = 15;
|
||||
optional bool private = 16;
|
||||
}
|
||||
|
||||
// POST /api/feeds/{id}/download-latest, the body.
|
||||
message HowMany {
|
||||
optional int64 count = 1;
|
||||
// One season, all of it (#482): its number, or "none" for the episodes naming none.
|
||||
optional string season = 2;
|
||||
}
|
||||
|
||||
// POST /api/feeds/{id}/download-latest, the answer.
|
||||
message Queued {
|
||||
uint64 queued = 1;
|
||||
}
|
||||
|
||||
// POST /api/feeds/{id}/read-all, POST /api/read-all.
|
||||
message Marked {
|
||||
uint64 marked = 1;
|
||||
}
|
||||
|
||||
// POST /api/fetch, the body.
|
||||
message FetchBody {
|
||||
optional string feed = 1;
|
||||
optional bool force = 2;
|
||||
}
|
||||
|
||||
// POST /api/opml, the body.
|
||||
message OpmlBody {
|
||||
string xml = 1;
|
||||
}
|
||||
|
||||
// POST /api/opml, the answer.
|
||||
message OpmlImported {
|
||||
uint64 added = 1;
|
||||
uint64 already = 2;
|
||||
}
|
||||
|
||||
// ---- items ----
|
||||
|
||||
message EncRow {
|
||||
int64 id = 1;
|
||||
optional int64 deadair = 2;
|
||||
optional bool deadair_pending = 3;
|
||||
string feed_id = 4;
|
||||
string guid = 5;
|
||||
string url = 6;
|
||||
optional string mime = 7;
|
||||
optional int64 length = 8;
|
||||
optional string path = 9;
|
||||
string state = 10;
|
||||
optional string last_error = 11;
|
||||
}
|
||||
|
||||
// GET /api/entries/{feed_id}/{guid}.
|
||||
message EntryRow {
|
||||
string guid = 1;
|
||||
string feed_id = 2;
|
||||
optional string title = 3;
|
||||
optional string link = 4;
|
||||
optional int64 published = 5;
|
||||
optional string description = 6;
|
||||
optional string markdown = 7;
|
||||
optional string markdown_html = 8;
|
||||
bool read = 9;
|
||||
bool flagged = 10;
|
||||
optional string image = 11;
|
||||
optional int64 duration = 12;
|
||||
optional int64 episode = 13;
|
||||
optional int64 season = 14;
|
||||
int64 position = 15;
|
||||
repeated EncRow enclosures = 16;
|
||||
}
|
||||
|
||||
// GET /api/entries, /api/feeds/{id}/entries, /api/signals/{id}/entries.
|
||||
message EntryPage {
|
||||
int64 total = 1;
|
||||
repeated EntryRow entries = 2;
|
||||
}
|
||||
|
||||
// POST /api/playlists and PATCH /api/playlists/{id}, the body: a smart playlist (#476).
|
||||
message PlaylistSpec {
|
||||
string name = 1;
|
||||
repeated string feeds = 2;
|
||||
repeated string priority = 3;
|
||||
bool only_unplayed = 4;
|
||||
bool oldest_first = 5;
|
||||
}
|
||||
|
||||
// A playlist in GET /api/playlists, with how many episodes it holds now.
|
||||
message PlaylistRow {
|
||||
int64 id = 1;
|
||||
string name = 2;
|
||||
repeated string feeds = 3;
|
||||
repeated string priority = 4;
|
||||
bool only_unplayed = 5;
|
||||
bool oldest_first = 6;
|
||||
int64 items = 7;
|
||||
}
|
||||
|
||||
message PlaylistRows {
|
||||
repeated PlaylistRow items = 1;
|
||||
}
|
||||
|
||||
// One season of a podcast in GET /api/feeds/{id}/seasons (#482): its number, unset for the
|
||||
// episodes that name none, how many episodes it has and how many are unread.
|
||||
message SeasonRow {
|
||||
optional int64 season = 1;
|
||||
int64 episodes = 2;
|
||||
int64 unread = 3;
|
||||
}
|
||||
|
||||
message SeasonRows {
|
||||
repeated SeasonRow items = 1;
|
||||
}
|
||||
|
||||
// POST /api/playlists, the answer.
|
||||
message PlaylistId {
|
||||
int64 id = 1;
|
||||
}
|
||||
|
||||
// One of Up Next's episodes (#473).
|
||||
message QueueItem {
|
||||
string feed_id = 1;
|
||||
string guid = 2;
|
||||
}
|
||||
|
||||
// PUT /api/queue, the body: Up Next in its new order. The answer is it as saved, an EntryPage.
|
||||
message QueueItems {
|
||||
repeated QueueItem items = 1;
|
||||
}
|
||||
|
||||
// POST /api/entries/{feed_id}/{guid}/flags, the body. The answer is the feed's FeedRow, or JSON
|
||||
// null when the feed is not yours.
|
||||
message Flags {
|
||||
optional bool read = 1;
|
||||
optional bool flagged = 2;
|
||||
}
|
||||
|
||||
// POST /api/entries/{feed_id}/{guid}/position, the body.
|
||||
message Position {
|
||||
int64 secs = 1;
|
||||
optional int64 duration = 2;
|
||||
optional int64 deadair = 3;
|
||||
}
|
||||
|
||||
// POST /api/entries/{feed_id}/{guid}/share, the body.
|
||||
message ShareBody {
|
||||
optional int64 enc = 1;
|
||||
}
|
||||
|
||||
// POST /api/entries/{feed_id}/{guid}/share, the answer.
|
||||
message ShareLink {
|
||||
optional string url = 1;
|
||||
optional string why = 2;
|
||||
}
|
||||
|
||||
// ---- Signals ----
|
||||
|
||||
message SignalRow {
|
||||
int64 id = 1;
|
||||
string name = 2;
|
||||
repeated string keywords = 3;
|
||||
optional string description = 4;
|
||||
optional string icon = 5;
|
||||
int64 items = 6;
|
||||
int64 unread = 7;
|
||||
}
|
||||
|
||||
// GET /api/signals.
|
||||
message SignalRows {
|
||||
repeated SignalRow items = 1;
|
||||
}
|
||||
|
||||
// POST /api/signals and PATCH /api/signals/{id}, the body.
|
||||
message SignalBody {
|
||||
optional string name = 1;
|
||||
Words keywords = 2;
|
||||
google.protobuf.Value description = 3;
|
||||
google.protobuf.Value icon = 4;
|
||||
}
|
||||
|
||||
// POST /api/signals, the answer.
|
||||
message SignalId {
|
||||
int64 id = 1;
|
||||
}
|
||||
|
||||
// POST /api/signals/icon, the body. Each candidate is [icon, label].
|
||||
message IconAsk {
|
||||
string name = 1;
|
||||
repeated string keywords = 2;
|
||||
optional string description = 3;
|
||||
repeated google.protobuf.ListValue candidates = 4;
|
||||
}
|
||||
|
||||
// POST /api/signals/icon, the answer.
|
||||
message Icon {
|
||||
string icon = 1;
|
||||
}
|
||||
|
||||
// ---- the directory ----
|
||||
|
||||
message PopularRow {
|
||||
string id = 1;
|
||||
optional string title = 2;
|
||||
optional string image = 3;
|
||||
int64 subscribers = 4;
|
||||
bool subscribed = 5;
|
||||
optional string feed = 6;
|
||||
optional string category = 7;
|
||||
optional string subcategory = 8;
|
||||
bool podcast = 9;
|
||||
}
|
||||
|
||||
// GET /api/popular, GET /api/directory.
|
||||
message PopularRows {
|
||||
repeated PopularRow items = 1;
|
||||
}
|
||||
|
||||
message ListedItem {
|
||||
string guid = 1;
|
||||
optional string title = 2;
|
||||
optional string link = 3;
|
||||
optional int64 published = 4;
|
||||
optional int64 duration = 5;
|
||||
optional string description = 6;
|
||||
}
|
||||
|
||||
// GET /api/directory/{id}.
|
||||
message Listed {
|
||||
optional string description = 1;
|
||||
optional string failing = 2;
|
||||
repeated ListedItem items = 3;
|
||||
bool can_admin = 4;
|
||||
}
|
||||
|
||||
// POST /api/popular/{id}, the answer.
|
||||
message FeedId {
|
||||
string id = 1;
|
||||
}
|
||||
|
||||
message BlockedRow {
|
||||
string id = 1;
|
||||
optional string title = 2;
|
||||
int64 at = 3;
|
||||
}
|
||||
|
||||
// GET /api/directory-admin.
|
||||
message DirAdmin {
|
||||
repeated directory.Instance instances = 1;
|
||||
repeated BlockedRow blocked = 2;
|
||||
}
|
||||
|
||||
// When the snapshot last went: a SnapshotResult's fields when it was taken, the error when not.
|
||||
message Shared {
|
||||
int64 at = 1;
|
||||
optional uint64 accepted = 2;
|
||||
optional uint64 refused_private = 3;
|
||||
optional uint64 refused_blocked = 4;
|
||||
optional uint64 refused_invalid = 5;
|
||||
optional string error = 6;
|
||||
}
|
||||
|
||||
// GET /api/directory-status.
|
||||
message DirStatus {
|
||||
bool configured = 1;
|
||||
optional string name = 2;
|
||||
optional string access = 3;
|
||||
optional string error = 4;
|
||||
optional Shared shared = 5;
|
||||
}
|
||||
|
||||
// ---- the server ----
|
||||
|
||||
// GET /api/status.
|
||||
message Status {
|
||||
uint64 feeds = 1;
|
||||
int64 pending = 2;
|
||||
int64 downloaded = 3;
|
||||
string version = 4;
|
||||
}
|
||||
|
||||
// GET /api/settings.
|
||||
message Settings {
|
||||
string schedule = 1;
|
||||
uint64 every_mins = 2;
|
||||
uint64 max_new_per_check = 3;
|
||||
repeated string media_types = 4;
|
||||
string download_dir = 5;
|
||||
double max_total_gb = 6;
|
||||
uint64 max_age_days = 7;
|
||||
uint64 art_cache_mb = 8;
|
||||
string directory_url = 9;
|
||||
bool directory_token_set = 10;
|
||||
bool directory_share = 11;
|
||||
string play_url = 12;
|
||||
bool play_token_set = 13;
|
||||
bool signals_on_server = 14;
|
||||
uint64 signal_days = 15;
|
||||
bool typesafe_server_key = 16;
|
||||
}
|
||||
|
||||
// PATCH /api/settings, the body.
|
||||
message SettingsPatch {
|
||||
optional string schedule = 1;
|
||||
optional uint64 max_new_per_check = 2;
|
||||
Words media_types = 3;
|
||||
optional double max_total_gb = 4;
|
||||
optional uint64 max_age_days = 5;
|
||||
optional uint64 art_cache_mb = 6;
|
||||
optional string directory_url = 7;
|
||||
optional string directory_token = 8;
|
||||
optional bool directory_share = 9;
|
||||
optional string play_url = 10;
|
||||
optional string play_token = 11;
|
||||
optional bool signals_on_server = 12;
|
||||
optional uint64 signal_days = 13;
|
||||
}
|
||||
|
||||
message Account {
|
||||
int64 id = 1;
|
||||
string name = 2;
|
||||
bool admin = 3;
|
||||
bool password = 4;
|
||||
optional int64 created = 5;
|
||||
optional int64 last_login = 6;
|
||||
}
|
||||
|
||||
// GET /api/users.
|
||||
message Accounts {
|
||||
repeated Account items = 1;
|
||||
}
|
||||
|
||||
// POST /api/users, the body.
|
||||
message NewUser {
|
||||
string name = 1;
|
||||
optional string password = 2;
|
||||
optional bool admin = 3;
|
||||
}
|
||||
|
||||
// PATCH /api/users/{id}, the body.
|
||||
message UserPatch {
|
||||
bool admin = 1;
|
||||
}
|
||||
|
||||
message LogLine {
|
||||
uint64 seq = 1;
|
||||
int64 ts = 2;
|
||||
string level = 3;
|
||||
string target = 4;
|
||||
string msg = 5;
|
||||
}
|
||||
|
||||
// GET /api/logs.
|
||||
message LogPage {
|
||||
repeated LogLine lines = 1;
|
||||
uint64 latest = 2;
|
||||
}
|
||||
|
||||
// POST /api/client, the body.
|
||||
message ClientEvent {
|
||||
string ev = 1;
|
||||
optional string msg = 2;
|
||||
optional string src = 3;
|
||||
optional uint32 line = 4;
|
||||
optional string feed = 5;
|
||||
optional int64 enclosure = 6;
|
||||
optional bool deadair = 7;
|
||||
optional uint64 ms = 8;
|
||||
optional string filter = 9;
|
||||
optional string trace = 10;
|
||||
optional string span = 11;
|
||||
optional uint64 start = 12;
|
||||
optional string state = 13;
|
||||
}
|
||||
25
crates/wire/proto/play.proto
Normal file
25
crates/wire/proto/play.proto
Normal file
@@ -0,0 +1,25 @@
|
||||
// ipx-play's API (crates/play/src/lib.rs), each message named after the Rust struct it mirrors.
|
||||
// A field added there goes here too; debug builds refuse an answer with a field missing here.
|
||||
syntax = "proto3";
|
||||
package play;
|
||||
|
||||
// POST /v1/shares, the body.
|
||||
message Episode {
|
||||
string feed_url = 1;
|
||||
string guid = 2;
|
||||
optional string feed_title = 3;
|
||||
optional string feed_image = 4;
|
||||
optional string title = 5;
|
||||
optional string description = 6;
|
||||
optional string link = 7;
|
||||
optional int64 published = 8;
|
||||
optional int64 duration = 9;
|
||||
string audio_url = 10;
|
||||
optional string mime = 11;
|
||||
}
|
||||
|
||||
// POST /v1/shares, the answer.
|
||||
message Shared {
|
||||
string id = 1;
|
||||
string url = 2;
|
||||
}
|
||||
345
crates/wire/src/lib.rs
Normal file
345
crates/wire/src/lib.rs
Normal file
@@ -0,0 +1,345 @@
|
||||
//! Protobuf beside JSON (#465). The handlers speak JSON, as they always have; a route's
|
||||
//! messages in `proto/` say what the same answer is in protobuf, and `negotiate` converts at the
|
||||
//! edge: a request body sent as `application/x-protobuf` becomes the JSON the handler reads, and
|
||||
//! an answer becomes protobuf for a client whose `Accept` asks for it. Anyone not asking gets JSON
|
||||
//! exactly as before.
|
||||
//!
|
||||
//! How a message stands for JSON is said at the top of `proto/ipx.proto`: names as in JSON, an
|
||||
//! Option `optional`, a message of only `repeated items` an array, null-to-clear a
|
||||
//! `google.protobuf.Value`.
|
||||
//!
|
||||
//! Debug builds convert every JSON answer of a route in the table, asked for or not, refusing
|
||||
//! fields the message lacks, and answer 500 when a route has no message: the cargo tests and the
|
||||
//! browser suite, which run debug builds, then fail on a field added in Rust and not in `proto/`.
|
||||
|
||||
use std::sync::LazyLock;
|
||||
|
||||
use axum::body::{Body, to_bytes};
|
||||
use axum::extract::{MatchedPath, Request, State};
|
||||
use axum::http::{HeaderMap, HeaderValue, StatusCode, header};
|
||||
use axum::middleware::Next;
|
||||
use axum::response::{IntoResponse, Response};
|
||||
use prost_reflect::{DescriptorPool, DeserializeOptions, DynamicMessage, Kind, MessageDescriptor, SerializeOptions};
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde_json::Value;
|
||||
|
||||
pub const PROTOBUF: &str = "application/x-protobuf";
|
||||
|
||||
/// For a client to encode and decode the structs in `pb` without naming prost itself.
|
||||
pub use prost::Message;
|
||||
|
||||
/// The messages as Rust structs, which the answers go into from their JSON and which a client
|
||||
/// in Rust reads and writes (#470): prost's own encoding, several times faster than going through
|
||||
/// the descriptors at run time.
|
||||
#[allow(clippy::all, clippy::pedantic, missing_docs)]
|
||||
pub mod pb {
|
||||
pub mod directory {
|
||||
include!(concat!(env!("OUT_DIR"), "/directory.rs"));
|
||||
}
|
||||
pub mod ipx {
|
||||
include!(concat!(env!("OUT_DIR"), "/ipx.rs"));
|
||||
}
|
||||
pub mod play {
|
||||
include!(concat!(env!("OUT_DIR"), "/play.rs"));
|
||||
}
|
||||
}
|
||||
|
||||
include!(concat!(env!("OUT_DIR"), "/fast.rs"));
|
||||
|
||||
/// The JSON an answer was into its struct, and that into protobuf.
|
||||
fn encode<T: DeserializeOwned + prost::Message>(json: &[u8]) -> Result<Vec<u8>, String> {
|
||||
Ok(serde_json::from_slice::<T>(json).map_err(|e| e.to_string())?.encode_to_vec())
|
||||
}
|
||||
|
||||
/// The most read of a body either way: axum's own limit for `Json`.
|
||||
const LIMIT: usize = 2 << 20;
|
||||
|
||||
static POOL: LazyLock<DescriptorPool> = LazyLock::new(|| {
|
||||
DescriptorPool::decode(include_bytes!(concat!(env!("OUT_DIR"), "/wire.bin")).as_slice())
|
||||
.expect("build.rs wrote a valid descriptor set")
|
||||
});
|
||||
|
||||
/// Each route's method, its path as the router has it, the message its body is and the message
|
||||
/// it answers, by full name; "" where it takes or answers no JSON.
|
||||
pub type Routes = &'static [(&'static str, &'static str, &'static str, &'static str)];
|
||||
|
||||
/// A message by full name (`ipx.Feed`).
|
||||
pub fn message(name: &str) -> Option<MessageDescriptor> {
|
||||
POOL.get_message_by_name(name)
|
||||
}
|
||||
|
||||
fn is_list(d: &MessageDescriptor) -> bool {
|
||||
let mut f = d.fields();
|
||||
matches!((f.next(), f.next()), (Some(only), None) if only.name() == "items" && only.is_list())
|
||||
}
|
||||
|
||||
/// Each message field of `d` in `v`, with its descriptor, for `wrap` and `unwrap` to go down.
|
||||
/// Not maps, and not Google's own types, whose JSON is theirs.
|
||||
fn children<'a>(d: &MessageDescriptor, v: &'a mut Value) -> Vec<(MessageDescriptor, &'a mut Value)> {
|
||||
let Some(obj) = v.as_object_mut() else { return vec![] };
|
||||
let mut out = Vec::new();
|
||||
for (key, val) in obj.iter_mut() {
|
||||
let Some(f) = d.get_field_by_name(key) else { continue };
|
||||
let Kind::Message(m) = f.kind() else { continue };
|
||||
if f.is_map() || m.package_name() == "google.protobuf" {
|
||||
continue;
|
||||
}
|
||||
if f.is_list() {
|
||||
out.extend(val.as_array_mut().into_iter().flatten().map(|i| (m.clone(), i)));
|
||||
} else {
|
||||
out.push((m, val));
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// A JSON array where a list message is, made that message.
|
||||
fn wrap(d: &MessageDescriptor, v: &mut Value) {
|
||||
if v.is_array() && is_list(d) {
|
||||
*v = serde_json::json!({ "items": v.take() });
|
||||
}
|
||||
for (m, c) in children(d, v) {
|
||||
wrap(&m, c);
|
||||
}
|
||||
}
|
||||
|
||||
/// A list message made the array it stands for; none sent is an empty one.
|
||||
fn unwrap(d: &MessageDescriptor, v: &mut Value) {
|
||||
for (m, c) in children(d, v) {
|
||||
unwrap(&m, c);
|
||||
}
|
||||
if is_list(d) {
|
||||
*v = match v["items"].take() {
|
||||
Value::Null => Value::Array(vec![]),
|
||||
items => items,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// JSON to protobuf. `strict` refuses a field the message does not have, where otherwise it is
|
||||
/// left out, as a newer server's field is for an older client. JSON null is the empty message.
|
||||
pub fn to_proto(d: &MessageDescriptor, mut v: Value, strict: bool) -> Result<Vec<u8>, String> {
|
||||
if v.is_null() {
|
||||
return Ok(vec![]);
|
||||
}
|
||||
wrap(d, &mut v);
|
||||
let opts = DeserializeOptions::new().deny_unknown_fields(strict);
|
||||
let msg =
|
||||
DynamicMessage::deserialize_with_options(d.clone(), v, &opts).map_err(|e| format!("{}: {e}", d.full_name()))?;
|
||||
Ok(msg.encode_to_vec())
|
||||
}
|
||||
|
||||
/// Protobuf to the JSON the same struct reads: field names as written, 64-bit numbers as numbers,
|
||||
/// a field without presence always, so a default value reads as itself, and an `optional` or a
|
||||
/// message only when sent, so an absent one reads as None.
|
||||
pub fn from_proto(d: &MessageDescriptor, bytes: &[u8]) -> Result<Value, String> {
|
||||
let msg = DynamicMessage::decode(d.clone(), bytes).map_err(|e| format!("{}: {e}", d.full_name()))?;
|
||||
let opts =
|
||||
SerializeOptions::new().use_proto_field_name(true).stringify_64_bit_integers(false).skip_default_fields(false);
|
||||
let mut v = msg.serialize_with_options(serde_json::value::Serializer, &opts).map_err(|e| e.to_string())?;
|
||||
unwrap(d, &mut v);
|
||||
whole(&mut v);
|
||||
Ok(v)
|
||||
}
|
||||
|
||||
/// A whole number as an integer: a google.protobuf.Value holds every number as a double, and
|
||||
/// `3.0` does not read as a usize where `3` reads as an f64 too.
|
||||
fn whole(v: &mut Value) {
|
||||
match v {
|
||||
Value::Number(n) => {
|
||||
if let Some(f) = n.as_f64().filter(|f| n.is_f64() && f.fract() == 0.0 && f.abs() < 9e15) {
|
||||
*v = Value::from(f as i64);
|
||||
}
|
||||
}
|
||||
Value::Array(a) => a.iter_mut().for_each(whole),
|
||||
Value::Object(o) => o.values_mut().for_each(whole),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
fn is(headers: &HeaderMap, name: header::HeaderName, mime: &str) -> bool {
|
||||
// shortcut: a substring, not a parsed Accept, so `q=0` still counts; parse it if a client sends one.
|
||||
headers.get(name).and_then(|v| v.to_str().ok()).is_some_and(|v| v.contains(mime))
|
||||
}
|
||||
|
||||
fn refuse(code: StatusCode, msg: String) -> Response {
|
||||
(code, axum::Json(serde_json::json!({ "error": msg }))).into_response()
|
||||
}
|
||||
|
||||
/// The layer, as `route_layer(from_fn_with_state(ROUTES, ipx_wire::negotiate))`, inside any
|
||||
/// request decompression so it reads the body unpacked.
|
||||
pub async fn negotiate(State(routes): State<Routes>, path: MatchedPath, req: Request, next: Next) -> Response {
|
||||
let method = req.method().as_str().to_owned();
|
||||
let (takes, answers) =
|
||||
routes.iter().find(|r| r.0 == method && r.1 == path.as_str()).map(|r| (r.2, r.3)).unwrap_or(("", ""));
|
||||
|
||||
let req = if is(req.headers(), header::CONTENT_TYPE, PROTOBUF) {
|
||||
let Some(d) = message(takes) else {
|
||||
return refuse(StatusCode::UNSUPPORTED_MEDIA_TYPE, format!("{method} {} takes no protobuf", path.as_str()));
|
||||
};
|
||||
let (mut parts, body) = req.into_parts();
|
||||
let Ok(bytes) = to_bytes(body, LIMIT).await else {
|
||||
return refuse(StatusCode::PAYLOAD_TOO_LARGE, "the body is too large".into());
|
||||
};
|
||||
let json = match from_proto(&d, &bytes) {
|
||||
Ok(v) => v.to_string(),
|
||||
Err(e) => return refuse(StatusCode::BAD_REQUEST, format!("the body did not read as {e}")),
|
||||
};
|
||||
parts.headers.insert(header::CONTENT_TYPE, HeaderValue::from_static("application/json"));
|
||||
parts.headers.remove(header::CONTENT_LENGTH);
|
||||
Request::from_parts(parts, Body::from(json))
|
||||
} else {
|
||||
req
|
||||
};
|
||||
|
||||
let wants = is(req.headers(), header::ACCEPT, PROTOBUF);
|
||||
let mut resp = next.run(req).await;
|
||||
if !answers.is_empty() {
|
||||
resp.headers_mut().append(header::VARY, HeaderValue::from_static("accept"));
|
||||
}
|
||||
let json = resp.status().is_success() && is(resp.headers(), header::CONTENT_TYPE, "application/json");
|
||||
if !json || !(wants || cfg!(debug_assertions)) {
|
||||
return resp;
|
||||
}
|
||||
let Some(d) = message(answers) else {
|
||||
if cfg!(debug_assertions) {
|
||||
return refuse(
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
format!("{method} {} answers JSON with no message for it in ipx-wire", path.as_str()),
|
||||
);
|
||||
}
|
||||
return resp;
|
||||
};
|
||||
let (mut parts, body) = resp.into_parts();
|
||||
let Ok(bytes) = to_bytes(body, usize::MAX).await else {
|
||||
return StatusCode::INTERNAL_SERVER_ERROR.into_response();
|
||||
};
|
||||
// None stays JSON null: as protobuf it would be the empty message, which is also every field
|
||||
// at its default, and a client could not tell "no feed row" from a blank one.
|
||||
if bytes.trim_ascii() == b"null" {
|
||||
return Response::from_parts(parts, Body::from(bytes));
|
||||
}
|
||||
let converted = json_to_proto(answers, &bytes).unwrap_or_else(|| {
|
||||
serde_json::from_slice(&bytes).map_err(|e| e.to_string()).and_then(|v| to_proto(&d, v, cfg!(debug_assertions)))
|
||||
});
|
||||
match converted {
|
||||
Ok(pb) if wants => {
|
||||
// Which message it is, for a client with no table of routes (the page).
|
||||
let ct = format!("{PROTOBUF}; messageType={answers}");
|
||||
parts.headers.insert(header::CONTENT_TYPE, HeaderValue::from_str(&ct).expect("a message name is ASCII"));
|
||||
parts.headers.remove(header::CONTENT_LENGTH);
|
||||
Response::from_parts(parts, Body::from(pb))
|
||||
}
|
||||
Err(e) if cfg!(debug_assertions) => refuse(StatusCode::INTERNAL_SERVER_ERROR, format!("not as protobuf: {e}")),
|
||||
// Asked for protobuf and it would not convert: the JSON, which says what it is.
|
||||
Err(e) => {
|
||||
tracing::warn!(route = path.as_str(), error = %e, "answer did not convert to protobuf");
|
||||
Response::from_parts(parts, Body::from(bytes))
|
||||
}
|
||||
Ok(_) => Response::from_parts(parts, Body::from(bytes)),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use axum::routing::post;
|
||||
use serde_json::json;
|
||||
use tower::ServiceExt as _;
|
||||
|
||||
fn round(name: &str, v: Value) -> Value {
|
||||
let d = message(name).unwrap();
|
||||
from_proto(&d, &to_proto(&d, v, true).unwrap()).unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn every_message_named_in_a_table_exists() {
|
||||
// The tables live in the services; these are the names that would break a client first.
|
||||
for m in ["ipx.FeedRows", "ipx.EntryPage", "ipx.Me", "ipx.Position", "directory.Snapshot", "play.Shared"] {
|
||||
assert!(message(m).is_some(), "{m}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn options_and_lists_come_back_as_they_went() {
|
||||
let ep = json!({ "feed_url": "https://a/f", "guid": "g", "title": "T", "published": 1_700_000_000_000_i64,
|
||||
"audio_url": "https://a/1.mp3" });
|
||||
let back = round("play.Episode", ep.clone());
|
||||
assert_eq!(back, ep, "absent optionals stay absent, 64-bit numbers stay numbers");
|
||||
|
||||
let rows = json!([{ "id": "a", "url": "u", "subscribers": 2, "podcast": true }]);
|
||||
let back = round("directory.Listings", rows);
|
||||
assert_eq!(back[0]["id"], "a");
|
||||
assert_eq!(back[0]["subscribers"], 2);
|
||||
assert_eq!(round("directory.Listings", json!([])), json!([]), "an empty list is an empty array");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_patch_tells_absent_from_empty_and_from_null() {
|
||||
assert_eq!(round("ipx.MePatch", json!({})), json!({}), "nothing sent changes nothing");
|
||||
assert_eq!(round("ipx.MePatch", json!({ "blocked": [] })), json!({ "blocked": [] }), "emptied");
|
||||
assert_eq!(round("ipx.MePatch", json!({ "typesafe_key": null })), json!({ "typesafe_key": null }), "cleared");
|
||||
assert_eq!(
|
||||
round("ipx.FeedPatch", json!({ "max_new_per_check": 3, "pinned": false })),
|
||||
json!({
|
||||
"max_new_per_check": 3, "pinned": false })
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_structs_encode_as_the_descriptors_do() {
|
||||
let feeds = json!([{ "id": "a", "url": "u", "title": null, "keywords": ["k"], "every_mins": 60,
|
||||
"failing": { "reason": "gone", "new_url": null }, "subscribers": 2, "private": true }]);
|
||||
let slow = to_proto(&message("ipx.FeedRows").unwrap(), feeds.clone(), false).unwrap();
|
||||
let fast = json_to_proto("ipx.FeedRows", feeds.to_string().as_bytes()).unwrap().unwrap();
|
||||
assert_eq!(fast, slow);
|
||||
assert!(json_to_proto("ipx.FeedPatch", b"{}").is_none(), "a body with a Value goes the slow way");
|
||||
if cfg!(debug_assertions) {
|
||||
assert!(json_to_proto("play.Shared", br#"{"id":"x","url":"u","new":1}"#).unwrap().is_err());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn strict_refuses_a_field_the_message_lacks() {
|
||||
let d = message("play.Shared").unwrap();
|
||||
assert!(to_proto(&d, json!({ "id": "x", "url": "u", "new": 1 }), true).is_err());
|
||||
assert!(to_proto(&d, json!({ "id": "x", "url": "u", "new": 1 }), false).is_ok());
|
||||
assert_eq!(to_proto(&message("ipx.FeedRow").unwrap(), Value::Null, true).unwrap(), Vec::<u8>::new());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn the_layer_converts_only_for_who_asks() {
|
||||
const ROUTES: Routes = &[("POST", "/v1/shares", "play.Episode", "play.Shared")];
|
||||
let app =
|
||||
axum::Router::new()
|
||||
.route(
|
||||
"/v1/shares",
|
||||
post(|axum::Json(e): axum::Json<Value>| async move {
|
||||
axum::Json(json!({ "id": e["guid"], "url": "u" }))
|
||||
}),
|
||||
)
|
||||
.route_layer(axum::middleware::from_fn_with_state(ROUTES, negotiate));
|
||||
let ep =
|
||||
pb::play::Episode { feed_url: "f".into(), guid: "g1".into(), audio_url: "a".into(), ..Default::default() };
|
||||
let body = ep.encode_to_vec();
|
||||
let ask = |accept: &'static str| {
|
||||
Request::post("/v1/shares")
|
||||
.header(header::CONTENT_TYPE, PROTOBUF)
|
||||
.header(header::ACCEPT, accept)
|
||||
.body(Body::from(body.clone()))
|
||||
.unwrap()
|
||||
};
|
||||
|
||||
let resp = app.clone().oneshot(ask(PROTOBUF)).await.unwrap();
|
||||
assert_eq!(resp.headers()[header::CONTENT_TYPE], "application/x-protobuf; messageType=play.Shared");
|
||||
let got = pb::play::Shared::decode(to_bytes(resp.into_body(), LIMIT).await.unwrap()).unwrap();
|
||||
assert_eq!((got.id.as_str(), got.url.as_str()), ("g1", "u"));
|
||||
|
||||
let resp = app.clone().oneshot(ask("application/json")).await.unwrap();
|
||||
assert_eq!(resp.headers()[header::CONTENT_TYPE], "application/json");
|
||||
|
||||
let bad =
|
||||
Request::post("/v1/shares").header(header::CONTENT_TYPE, PROTOBUF).body(Body::from("\u{ff}")).unwrap();
|
||||
assert_eq!(app.oneshot(bad).await.unwrap().status(), StatusCode::BAD_REQUEST);
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,11 @@ ignore = [
|
||||
# ringbuf 0.4's double free needs an element whose Drop panics; librqbit-utp's buffer holds
|
||||
# only u8, which has no Drop. Its fix is ringbuf 0.5, which librqbit 9.0.1 cannot take (#281).
|
||||
{ id = "RUSTSEC-2026-0293", reason = "librqbit-utp's ring buffer holds u8: no Drop to panic" },
|
||||
# SQLite before 3.54 can return stale memory when it opens a database with a -wal file another
|
||||
# party crafted. ipx opens only its own state.db, in its own data directory, and production is
|
||||
# on Postgres. The fix is libsqlite3-sys 0.39, which sqlx-sqlite 0.9.0, the latest, cannot take:
|
||||
# drop this when sqlx allows it (#467).
|
||||
{ id = "RUSTSEC-2026-0459", reason = "opens only its own SQLite files; waits on sqlx for libsqlite3-sys 0.39 (#467)" },
|
||||
]
|
||||
|
||||
[licenses]
|
||||
|
||||
@@ -5,7 +5,7 @@ Three services, each a container of its own with its own version and changelog (
|
||||
| Service | What it is | Source | Image |
|
||||
|---|---|---|---|
|
||||
| ipx-web | Caddy: serves the page, a static site built from `web/`, and passes `/api` and `/media` to ipx on the same address. It asks ipx before it serves the app or the admin page (`/api/page-check`) | `web/`, `web/Caddyfile` | `Dockerfile.web` |
|
||||
| ipx | The daemon and CLI, one binary. `ipx daemon` runs a scheduler, a Unix-socket control server and the HTTP API in one process; everything else is a CLI that either does the work itself or hands it to a running daemon | `crates/ipx` | `Dockerfile` |
|
||||
| ipx | The daemon and CLI, one binary, `ipxd`. `ipxd daemon` runs a scheduler, a Unix-socket control server and the HTTP API in one process; everything else is a CLI that either does the work itself or hands it to a running daemon | `crates/ipx` | `Dockerfile` |
|
||||
| ipx-directory | The directory of public podcasts that ipx servers share. ipx asks it on its people's behalf; the page never talks to it ([directory.md](directory.md)) | `crates/directory` | `Dockerfile.directory` |
|
||||
|
||||
The Rust source is a Cargo workspace (#202); `crates/feed` (`ipx-feed`) fetches and parses feeds
|
||||
@@ -27,7 +27,8 @@ id, and Caddy's request lines theirs. ipx also serves Prometheus metrics (`[web]
|
||||
|
||||
| File | Responsibility | What it replaced in the Python |
|
||||
|---|---|---|
|
||||
| `crates/ipx/src/main.rs` | The module list and `main`: reads the command line and hands it on | `iPXAgent.py` |
|
||||
| `crates/ipx/src/lib.rs` | The module list and `main`: reads the command line and hands it on | `iPXAgent.py` |
|
||||
| `crates/ipx/src/main.rs` | `ipxd`, which calls `main`: a library under it keeps the modules' log targets `ipx::...` | — |
|
||||
| `crates/ipx/src/cli.rs` | The commands, and what each does when no daemon takes it | `iPXAgent.py` |
|
||||
| `crates/ipx/src/ctx.rs` | `Ctx`, what every command shares: configuration, database, clients | — |
|
||||
| `crates/ipx/src/daemon.rs` | The daemon: worker loop, web server, shutdown, `status` | `iPXAgent.py` |
|
||||
@@ -68,12 +69,13 @@ id, and Caddy's request lines theirs. ipx also serves Prometheus metrics (`[web]
|
||||
| `crates/directory/src/store.rs` | The directory's database: feeds, snapshots, blocked | — |
|
||||
| `crates/directory/src/config.rs` | The directory's config file: its tokens and their access | — |
|
||||
| `crates/directory/src/otel.rs` | Traces for both Rust services: the exporter, `traceparent` in and out | — |
|
||||
| `crates/tui/src/main.rs` | `ipx` (package `ipx-tui`), the reader in a terminal: ratatui over the API, with an API token | — |
|
||||
| `web/index.html` | The app's markup | — |
|
||||
| `web/admin.html` | The admin page's markup: server settings, accounts, the log. Sent to admins only | — |
|
||||
| `web/app.css` | The stylesheet both pages share | — |
|
||||
| `web/src/*.ts` | The page's script, one scope split across files, type-checked by `npx tsc` | — |
|
||||
| `web/build.mjs` | swc: strips the types into `app.js`/`admin.js`/`login.js`, named in the page by a hash of their contents, minifies, and writes the static site to `web/dist` | — |
|
||||
| `web/boot.js` | In `<head>`: the theme from its cookie, and an admin's link, before the first frame | — |
|
||||
| `web/build.mts` | swc: strips the types into `app.js`/`admin.js`/`login.js`, named in the page by a hash of their contents, minifies, and writes the static site to `web/dist` | — |
|
||||
| `web/boot.ts` | In `<head>`: the theme from its cookie, and an admin's link, before the first frame | — |
|
||||
| `web/Caddyfile` | `ipx-web`: serves `web/dist`, asks ipx before serving a page, passes `/api` and `/media` to ipx | — |
|
||||
|
||||
The page is a separate image, `ipx-web` (`Dockerfile.web`, #203), so **editing `web/` needs
|
||||
@@ -156,7 +158,7 @@ printf '{"cmd":"fetch","force":true}\n' | socat - UNIX-CONNECT:$XDG_RUNTIME_DIR/
|
||||
|
||||
**Commands** — `fetch` (optional `feed`, `force`), `reap` (optional `dry_run`), `download`
|
||||
(`enclosure`), `status`, `reload` (read the catalogue and settings again from the database, which
|
||||
`ipx add`, `rm` and `import` send after changing them in a process of their own; answered with
|
||||
`ipxd add`, `rm` and `import` send after changing them in a process of their own; answered with
|
||||
`status`).
|
||||
|
||||
**Events** — `feed_start`, `feed_skip`, `feed_done`, `feed_error`, `progress`, `download_done`,
|
||||
@@ -206,6 +208,20 @@ else a `401`. A feed's items and files (its entries, `download-latest`, `/api/en
|
||||
| `GET /api/logs` | admin-only; the ring buffer, with a sequence cursor |
|
||||
| `GET /media/{id}` | the file, with Range support so seeking works |
|
||||
|
||||
Every route that takes or answers JSON also speaks protobuf (#465): `Content-Type:
|
||||
application/x-protobuf` for a body, `Accept: application/x-protobuf` for an answer, JSON
|
||||
otherwise. The messages are in `crates/wire/proto/` (ipx, directory and play), and the handlers do
|
||||
not know: `ipx_wire::negotiate`, a layer on each service's routes, converts the JSON at the edge
|
||||
by its table of each route's messages (`WIRE` in `web.rs`, `api.rs` and play's `lib.rs`). A route
|
||||
added to a router goes in its table. Debug builds convert every JSON answer strictly, so the cargo
|
||||
tests and the browser suite answer `500` for a field missing from the `.proto` or a route missing
|
||||
from the table. Errors stay JSON, and so does an answer of null. An answer goes into a struct
|
||||
generated from its message (prost, in `crates/wire/build.rs`) and out as protobuf, named in its
|
||||
`Content-Type` (`application/x-protobuf; messageType=ipx.FeedRows`); the page reads it with
|
||||
`web/src/wire.ts`, a decoder of its own driven by the same `.proto` files, which `web/build.mts`
|
||||
reads into the page and the offline worker (#470). The iOS app's own calls go in protobuf (#469), from Swift
|
||||
generated off the same files: a change to them is `sh ios/scripts/proto.sh` too.
|
||||
|
||||
Show notes are feed-supplied HTML from an untrusted source, sanitized with `ammonia` server-side
|
||||
before they reach the page.
|
||||
|
||||
@@ -213,10 +229,10 @@ before they reach the page.
|
||||
|
||||
```sh
|
||||
cargo test # parsing, filters, retention, schedules, SQL, per-user isolation
|
||||
node tests/page-smoke.js # the page script loads and every selector it wires at load exists
|
||||
node tests/native-bridge.js # the page hands playback to a native shell
|
||||
node tests/page-smoke.ts # the page script loads and every selector it wires at load exists
|
||||
node tests/native-bridge.ts # the page hands playback to a native shell
|
||||
npx playwright test # a real browser against a real daemon on fixture feeds
|
||||
node tests/load/run.js # k6: many people at once against a daemon with 1,500 generated feeds
|
||||
node tests/load/run.ts # k6: many people at once against a daemon with 1,500 generated feeds
|
||||
```
|
||||
|
||||
The Rust tests cannot see a wrong selector, a handler that runs and does nothing, or a page that
|
||||
|
||||
42
docs/cli.md
42
docs/cli.md
@@ -10,15 +10,15 @@ processes must never download the same thing. `--local` forces the work to happe
|
||||
|
||||
| Command | What it does |
|
||||
|---|---|
|
||||
| `ipx list` | Subscriptions and their state |
|
||||
| `ipx status` | Counts: feeds, pending, downloaded |
|
||||
| `ipx fetch [FEED] [--force]` | Scan everything, or one feed. `--force` ignores the TTL |
|
||||
| `ipx add <url> [--folder X] [--keywords a,b] [--list] [--category C]` | Subscribe; the id comes from the feed title. `--list` subscribes no one but keeps it in the catalogue, and so in what this server shares with the directory, when its last subscriber leaves; `--category` is the category the directory files it under when the feed names none. Run for a feed already in the catalogue, these list it or set its category |
|
||||
| `ipx rm <feed>` | Unsubscribe; downloads and history are kept |
|
||||
| `ipx import <file.opml>` / `ipx export <file.opml>` | Move subscriptions in or out. Import subscribes the first admin, as the shared web token does; in the web UI it subscribes whoever is signed in |
|
||||
| `ipx reap [--dry-run]` | Run retention now |
|
||||
| `ipx user <add\|list\|passwd\|rm>` | Accounts for the web UI |
|
||||
| `ipx daemon [--web ADDR]` | Scheduler, control socket and the HTTP API (the page is ipx-web's) |
|
||||
| `ipxd list` | Subscriptions and their state |
|
||||
| `ipxd status` | Counts: feeds, pending, downloaded |
|
||||
| `ipxd fetch [FEED] [--force]` | Scan everything, or one feed. `--force` ignores the TTL |
|
||||
| `ipxd add <url> [--folder X] [--keywords a,b] [--list] [--category C]` | Subscribe; the id comes from the feed title. `--list` subscribes no one but keeps it in the catalogue, and so in what this server shares with the directory, when its last subscriber leaves; `--category` is the category the directory files it under when the feed names none. Run for a feed already in the catalogue, these list it or set its category |
|
||||
| `ipxd rm <feed>` | Unsubscribe; downloads and history are kept |
|
||||
| `ipxd import <file.opml>` / `ipxd export <file.opml>` | Move subscriptions in or out. Import subscribes the first admin, as the shared web token does; in the web UI it subscribes whoever is signed in |
|
||||
| `ipxd reap [--dry-run]` | Run retention now |
|
||||
| `ipxd user <add\|list\|passwd\|rm>` | Accounts for the web UI |
|
||||
| `ipxd daemon [--web ADDR]` | Scheduler, control socket and the HTTP API (the page is ipx-web's) |
|
||||
|
||||
`add`, `rm` and `import` change the catalogue in a process of their own. With a daemon running they
|
||||
then tell it to read the catalogue again; without that it kept its own copy and wrote it back at
|
||||
@@ -29,11 +29,11 @@ its next change, undoing them. If it does not answer they say so: restart it.
|
||||
Passwords are read from **stdin**, so they miss the shell history and any `ps` listing.
|
||||
|
||||
```sh
|
||||
echo -n 'a good password' | ipx user add ray # local account
|
||||
ipx user add ray@example.com --no-password # signs in through the proxy only
|
||||
echo -n 'a good password' | ipx user passwd admin # change a password; signs the account out everywhere
|
||||
ipx user list # who exists, and how each signs in
|
||||
ipx user rm sam # account, subscriptions and read state
|
||||
echo -n 'a good password' | ipxd user add ray # local account
|
||||
ipxd user add ray@example.com --no-password # signs in through the proxy only
|
||||
echo -n 'a good password' | ipxd user passwd admin # change a password; signs the account out everywhere
|
||||
ipxd user list # who exists, and how each signs in
|
||||
ipxd user rm sam # account, subscriptions and read state
|
||||
```
|
||||
|
||||
The first account created is an admin; later ones are ordinary users. A database with no accounts
|
||||
@@ -43,14 +43,14 @@ config.toml (or `IPX_ADMIN_PASSWORD`), announced in the log — change it.
|
||||
To avoid even the command line, read it interactively:
|
||||
|
||||
```sh
|
||||
read -s PW && echo -n "$PW" | ipx user passwd admin
|
||||
read -s PW && echo -n "$PW" | ipxd user passwd admin
|
||||
```
|
||||
|
||||
## Scanning
|
||||
|
||||
```sh
|
||||
ipx fetch # everything due
|
||||
ipx fetch atp --force # one feed, ignoring its TTL and schedule
|
||||
ipxd fetch # everything due
|
||||
ipxd fetch atp --force # one feed, ignoring its TTL and schedule
|
||||
```
|
||||
|
||||
A scan: conditional GET (`If-None-Match` / `If-Modified-Since`), parse, record new entries, apply
|
||||
@@ -60,8 +60,8 @@ answers `304` and costs one request.
|
||||
## Retention
|
||||
|
||||
```sh
|
||||
ipx reap --dry-run # what would go, oldest first
|
||||
ipx reap # actually delete
|
||||
ipxd reap --dry-run # what would go, oldest first
|
||||
ipxd reap # actually delete
|
||||
```
|
||||
|
||||
Files are deleted to get back under `max_total_gb`, oldest first, and items past `max_age_days`
|
||||
@@ -72,8 +72,8 @@ stops the next scan fetching it again.
|
||||
## The daemon
|
||||
|
||||
```sh
|
||||
ipx daemon # scheduler + socket + HTTP API
|
||||
ipx daemon --web 0.0.0.0:8099 # override the configured bind for one run
|
||||
ipxd daemon # scheduler + socket + HTTP API
|
||||
ipxd daemon --web 0.0.0.0:8099 # override the configured bind for one run
|
||||
```
|
||||
|
||||
One daemon per socket; a second refuses to start rather than fight over the database. It shuts down
|
||||
|
||||
@@ -4,7 +4,7 @@ Two places. **config.toml** holds what ipx needs before it reaches its database,
|
||||
who gets in: where things are (`download_dir`, `socket`, `organize`), `[torrent]` and `[web]`.
|
||||
**The database** holds the catalogue of feeds (`[feeds.<id>]` below) and the server settings the
|
||||
admin page edits (`schedule`, `max_total_gb`, `max_age_days`, `max_new_per_check`,
|
||||
`media_types`, `art_cache_mb`). Change those in the web UI, or with `ipx add`, `ipx rm` and `ipx import`; they
|
||||
`media_types`, `art_cache_mb`). Change those in the web UI, or with `ipxd add`, `ipxd rm` and `ipxd import`; they
|
||||
take effect without a restart.
|
||||
|
||||
The first time ipx meets a database that holds no catalogue, it takes the feeds and those
|
||||
@@ -137,11 +137,35 @@ metrics_bind = "" # e.g. "0.0.0.0:9187"; empty serves no
|
||||
It is plain HTTP. On a LAN bind everything crosses the network in the clear — and a feed URL can
|
||||
itself carry a credential. Put TLS in front of it if that matters.
|
||||
|
||||
## `[push]`
|
||||
|
||||
```toml
|
||||
[push]
|
||||
key_file = "/config/AuthKey_ABC123DEFG.p8" # the APNs key Apple gives once
|
||||
key_id = "ABC123DEFG" # shown beside it in the developer account
|
||||
team_id = "8743L8D729" # the team the iOS app is signed by
|
||||
topic = "net.sdf1.ipx" # the app's bundle id, the default
|
||||
```
|
||||
|
||||
The iOS app's icon shows All Subscriptions' unread count. With this set, ipx keeps it right while
|
||||
the app is closed (#433): once a minute it compares each phone's count with the last one sent, and
|
||||
sends a changed one through Apple's push service, a badge alone, with no alert or sound. Without
|
||||
it, the count waits on the background refresh iOS runs when it chooses.
|
||||
|
||||
The key is made in the Apple developer account under Certificates, Identifiers & Profiles, Keys,
|
||||
with Apple Push Notifications service enabled, and can be downloaded once. One key serves every
|
||||
app of the team, the TestFlight and debug builds both. Keep it as private as the database: it lets
|
||||
anyone send to the team's apps. A key, id or team that is wrong shows as a warning in the log,
|
||||
`push: Apple refused the signing token`, and nothing is sent until it is right.
|
||||
|
||||
The app registers each phone through the page as the person signed in there
|
||||
(`POST /api/push/devices`), and signing out removes it.
|
||||
|
||||
## `[feeds.<id>]`
|
||||
|
||||
Kept in the database once ipx has moved them in: a feed's settings are changed in the web UI, and
|
||||
feeds come and go with `ipx add`, `ipx rm` and `ipx import`. The table key is the feed id: stable,
|
||||
human-readable, and used in paths and the API. `ipx add` derives it from the feed title.
|
||||
feeds come and go with `ipxd add`, `ipxd rm` and `ipxd import`. The table key is the feed id: stable,
|
||||
human-readable, and used in paths and the API. `ipxd add` derives it from the feed title.
|
||||
|
||||
```toml
|
||||
[feeds.atp]
|
||||
@@ -163,12 +187,17 @@ and are the same for everyone. See [users.md](users.md).
|
||||
`username` and its password go to the feed's own host alone: not to episodes on another host, nor to
|
||||
the feeds an OPML lists on other hosts, which would hand the password to whoever runs them.
|
||||
|
||||
**Shared episodes** go to ipx-play ([play.md](play.md)), set up in Admin, Directory, Sharing
|
||||
episodes: its address and this server's token, kept in the database (`play_url`, `play_token`).
|
||||
Only episodes of feeds the directory would be sent; without it, Share hands over an episode's own
|
||||
address.
|
||||
|
||||
**The Directory** is the directory service's ([directory.md](directory.md)), which an admin sets
|
||||
up in Admin, Directory: its address, this server's token and whether to share. They are server
|
||||
settings, kept in the database (`directory_url`, `directory_token`, `directory_share`), not here.
|
||||
With sharing on, this server sends it every feed in the catalogue that is not private, with how
|
||||
many people here subscribe, and the `category` above for a feed that names none. To offer one with
|
||||
nobody subscribed, `ipx add --list --category News <url>`: it subscribes no one, and a listed feed
|
||||
nobody subscribed, `ipxd add --list --category News <url>`: it subscribes no one, and a listed feed
|
||||
stays in the catalogue, and so in what is shared, when its last subscriber leaves, where another
|
||||
is dropped. A feed nobody
|
||||
subscribes to is checked once a day. One that is dead (failing for 30 days) or quiet (nothing
|
||||
|
||||
@@ -162,6 +162,9 @@ blocks it, so no snapshot brings it back.
|
||||
| `DELETE /v1/instances/{name}` | rwa | drop one token's snapshot, its counts with it |
|
||||
| `POST /v1/feeds/{id}/refresh` | rwa | fetch the feed now, answering with its page |
|
||||
|
||||
Each also speaks protobuf, by `Accept` and `Content-Type: application/x-protobuf` (#465), in the
|
||||
messages of `crates/wire/proto/directory.proto`; ipx asks for it.
|
||||
|
||||
A feed's `id` is the first 64 bits of the SHA-256 of its address, normalised (scheme and host in
|
||||
lower case, no default port or fragment), so every server arrives at the same id. `subscribers`
|
||||
is the sum over every token's latest snapshot. A feed is listed once the directory has read it as
|
||||
|
||||
106
docs/play.md
Normal file
106
docs/play.md
Normal file
@@ -0,0 +1,106 @@
|
||||
# ipx-play: shared episodes (#447)
|
||||
|
||||
When someone shares an episode in iPX, their server sends it to ipx-play, which keeps it and
|
||||
answers with a short link, `https://play.ipxreader.app/e/<id>`. Anyone can open that link: it
|
||||
shows the episode, plays it from the publisher's own file (from the time it was shared at, with
|
||||
`?t=`), and offers to subscribe in the reader's own podcast app. Overcast's links are the model.
|
||||
|
||||
Only episodes of public feeds are sent, by the same rule that decides what the directory is sent:
|
||||
nothing from a feed marked private, one with a login, one whose address carries a key, or one in
|
||||
a private OPML. ipx-play refuses an address that looks like one too. Everything else is shared by
|
||||
its own address, as before. ipx-play keeps what an episode is and where its file is; it serves no
|
||||
file itself.
|
||||
|
||||
## Running it
|
||||
|
||||
A container of its own, `ipx-play`, in the Arcane project `ipx`, from its own image:
|
||||
|
||||
```sh
|
||||
sh scripts/build-image.sh play
|
||||
docker compose -f /mnt/fast/arcane/projects/ipx/compose.yaml pull ipx-play
|
||||
docker compose -f /mnt/fast/arcane/projects/ipx/compose.yaml up -d ipx-play
|
||||
```
|
||||
|
||||
The compose service, beside `ipx-directory`:
|
||||
|
||||
```yaml
|
||||
ipx-play:
|
||||
image: 192.168.1.130:5000/ipx-play:latest
|
||||
container_name: ipx-play
|
||||
restart: unless-stopped
|
||||
env_file: play.env # PLAY_DATABASE_URL=postgres://...
|
||||
environment:
|
||||
PLAY_LOG_FORMAT: json
|
||||
OTEL_EXPORTER_OTLP_ENDPOINT: http://192.168.1.130:4318 # Tempo is not on this network
|
||||
# The dashboard's trace panels show only spans with this tag, as the others' are.
|
||||
OTEL_RESOURCE_ATTRIBUTES: deployment.environment.name=production
|
||||
volumes:
|
||||
- /mnt/fast/appdata/ipx-play:/config
|
||||
ports:
|
||||
- "8102:8102"
|
||||
- "9191:9191" # /metrics, for Prometheus on the LAN; not in the tunnel
|
||||
```
|
||||
|
||||
Its config, `/mnt/fast/appdata/ipx-play/play.toml`:
|
||||
|
||||
```toml
|
||||
bind = "0.0.0.0:8102"
|
||||
public_url = "https://play.ipxreader.app"
|
||||
# Prometheus metrics, on a port of their own and not the one the tunnel publishes. Empty, or
|
||||
# left out, serves none.
|
||||
metrics_bind = "0.0.0.0:9191"
|
||||
|
||||
# One for each ipx server allowed to make links; `ipx-play token <name>` prints the lines.
|
||||
[[tokens]]
|
||||
name = "tower"
|
||||
token_sha256 = "..."
|
||||
```
|
||||
|
||||
Tokens are read at start: after adding one, restart the container.
|
||||
|
||||
Its database is Postgres, `ipx_play` on the `databases` project's server, its URL in `play.env`
|
||||
beside the compose file (mode 600), as the directory's is. It makes its one table, `shares`, on
|
||||
start.
|
||||
|
||||
The tunnel publishes `play.ipxreader.app` to `http://192.168.1.130:8102` with no Access in front:
|
||||
the pages are anyone's. cloudflared is not on the project's network, so not `ipx-play:8102`, as
|
||||
the directory's route is the host's port too. `/v1` takes a token for everything.
|
||||
|
||||
## Watching it
|
||||
|
||||
Its traces go to Tempo as service `ipx-play`: a span a request, a child of ipx's when ipx shared
|
||||
an episode, so one trace runs from the page's Share through ipx to here. Its log is a JSON line a
|
||||
request (`target` `ipx_play::http`: `method`, `path`, `status`, `duration_ms`, `token`, the
|
||||
server's name and never its token) and one a share (`an episode was shared`: `server`, `id`).
|
||||
|
||||
Its metrics (#458), on `metrics_bind`, scraped as `job="ipx-play"`, beside the directory's job in
|
||||
Prometheus's config:
|
||||
|
||||
```yaml
|
||||
- job_name: ipx-play
|
||||
static_configs:
|
||||
- targets: ["192.168.1.130:9191"]
|
||||
```
|
||||
|
||||
`play_http_request_duration_seconds{method,route,status}` (a page opened is `route="/e/{id}"`, an
|
||||
address no route has `route=""`), `play_shares_total`, `play_process_resident_bytes`,
|
||||
`play_process_open_fds`, `play_uptime_seconds` and `play_build_info`. The iPX dashboard's
|
||||
`ipx-play` row draws them, and the alert `ipx-play is down` fires when Prometheus cannot reach it.
|
||||
|
||||
## Setting up ipx
|
||||
|
||||
In Admin, Directory, Sharing episodes: the address ipx reaches it at, `http://ipx-play:8102`
|
||||
inside Docker (the public address would go out through Cloudflare and back), and the token
|
||||
`ipx-play token tower` printed. Without them Share hands over the episode's own address.
|
||||
|
||||
## The page
|
||||
|
||||
`/e/<id>` shows the show's art, its name, the episode's title, date and length, a player starting
|
||||
at `?t=` (seconds, or `12:34`), the episode's own page if it has one, its notes (sanitized by
|
||||
ammonia), and buttons that open Apple Podcasts, Overcast, Pocket Casts and Castro at the feed, with
|
||||
the feed's address to paste into anything else. Open Graph tags give it a preview in Messages,
|
||||
Slack and the like. It runs no script: the start time is the media fragment `#t=` on the file. Its
|
||||
Content-Security-Policy lets it load only its stylesheet, the art and the file.
|
||||
|
||||
A show that inserts its ads per listener can move the time by an ad's length, as Overcast's links
|
||||
can: the file is the publisher's, fetched fresh by each listener.
|
||||
@@ -364,7 +364,7 @@ erDiagram
|
||||
- `signals` and `signal_verdicts` are created from their entities like every table
|
||||
(`crates/ipx/src/entity.rs`). `users.typesafe_key`, `signals.description` and `signals.icon`
|
||||
were added to existing tables, so `create_missing` adds each column when it is missing.
|
||||
- The icons are files of ipx-web's, not the database's: `web/build.mjs` copies Font Awesome Free's
|
||||
- The icons are files of ipx-web's, not the database's: `web/build.mts` copies Font Awesome Free's
|
||||
SVGs to `/icons/<style>/<name>.svg`, kept a day, and writes `/icons/index.json`, names, labels
|
||||
and search terms, about 90 KB compressed, named by its hash and fetched when the picker first
|
||||
opens. A Signal's tile draws its one file as a CSS mask, so it takes the theme's colour.
|
||||
@@ -406,7 +406,7 @@ erDiagram
|
||||
- `cargo test -p ipx`: the request's shape and the threshold (`signals::tests`), and the queue,
|
||||
verdicts, ownership, unread counts and new keywords (`db::tests::a_signal_shows_what_jev_said_fits_and_only_to_its_owner`),
|
||||
on SQLite and Postgres both.
|
||||
- `node --test tests/page-unit.js`: a Signal's place id and its list's address.
|
||||
- `node --test tests/page-unit.ts`: a Signal's place id and its list's address.
|
||||
- `npx playwright test -g Signal`: makes a Signal from the folder's +, and checks that the matching
|
||||
item shows and that reading it reads it in its feed and clears the badge. TypeSafe is stood in for
|
||||
by the fixture server's `/jev` (`IPX_TYPESAFE_URL`), which refuses the shape TypeSafe refuses, and
|
||||
|
||||
34
docs/sso.md
34
docs/sso.md
@@ -63,7 +63,7 @@ Cloudflare and Authentik were already right. Three things on the ipx side were n
|
||||
see the next section.
|
||||
3. **The account had the wrong name.** It was made by hand as `rays`, but the header carries
|
||||
`rays@sdf1.net`. With `auto_create_users` on, the first visit would have made a second, empty
|
||||
account. `ipx user rename rays rays@sdf1.net` fixed that without losing anything.
|
||||
account. `ipxd user rename rays rays@sdf1.net` fixed that without losing anything.
|
||||
|
||||
### The address to trust, and why it is 192.168.64.1
|
||||
|
||||
@@ -105,7 +105,7 @@ ordinary user with no feeds. An account made before the proxy can be given the n
|
||||
send:
|
||||
|
||||
```sh
|
||||
docker exec iPX ipx user rename <old name> <email address>
|
||||
docker exec iPX ipxd user rename <old name> <email address>
|
||||
```
|
||||
|
||||
### Signing out
|
||||
@@ -225,19 +225,19 @@ Check it: the busybox request under [Check it](#check-it), which sends the email
|
||||
token from the Docker bridge, now gets `sign in`, and the site still signs you in through Authentik.
|
||||
|
||||
**Turning it off:** clear `trusted_header` and restart. Proxy-made accounts stay, but nobody can sign
|
||||
in with them until they are given a password (`ipx user passwd <name>`).
|
||||
in with them until they are given a password (`ipxd user passwd <name>`).
|
||||
|
||||
---
|
||||
|
||||
## Everyday administration
|
||||
|
||||
```sh
|
||||
ipx user list # who exists, how each signs in, and when
|
||||
echo -n 'secret123' | ipx user add sam # local account, password on stdin
|
||||
ipx user add sam@example.com --no-password # proxy-only account, made ahead of time
|
||||
ipx user rename sam sam@example.com # give an account the name the proxy sends
|
||||
echo -n 'newsecret' | ipx user passwd sam # change a password
|
||||
ipx user rm sam # remove the account
|
||||
ipxd user list # who exists, how each signs in, and when
|
||||
echo -n 'secret123' | ipxd user add sam # local account, password on stdin
|
||||
ipxd user add sam@example.com --no-password # proxy-only account, made ahead of time
|
||||
ipxd user rename sam sam@example.com # give an account the name the proxy sends
|
||||
echo -n 'newsecret' | ipxd user passwd sam # change a password
|
||||
ipxd user rm sam # remove the account
|
||||
```
|
||||
|
||||
In the container, put `docker exec iPX` in front, and `docker exec -i iPX` for the ones
|
||||
@@ -261,5 +261,19 @@ the `ipodderx` application, and send `CF-Access-Client-Id` and `CF-Access-Client
|
||||
API token as who is asking. Do not bypass Access for `/api/*` instead; the token would then be the
|
||||
only thing between the internet and the API.
|
||||
|
||||
### `ipxapi.sdf1.net`, for the iOS app (#419)
|
||||
|
||||
The app's player cannot send an Access service token's pair of headers as well as sign in, and its
|
||||
cookie from Access ends mid-drive, so it has an address of its own: `ipxapi.sdf1.net`, a route of
|
||||
the `rays-unraid` tunnel to the same `192.168.1.130:8099` as `ipodderx.sdf1.net`, with a proxied
|
||||
CNAME to the tunnel and **no Access application**. That is the exposure the paragraph above warns
|
||||
against, held narrow another way: a custom firewall rule on the `sdf1.net` zone (Security, WAF)
|
||||
blocks every request to that host unless it carries `Authorization: Bearer` and is for
|
||||
`/api/feeds`, `/api/me`, `/api/art`, `/api/entries/*` or `/media/*`. So the page, the sign-in form,
|
||||
`/api/login`, `?token=` sign-in and the admin API answer 403 there, and ipx still checks the
|
||||
token on what is let through. A new path the app needs is added to that rule's expression. The
|
||||
rule is on Cloudflare, not in this repository; if the hostname is ever served without it, the
|
||||
whole of ipx is on the internet behind bearer tokens alone.
|
||||
|
||||
See also [users.md](users.md) for what several people share, [configuration.md](configuration.md)
|
||||
for every `[web]` key, and [cli.md](cli.md) for the `ipx user` commands.
|
||||
for every `[web]` key, and [cli.md](cli.md) for the `ipxd user` commands.
|
||||
|
||||
@@ -56,13 +56,13 @@ A database with no accounts creates **admin** on the next daemon start, with the
|
||||
readable by ipx's user alone. The log says where the password is, never what it is. Change it:
|
||||
|
||||
```sh
|
||||
echo -n 'a good password' | ipx user passwd admin
|
||||
echo -n 'a good password' | ipxd user passwd admin
|
||||
```
|
||||
|
||||
## Adding someone
|
||||
|
||||
```sh
|
||||
echo -n 'their password' | ipx user add sam
|
||||
echo -n 'their password' | ipxd user add sam
|
||||
```
|
||||
|
||||
They sign in at `/login` and start with **no feeds**: subscriptions are per person. Adding a feed
|
||||
@@ -107,5 +107,5 @@ and who else is an admin, and read the log, which names everyone's feeds and sig
|
||||
gets the Settings and Log buttons hidden and a `403` if they ask anyway.
|
||||
|
||||
```sh
|
||||
ipx user list # the admin column says who
|
||||
ipxd user list # the admin column says who
|
||||
```
|
||||
|
||||
93
grafana/alerts.py
Normal file
93
grafana/alerts.py
Normal file
@@ -0,0 +1,93 @@
|
||||
"""The iPX alert rules in Grafana (#365): what should be looked at now rather than at the next prod
|
||||
check, written out as Grafana's alerting provisioning, beside the dashboard.
|
||||
|
||||
python3 grafana/alerts.py > /mnt/fast/arcane/projects/monitoring/grafana-provisioning/alerting/ipx.json
|
||||
|
||||
Grafana reads it at start, or at once from POST /api/admin/provisioning/alerting/reload. There is
|
||||
no contact point yet: a rule that fires shows in Grafana's Alerting page, and adding one (email,
|
||||
ntfy, a webhook) is a file in the same folder. Each rule is a query and a threshold on it; the
|
||||
query's value is in the alert, so it says how bad as well as what.
|
||||
"""
|
||||
import json
|
||||
|
||||
# Prometheus was added in Grafana's settings, not provisioned, so its uid is Grafana's own.
|
||||
PROM = "eenybu3ao2t4wb"
|
||||
LOKI = "P8E80F9AEF21F6940"
|
||||
|
||||
|
||||
def rule(uid, title, expr, op, value, for_, summary, *, ds=PROM, window=600, no_data="OK"):
|
||||
"""Fires when `expr` is `op` (gt, lt) `value` for `for_`. No data is fine by default: most of
|
||||
these metrics are absent until there is something to say (no job running, no scan yet)."""
|
||||
return {
|
||||
"uid": f"ipx-{uid}", "title": title, "condition": "C", "for": for_,
|
||||
"data": [
|
||||
{"refId": "A", "relativeTimeRange": {"from": window, "to": 0}, "datasourceUid": ds,
|
||||
# Loki's instant query is a queryType; Prometheus's a flag.
|
||||
"model": {"refId": "A", "expr": expr,
|
||||
**({"queryType": "instant"} if ds == LOKI else {"instant": True})}},
|
||||
{"refId": "C", "datasourceUid": "__expr__",
|
||||
"model": {"refId": "C", "type": "threshold", "expression": "A",
|
||||
"conditions": [{"evaluator": {"type": op, "params": [value]}}]}},
|
||||
],
|
||||
"noDataState": no_data, "execErrState": "Error",
|
||||
"annotations": {"summary": summary},
|
||||
"labels": {"service": "ipx"},
|
||||
}
|
||||
|
||||
|
||||
rules = [
|
||||
# Down: Prometheus cannot reach it. A container gone is no data at all, so that fires too.
|
||||
rule("down", "ipx is down", 'up{job="ipx"}', "lt", 1, "2m",
|
||||
"Prometheus cannot reach ipx's /metrics: the iPX container is down or restarting.", no_data="Alerting"),
|
||||
rule("directory-down", "ipx-directory is down", 'up{job="ipx-directory"}', "lt", 1, "5m",
|
||||
"Prometheus cannot reach the directory's /metrics.", no_data="Alerting"),
|
||||
rule("play-down", "ipx-play is down", 'up{job="ipx-play"}', "lt", 1, "5m",
|
||||
"Prometheus cannot reach ipx-play's /metrics: shared links are likely not opening.", no_data="Alerting"),
|
||||
rule("web-down", "ipx-web is down", 'up{job="ipx-web"}', "lt", 1, "5m",
|
||||
"Prometheus cannot reach Caddy's /metrics: the page is likely down too.", no_data="Alerting"),
|
||||
# The worker: the daemon answers `ipxd status` while it is stuck, so only these say so.
|
||||
rule("scan-stale", "ipx's scans have stopped", 'max(ipx_last_scan_age_seconds{job="ipx"})', "gt", 900, "5m",
|
||||
"No scan has finished for over 15 minutes; scans run at least every 10. The worker is stuck: its job and trace say on what."),
|
||||
rule("job-long", "ipx's worker is stuck on one job", 'max(ipx_worker_job_seconds{job="ipx"})', "gt", 1800, "1m",
|
||||
"One job has run for over 30 minutes (ipx_worker_job_seconds says which kind)."),
|
||||
rule("pool-full", "ipx's database connections are all in use",
|
||||
'max_over_time(ipx_db_connections{job="ipx",state="idle"}[5m])', "lt", 1, "5m",
|
||||
"No idle connection for five minutes: requests and scans are waiting for one (ipx_db_wait_seconds)."),
|
||||
rule("5xx", "ipx is answering with server errors",
|
||||
'sum(increase(ipx_http_request_duration_seconds_count{job="ipx",status=~"5.."}[10m]))', "gt", 5, "0s",
|
||||
"More than five 5xx answers in ten minutes; ipx's log has a warning for each, with its trace."),
|
||||
rule("502", "ipx-web cannot reach ipx",
|
||||
'sum(increase(caddy_http_request_duration_seconds_count{job="ipx-web",code=~"502|504"}[10m]))', "gt", 5, "0s",
|
||||
"Caddy answered 502 or 504 for ipx more than five times in ten minutes: ipx is not answering it."),
|
||||
rule("jev", "ipx's calls to Jev are refused",
|
||||
'sum(increase(ipx_jev_requests_total{job="ipx",result=~"rate_limited|malformed"}[15m]))', "gt", 0, "0s",
|
||||
"TypeSafe refused calls as over its limits (#277) or malformed (#257): ipx is asking wrongly."),
|
||||
rule("signals-stale", "Signals have stopped", 'max(ipx_signals_last_pass_age_seconds{job="ipx"})', "gt", 900, "5m",
|
||||
"The Signals judge has not finished a pass for 15 minutes; its signals trace shows which Jev call holds it."),
|
||||
# Only once it has been up 13 hours: its rounds come in bursts about twelve hours apart, and the
|
||||
# count starts again at each restart, so a deploy would read as stopped.
|
||||
rule("directory-refresh", "The directory has stopped checking feeds",
|
||||
'sum(increase(directory_feed_checks_total{job="ipx-directory"}[13h])) < 1'
|
||||
' and on() max(directory_uptime_seconds{job="ipx-directory"}) > 46800', "lt", 1, "30m",
|
||||
"No feed checked in 13 hours; each is due every 12.", window=46800),
|
||||
# Tower, which everything shares: a stall here is every request slow (2026-10-18, load 160).
|
||||
rule("load", "Tower is overloaded", 'max(node_load5{job="node"})', "gt", 48, "10m",
|
||||
"Load over 48 on twelve threads for ten minutes: builds, or processes blocked on a disk."),
|
||||
rule("memory", "Tower is out of memory", 'min(node_memory_MemAvailable_bytes{job="node"})', "lt", 2e9, "5m",
|
||||
"Under 2 GB available, with no swap: next comes stalling on page faults, then the OOM killer."),
|
||||
# The monitoring itself (#366): nothing else watches it.
|
||||
rule("monitoring-down", "Part of the monitoring is down", 'min by (job) (up{job=~"prometheus|tempo|loki|alloy"})',
|
||||
"lt", 1, "5m", "Prometheus cannot reach Tempo, Loki or Alloy."),
|
||||
rule("tempo-blocks", "Tempo cannot read its stored traces",
|
||||
'(max(tempodb_blocklist_tenant_index_age_seconds) > bool 900) + (max(tempodb_blocklist_length) < bool 1)',
|
||||
"gt", 0, "10m",
|
||||
"Tempo's look over its stored blocks is failing (2026-10-25: one empty block); searches see only the last 15 "
|
||||
"minutes. Its log says which block: move it out of /mnt/user/logs/tempo/blocks/single-tenant."),
|
||||
rule("logs-stopped", "No lines from iPX reach Loki", 'sum(count_over_time({container="iPX"}[3h]))', "lt", 1, "0s",
|
||||
"Loki has nothing from iPX in three hours: Alloy is not shipping it, or the container is down.",
|
||||
ds=LOKI, window=10800, no_data="Alerting"),
|
||||
]
|
||||
|
||||
print(json.dumps({"apiVersion": 1, "groups": [
|
||||
{"orgId": 1, "name": "ipx", "folder": "iPX", "interval": "1m", "rules": rules},
|
||||
]}, indent=2))
|
||||
@@ -1,14 +1,14 @@
|
||||
"""The iPX dashboard in Grafana: the three containers of the Arcane project ipx (ipx, ipx-web,
|
||||
ipx-directory) and Tower under them, from Prometheus, Loki (their logs, shipped by Alloy) and Tempo.
|
||||
"""The iPX dashboard in Grafana: the four containers of the Arcane project ipx (ipx, ipx-web,
|
||||
ipx-directory, ipx-play) and Tower under them, from Prometheus, Loki (their logs, shipped by Alloy) and Tempo.
|
||||
|
||||
python3 grafana/dashboard.py > /mnt/fast/arcane/projects/monitoring/grafana-provisioning/dashboards/ipx.json
|
||||
|
||||
Grafana reads that file on its own within a minute; edits made in Grafana are refused. Rows go
|
||||
top-down as a request does: an overview, Tower, then each container (ipx-web, ipx's API, its
|
||||
worker and its process, the directory), then traces and logs. Prometheus scrapes ipx's /metrics
|
||||
worker and its process, the directory, ipx-play), then traces and logs. Prometheus scrapes ipx's /metrics
|
||||
(job ipx, #194) and Tower's node exporter (job node); nothing scrapes the containers themselves,
|
||||
so ipx-web and the directory are read from their logs. The log panels read fields: ipx's
|
||||
(web.rs access_log, ipc.rs log_event), the directory's and Caddy's; renaming one has to be
|
||||
(ipc.rs log_event), ipx's metrics (metrics.rs), the directory's and Caddy's; renaming one has to be
|
||||
matched here. `dashboard.py queries` prints each Loki query, to try against Loki.
|
||||
"""
|
||||
import json, sys
|
||||
@@ -18,11 +18,13 @@ TEMPO = {"type": "tempo", "uid": "${tempo}"}
|
||||
PROM = {"type": "prometheus", "uid": "${prometheus}"}
|
||||
SEL = '{container="iPX"}'
|
||||
# ipx logs one JSON object a line (IPX_LOG_FORMAT=json). Each scan and download event carries its
|
||||
# fields (ev, feed, new, bytes, msg, error.type, ...); each request its method, path, route, status
|
||||
# and duration_ms. Events are logged under ipx::scan, the healthcheck's status under ipx::io, so
|
||||
# the filter is on the ev field, not the target.
|
||||
# fields (ev, feed, new, bytes, msg, error.type, ...); a failed request its method, path, route,
|
||||
# status and duration_ms. Events are logged under ipx::scan, `ipxd status`'s answer under ipx::io,
|
||||
# so the filter is on the ev field, not the target.
|
||||
EV = SEL + ' |= "\\"ev\\":\\"" | json | __error__="" | ev != ""'
|
||||
HTTP = SEL + ' |= "\\"target\\":\\"ipx::http\\"" | json | __error__="" | path != "/api/events"'
|
||||
# Each request ipx answers, the event stream aside, as a histogram (#364): its line in the log is
|
||||
# debug, so only a failed request's is in Loki.
|
||||
REQ = "ipx_http_request_duration_seconds"
|
||||
BAD = SEL + ' | json | __error__="" | level =~ "WARN|ERROR"'
|
||||
TEXT = ' | line_format "{{.level}} {{.target}}: {{.message}}"'
|
||||
# Every production span: ipx-web's, ipx's and the directory's, one trace from the page to the
|
||||
@@ -39,7 +41,9 @@ WEB = ('{container="ipx-web"} |= "http.log.access" | json | __error__=""'
|
||||
' | request_uri !~ "/(api|media)/.*" | request_remote_ip != "127.0.0.1"') # not its healthcheck
|
||||
# Less what Caddy says at every start, by design: no admin endpoint, and no HTTPS or HTTP/3
|
||||
# behind the tunnel; and its goodbye on a restart.
|
||||
OTHERS_BAD = ('{container=~"ipx-web|ipx-directory"} | json | __error__="" | level =~ "(?i)warn|error"'
|
||||
# ipx-play's (#447): a line a request (method, path, status, duration_ms, token), and one a share.
|
||||
PLAY_REQ = "play_http_request_duration_seconds"
|
||||
OTHERS_BAD = ('{container=~"ipx-web|ipx-directory|ipx-play"} | json | __error__="" | level =~ "(?i)warn|error"'
|
||||
' | msg !~ "admin endpoint disabled|automatic HTTPS is completely disabled|HTTP/[23] skipped.*|exiting; byeee.*"')
|
||||
|
||||
|
||||
@@ -70,9 +74,11 @@ def loki(expr, ref="A", legend=None, instant=False, kind=None):
|
||||
return t
|
||||
|
||||
|
||||
def prom(expr, ref="A", legend=None):
|
||||
def prom(expr, ref="A", legend=None, instant=False):
|
||||
# ipx's /metrics (#194), scraped by Prometheus as job ipx.
|
||||
t = {"refId": ref, "datasource": PROM, "expr": expr}
|
||||
if instant:
|
||||
t["instant"], t["range"], t["format"] = True, False, "table"
|
||||
if legend:
|
||||
t["legendFormat"] = legend
|
||||
return t
|
||||
@@ -105,7 +111,7 @@ def ts(title, targets, x, w=12, h=8, unit="short", bars=False, stack=False, desc
|
||||
"tooltip": {"mode": "multi", "sort": "desc"}})
|
||||
|
||||
|
||||
def table(title, targets, x, w=12, h=8, rename=None, sort=None, desc=""):
|
||||
def table(title, targets, x, w=12, h=8, rename=None, sort=None, desc="", unit=None):
|
||||
return panel("table", title, w, h, x, targets, description=desc,
|
||||
transformations=[{"id": "labelsToFields", "options": {"mode": "columns"}},
|
||||
{"id": "organize", "options": {
|
||||
@@ -113,7 +119,7 @@ def table(title, targets, x, w=12, h=8, rename=None, sort=None, desc=""):
|
||||
"service_name": True},
|
||||
"renameByName": rename or {}}}],
|
||||
options={"showHeader": True, "sortBy": sort or []},
|
||||
fieldConfig={"defaults": {}, "overrides": []})
|
||||
fieldConfig={"defaults": {"unit": unit} if unit else {}, "overrides": []})
|
||||
|
||||
|
||||
GOOD_BAD = [{"color": "green", "value": None}, {"color": "orange", "value": 1}]
|
||||
@@ -145,7 +151,7 @@ stat("Page and API errors", loki(f'sum(count_over_time({WEB_ALL} | status >= 500
|
||||
thresholds=GOOD_BAD, desc="Answers of 500 and up from ipx-web, for the page or passed on from ipx: 502 is ipx not answering.")
|
||||
stat("Warnings and errors", loki(f'sum(count_over_time({BAD} [$__range])) + sum(count_over_time({OTHERS_BAD} [$__range])) or vector(0)', instant=True), 20,
|
||||
thresholds=[{"color": "green", "value": None}, {"color": "orange", "value": 1}, {"color": "red", "value": 50}],
|
||||
desc="All three containers, in the time range.")
|
||||
desc="All four containers, in the time range.")
|
||||
y += 4
|
||||
stat("Feed failures", loki(f'sum(count_over_time({EV} | ev="feed_error" [$__range])) or vector(0)', instant=True), 0,
|
||||
thresholds=GOOD_BAD, desc="Failed feed checks in the time range.")
|
||||
@@ -194,19 +200,27 @@ ts("A click to the list shown", [loki(f'quantile_over_time(0.95, {LIST} | unwrap
|
||||
table("Slowest lists by view", [loki(f'topk(10, max_over_time({LIST} | unwrap ms [$__range]) by (feed, filter))', instant=True)],
|
||||
12, rename={"feed": "View", "filter": "Filter", "Value": "Slowest (ms)"}, sort=[{"displayName": "Slowest (ms)", "desc": True}])
|
||||
y += 8
|
||||
# Swipes in the reader and what came of them, and a reader that would not scroll (#384).
|
||||
SWIPE = SEL + ' |= "\\"ev\\":\\"swipe\\"" | json | __error__=""'
|
||||
STUCK = SEL + ' |= "\\"ev\\":\\"scroll_stuck\\"" | json | __error__=""'
|
||||
ts("Swipes in the reader", [loki(f'sum by (msg) (count_over_time({SWIPE} [$__interval]))', legend="{{msg}}"),
|
||||
loki(f'sum(count_over_time({STUCK} [$__interval]))', ref="B", legend="would not scroll")],
|
||||
0, w=24, bars=True, stack=True,
|
||||
desc="next, prev and list turned the item; end bounced off the last one loaded; short was too short or slow to count. Would not scroll is a drag with room to move the reader that left it where it was: its line in the log says what lay over it.")
|
||||
y += 8
|
||||
|
||||
# ---- ipx: the API it answers behind ipx-web
|
||||
row("ipx: API")
|
||||
ts("Requests by status", [loki(f'sum by (status) (count_over_time({HTTP} [$__interval]))', legend="{{status}}")],
|
||||
ts("Requests by status", [prom(f'sum by (status) (increase({REQ}_count{{job="ipx"}}[$__interval]))', legend="{{status}}")],
|
||||
0, bars=True, stack=True, desc="The event stream the page keeps open is left out.")
|
||||
ts("Response time", [loki(f'quantile_over_time(0.5, {HTTP} | unwrap duration_ms [$__interval]) by ()', legend="median"),
|
||||
loki(f'quantile_over_time(0.95, {HTTP} | unwrap duration_ms [$__interval]) by ()', ref="B", legend="95th percentile"),
|
||||
loki(f'max_over_time({HTTP} | unwrap duration_ms [$__interval]) by ()', ref="C", legend="slowest")],
|
||||
12, unit="ms")
|
||||
ts("Response time", [prom(f'histogram_quantile(0.5, sum by (le) (rate({REQ}_bucket{{job="ipx"}}[$__rate_interval])))', legend="median"),
|
||||
prom(f'histogram_quantile(0.95, sum by (le) (rate({REQ}_bucket{{job="ipx"}}[$__rate_interval])))', ref="B", legend="95th percentile"),
|
||||
prom(f'histogram_quantile(0.99, sum by (le) (rate({REQ}_bucket{{job="ipx"}}[$__rate_interval])))', ref="C", legend="99th percentile")],
|
||||
12, unit="s", desc="From ipx_http_request_duration_seconds, whose buckets run from 5 ms to 10 s; the slowest requests themselves are in Tempo.")
|
||||
y += 8
|
||||
table("Slowest routes", [loki(f'topk(15, avg_over_time({HTTP} | route != "" | unwrap duration_ms [$__range]) by (method, route))', instant=True)],
|
||||
0, w=8, rename={"method": "Method", "route": "Route", "Value": "Average ms"}, sort=[{"displayName": "Average ms", "desc": True}])
|
||||
table("Busiest routes", [loki(f'topk(15, sum by (method, route) (count_over_time({HTTP} | route != "" [$__range])))', instant=True)],
|
||||
table("Slowest routes", [prom(f'topk(15, sum by (method, route) (increase({REQ}_sum{{job="ipx",route!=""}}[$__range])) / sum by (method, route) (increase({REQ}_count{{job="ipx",route!=""}}[$__range])))', instant=True)],
|
||||
0, w=8, unit="s", rename={"method": "Method", "route": "Route", "Value": "Average"}, sort=[{"displayName": "Average", "desc": True}])
|
||||
table("Busiest routes", [prom(f'topk(15, sum by (method, route) (increase({REQ}_count{{job="ipx",route!=""}}[$__range])))', instant=True)],
|
||||
8, w=8, rename={"method": "Method", "route": "Route", "Value": "Requests"}, sort=[{"displayName": "Requests", "desc": True}])
|
||||
ts("Open connections", [prom('max(ipx_http_requests_in_flight{job="ipx"})', legend="requests in flight"),
|
||||
prom('max(ipx_event_listeners{job="ipx"})', ref="B", legend="live-update listeners"),
|
||||
@@ -214,6 +228,13 @@ ts("Open connections", [prom('max(ipx_http_requests_in_flight{job="ipx"})', lege
|
||||
prom('sum(increase(ipx_events_missed_total{job="ipx"}[$__rate_interval]))', ref="D", legend="live updates missed")],
|
||||
16, w=8, desc="Each open page holds one live-update listener. Missed updates mean a page fell behind and reloads its lists.")
|
||||
y += 8
|
||||
q95 = lambda m, by="": f'histogram_quantile(0.95, sum by (le{by}) (rate({m}_bucket{{job="ipx"}}[$__rate_interval])))'
|
||||
ts("Database", [prom(q95("ipx_db_wait_seconds"), legend="wait for a connection, 95th"),
|
||||
prom(q95("ipx_db_query_seconds", ", kind"), ref="B", legend="{{kind}}, 95th")],
|
||||
0, unit="s", desc="A statement's time once it has a connection, and the wait for one (hand-written SQL only, #367). A wait that grows with the queries flat is the pool full.")
|
||||
ts("The page's timings", [prom(f'histogram_quantile(0.95, sum by (le, ev) (rate(ipx_page_seconds_bucket{{job="ipx"}}[1h])))', legend="{{ev}}, 95th")],
|
||||
12, unit="s", desc="What the browser timed (#369): list_shown a click to its list drawn, play_start play to sound, stall a wait mid-play. Over the hour, as there are few.")
|
||||
y += 8
|
||||
|
||||
# ---- ipx: the worker, one job at a time: scans, downloads, DeadAir
|
||||
row("ipx: worker")
|
||||
@@ -230,7 +251,7 @@ ts("Waiting", [prom('ipx_worker_queue{job="ipx"}', legend="commands for the work
|
||||
prom('max(ipx_feed_retries{job="ipx"})', ref="C", legend="feeds to retry a lookup"),
|
||||
prom('max(ipx_deadair_backlog{job="ipx"})', ref="D", legend="files for DeadAir")],
|
||||
0, w=8, desc="A queue that only grows means the worker is stuck or behind.")
|
||||
ts("Feeds checked and new items", [loki(f'sum(sum_over_time({EV} | ev="scan_done" | unwrap feeds [$__interval]))', legend="feeds checked"),
|
||||
ts("Feeds checked and new items", [prom('sum(increase(ipx_feed_fetches_total{job="ipx"}[$__interval]))', legend="feeds checked"),
|
||||
loki(f'sum(sum_over_time({EV} | ev="feed_done" | unwrap new [$__interval]))', ref="B", legend="new items")],
|
||||
8, w=8, bars=True, desc="Feeds that were due and fetched; the rest were skipped as not due.")
|
||||
ts("Downloads", [loki(f'sum(count_over_time({EV} | ev="download_done" [$__interval]))', legend="saved"),
|
||||
@@ -375,7 +396,33 @@ ts("Feed bytes fetched", [prom('rate(ipx_feed_bytes_total{job="ipx"}[$__rate_int
|
||||
12, unit="Bps", desc="Feeds are read whole, so this is what scans and checks allocate; set it beside memory.")
|
||||
y += 8
|
||||
|
||||
# ---- Traces: all three send them to Tempo (#213)
|
||||
# ---- ipx-play: the page a shared episode opens, public at play.ipxreader.app (#447), from its
|
||||
# /metrics on a port apart from the tunnel's (#458)
|
||||
row("ipx-play")
|
||||
stat("ipx-play", prom('max(up{job="ipx-play"})'), 0, thresholds=[{"color": "red", "value": None}, {"color": "green", "value": 1}],
|
||||
desc="Prometheus reaching ipx-play's /metrics: 1 is up.")["fieldConfig"]["defaults"]["mappings"] = [
|
||||
{"type": "value", "options": {"0": {"text": "down"}, "1": {"text": "up"}}}]
|
||||
stat("Episodes shared", prom('sum(increase(play_shares_total{job="ipx-play"}[$__range])) or vector(0)', instant=True), 4,
|
||||
desc="Links ipx servers asked for, the same episode again included.")
|
||||
stat("Pages opened", prom(f'sum(increase({PLAY_REQ}_count{{job="ipx-play",route="/e/{{id}}",status="200"}}[$__range])) or vector(0)', instant=True), 8,
|
||||
desc="Shared episodes someone opened, a link preview's fetch included.")
|
||||
stat("Links not found", prom(f'sum(increase({PLAY_REQ}_count{{job="ipx-play",status="404"}}[$__range])) or vector(0)', instant=True), 12,
|
||||
desc="A link mistyped, or someone trying addresses.")
|
||||
stat("Server errors", prom(f'sum(increase({PLAY_REQ}_count{{job="ipx-play",status=~"5.."}}[$__range])) or vector(0)', instant=True), 16,
|
||||
thresholds=GOOD_BAD, desc="Mostly its database not answering; its log has an error for each.")
|
||||
stat("Requests refused", prom(f'sum(increase({PLAY_REQ}_count{{job="ipx-play",status=~"401|422"}}[$__range])) or vector(0)', instant=True), 20,
|
||||
thresholds=GOOD_BAD, desc="A share without a valid token (401), or of an episode that looked private (422): ipx's own check should have kept it.")
|
||||
y += 4
|
||||
ts("Requests by route", [prom(f'sum by (route, status) (increase({PLAY_REQ}_count{{job="ipx-play"}}[$__interval]))', legend="{{status}} {{route}}")],
|
||||
0, w=8, bars=True, stack=True, desc="An empty route is an address no route has.")
|
||||
ts("Response time", [prom(f'histogram_quantile(0.5, sum by (le) (rate({PLAY_REQ}_bucket{{job="ipx-play"}}[$__rate_interval])))', legend="median"),
|
||||
prom(f'histogram_quantile(0.95, sum by (le) (rate({PLAY_REQ}_bucket{{job="ipx-play"}}[$__rate_interval])))', ref="B", legend="95th percentile")],
|
||||
8, w=8, unit="s")
|
||||
ts("ipx-play memory", [prom('play_process_resident_bytes{job="ipx-play"}', legend="resident")], 16, w=8, unit="bytes",
|
||||
desc="mimalloc, as the directory's: a rise that never comes down is a leak.")
|
||||
y += 8
|
||||
|
||||
# ---- Traces: all four send them to Tempo (#213)
|
||||
row("Traces")
|
||||
for x, title, q in [(0, "Recent", TRACES + "}"),
|
||||
(8, "Slow (over 2s)", TRACES + " && duration > 2s}"),
|
||||
@@ -389,7 +436,7 @@ y += 10
|
||||
# ---- Logs
|
||||
row("Logs")
|
||||
panel("logs", "ipx: warnings and errors", 12, 10, 0, [loki(BAD + TEXT)], options=LOG_OPTS)
|
||||
panel("logs", "ipx-web and ipx-directory: warnings and errors", 12, 10, 12,
|
||||
panel("logs", "ipx-web, ipx-directory and ipx-play: warnings and errors", 12, 10, 12,
|
||||
[loki(OTHERS_BAD + ' | line_format "{{.container}} {{.level}}: {{.message}}{{.msg}}"')], options=LOG_OPTS)
|
||||
y += 10
|
||||
panel("logs", "ipx log", 24, 12, 0,
|
||||
@@ -398,6 +445,37 @@ panel("logs", "ipx log", 24, 12, 0,
|
||||
description="Without the event stream's requests, not-due skips and healthcheck status calls.", options=LOG_OPTS)
|
||||
y += 12
|
||||
|
||||
# ---- Coverage: what the tests reach (#390, #394). scripts/coverage.sh writes each run's numbers
|
||||
# for node-exporter's textfile collector, so they are job node's, and stay as the last run left them.
|
||||
COV = 'ipx_coverage_lines{job="node"'
|
||||
def cov(suite, by=""):
|
||||
"""Lines covered over lines there are, for a suite, summed over its parts or by `by`."""
|
||||
group = f" by ({by})" if by else ""
|
||||
return (f'sum{group} ({COV},suite="{suite}",state="covered"}}) / '
|
||||
f'sum{group} ({COV},suite="{suite}",state="total"}})')
|
||||
row("Coverage")
|
||||
COV_STEPS = [{"color": "red", "value": None}, {"color": "orange", "value": 0.6}, {"color": "green", "value": 0.8}]
|
||||
stat("Rust, cargo tests", prom(cov("cargo")), 0, unit="percentunit", thresholds=COV_STEPS,
|
||||
desc="Lines of ipx, ipx-directory and ipx-feed the cargo tests reach: sh scripts/coverage.sh. The tests' own code counts too, as it runs whole, so this reads higher than the code alone would.")
|
||||
stat("Rust, browser tests", prom(cov("browser")), 4, unit="percentunit", thresholds=COV_STEPS,
|
||||
desc="Lines of ipx and ipx-directory the browser suite reaches through the API, against an instrumented build: sh scripts/coverage.sh api.")
|
||||
stat("Page, browser tests", prom(cov("page")), 8, unit="percentunit", thresholds=COV_STEPS,
|
||||
desc="Lines of web/src the browser suite runs: npm run coverage:page, or coverage.sh api.")
|
||||
stat("Since the last coverage run", prom('time() - max(ipx_coverage_run_timestamp_seconds{job="node"})'), 12, unit="s",
|
||||
desc="The newest of the three suites' reports. Each suite's own is in the table beside it.")
|
||||
table("Last run of each suite", [prom('time() - ipx_coverage_run_timestamp_seconds{job="node"}', instant=True)], 16, w=8, h=4,
|
||||
rename={"Value": "ago", "suite": "suite"}, unit="s")
|
||||
y += 4
|
||||
ts("Lines covered", [prom(cov("cargo"), legend="Rust, cargo tests"), prom(cov("browser"), ref="B", legend="Rust, browser tests"),
|
||||
prom(cov("page"), ref="C", legend="Page, browser tests")],
|
||||
0, w=12, unit="percentunit", desc="As each suite's last run found them: a step is a run that found more, or less.")
|
||||
table("By part", [prom(cov("cargo", "part"), ref="A", instant=True), prom(cov("browser", "part"), ref="B", instant=True),
|
||||
prom(cov("page", "part"), ref="C", instant=True)],
|
||||
12, w=12, unit="percentunit", rename={"Value #A": "cargo tests", "Value #B": "browser tests", "Value #C": "page"},
|
||||
desc="Each crate's lines, and the page's, by the suite that reached them. A crate has more lines under the cargo tests: their own code, which a build for the browser suite leaves out.")
|
||||
panels[-1]["transformations"].insert(0, {"id": "merge", "options": {}})
|
||||
y += 8
|
||||
|
||||
dash = {
|
||||
"uid": "ipx", "title": "iPX", "tags": ["ipx"], "timezone": "browser", "schemaVersion": 39,
|
||||
"time": {"from": "now-24h", "to": "now"}, "refresh": "1m", "editable": True,
|
||||
|
||||
8
ios/.gitignore
vendored
Normal file
8
ios/.gitignore
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
*.xcodeproj/
|
||||
build/
|
||||
DerivedData/
|
||||
Resources/Info.plist
|
||||
.envrc
|
||||
Resources/iPX.entitlements
|
||||
Widgets/Info.plist
|
||||
Widgets/iPXWidgets.entitlements
|
||||
30
ios/CHANGELOG.md
Normal file
30
ios/CHANGELOG.md
Normal file
@@ -0,0 +1,30 @@
|
||||
# Changelog
|
||||
|
||||
The iOS app, `ios/`. Format: [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
### Added
|
||||
|
||||
- Up Next plays on with the screen locked: the app is told what follows and starts it when an episode ends, without waiting for the page (#473).
|
||||
- iPX on the iPhone: the page in an app, playing through the phone's own player, so an episode plays on with the screen locked and the lock screen, Control Center and a car's buttons drive it (#418).
|
||||
- Player token (Settings, This device): an ipx API token, kept in the keychain, which the player, the badge and CarPlay send to `https://ipxapi.sdf1.net` instead of using the page's sign-in cookie, so playback does not stop when that cookie expires (#419).
|
||||
- The icon shows All Subscriptions' unread count, as the page's badge does: set when the app comes forward, after an item is marked read, and by a refresh iOS runs now and then while the app is closed, so it can be hours behind (#421). Reading in the app and new items arriving while it is open set it at once, and while an episode plays it is checked every quarter of an hour.
|
||||
- Episodes kept on the phone, with the page's Keep button, play with no connection: the app fetches the file in the background, even with the app closed, and plays it from the phone; where you got to and what you finished offline is sent once the connection is back. Signing out removes them (#425).
|
||||
- A sleep timer in the player, kept by the phone, so it pauses on time with the screen locked, fading out over the last ten seconds; and an AirPlay button beside it, for another speaker or a TV (#429).
|
||||
- iPX opens with no connection, showing the lists it last had, as it does in a browser: the page's service worker now runs in the app. This is for `ipodderx.sdf1.net` only, and links to other sites open in Safari (#435).
|
||||
- Siri and Shortcuts: "Resume iPX", "Pause iPX", "Skip forward in iPX", "Skip back in iPX", "Set a sleep timer in iPX" and "How many unread in iPX", with nothing to set up, and each an action in Shortcuts. Resume works with the app closed, carrying on with the last episode from where you left off (#427).
|
||||
- Settings in the app has a This device section: the server the app opens and the player token, with a button to make one (#445).
|
||||
- On an iPhone the app stays upright; on an iPad it turns every way, and works in Split View and Slide Over (#434, #445).
|
||||
- CarPlay: In Progress, Unread and Shows on the car's screen, an episode playing from where you left off and saving its place as it does on the phone, and the car's Now Playing for the rest. It lists only shows and episodes with a downloaded audio file, and the phone's player bar takes up what the car plays (#420, #442, #443).
|
||||
- A widget for the home screen and lock screen: the unread count, and the episode you were on with a button to resume or pause it (#428).
|
||||
- The icon's count stays right with the app closed: ipx sends it to the phone when it changes, once its `[push]` is set up, in place of waiting for iOS's background refresh (#433).
|
||||
- Presses are felt: a light tap on play, pause, flag and pull to refresh, a success on marking read, a warning with an error (#430).
|
||||
|
||||
### Changed
|
||||
|
||||
- The player, the badge and CarPlay talk to ipx in protobuf rather than JSON, which is smaller and quicker to read; they need an ipx with protobuf (#465), and a place saved offline by an older build is still sent (#469).
|
||||
|
||||
### Fixed
|
||||
|
||||
- A question the page asks before going ahead (deleting a file, sharing an address that carries your access) is shown with Cancel and OK, rather than answered Cancel at once with nothing shown (#453).
|
||||
156
ios/README.md
Normal file
156
ios/README.md
Normal file
@@ -0,0 +1,156 @@
|
||||
# iPX for iOS
|
||||
|
||||
ipx's page in an app, with the audio played by the phone rather than the page: it plays on with
|
||||
the screen locked, and the lock screen, Control Center, headphones and a car over Bluetooth or USB
|
||||
drive it. Everything you see is the page, as on the home screen; the app adds the player behind it.
|
||||
|
||||
## Build it
|
||||
|
||||
On a Mac with Xcode:
|
||||
|
||||
```sh
|
||||
brew install xcodegen
|
||||
cd ios && xcodegen generate && open iPX.xcodeproj
|
||||
```
|
||||
|
||||
Set your team under Signing & Capabilities, pick your phone, and run. The project is generated
|
||||
from `project.yml`, so `.xcodeproj` is not in git: edit the yml. Nothing on Tower can build it.
|
||||
|
||||
A free personal team works, but a build on it expires after seven days and is reinstalled from
|
||||
Xcode. A paid account ($99 a year) makes that a year and adds TestFlight. Playing with the screen
|
||||
locked is a plist key (`UIBackgroundModes: audio`), not an entitlement, so it works on either.
|
||||
CarPlay does not: Apple grants `com.apple.developer.carplay-audio` on request (granted for team
|
||||
8743L8D729 on 2026-11-09), and the App ID needs it ticked under Additional Capabilities.
|
||||
|
||||
## Which server
|
||||
|
||||
The first launch asks, with `https://ipodderx.sdf1.net` filled in; a bare host gets `https://`.
|
||||
Change it in the page's Settings, under This device, or from the alert that comes up when the
|
||||
server cannot be reached. Plain `http://` only on your own network
|
||||
(`http://192.168.1.130:8099`), which is all `NSAllowsLocalNetworking` allows. Sign in on the page
|
||||
that follows, through Cloudflare Access or ipx's own form; the cookies that leaves are what the
|
||||
player uses (`CookieBridge`). `-ipx.server <url>` as a launch argument overrides it for a run. `-ipx.noBadgePrompt YES` skips the badge permission dialog, which covers the page in a simulator run nobody is there to tap.
|
||||
|
||||
### Opening with no connection
|
||||
|
||||
The page's service worker, which keeps iPX readable offline in a browser (#389), runs in the app
|
||||
only for a server in `WKAppBoundDomains` in `project.yml` (#435): `ipodderx.sdf1.net`, with
|
||||
Cloudflare Access's and Authentik's hosts for signing in. For those the web view is app-bound, and
|
||||
iPX opens without a connection from the second launch on (the first installs the worker). What
|
||||
that costs:
|
||||
|
||||
* The web view goes nowhere outside the list. A link to anywhere else opens in Safari; an
|
||||
embedded player from another site in an item's text may not load.
|
||||
* A server not in the list still opens, but WebKit then cuts the bridge, so the page plays its own
|
||||
audio: no lock screen, no CarPlay, nothing kept on the phone. That includes the LAN address.
|
||||
Another server means adding it to the list (ten at most) and building again.
|
||||
* Signing in that leaves those hosts, through another identity provider, opens in Safari and does
|
||||
not come back. Add its host too.
|
||||
|
||||
A debug build adds `localhost`, so a simulator can test the worker against a page served on the Mac.
|
||||
|
||||
## Player token
|
||||
|
||||
The player normally rides on the web view's cookie, which ends with Cloudflare Access's session.
|
||||
To make it outlast that, open Settings in the app and, under This device, press Make, or paste an
|
||||
ipx API token (Settings, API tokens), with the address to send it to, `https://ipxapi.sdf1.net`. The token is
|
||||
kept in the keychain (`Credentials.swift`). The player's audio, positions, read flags, artwork and
|
||||
the unread badge go to that address with `Authorization: Bearer`; the page still signs in as it
|
||||
did, at the server address. Without a token the player sends only the cookie, as before.
|
||||
|
||||
`ipxapi.sdf1.net` is the tunnel's route to the same ipx-web as `ipodderx.sdf1.net`, with no Access
|
||||
application in front, which would turn a bearer header away. Cloudflare's firewall (a custom rule
|
||||
on the `sdf1.net` zone) blocks every request to it except a bearer-token one for `/api/feeds`,
|
||||
`/api/me`, `/api/art`, `/api/entries/*` and `/media/*`; ipx still checks the token.
|
||||
|
||||
## How it hangs together
|
||||
|
||||
| | |
|
||||
|---|---|
|
||||
| `Sources/App.swift` | the app and its one scene |
|
||||
| `Sources/WebViewController.swift` | the `WKWebView`, the server question, a page that will not load |
|
||||
| `Sources/Bridge.swift` | the messages to and from `web/src/native.ts` |
|
||||
| `Sources/Playback.swift` | `AVPlayer`, the audio session, now playing, the remote commands, saving positions |
|
||||
| `Sources/CookieBridge.swift` | the web view's cookies into `HTTPCookieStorage.shared`, for the player |
|
||||
| `Sources/Server.swift` | which server |
|
||||
| `Sources/Intents.swift`, `Shared/PlayPauseIntent.swift` | Siri and Shortcuts (#427) |
|
||||
| `Sources/API.swift`, `Sources/Wire` | ipx's API without the page, in protobuf (#469): `Wire` is generated from `crates/wire/proto` by `scripts/proto.sh`, and committed |
|
||||
| `Sources/LastPlayed.swift` | the last episode and its place, so Resume works with the app closed |
|
||||
| `Widgets/`, `Shared/Snapshot.swift`, `Sources/WidgetFeed.swift` | the widget (#428), and what the app writes for it |
|
||||
|
||||
### Siri and Shortcuts
|
||||
|
||||
With nothing to set up, Siri answers "Resume iPX" (or "Play iPX"), "Pause iPX", "Skip forward
|
||||
in iPX", "Skip back in iPX", "Set a sleep timer in iPX" and "How many unread in iPX"; each is also
|
||||
an action in the Shortcuts app. The playback ones are `AudioPlaybackIntent`s, which iOS runs in the
|
||||
app and lets start audio with the phone locked. Resume carries on with what is loaded, or else the
|
||||
last episode from where it got to (`LastPlayed`), which plays without the page: opening the app
|
||||
afterwards shows the page with nothing in its player, while the lock screen has it.
|
||||
|
||||
### The widget
|
||||
|
||||
Small and medium on the home screen, circular and rectangular on the lock screen: All
|
||||
Subscriptions' unread count and the last episode, with a Resume or Pause button (the same
|
||||
`PlayPauseIntent`). The widget has no sign-in and no player, so it draws what the app last wrote
|
||||
to the App Group `group.net.sdf1.ipx` (`Snapshot`), and the app asks WidgetKit to redraw on a play,
|
||||
a pause, a new episode or a new count, not on every position saved, since iOS rations redraws.
|
||||
With the app closed, ipx's push (#433) carries `content-available`, which wakes the app for a
|
||||
moment to give the widget the new count; iOS rations those wakes too, so the widget can trail the
|
||||
icon. `Shared/` is built into both targets, with `WIDGET` set in the widget's, where the player
|
||||
does not exist.
|
||||
|
||||
## The messages
|
||||
|
||||
The page's half is `web/src/native.ts`, and `node tests/native-bridge.ts` holds it to these. A
|
||||
change to them is a change to both halves in one commit, and the page has to be deployed before
|
||||
an app that needs it.
|
||||
|
||||
Page to app, on `webkit.messageHandlers.ipx`:
|
||||
|
||||
| | |
|
||||
|---|---|
|
||||
| `{t:"ready", version, rate, volume}` | the bridge is in |
|
||||
| `{t:"load", url, enc, feedId, guid, title, feedTitle, artwork, position, duration, rate, volume}` | the element's `src` was set to an audio file |
|
||||
| `{t:"play"}` `{t:"pause"}` `{t:"stop"}` | |
|
||||
| `{t:"seek", to}` `{t:"rate", v}` `{t:"volume", v}` | |
|
||||
| `{t:"position", url}` | save where we are; a path, not a time, since a page frozen in the background holds an old one |
|
||||
| `{t:"keep", url}` `{t:"unkeep", url}` | keep a file on the phone, or let it go: the page's path for it, `/media/42` or `/media/42/deadair` (#425) |
|
||||
| `{t:"clearKept"}` | signing out: everything kept goes |
|
||||
| `{t:"sleep", secs}` | pause after `secs`, fading out over the last ten; 0 turns the timer off (#429) |
|
||||
| `{t:"unread", n}` | All Subscriptions' unread count, whenever the page's list is drawn with a new one: the icon's badge (#421) |
|
||||
| `{t:"haptic", kind}` | a press felt: `tap`, `done` (marked read) or `warn` (an error) (#430) |
|
||||
| `{t:"setServer", url}` | Settings' This device: open another server, which replaces the page (#445) |
|
||||
| `{t:"setToken", api, token?}` `{t:"clearToken"}` | Settings' This device: the player address, and a token to keep in the keychain or none (#445) |
|
||||
| `{t:"route", x, y, w, h}` | open the AirPlay picker, pointing at the page's button, in the web view's points (#429) |
|
||||
|
||||
App to page, as `window.ipxNative.on(...)`:
|
||||
|
||||
| | |
|
||||
|---|---|
|
||||
| `{t:"time", cur, dur}` | `timeupdate` |
|
||||
| `{t:"meta", dur}` | `loadedmetadata`: the page seeks to where you left off |
|
||||
| `{t:"state", playing}` | `play` or `pause` |
|
||||
| `{t:"ended"}` | |
|
||||
| `{t:"error", message}` | the page says so |
|
||||
| `{t:"kept", urls, keeping}` | what the phone holds and is fetching, at `ready` and after each change; sent whether or not anything plays |
|
||||
| `{t:"keepFailed", url, message}` | one that could not be kept, and why |
|
||||
| `{t:"host", can}` | at `ready`: what this build does itself (`sleep`, `route`, `settings`), so a page newer than the app offers only those |
|
||||
| `{t:"appSettings", server, api, tokenSet, saved}` | at `ready` and after a change: the app's server, player address and whether a player token is set, for Settings' This device (#445) |
|
||||
| `{t:"appSettingsError", message}` | a server or player address the app would not take |
|
||||
| `{t:"push", token, sandbox}` | at `ready` and when Apple gives a new one: the phone's push token, for the page to register with ipx (`POST /api/push/devices`) (#433) |
|
||||
| `{t:"sleep", left}` | at `ready`, while the timer runs: seconds left, for a page that reloaded |
|
||||
| `{t:"slept"}` | the timer ran out and playback paused |
|
||||
| `{t:"nowPlaying", feedId, guid, enc, title, feedTitle, artwork, playing, cur, dur}` | what the phone plays that the page did not choose: the car's pick, and at `ready` and on coming forward whatever is loaded; the page's player bar takes it up (#442) |
|
||||
| `{t:"replaced"}` | builds before `nowPlaying`: the car chose another episode, and the page's player bar closes without saving |
|
||||
|
||||
The app writes the position (every ten seconds and on pause) and marks an item read at 90%, from
|
||||
its own clock, so both happen while the page is frozen. Video stays in the page.
|
||||
|
||||
## Not yet
|
||||
|
||||
* Without the player token the cookie is the only credential: when Cloudflare Access's expires
|
||||
mid-drive, playback stops with "Sign in again", and opening the app signs in.
|
||||
* CarPlay (`CarScreen.swift`) reads ipx with the player's credentials and nothing else: launched
|
||||
by the car with the phone's app never opened since a restart, it has only cookies saved to disk,
|
||||
and without the player token a lapsed Access cookie shows "iPX can't reach your server". Its
|
||||
lists need a connection; a kept episode plays without one once it is picked.
|
||||
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"images" : [
|
||||
{ "filename" : "icon-1024.png", "idiom" : "universal", "platform" : "ios", "size" : "1024x1024" }
|
||||
],
|
||||
"info" : { "author" : "xcode", "version" : 1 }
|
||||
}
|
||||
BIN
ios/Resources/Assets.xcassets/AppIcon.appiconset/icon-1024.png
Normal file
BIN
ios/Resources/Assets.xcassets/AppIcon.appiconset/icon-1024.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 47 KiB |
1
ios/Resources/Assets.xcassets/Contents.json
Normal file
1
ios/Resources/Assets.xcassets/Contents.json
Normal file
@@ -0,0 +1 @@
|
||||
{ "info": { "author": "xcode", "version": 1 } }
|
||||
18
ios/Shared/PlayPauseIntent.swift
Normal file
18
ios/Shared/PlayPauseIntent.swift
Normal file
@@ -0,0 +1,18 @@
|
||||
import AppIntents
|
||||
|
||||
/// Play or pause (#427, #428): Siri, Shortcuts and the widget's button. An `AudioPlaybackIntent`,
|
||||
/// so iOS runs it in the app, which may start audio from the background, wherever it is asked
|
||||
/// from. Compiled into the widget too, which needs the type for its button but never runs it.
|
||||
struct PlayPauseIntent: AudioPlaybackIntent {
|
||||
static let title: LocalizedStringResource = "Play or Pause"
|
||||
static let description = IntentDescription(
|
||||
"Pauses what iPX is playing, or carries on with the last episode where you left off.")
|
||||
|
||||
@MainActor
|
||||
func perform() async throws -> some IntentResult {
|
||||
#if !WIDGET
|
||||
if Playback.shared.isPlaying { Playback.shared.pause() } else { Playback.shared.resumeLast() }
|
||||
#endif
|
||||
return .result()
|
||||
}
|
||||
}
|
||||
30
ios/Shared/Snapshot.swift
Normal file
30
ios/Shared/Snapshot.swift
Normal file
@@ -0,0 +1,30 @@
|
||||
import Foundation
|
||||
|
||||
/// What the widget shows (#428): the unread count and the episode you were last on. Written by
|
||||
/// the app into the App Group the two share, since the widget has no sign-in and no player of its
|
||||
/// own; it shows what the app last knew.
|
||||
struct Snapshot: Codable, Equatable {
|
||||
var unread: Int?
|
||||
var title: String?
|
||||
var feedTitle: String?
|
||||
var position: Double?
|
||||
var duration: Double?
|
||||
var playing = false
|
||||
|
||||
static let group = "group.net.sdf1.ipx"
|
||||
private static let key = "ipx.snapshot"
|
||||
|
||||
static func load(from defaults: UserDefaults? = UserDefaults(suiteName: group)) -> Snapshot {
|
||||
defaults?.data(forKey: key).flatMap { try? JSONDecoder().decode(Snapshot.self, from: $0) } ?? Snapshot()
|
||||
}
|
||||
|
||||
func save(to defaults: UserDefaults? = UserDefaults(suiteName: group)) {
|
||||
if let data = try? JSONEncoder().encode(self) { defaults?.set(data, forKey: Self.key) }
|
||||
}
|
||||
|
||||
/// How far through, 0 to 1, when both are known.
|
||||
var progress: Double? {
|
||||
guard let position, let duration, duration > 0 else { return nil }
|
||||
return min(1, max(0, position / duration))
|
||||
}
|
||||
}
|
||||
40
ios/Sources/API.swift
Normal file
40
ios/Sources/API.swift
Normal file
@@ -0,0 +1,40 @@
|
||||
import Foundation
|
||||
import SwiftProtobuf
|
||||
|
||||
/// Reading ipx without the page: the badge and CarPlay (#420), which has no web view. Signed in
|
||||
/// as the player is, by the credentials or else the cookies the web view left (CookieBridge).
|
||||
enum API {
|
||||
/// What ipx answers and reads in protobuf rather than JSON (#465, #469). The messages are
|
||||
/// `Sources/Wire`, generated from ipx's own `.proto` files by `scripts/proto.sh`.
|
||||
static let protobuf = "application/x-protobuf"
|
||||
|
||||
private static let session: URLSession = {
|
||||
let cfg = URLSessionConfiguration.default
|
||||
cfg.httpCookieStorage = .shared
|
||||
cfg.timeoutIntervalForRequest = 20
|
||||
return URLSession(configuration: cfg)
|
||||
}()
|
||||
|
||||
/// The body of a 200, or nil: not signed in, no server, no connection.
|
||||
static func get(_ path: String, accept: String? = nil) async -> Data? {
|
||||
guard let (data, resp) = await fetch(path, accept: accept), resp.statusCode == 200 else { return nil }
|
||||
return data
|
||||
}
|
||||
|
||||
/// A 200's body as message `M`, or nil. Nil too for an answer not in protobuf: a server from
|
||||
/// before #465 answers JSON whatever it is asked, and JSON can read as a message of nothing.
|
||||
static func get<M: SwiftProtobuf.Message>(_ type: M.Type, _ path: String) async -> M? {
|
||||
guard let (data, resp) = await fetch(path, accept: protobuf), resp.statusCode == 200,
|
||||
resp.value(forHTTPHeaderField: "Content-Type")?.hasPrefix(protobuf) == true else { return nil }
|
||||
return try? M(serializedBytes: data)
|
||||
}
|
||||
|
||||
private static func fetch(_ path: String, accept: String?) async -> (Data, HTTPURLResponse)? {
|
||||
guard let url = Server.resolveForPlayer(path) else { return nil }
|
||||
var req = URLRequest(url: url)
|
||||
if let accept { req.setValue(accept, forHTTPHeaderField: "Accept") }
|
||||
Credentials.headers.forEach { req.setValue($1, forHTTPHeaderField: $0) }
|
||||
guard let (data, resp) = try? await session.data(for: req), let http = resp as? HTTPURLResponse else { return nil }
|
||||
return (data, http)
|
||||
}
|
||||
}
|
||||
73
ios/Sources/App.swift
Normal file
73
ios/Sources/App.swift
Normal file
@@ -0,0 +1,73 @@
|
||||
import UIKit
|
||||
|
||||
@main
|
||||
final class AppDelegate: UIResponder, UIApplicationDelegate {
|
||||
func application(_ application: UIApplication,
|
||||
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
|
||||
Badge.registerBackgroundRefresh()
|
||||
Offline.shared.resume()
|
||||
Outbox.watch()
|
||||
return true
|
||||
}
|
||||
|
||||
/// A download kept going while the app was closed has finished (#425): the system launched us
|
||||
/// to hand it over, and waits on this handler.
|
||||
func application(_ application: UIApplication, handleEventsForBackgroundURLSession identifier: String,
|
||||
completionHandler: @escaping () -> Void) {
|
||||
Offline.shared.backgroundCompletion = completionHandler
|
||||
Offline.shared.resume()
|
||||
}
|
||||
|
||||
/// Apple's address for this phone's copy of the app (#433), handed to the page to register.
|
||||
func application(_ application: UIApplication,
|
||||
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
|
||||
Push.token = deviceToken.map { String(format: "%02x", $0) }.joined()
|
||||
}
|
||||
|
||||
/// ipx's count, arriving with the app closed (#433): `content-available` wakes the app for a
|
||||
/// moment, which the badge alone does not, so the widget's count follows the icon's (#428).
|
||||
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
|
||||
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
|
||||
guard let n = (userInfo["aps"] as? [String: Any])?["badge"] as? Int else {
|
||||
completionHandler(.noData)
|
||||
return
|
||||
}
|
||||
WidgetFeed.update { $0.unread = n }
|
||||
completionHandler(.newData)
|
||||
}
|
||||
|
||||
func application(_ application: UIApplication,
|
||||
didFailToRegisterForRemoteNotificationsWithError error: Error) {
|
||||
NSLog("ipx: push registration: %@", error.localizedDescription)
|
||||
}
|
||||
}
|
||||
|
||||
/// The one window. iOS will not run an app that has not adopted scenes, and a CarPlay app would
|
||||
/// be a second scene beside this one.
|
||||
final class SceneDelegate: UIResponder, UIWindowSceneDelegate {
|
||||
var window: UIWindow?
|
||||
|
||||
func scene(_ scene: UIScene, willConnectTo session: UISceneSession,
|
||||
options: UIScene.ConnectionOptions) {
|
||||
guard let scene = scene as? UIWindowScene else { return }
|
||||
let window = UIWindow(windowScene: scene)
|
||||
window.rootViewController = WebViewController()
|
||||
window.makeKeyAndVisible()
|
||||
self.window = window
|
||||
}
|
||||
|
||||
/// iOS wants the request made as the app leaves, with the user's last look at the list; the
|
||||
/// one made on coming forward stays for a run that was cut short.
|
||||
func sceneDidEnterBackground(_ scene: UIScene) {
|
||||
Badge.scheduleBackgroundRefresh()
|
||||
}
|
||||
|
||||
func sceneDidBecomeActive(_ scene: UIScene) {
|
||||
Badge.requestPermission()
|
||||
Badge.scheduleBackgroundRefresh()
|
||||
Task {
|
||||
await Outbox.flush()
|
||||
await Badge.refresh()
|
||||
}
|
||||
}
|
||||
}
|
||||
89
ios/Sources/Badge.swift
Normal file
89
ios/Sources/Badge.swift
Normal file
@@ -0,0 +1,89 @@
|
||||
import BackgroundTasks
|
||||
import UIKit
|
||||
import UserNotifications
|
||||
|
||||
/// The number on the icon: All Subscriptions' unread count, as the page's badge shows it (#421).
|
||||
///
|
||||
/// Set when the app comes forward, after the player marks an item read, and by a background
|
||||
/// refresh while it is closed. iOS decides when that runs (a few times a day for an app that is
|
||||
/// opened often), so the number can be hours behind; push would make it exact and needs a sender
|
||||
/// in ipx.
|
||||
enum Badge {
|
||||
static let taskId = "net.sdf1.ipx.refresh"
|
||||
|
||||
/// web/src/feeds.ts `unreadInAll`: a feed counts unless it, or the OPML it came from, is
|
||||
/// hidden from All Subscriptions.
|
||||
static func unread(inAll rows: [Ipx_FeedRow]) -> Int {
|
||||
let hidden = Set(rows.filter(\.hideFromAll).map(\.id))
|
||||
return rows.reduce(0) { n, f in
|
||||
if f.hideFromAll || (f.hasGroup && hidden.contains(f.group)) { return n }
|
||||
return n + max(0, Int(f.unread))
|
||||
}
|
||||
}
|
||||
|
||||
/// Asks once, for the badge alone: no alerts, no sounds.
|
||||
static func requestPermission() {
|
||||
// `-ipx.noBadgePrompt YES` on the launch arguments: a simulator run that drives the page
|
||||
// has nobody to tap Allow, and the dialog covers it.
|
||||
if UserDefaults.standard.bool(forKey: "ipx.noBadgePrompt") { return }
|
||||
UNUserNotificationCenter.current().requestAuthorization(options: [.badge]) { granted, _ in
|
||||
// Each launch, not once: Apple can change the token, and says to ask every time.
|
||||
guard granted else { return }
|
||||
Task { @MainActor in UIApplication.shared.registerForRemoteNotifications() }
|
||||
}
|
||||
}
|
||||
|
||||
/// The page's own count, sent as its list changes (#421).
|
||||
@MainActor static func set(_ n: Int) {
|
||||
lastSet = Date()
|
||||
WidgetFeed.update { $0.unread = max(0, n) }
|
||||
Task { try? await UNUserNotificationCenter.current().setBadgeCount(max(0, n)) }
|
||||
}
|
||||
|
||||
/// When the icon was last set from a count, by either way.
|
||||
@MainActor private static var lastSet = Date.distantPast
|
||||
|
||||
/// While an episode plays with the screen locked the app is awake, unlike the page: so new
|
||||
/// items reach the icon every quarter of an hour, which background refresh does not promise.
|
||||
@MainActor static func refreshIfStale() {
|
||||
guard Date().timeIntervalSince(lastSet) > 15 * 60 else { return }
|
||||
lastSet = Date()
|
||||
Task { await refresh() }
|
||||
}
|
||||
|
||||
/// Asks ipx and sets the icon. Left as it is when ipx cannot be reached or does not answer
|
||||
/// with a feed list: an old number is better than a wrong one.
|
||||
@discardableResult
|
||||
static func refresh() async -> Bool {
|
||||
guard let n = await fetch() else { return false }
|
||||
await MainActor.run { set(n) }
|
||||
return true
|
||||
}
|
||||
|
||||
/// All Subscriptions' unread count from ipx, or nil when it cannot be had: an answer that is
|
||||
/// not a feed list leaves the badge as it was rather than turning it to zero.
|
||||
static func fetch() async -> Int? {
|
||||
guard let feeds = await API.get(Ipx_FeedRows.self, "/api/feeds") else { return nil }
|
||||
return unread(inAll: feeds.items)
|
||||
}
|
||||
|
||||
// MARK: - while the app is closed
|
||||
|
||||
/// Before the app finishes launching, which is when iOS wants the handler registered.
|
||||
static func registerBackgroundRefresh() {
|
||||
BGTaskScheduler.shared.register(forTaskWithIdentifier: taskId, using: nil) { task in
|
||||
// The next one first, so a run that fails or is cut short does not end the chain.
|
||||
scheduleBackgroundRefresh()
|
||||
let work = Task {
|
||||
task.setTaskCompleted(success: await refresh())
|
||||
}
|
||||
task.expirationHandler = { work.cancel() }
|
||||
}
|
||||
}
|
||||
|
||||
static func scheduleBackgroundRefresh() {
|
||||
let req = BGAppRefreshTaskRequest(identifier: taskId)
|
||||
req.earliestBeginDate = Date(timeIntervalSinceNow: 30 * 60)
|
||||
try? BGTaskScheduler.shared.submit(req)
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user