2020-05-17 13:43:52 +01:00
#!/usr/bin/env bash
source /mnt/hdd/raspiblitz.conf
# command info
if [ $# -eq 0 ] || [ " $1 " = "-h" ] || [ " $1 " = "--help" ] || [ " $1 " = "-help" ] ; then
echo "the RaspiBlitz Web Interface(s)"
echo "blitz.web.sh [on|off]"
exit 1
fi
# using ${APOST} is a workaround to be able to use sed with '
2020-05-17 20:34:42 +01:00
APOST = \' # close tag for linters: '
2020-05-17 13:43:52 +01:00
###################
# SWITCH ON
###################
if [ " $1 " = "1" ] || [ " $1 " = "on" ] ; then
2020-05-18 20:07:22 +01:00
echo "Turning ON: Web"
2020-05-17 13:43:52 +01:00
# install
sudo apt-get update >/dev/null
2020-05-18 20:07:22 +01:00
sudo apt-get install -y nginx apache2-utils >/dev/null
2020-05-17 13:43:52 +01:00
2020-05-17 14:04:25 +01:00
# make sure that it is enabled and started
2020-05-17 13:43:52 +01:00
sudo systemctl enable nginx >/dev/null
sudo systemctl start nginx
### Welcome Server on HTTP Port 80
sudo rm -f /etc/nginx/sites-enabled/default
sudo rm -f /var/www/html/index.nginx-debian.html
2020-05-17 14:04:25 +01:00
if [ -f /etc/nginx/sites-available/default ] ; then
sudo mv /etc/nginx/sites-available/default /etc/nginx/sites-available/public.conf
else
if ! [ -f /etc/nginx/sites-available/public.conf ] ; then
echo "fail"
exit 1
fi
fi
2020-05-17 13:43:52 +01:00
sudo sed -i 's|root /var/www/html;|root /var/www/public;|g' /etc/nginx/sites-available/public.conf
sudo sed -i 's|index index.html index.htm index.nginx-debian.html;|index index.html;|g' /etc/nginx/sites-available/public.conf
if ! grep -Eq '^\s*sub_filter.*$' /etc/nginx/sites-available/public.conf; then
# search for "location /" entry and add three lines below
2020-05-17 14:04:25 +01:00
sudo sed -i -E ' /^\s *location \/ \{ $/a \
2020-05-17 13:43:52 +01:00
# make sure to have https link to exact same host that was called\n sub_filter '$APOST'<a href="https:\/\/HOST_SET_BY_NGINX\/'$APOST' '$APOST'<a href="https:\/\/$host\/'$APOST';\n' /etc/nginx/sites-available/public.conf
fi
# copy webroot
2020-05-17 14:04:25 +01:00
if ! [ -d /var/www/public ] ; then
sudo cp -a /home/admin/assets/www_public/ /var/www/public
sudo chown www-data:www-data /var/www/public
fi
2020-05-17 13:43:52 +01:00
sudo ln -sf /etc/nginx/sites-available/public.conf /etc/nginx/sites-enabled/public.conf
# open firewall
2020-05-17 14:04:25 +01:00
sudo ufw allow 80 comment 'nginx http_80' 2>/dev/null
2020-05-17 13:43:52 +01:00
### RaspiBlitz Webserver on HTTPS 443
# copy webroot
2020-05-17 14:04:25 +01:00
if ! [ -d /var/www/blitzweb ] ; then
sudo cp -a /home/admin/assets/www_blitzweb/ /var/www/blitzweb
sudo chown www-data:www-data /var/www/blitzweb
fi
2020-05-17 13:43:52 +01:00
2020-05-17 20:34:42 +01:00
# make sure jinja2 is installed and install j2cli
sudo apt-get install python3-jinja2 >/dev/null
sudo -H python3 -m pip install j2cli
2020-05-17 13:43:52 +01:00
# create nginx app-data dir and use LND cert by default
2020-05-17 14:04:25 +01:00
sudo mkdir /mnt/hdd/app-data/nginx/ 2>/dev/null
2020-05-17 13:43:52 +01:00
sudo ln -sf /mnt/hdd/lnd/tls.cert /mnt/hdd/app-data/nginx/tls.cert
sudo ln -sf /mnt/hdd/lnd/tls.key /mnt/hdd/app-data/nginx/tls.key
# config
sudo cp /home/admin/assets/blitzweb.conf /etc/nginx/sites-available/blitzweb.conf
sudo ln -sf /etc/nginx/sites-available/blitzweb.conf /etc/nginx/sites-enabled/
2020-05-18 20:07:22 +01:00
if ! [ -f /etc/nginx/.htpasswd ] ; then
2020-05-19 12:40:33 +01:00
PASSWORD_B = $( sudo cat /mnt/hdd/${ network } /${ network } .conf | grep rpcpassword | cut -c 13-)
echo " ${ PASSWORD_B } " | sudo htpasswd -c -i /etc/nginx/.htpasswd admin
2020-05-18 20:07:22 +01:00
sudo chown www-data:www-data /etc/nginx/.htpasswd
sudo chmod 640 /etc/nginx/.htpasswd
else
sudo chown www-data:www-data /etc/nginx/.htpasswd
sudo chmod 640 /etc/nginx/.htpasswd
fi
2020-05-17 13:43:52 +01:00
# open firewall
2020-05-17 14:04:25 +01:00
sudo ufw allow 443 comment 'nginx https_443' 2>/dev/null
2020-05-17 13:43:52 +01:00
# restart NGINX
sudo systemctl restart nginx
###################
# SWITCH OFF
###################
elif [ " $1 " = "0" ] || [ " $1 " = "off" ] ; then
2020-05-18 20:07:22 +01:00
echo "Turning OFF: Web"
2020-05-17 13:43:52 +01:00
sudo systemctl stop nginx
sudo systemctl disable nginx >/dev/null
else
echo "# FAIL: parameter not known - run with -h for help"
fi