4.0.1: serve the chat through nginx on GL.iNet, https://<name>/ support

- GL.iNet: nginx terminates https with the web admin's certificate and
  serves static files (conf.d/zz-cabinchat.conf, after gl.conf so the
  admin stays nginx's default server); /cgi-bin/ goes to uhttpd on
  127.0.0.1 (fcgiwrap is shared with the admin and runs only 4 scripts)
- --name <host>: also serve https://<host>/ on port 443 plus an http
  redirect, add the name to dnsmasq; guessed from dnsmasq entries that
  contain "chat" and match the certificate; server_name as regex since
  GL's server_names_hash_bucket_size is too small
- config checked with nginx -t, falls back to uhttpd; kept on firmware
  updates via /etc/sysupgrade.conf
- fix: TLS detection failed when /usr/lib/libustream-ssl.so is missing
- fix: previous version read from the wrong place ("upgrading 1.x")
- fix: photos of expired messages and deleted accounts were never
  removed (file name check rejected ".jpg")

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Pascal Suter
2026-11-16 17:49:56 +01:00
co-authored by Claude Opus 5.5
parent 8928ae7077
commit 907a54eb57
5 changed files with 454 additions and 71 deletions
+205 -29
View File
@@ -1,20 +1,24 @@
#!/bin/sh
# Cabin Chat 4.0.0 - self-contained installer for OpenWrt / GL.iNet routers.
# Uses only what the firmware already ships (uhttpd, busybox, uci).
# Cabin Chat 4.0.1 - self-contained installer for OpenWrt / GL.iNet routers.
# Uses only what the firmware already ships (uhttpd, busybox, uci; nginx if there).
#
# sh install-cabinchat.sh install, or upgrade an existing install
# (keeps accounts, messages, photos, port)
# (keeps accounts, messages, photos, settings)
# sh install-cabinchat.sh -p 8090 install/upgrade on a specific port
# sh install-cabinchat.sh --name chat.example.com also serve https://<name>/
# sh install-cabinchat.sh --http plain http instead of https
# sh install-cabinchat.sh --cert C --key K https with your own certificate
# sh install-cabinchat.sh --no-nginx don't use nginx (GL.iNet), only uhttpd
# sh install-cabinchat.sh wipe delete all messages and photos
# sh install-cabinchat.sh uninstall remove Cabin Chat completely
#
# https uses the certificate of the router's web admin unless --cert is given.
# On GL.iNet firmware nginx serves https and passes the chat requests on to
# uhttpd on 127.0.0.1.
#
# Generated by build.sh - edit www/ and installer.sh.in instead of this file.
VERSION="4.0.0"
VERSION="4.0.1"
APP=/www-chat
WEB=$APP/htdocs
DATA=$APP/data
@@ -22,14 +26,16 @@ RUN=/tmp/cabinchat
CLOCK=/etc/cabinchat.clock
DNSNAME=chat.lan
DEFAULT_PORT=8088
# after gl.conf: the first server on port 443 is nginx's default (the GL admin)
NGINX_CONF=/etc/nginx/conf.d/zz-cabinchat.conf
STAGE=/tmp/cabinchat-stage.$$
info() { echo ">> $*"; }
warn() { echo "WARNING: $*" >&2; }
die() { echo "ERROR: $*" >&2; rm -rf "$STAGE"; exit 1; }
usage() {
if [ -f "$0" ]; then sed -n '4,13p' "$0" | sed 's/^# \{0,1\}//'
else echo "usage: sh install-cabinchat.sh [-p <port>] [--http | --cert <file> --key <file>] [wipe | uninstall]"; fi
if [ -f "$0" ]; then sed -n '4,18p' "$0" | sed 's/^# \{0,1\}//'
else echo "usage: sh install-cabinchat.sh [-p <port>] [--name <host>] [--http | --cert <file> --key <file>] [--no-nginx] [wipe | uninstall]"; fi
}
listening() { netstat -tln 2>/dev/null | grep -qE "[:.]$1[[:space:]]"; }
@@ -78,8 +84,105 @@ EOF
return 1
}
# DNS names in a certificate, one per line
cert_names() {
command -v openssl >/dev/null || return 0
openssl x509 -in "$1" -noout -text 2>/dev/null | grep -o 'DNS:[^,[:space:]]*' | sed 's/^DNS://'
}
# does host name $1 match a certificate name $2 (exactly or via *.)?
name_match() {
case $2 in
"$1") return 0 ;;
\*.*) [ "${1#*.}" != "$1" ] && [ "${1#*.}" = "${2#\*.}" ] ;;
*) return 1 ;;
esac
}
# a name that points to this router (dnsmasq), is covered by the certificate
# and looks like it is meant for the chat -> NAME
guess_name() {
names=$(cert_names "$CERT")
[ -n "$names" ] || return 1
for a in $(uci -q get dhcp.@dnsmasq[0].address); do
h=${a#/}; h=${h%%/*}
[ "${a##*/}" = "$LANIP" ] || continue
case ${h%%.*} in *chat*) ;; *) continue ;; esac
set -f # "*.example.com" is no file pattern
for n in $names; do
name_match "$h" "$n" && { set +f; NAME=$h; return 0; }
done
set +f
done
return 1
}
# uhttpd does TLS through libustream-ssl
have_tls() { ls /lib/libustream-ssl.so* /usr/lib/libustream-ssl.so* >/dev/null 2>&1; }
have_tls() {
for f in /lib/libustream-ssl.so* /usr/lib/libustream-ssl.so*; do
[ -e "$f" ] && return 0
done
return 1
}
# GL.iNet firmware: nginx is the web server and reads conf.d/*.conf
have_nginx() {
[ -x /usr/sbin/nginx ] && [ -d /etc/nginx/conf.d ] &&
grep -qs 'include[[:space:]]*/etc/nginx/conf.d/\*\.conf' /etc/nginx/nginx.conf
}
# nginx terminates https (port $PORT and, with a name, 443) and passes the
# chat requests to uhttpd on 127.0.0.1:$PORT; static files come from $WEB
write_nginx() {
# as a regex: GL's nginx has a small server_names_hash_bucket_size, and
# raising it would mean an http-level setting that may clash after updates
NAMERE="~^$(echo "$NAME" | sed 's/\./\\./g')\$"
{
echo "# Cabin Chat - written by install-cabinchat.sh, overwritten on upgrades"
echo "server {"
echo " listen $LANIP:$PORT ssl;"
if [ -n "$NAME" ]; then
echo " listen 443 ssl;"
echo " listen [::]:443 ssl;"
echo " server_name $NAMERE;"
fi
cat <<EOF
ssl_certificate $CERT;
ssl_certificate_key $KEY;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_session_tickets off;
root $WEB;
index index.html;
location / {
add_header Cache-Control "no-cache";
}
location /img/ {
add_header Cache-Control "public, max-age=2592000, immutable";
}
location /cgi-bin/ {
proxy_pass http://127.0.0.1:$PORT;
proxy_set_header Host \$host;
proxy_buffering off;
# long polls wait up to 25 s
proxy_read_timeout 90s;
}
}
EOF
[ -n "$NAME" ] && cat <<EOF
server {
listen 80;
listen [::]:80;
server_name $NAMERE;
return 301 https://\$host\$request_uri;
}
EOF
} > "$NGINX_CONF"
}
nginx_reload() { nginx -s reload 2>/dev/null || /etc/init.d/nginx restart; }
remove_config() {
if uci -q get uhttpd.chat >/dev/null; then
@@ -89,8 +192,19 @@ remove_config() {
for a in $(uci -q get dhcp.@dnsmasq[0].address); do
case $a in "/$DNSNAME/"*) uci del_list dhcp.@dnsmasq[0].address="$a" ;; esac
done
if [ -f "$DATA/dns-added" ]; then
n=$(cat "$DATA/dns-added")
for a in $(uci -q get dhcp.@dnsmasq[0].address); do
case $a in "/$n/"*) uci del_list dhcp.@dnsmasq[0].address="$a" ;; esac
done
rm -f "$DATA/dns-added"
fi
uci commit dhcp
[ -f /etc/sysupgrade.conf ] && sed -i "\#^$APP/\$#d" /etc/sysupgrade.conf
if [ -f "$NGINX_CONF" ]; then
rm -f "$NGINX_CONF"
nginx_reload >/dev/null 2>&1
fi
[ -f /etc/sysupgrade.conf ] && sed -i -e "\#^$APP/\$#d" -e "\#^$NGINX_CONF\$#d" /etc/sysupgrade.conf
}
extract_payload() {
@@ -626,7 +740,7 @@ needs_prune() {
del_photos() {
[ -f "$DATA/.del" ] || return 0
while IFS= read -r f; do
case $f in .*|*[!0-9a-f.-]*) ;; *.jpg) rm -f "$IMG/$f" ;; esac
case $f in .*|*[!0-9a-z.-]*) ;; *.jpg) rm -f "$IMG/$f" ;; esac
done < "$DATA/.del"
rm -f "$DATA/.del"
}
@@ -4196,7 +4310,7 @@ self.addEventListener('notificationclick', e => {
});
__CABINCHAT_EOF__
cat > "$STAGE/VERSION" <<'__CABINCHAT_EOF__'
4.0.0
4.0.1
__CABINCHAT_EOF__
[ -f "$STAGE/index.html" ] && [ -f "$STAGE/cgi-bin/chat" ] && [ -f "$STAGE/VERSION" ] \
|| die "installer payload is incomplete"
@@ -4221,7 +4335,9 @@ do_install() {
[ -n "$LANIP" ] || die "could not read the LAN IP (network.lan.ipaddr)"
OLDVER=
if [ -f "$APP/VERSION" ]; then
if [ -f "$WEB/VERSION" ]; then
OLDVER=$(cat "$WEB/VERSION")
elif [ -f "$APP/VERSION" ]; then
OLDVER=$(cat "$APP/VERSION")
elif [ -e "$APP/index.html" ] || uci -q get uhttpd.chat >/dev/null; then
OLDVER="1.x"
@@ -4234,7 +4350,6 @@ do_install() {
fi
# https unless asked otherwise; once --http was given, upgrades stay on http
OLDCERT=$(uci -q get uhttpd.chat.cert)
TLS_ARG=$TLS
if [ -z "$TLS" ]; then
TLS=1
@@ -4244,20 +4359,35 @@ do_install() {
if [ "$TLS" = 1 ]; then
if [ -n "$CERT_ARG" ]; then
[ -s "$CERT_ARG" ] && [ -s "$KEY_ARG" ] || die "--cert and --key need two existing files (PEM)"
elif [ "$OLDCERT" = "$DATA/tls/cert.pem" ] && [ -s "$OLDCERT" ] && [ -s "$DATA/tls/key.pem" ]; then
CERT=$OLDCERT; KEY=$DATA/tls/key.pem # keep the certificate given last time
CERT=$CERT_ARG; KEY=$KEY_ARG
elif [ -s "$DATA/tls/cert.pem" ] && [ -s "$DATA/tls/key.pem" ]; then
CERT=$DATA/tls/cert.pem; KEY=$DATA/tls/key.pem # the certificate given last time
elif ! admin_cert; then
warn "no certificate of the router's web admin found - using plain http"
TLS=0
fi
if [ "$TLS" = 1 ] && ! have_tls; then
warn "uhttpd has no TLS support (libustream-ssl missing) - using plain http"
warn " to get https: opkg update && opkg install libustream-openssl, then run this installer again"
TLS=0
fi
fi
# who does https: nginx (GL.iNet) or uhttpd itself
[ -n "$NGINX_ARG" ] || { [ -f "$DATA/no-nginx" ] && NGINX_ARG=0; }
FRONT=uhttpd
if [ "$TLS" = 1 ] && [ "$NGINX_ARG" != 0 ] && have_nginx; then
FRONT=nginx
elif [ "$TLS" = 1 ] && ! have_tls; then
warn "uhttpd has no TLS support (libustream-ssl missing) - using plain http"
warn " to get https: opkg update && opkg install libustream-openssl, then run this installer again"
TLS=0
fi
[ "$TLS" = 1 ] && SCHEME=https || SCHEME=http
# host name for https://<name>/ (nginx only): --name, the one from last time, or a guess
if [ "$FRONT" = nginx ]; then
[ -n "$NAME" ] || NAME=$(cat "$DATA/hostname" 2>/dev/null)
[ -n "$NAME" ] || guess_name
[ "$NAME" = - ] && NAME=
else
NAME=
fi
if [ -n "$OLDVER" ]; then
info "upgrading Cabin Chat $OLDVER -> $VERSION"
else
@@ -4299,6 +4429,7 @@ do_install() {
[ -f "$CLOCK" ] || date +%s > "$CLOCK"
if [ "$TLS_ARG" = 0 ]; then touch "$DATA/http-only"; elif [ "$TLS" = 1 ]; then rm -f "$DATA/http-only"; fi
if [ "$NGINX_ARG" = 0 ]; then touch "$DATA/no-nginx"; elif [ "$NGINX_ARG" = 1 ]; then rm -f "$DATA/no-nginx"; fi
if [ "$TLS" = 1 ] && [ -n "$CERT_ARG" ]; then
mkdir -p "$DATA/tls"
cp "$CERT_ARG" "$DATA/tls/cert.pem" && cp "$KEY_ARG" "$DATA/tls/key.pem" || die "cannot copy the certificate"
@@ -4306,14 +4437,31 @@ do_install() {
CERT=$DATA/tls/cert.pem; KEY=$DATA/tls/key.pem
fi
info "configuring web server on $SCHEME://$LANIP:$PORT"
if [ "$FRONT" = nginx ]; then
info "configuring nginx for https://$LANIP:$PORT${NAME:+ and https://$NAME}"
info "certificate: $CERT"
write_nginx
if ! nginx -t >/dev/null 2>&1; then
nginx -t
rm -f "$NGINX_CONF"
warn "nginx did not accept the configuration - using uhttpd alone"
FRONT=uhttpd
NAME=
have_tls || { TLS=0; SCHEME=http; }
fi
fi
uci set uhttpd.chat=uhttpd
if [ "$TLS" = 1 ]; then
if [ "$FRONT" = nginx ]; then
echo "${NAME:--}" > "$DATA/hostname"
uci add_list uhttpd.chat.listen_http="127.0.0.1:$PORT" # only for nginx
elif [ "$TLS" = 1 ]; then
info "configuring web server on https://$LANIP:$PORT"
info "certificate: $CERT"
uci add_list uhttpd.chat.listen_https="$LANIP:$PORT"
uci set uhttpd.chat.cert="$CERT"
uci set uhttpd.chat.key="$KEY"
else
info "configuring web server on http://$LANIP:$PORT"
uci add_list uhttpd.chat.listen_http="$LANIP:$PORT"
fi
uci set uhttpd.chat.home="$WEB"
@@ -4328,35 +4476,57 @@ do_install() {
uci commit uhttpd
/etc/init.d/uhttpd enable
/etc/init.d/uhttpd restart
[ "$FRONT" = nginx ] && nginx_reload
info "adding DNS name $DNSNAME"
uci add_list dhcp.@dnsmasq[0].address="/$DNSNAME/$LANIP"
# the name must resolve without internet too
if [ -n "$NAME" ] && ! uci -q get dhcp.@dnsmasq[0].address | tr ' ' '\n' | grep -q "^/$NAME/"; then
info "adding DNS name $NAME"
uci add_list dhcp.@dnsmasq[0].address="/$NAME/$LANIP"
echo "$NAME" > "$DATA/dns-added"
fi
uci commit dhcp
/etc/init.d/dnsmasq restart >/dev/null 2>&1
touch /etc/sysupgrade.conf
grep -qx "$APP/" /etc/sysupgrade.conf || echo "$APP/" >> /etc/sysupgrade.conf
if [ "$FRONT" = nginx ]; then
grep -qx "$NGINX_CONF" /etc/sysupgrade.conf || echo "$NGINX_CONF" >> /etc/sysupgrade.conf
fi
sleep 1
listening "$PORT" || die "uhttpd is not listening on port $PORT - check: logread | grep uhttpd"
listening "$PORT" || die "nothing is listening on port $PORT - check: logread | grep -E 'uhttpd|nginx'"
if command -v wget >/dev/null; then
case $(wget -q --no-check-certificate -O - "$SCHEME://$LANIP:$PORT/cgi-bin/chat?a=ping" 2>/dev/null | head -n 1) in
@cabinchat*) info "self-test passed" ;;
*) warn "the chat backend did not answer as expected - check: logread | grep uhttpd"
*) warn "the chat backend did not answer as expected - check: logread | grep -E 'uhttpd|nginx'"
[ "$TLS" = 1 ] && warn "if https is the problem, run the installer again with --http" ;;
esac
if [ -n "$NAME" ]; then
case $(wget -q -O - "https://$NAME/cgi-bin/chat?a=ping" 2>/dev/null | head -n 1) in
@cabinchat*) info "self-test of https://$NAME passed (certificate valid)" ;;
*) warn "https://$NAME did not answer - does the certificate cover that name?" ;;
esac
fi
fi
n=$(grep -c . "$DATA/messages" 2>/dev/null); n=${n:-0}
a=$(ls "$DATA/accounts" 2>/dev/null | grep -c .); a=${a:-0}
echo
info "Cabin Chat $VERSION is running ($a accounts, $n messages from the last 24 h kept)"
echo " $SCHEME://$LANIP:$PORT"
echo " $SCHEME://$DNSNAME:$PORT"
if [ "$TLS" = 1 ] && ! ca_signed "$CERT"; then
echo " (the certificate looks self-signed: browsers warn once, accept it to continue)"
elif [ "$TLS" = 1 ]; then
echo " or https://<the name in the certificate>:$PORT once that name points to $LANIP"
if [ -n "$NAME" ]; then
echo " https://$NAME"
echo " (also $SCHEME://$LANIP:$PORT and $SCHEME://$DNSNAME:$PORT, with a certificate warning)"
else
echo " $SCHEME://$LANIP:$PORT"
echo " $SCHEME://$DNSNAME:$PORT"
if [ "$TLS" = 1 ] && ! ca_signed "$CERT"; then
echo " (the certificate looks self-signed: browsers warn once, accept it to continue)"
elif [ "$TLS" = 1 ]; then
echo " For a warning-free https://<name>/ run the installer again with --name <name>"
echo " (a name the certificate covers: $(cert_names "$CERT" | tr '\n' ' '))"
fi
fi
}
@@ -4387,6 +4557,8 @@ PORT=
TLS=
CERT_ARG=
KEY_ARG=
NAME=
NGINX_ARG=
while [ $# -gt 0 ]; do
case $1 in
-p|--port) [ -n "$2" ] || die "-p needs a port number"; PORT=$2; shift 2 ;;
@@ -4394,6 +4566,10 @@ while [ $# -gt 0 ]; do
--https) TLS=1; shift ;;
--cert) [ -n "$2" ] || die "--cert needs a file"; CERT_ARG=$2; TLS=1; shift 2 ;;
--key) [ -n "$2" ] || die "--key needs a file"; KEY_ARG=$2; shift 2 ;;
--name) [ -n "$2" ] || die "--name needs a host name"; NAME=$2; shift 2 ;;
--no-name) NAME=-; shift ;;
--no-nginx) NGINX_ARG=0; shift ;;
--nginx) NGINX_ARG=1; shift ;;
[0-9]*) PORT=$1; shift ;;
install|upgrade) CMD=install; shift ;;
uninstall|remove) CMD=uninstall; shift ;;