2023-07-11 08:29:17 -06:00
|
|
|
#! /usr/bin/env bash
|
2023-07-27 15:53:47 -06:00
|
|
|
# This file imports all *.zim files in the Zim media directory, then starts kiwix-serve.
|
2026-07-20 13:00:38 -06:00
|
|
|
set -u
|
2023-07-11 08:29:17 -06:00
|
|
|
|
2026-07-20 18:24:03 -06:00
|
|
|
# The media directory and project directory are overridable for testing;
|
|
|
|
|
# they default to the production locations.
|
2026-07-20 13:00:38 -06:00
|
|
|
MEDIA_DIRECTORY="${MEDIA_DIRECTORY:-/media/wrolpi}"
|
2026-07-20 18:24:03 -06:00
|
|
|
PROJECT_DIR="${PROJECT_DIR:-/opt/wrolpi}"
|
|
|
|
|
# Default Zim shipped in /opt/wrolpi-blobs; served when no real zim is available.
|
|
|
|
|
DEFAULT_ZIM="${DEFAULT_ZIM:-/opt/wrolpi-blobs/default.zim}"
|
2023-07-11 08:29:17 -06:00
|
|
|
|
2026-07-20 13:00:38 -06:00
|
|
|
[ ! -d "${MEDIA_DIRECTORY}" ] && echo "Cannot start kiwix without media directory mounted" && exit 1
|
2023-07-11 08:29:17 -06:00
|
|
|
|
2026-07-20 18:24:03 -06:00
|
|
|
# Honor the configured Zim directory (wrolpi.yaml `zims_destination`, always
|
|
|
|
|
# relative to the media directory), defaulting to `zims`.
|
|
|
|
|
ZIMS_DEST="$(MEDIA_DIRECTORY="${MEDIA_DIRECTORY}" "${PROJECT_DIR}/wrolpi/scripts/read_config_value.sh" zims_destination zims)"
|
2026-07-20 18:44:19 -06:00
|
|
|
# Defense in depth: never let an absolute path, traversal, or empty value escape
|
|
|
|
|
# the media root (the settings API also rejects these). Fall back to `zims`.
|
|
|
|
|
case "${ZIMS_DEST}" in
|
|
|
|
|
''|/*|*..*)
|
|
|
|
|
echo "Ignoring unsafe zims_destination '${ZIMS_DEST}'; using 'zims'" >&2
|
|
|
|
|
ZIMS_DEST=zims
|
|
|
|
|
;;
|
|
|
|
|
esac
|
2026-07-20 18:24:03 -06:00
|
|
|
ZIM_DIRECTORY="${MEDIA_DIRECTORY}/${ZIMS_DEST}"
|
2026-07-20 13:00:38 -06:00
|
|
|
LIBRARY="${ZIM_DIRECTORY}/library.xml"
|
|
|
|
|
|
|
|
|
|
# Wait up to 30 seconds for the zims directory to be mounted.
|
2023-07-27 15:53:47 -06:00
|
|
|
COUNT=0
|
|
|
|
|
while [ ${COUNT} -lt 30 ]; do
|
2026-07-20 13:00:38 -06:00
|
|
|
if [ -d "${ZIM_DIRECTORY}" ]; then
|
2023-07-27 15:53:47 -06:00
|
|
|
break
|
|
|
|
|
fi
|
2026-07-20 13:00:38 -06:00
|
|
|
COUNT=$((COUNT + 1))
|
2023-07-27 15:53:47 -06:00
|
|
|
sleep 1
|
|
|
|
|
done
|
|
|
|
|
|
2023-07-11 08:29:17 -06:00
|
|
|
# Create the zim directory, if necessary.
|
2026-07-20 13:00:38 -06:00
|
|
|
[ ! -d "${ZIM_DIRECTORY}" ] && mkdir -p "${ZIM_DIRECTORY}"
|
|
|
|
|
|
2023-07-11 08:29:17 -06:00
|
|
|
# We will replace the library file.
|
2026-07-20 13:00:38 -06:00
|
|
|
rm -f "${LIBRARY}"
|
|
|
|
|
|
|
|
|
|
# Import every *.zim. A corrupt or partial zim is rejected by kiwix-manage
|
|
|
|
|
# ("Cannot add zim ..."); it must NOT prevent the remaining valid zims from
|
|
|
|
|
# being served, nor stop the server from starting. So we add each zim
|
|
|
|
|
# independently and never abort on a single failure.
|
|
|
|
|
found_zim=false
|
|
|
|
|
added_zim=false
|
|
|
|
|
while IFS= read -r -d '' zim; do
|
|
|
|
|
found_zim=true
|
|
|
|
|
if kiwix-manage "${LIBRARY}" add "${zim}"; then
|
|
|
|
|
added_zim=true
|
|
|
|
|
else
|
|
|
|
|
echo "Skipping zim which could not be added (corrupt or incomplete?): ${zim}" >&2
|
|
|
|
|
fi
|
|
|
|
|
done < <(find "${ZIM_DIRECTORY}" -iname '*.zim' -print0)
|
|
|
|
|
|
|
|
|
|
if [ "${found_zim}" = false ]; then
|
|
|
|
|
echo "Could not find any Zim files to import"
|
|
|
|
|
elif [ "${added_zim}" = false ]; then
|
2026-07-20 18:24:03 -06:00
|
|
|
echo "Found Zim files but none could be added (all corrupt or incomplete?)" >&2
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# When no functioning zim was added, fall back to the default Zim shipped in
|
|
|
|
|
# /opt/wrolpi-blobs so the user sees a helpful placeholder ("download some Zim
|
|
|
|
|
# files") instead of a blank Kiwix. A single corrupt zim must never take down
|
|
|
|
|
# the server.
|
|
|
|
|
if [ "${added_zim}" = false ] && [ -f "${DEFAULT_ZIM}" ]; then
|
|
|
|
|
echo "No usable Zim files found; serving the default WROLPi Zim: ${DEFAULT_ZIM}" >&2
|
|
|
|
|
kiwix-manage "${LIBRARY}" add "${DEFAULT_ZIM}" || echo "Failed to add the default Zim" >&2
|
2026-07-20 13:00:38 -06:00
|
|
|
fi
|
2023-07-11 08:29:17 -06:00
|
|
|
|
2026-07-20 13:00:38 -06:00
|
|
|
# Ensure a library file always exists so kiwix-serve can start even when there
|
2026-07-20 18:24:03 -06:00
|
|
|
# are no valid zims and the default Zim is missing.
|
2026-07-20 13:00:38 -06:00
|
|
|
if [ ! -f "${LIBRARY}" ]; then
|
|
|
|
|
printf '<?xml version="1.0" encoding="UTF-8"?>\n<library version="20110515">\n</library>\n' > "${LIBRARY}"
|
|
|
|
|
fi
|
2023-07-11 08:29:17 -06:00
|
|
|
|
Replace nginx with Caddy for TLS termination and reverse proxying.
- Replace nginx with Caddy as the web server for both Docker and native
(RPi/Debian) deployments. Caddy handles HTTPS termination, reverse
proxying to all services (API, React app, Controller, Kiwix, Help, Map),
and serves the media/download file browsers.
- Generate a Root CA and signed leaf certificates for HTTPS. The Root CA
uses Name Constraints to restrict signing to local addresses only
(.local, localhost, and RFC 1918 private IP ranges), mitigating MITM
risk if the CA key is compromised.
- Add an HTTP landing page that guides users through one-time CA
certificate installation on iOS, Android, macOS, Windows, Linux, and
Firefox, with auto-redirect when the cert is already trusted.
- Add trailing-slash redirects for /controller, /docs, /media, and
/download so bare paths don't fall through to the React catch-all.
- Add a Root CA download link on the Settings page under Configs.
- Update help.sh diagnostics for IPv6 listener detection and HTTP/2
status codes.
- Remove all nginx config files and the https_proxy Docker container.
- Bump wrolpi-help submodule with certificate documentation.
2026-03-07 12:08:27 -07:00
|
|
|
# Serve HTTP kiwix on 9085. Caddy will serve HTTPS on 8085.
|
2026-07-14 20:32:09 -06:00
|
|
|
# Bind all interfaces by omitting --address: kiwix-serve defaults to all
|
|
|
|
|
# interfaces, and trixie's kiwix-tools 3.7.0 rejects the literal
|
|
|
|
|
# "--address 0.0.0.0" ("IP address is not available on this system").
|
2026-07-20 13:00:38 -06:00
|
|
|
exec kiwix-serve --library "${LIBRARY}" --port 9085
|