Files
cabinchat/installer.sh.in
T
Pascal SuterandClaude Opus 5.5 1459b3228d Cabin Chat 4.0: accounts, https, PWA, mentions, notifications
- accounts with name + password (salted sha256), one account on several
  devices at once, profile menu: change password, profile picture,
  link another device (QR code + one-time link), logout, delete account
- @mentions with picker and highlighting
- https by default with the router web admin's certificate; --http,
  --cert/--key installer options
- logo/favicon, app manifest, service worker (installable PWA)
- browser notifications via the service worker, no internet needed
- devserver: https option, ignore connections reset by the browser

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 15:28:11 +01:00

286 lines
10 KiB
Bash

#!/bin/sh
# Cabin Chat @@VERSION@@ - self-contained installer for OpenWrt / GL.iNet routers.
# Uses only what the firmware already ships (uhttpd, busybox, uci).
#
# sh install-cabinchat.sh install, or upgrade an existing install
# (keeps accounts, messages, photos, port)
# sh install-cabinchat.sh -p 8090 install/upgrade on a specific port
# 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 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.
#
# Generated by build.sh - edit www/ and installer.sh.in instead of this file.
VERSION="@@VERSION@@"
APP=/www-chat
WEB=$APP/htdocs
DATA=$APP/data
RUN=/tmp/cabinchat
CLOCK=/etc/cabinchat.clock
DNSNAME=chat.lan
DEFAULT_PORT=8088
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
}
listening() { netstat -tln 2>/dev/null | grep -qE "[:.]$1[[:space:]]"; }
configured_port() {
{ uci -q get uhttpd.chat.listen_https; uci -q get uhttpd.chat.listen_http; } |
tr ' ' '\n' | sed -n 's/.*:\([0-9][0-9]*\)$/\1/p' | head -n 1
}
# the certificate of the router's own web admin -> CERT, KEY
# (GL.iNet firmware: nginx; plain OpenWrt: uhttpd)
admin_cert() {
for f in /etc/nginx/conf.d/*.conf /etc/nginx/nginx.conf; do
[ -f "$f" ] || continue
c=$(sed -n 's/^[[:space:]]*ssl_certificate[[:space:]]\{1,\}\([^;[:space:]]*\);.*/\1/p' "$f" | head -n 1)
k=$(sed -n 's/^[[:space:]]*ssl_certificate_key[[:space:]]\{1,\}\([^;[:space:]]*\);.*/\1/p' "$f" | head -n 1)
if [ -s "$c" ] && [ -s "$k" ]; then CERT=$c; KEY=$k; return 0; fi
done
c=$(uci -q get uhttpd.main.cert); k=$(uci -q get uhttpd.main.key)
if [ -s "$c" ] && [ -s "$k" ]; then CERT=$c; KEY=$k; return 0; fi
if [ -s /etc/uhttpd.crt ] && [ -s /etc/uhttpd.key ]; then CERT=/etc/uhttpd.crt; KEY=/etc/uhttpd.key; return 0; fi
return 1
}
# uhttpd does TLS through libustream-ssl
have_tls() { ls /lib/libustream-ssl.so* /usr/lib/libustream-ssl.so* >/dev/null 2>&1; }
remove_config() {
if uci -q get uhttpd.chat >/dev/null; then
uci delete uhttpd.chat
uci commit uhttpd
fi
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
uci commit dhcp
[ -f /etc/sysupgrade.conf ] && sed -i "\#^$APP/\$#d" /etc/sysupgrade.conf
}
extract_payload() {
rm -rf "$STAGE"
mkdir -p "$STAGE" || die "cannot create $STAGE"
@@PAYLOAD@@
[ -f "$STAGE/index.html" ] && [ -f "$STAGE/cgi-bin/chat" ] && [ -f "$STAGE/VERSION" ] \
|| die "installer payload is incomplete"
}
# version 2 lines (6 fields) -> version 3 (8 fields: + recipient "*" and no quote)
migrate_messages() {
[ -s "$DATA/messages" ] || return 0
awk -F '\t' 'BEGIN { OFS = "\t" }
NF == 6 { print $1, $2, $3, $4, "*", 0, $5, $6; next }
NF == 8 { print }' "$DATA/messages" > "$DATA/messages.new" &&
mv "$DATA/messages.new" "$DATA/messages"
}
# ------------------------------------------------------------------ install
do_install() {
[ -x /usr/sbin/uhttpd ] || die "uhttpd is not installed.
With internet access run: opkg update && opkg install uhttpd
then run this installer again."
LANIP=$(uci -q get network.lan.ipaddr | cut -d/ -f1)
[ -n "$LANIP" ] || die "could not read the LAN IP (network.lan.ipaddr)"
OLDVER=
if [ -f "$APP/VERSION" ]; then
OLDVER=$(cat "$APP/VERSION")
elif [ -e "$APP/index.html" ] || uci -q get uhttpd.chat >/dev/null; then
OLDVER="1.x"
fi
OLDPORT=$(configured_port)
[ -n "$PORT" ] || PORT=${OLDPORT:-$DEFAULT_PORT}
case $PORT in ''|*[!0-9]*) die "invalid port: $PORT" ;; esac
if [ "$PORT" != "$OLDPORT" ] && listening "$PORT"; then
die "port $PORT is already in use - choose another one with -p <port>"
fi
# https unless asked otherwise; an upgrade of a 4.x+ http install stays on http
OLDCERT=$(uci -q get uhttpd.chat.cert)
if [ -z "$TLS" ]; then
TLS=1
case $OLDVER in [4-9].*|[1-9][0-9]*)
uci -q get uhttpd.chat.listen_https >/dev/null || TLS=0 ;;
esac
fi
CERT=; KEY=
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
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"
TLS=0
fi
fi
[ "$TLS" = 1 ] && SCHEME=https || SCHEME=http
if [ -n "$OLDVER" ]; then
info "upgrading Cabin Chat $OLDVER -> $VERSION"
else
info "installing Cabin Chat $VERSION"
fi
# unpack first: if anything is wrong with this file, nothing has been touched yet
extract_payload
if [ -n "$OLDVER" ]; then
info "removing the previous version (messages and photos are kept)"
fi
remove_config
mkdir -p "$APP" || die "cannot create $APP"
mkdir -p "$DATA"
# keep the photos aside (version 2 kept them in data/img, version 3 in htdocs/img)
KEEPIMG=$APP/.img-keep
mkdir -p "$KEEPIMG"
for d in "$WEB/img" "$DATA/img"; do
[ -d "$d" ] && mv "$d"/* "$KEEPIMG"/ 2>/dev/null
rm -rf "$d"
done
for f in "$APP"/* "$APP"/.[!.]*; do
[ -e "$f" ] || continue
case $f in "$DATA"|"$KEEPIMG") continue ;; esac
rm -rf "$f"
done
rm -rf /tmp/chat.log /tmp/chat.lock /tmp/cabinchat.lock # leftovers from versions 1 and 2
migrate_messages
info "installing files to $WEB"
mkdir -p "$WEB"
cp -R "$STAGE"/* "$WEB"/ || die "copying files failed"
rm -rf "$STAGE"
chmod 755 "$WEB"/cgi-bin/*
mkdir -p "$WEB/img"
mv "$KEEPIMG"/* "$WEB/img"/ 2>/dev/null
rm -rf "$KEEPIMG"
[ -f "$CLOCK" ] || date +%s > "$CLOCK"
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"
chmod 600 "$DATA/tls/key.pem"
CERT=$DATA/tls/cert.pem; KEY=$DATA/tls/key.pem
fi
info "configuring web server on $SCHEME://$LANIP:$PORT"
uci set uhttpd.chat=uhttpd
if [ "$TLS" = 1 ]; then
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
uci add_list uhttpd.chat.listen_http="$LANIP:$PORT"
fi
uci set uhttpd.chat.home="$WEB"
uci set uhttpd.chat.cgi_prefix='/cgi-bin'
uci set uhttpd.chat.index_page='index.html'
uci set uhttpd.chat.no_dirlists='1'
# long polling keeps one request per open chat waiting for up to 25 s
uci set uhttpd.chat.script_timeout='60'
uci set uhttpd.chat.network_timeout='60'
uci set uhttpd.chat.max_requests='100'
uci set uhttpd.chat.max_connections='100'
uci commit uhttpd
/etc/init.d/uhttpd enable
/etc/init.d/uhttpd restart
info "adding DNS name $DNSNAME"
uci add_list dhcp.@dnsmasq[0].address="/$DNSNAME/$LANIP"
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
sleep 1
listening "$PORT" || die "uhttpd is not listening on port $PORT - check: logread | grep uhttpd"
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"
[ "$TLS" = 1 ] && warn "if https is the problem, run the installer again with --http" ;;
esac
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 ] && [ -z "$CERT_ARG" ] && [ "$CERT" != "$DATA/tls/cert.pem" ]; then
echo " (the router's certificate is self-signed: browsers warn once, accept it to continue)"
fi
}
# ------------------------------------------------------------------ uninstall
do_uninstall() {
info "removing Cabin Chat"
remove_config
rm -rf "$APP" "$CLOCK" "$RUN" /tmp/chat.log /tmp/chat.lock /tmp/cabinchat.lock
/etc/init.d/uhttpd restart >/dev/null 2>&1
/etc/init.d/dnsmasq restart >/dev/null 2>&1
info "done"
}
do_wipe() {
[ -d "$APP" ] || die "Cabin Chat is not installed"
rm -rf "$DATA/messages" "$DATA/seq" "$WEB/img"
mkdir -p "$WEB/img"
g=$(cat "$DATA/gen" 2>/dev/null); case $g in ''|*[!0-9]*) g=0 ;; esac
echo $((g + 1)) > "$DATA/gen"
# wake up waiting browsers so they notice the empty chat
[ -d "$RUN" ] && echo "wipe.$$" > "$RUN/ev"
info "all messages and photos deleted (accounts are kept)"
}
# ------------------------------------------------------------------ main
CMD=install
PORT=
TLS=
CERT_ARG=
KEY_ARG=
while [ $# -gt 0 ]; do
case $1 in
-p|--port) [ -n "$2" ] || die "-p needs a port number"; PORT=$2; shift 2 ;;
--http|--no-https) TLS=0; shift ;;
--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 ;;
[0-9]*) PORT=$1; shift ;;
install|upgrade) CMD=install; shift ;;
uninstall|remove) CMD=uninstall; shift ;;
wipe) CMD=wipe; shift ;;
-h|--help|help) usage; exit 0 ;;
*) usage; die "unknown argument: $1" ;;
esac
done
[ "$(id -u)" = "0" ] || die "please run as root"
command -v uci >/dev/null || die "uci not found - is this OpenWrt?"
case $CMD in
install) do_install ;;
uninstall) do_uninstall ;;
wipe) do_wipe ;;
esac