#!/bin/bash use_dnbd3() { mountpoint -q /mnt/store && [ -x "/opt/dnbd3/dnbd3-server" ] } is_dnbd3() { systemctl -q is-active dnbd3-server.service || return 1 systemctl -q is-active dnbd3-ssl-in.service || return 1 systemctl -q is-active dnbd3-ssl-out.service || return 1 return 0 } enable_dnbd3() { echo "Enabling dnbd3 proxy" systemctl disable --now bwlp-master-socat@5006.service systemctl enable --now dnbd3-server.service systemctl enable --now dnbd3-ssl-in.service systemctl enable --now dnbd3-ssl-out.service } disable_dnbd3() { echo "Disabling dnbd3 proxy" systemctl disable --now dnbd3-server.service systemctl disable --now dnbd3-ssl-in.service systemctl disable --now dnbd3-ssl-out.service } is_passthrough() { systemctl -q is-active apache2.service || return 1 systemctl -q is-active bwlp-master-socat@9090.service || return 1 systemctl -q is-active bwlp-master-socat@9091.service || return 1 local lnk=$( readlink -f /etc/apache2/sites-enabled/110-bwlp.conf ) [ "$lnk" = "/etc/apache2/sites-available/110-bwlp-passthrough.conf" ] || return 1 return 0 } is_active() { systemctl -q is-active apache2.service || return 1 systemctl -q is-active bwlp-master.service || return 1 local lnk=$( readlink -f /etc/apache2/sites-enabled/110-bwlp.conf ) [ "$lnk" = "/etc/apache2/sites-available/110-bwlp-active.conf" ] || return 1 return 0 } passthrough() { is_passthrough && return 0 echo "Enabling passthrough" systemctl disable --now bwlp-master.service ln -nfs ../sites-available/110-bwlp-passthrough.conf /etc/apache2/sites-enabled/110-bwlp.conf systemctl restart apache2.service systemctl enable --now bwlp-master-socat@9090.service systemctl enable --now bwlp-master-socat@9091.service systemctl enable --now bwlp-master-socat@9050.service systemctl enable --now bwlp-master-socat@9051.service if ! use_dnbd3; then systemctl enable --now bwlp-master-socat@5006.service fi } activate() { is_active && return 0 echo "Enabling active mode" systemctl disable --now bwlp-master-socat@9090.service systemctl disable --now bwlp-master-socat@9091.service systemctl disable --now bwlp-master-socat@9050.service systemctl disable --now bwlp-master-socat@9051.service systemctl disable --now bwlp-master-socat@5006.service ln -nfs ../sites-available/110-bwlp-active.conf /etc/apache2/sites-enabled/110-bwlp.conf systemctl restart apache2.service systemctl enable --now bwlp-master.service } # Check connectivity if curl -L -m 10 -o /dev/null -sS \ --retry-max-time 40 --retry 4 --retry-all-errors \ https://%DOMAIN%/webif/; then # OK passthrough else # Take over activate fi if use_dnbd3; then is_dnbd3 || enable_dnbd3 else is_dnbd3 && disable_dnbd3 fi exit 0