sites

Source for the daat.foo sites
git clone https://git.daat.foo/sites.git
Log | Files | Refs | Submodules

install.sh (7238B)


      1 #!/bin/sh
      2 set -eu
      3 umask 077
      4 
      5 project=$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd -P)
      6 cd "$project"
      7 host=inv.daat.foo
      8 port=8300
      9 compose_file="$project/compose.yaml"
     10 secrets="$project/.env"
     11 font=/usr/local/searxng/searxng-src/searx/static/fonts/unifont-18.0.01.otf
     12 lighttpd_dir=/etc/lighttpd
     13 main_config="$lighttpd_dir/lighttpd.conf"
     14 vhost_config="$lighttpd_dir/$host.conf"
     15 include_line="include \"$vhost_config\""
     16 cert_dir="$lighttpd_dir/certs/$host"
     17 acme_home=/root/.acme.sh
     18 acme="$acme_home/acme.sh"
     19 auth_file="$lighttpd_dir/auth/searxng.htpasswd"
     20 webroot=/var/www/acme
     21 
     22 fail() {
     23     printf 'inv: %s\n' "$*" >&2
     24     exit 1
     25 }
     26 
     27 if [ "${1:-}" = --check ]; then
     28     INV_DB_PASSWORD=check INV_HMAC_KEY=check INV_COMPANION_KEY=check \
     29         docker compose --env-file /dev/null -f "$compose_file" config --quiet
     30     printf '%s\n' 'Compose configuration is valid.'
     31     exit 0
     32 fi
     33 case "$#:${1:-}" in
     34     0:) mode=install ;;
     35     1:--proxy-only) mode=proxy ;;
     36     *) fail 'usage: install.sh [--check | --proxy-only]' ;;
     37 esac
     38 if [ "$(id -u)" -ne 0 ]; then
     39     exec doas "$project/install.sh" "$@"
     40 fi
     41 
     42 exec 9>/run/lock/daat-invidious.lock
     43 flock -n 9 || fail 'another install is running'
     44 test -s "$auth_file" || fail "the SearXNG password file is missing or empty: $auth_file"
     45 test -d "$webroot/.well-known/acme-challenge" || fail "ACME webroot is missing: $webroot"
     46 rc-service lighttpd status >/dev/null || fail 'start lighttpd first'
     47 
     48 if grep -Fxq "$include_line" "$main_config"; then
     49     cmp -s "$project/lighttpd.conf" "$vhost_config" ||
     50         fail "the installed vhost differs from $project/lighttpd.conf"
     51 else
     52     if [ -e "$vhost_config" ] || grep -Fq "\"$host\"" "$main_config"; then
     53         fail "an existing lighttpd configuration for $host needs to be reviewed"
     54     fi
     55 fi
     56 
     57 prepare_secrets() {
     58     if [ ! -e "$secrets" ]; then
     59         db_password=$(openssl rand -hex 32)
     60         hmac_key=$(openssl rand -hex 32)
     61         companion_key=$(openssl rand -hex 8)
     62         (
     63             set -C
     64             printf 'INV_DB_PASSWORD=%s\nINV_HMAC_KEY=%s\nINV_COMPANION_KEY=%s\n' \
     65                 "$db_password" "$hmac_key" "$companion_key" > "$secrets"
     66         )
     67     elif grep -Eq '^INV_COMPANION_KEY=[a-f0-9]{64}$' "$secrets"; then
     68         companion_key=$(openssl rand -hex 8)
     69         migrated_secrets=$(mktemp "$project/.env.XXXXXXXX")
     70         if ! sed "s/^INV_COMPANION_KEY=[a-f0-9]\\{64\\}$/INV_COMPANION_KEY=$companion_key/" \
     71             "$secrets" > "$migrated_secrets" || ! mv -f "$migrated_secrets" "$secrets"; then
     72             rm -f "$migrated_secrets"
     73             fail 'could not update the Companion key'
     74         fi
     75         printf '%s\n' 'Replaced the old Companion key with a 16-character key.'
     76     fi
     77     chmod 600 "$secrets"
     78     unset INV_DB_PASSWORD INV_HMAC_KEY INV_COMPANION_KEY
     79 }
     80 
     81 compose() {
     82     docker compose --env-file "$secrets" -f "$compose_file" "$@"
     83 }
     84 
     85 certificate_ready() {
     86     [ -s "$cert_dir/privkey.pem" ] &&
     87         openssl x509 -in "$cert_dir/fullchain.pem" -noout -checkend 86400 >/dev/null 2>&1 &&
     88         openssl x509 -in "$cert_dir/fullchain.pem" -noout -checkhost "$host" |
     89             grep -Fq 'does match certificate'
     90 }
     91 
     92 setup_certificate() {
     93     if certificate_ready && grep -Fxq "$include_line" "$main_config"; then
     94         return
     95     fi
     96 
     97     if [ ! -x "$acme" ]; then
     98         acme_checkout="$project/../acme.sh/acme.sh"
     99         test -f "$acme_checkout" || fail "install acme.sh under $acme_home first"
    100         install -d -m 0700 "$acme_home"
    101         install -m 0700 "$acme_checkout" "$acme"
    102     fi
    103     if ! certificate_ready; then
    104         printf 'Requesting the TLS certificate for %s\n' "$host"
    105         cert_status=0
    106         "$acme" --home "$acme_home" --issue --server letsencrypt \
    107             -d "$host" --keylength ec-256 -w "$webroot" || cert_status=$?
    108         [ "$cert_status" -eq 0 ] || [ "$cert_status" -eq 2 ] ||
    109             fail "certificate issuance failed for $host"
    110     fi
    111 
    112     install -d -m 0700 "$cert_dir"
    113     touch "$cert_dir/fullchain.pem" "$cert_dir/privkey.pem"
    114     chmod 600 "$cert_dir/fullchain.pem" "$cert_dir/privkey.pem"
    115     "$acme" --home "$acme_home" --install-cronjob
    116     "$acme" --home "$acme_home" --install-cert -d "$host" --ecc \
    117         --fullchain-file "$cert_dir/fullchain.pem" --key-file "$cert_dir/privkey.pem" \
    118         --reloadcmd 'if grep -Fxq "include \"/etc/lighttpd/inv.daat.foo.conf\"" /etc/lighttpd/lighttpd.conf; then rc-service lighttpd restart; fi'
    119     certificate_ready || fail "the installed certificate is not valid for $host"
    120 }
    121 
    122 http_code() {
    123     curl --noproxy '*' -sS --max-time 5 -o /dev/null -w '%{http_code}' \
    124         --resolve "$host:443:127.0.0.1" --resolve "$host:80:127.0.0.1" "$1"
    125 }
    126 
    127 check_proxy() {
    128     [ "$(http_code "https://$host/")" = 401 ] &&
    129         [ "$(http_code "https://$host/css/default.css")" = 401 ] &&
    130         [ "$(http_code "https://$host/api/v1/search?q=test")" = 401 ] &&
    131         [ "$(http_code "https://$host/videoplayback")" = 401 ] &&
    132         [ "$(http_code "http://$host/")" = 403 ]
    133 }
    134 
    135 publish_proxy() {
    136     if grep -Fxq "$include_line" "$main_config"; then
    137         check_proxy || fail "check the login and TLS configuration for $host"
    138         return
    139     fi
    140 
    141     previous_config=$(mktemp "$lighttpd_dir/.pre-invidious.XXXXXXXX")
    142     candidate_config=$(mktemp "$lighttpd_dir/.with-invidious.XXXXXXXX")
    143     cp -p "$main_config" "$previous_config"
    144     cp -p "$main_config" "$candidate_config"
    145     printf '\n%s\n' "$include_line" >> "$candidate_config"
    146     install -m 0644 "$project/lighttpd.conf" "$vhost_config"
    147     if ! lighttpd -tt -f "$candidate_config"; then
    148         rm -f "$previous_config" "$candidate_config" "$vhost_config"
    149         fail 'lighttpd rejected the Invidious vhost'
    150     fi
    151     mv -f "$candidate_config" "$main_config"
    152     if rc-service lighttpd restart; then
    153         attempts=0
    154         until check_proxy; do
    155             attempts=$((attempts + 1))
    156             [ "$attempts" -lt 10 ] || break
    157             sleep 1
    158         done
    159         if [ "$attempts" -lt 10 ]; then
    160             printf 'Previous lighttpd configuration saved in %s\n' "$previous_config"
    161             return
    162         fi
    163     fi
    164 
    165     mv -f "$previous_config" "$main_config"
    166     rm -f "$vhost_config"
    167     rc-service lighttpd restart || fail 'the previous config was restored, but lighttpd could not start'
    168     fail 'TLS or login verification failed, and the previous lighttpd configuration was restored'
    169 }
    170 
    171 backend_ready() {
    172     curl --noproxy '*' -fsS --max-time 5 "http://127.0.0.1:$port/css/default.css" 2>/dev/null |
    173         grep -Fq -- '--inv-amber:'
    174 }
    175 
    176 if [ "$mode" = install ]; then
    177     test -r "$font" || fail "the SearXNG Unifont file is missing: $font"
    178     docker info --format '{{.ServerVersion}}' >/dev/null
    179     prepare_secrets
    180     install -m 0644 "$font" "$project/daat-unifont.otf"
    181     compose up -d --build --wait --wait-timeout 120
    182     attempts=0
    183     until backend_ready; do
    184         attempts=$((attempts + 1))
    185         [ "$attempts" -lt 30 ] || fail 'Invidious did not serve its theme, check docker compose logs'
    186         sleep 2
    187     done
    188 else
    189     backend_ready || fail 'the themed Invidious backend is not answering on localhost'
    190 fi
    191 setup_certificate
    192 publish_proxy
    193 printf 'Open https://%s/ and use your SearXNG login.\n' "$host"