raspiblitz/home.admin/config.scripts/bonus.lnbits.sh

369 lines
13 KiB
Bash
Raw Normal View History

2020-02-10 12:33:03 +01:00
#!/bin/bash
2020-05-02 19:56:30 +02:00
# https://github.com/lnbits/lnbits
2020-02-10 12:33:03 +01:00
# command info
if [ $# -eq 0 ] || [ "$1" = "-h" ] || [ "$1" = "-help" ]; then
echo "small config script to switch LNbits on or off"
echo "bonus.lnbits.sh [on|off|status|menu|write-macaroons]"
exit 1
2020-02-10 12:33:03 +01:00
fi
2020-04-28 18:01:50 +02:00
source /mnt/hdd/raspiblitz.conf
2020-02-10 12:33:03 +01:00
# show info menu
if [ "$1" = "menu" ]; then
2020-07-14 21:04:14 +02:00
# get LNbits status info
2020-02-10 12:33:03 +01:00
echo "# collecting status info ... (please wait)"
source <(sudo /home/admin/config.scripts/bonus.lnbits.sh status)
if [ ${#ip2torWarn} -gt 0 ]; then
whiptail --title " Warning " --msgbox "Your IP2TOR+LetsEncrypt may have problems:\n${ip2torWarn}" 8 55
fi
2020-07-14 21:04:14 +02:00
text="Local Webrowser: https://${localIP}:${httpsPort}"
if [ ${#publicDomain} -gt 0 ]; then
text="${text}
Public Domain: https://${publicDomain}:${httpsPort}
port forwarding on router needs to be active & may change port"
fi
text="${text}
2020-07-14 21:11:59 +02:00
SHA1 ${sslFingerprintIP}"
2020-02-10 12:33:03 +01:00
2020-07-14 21:04:14 +02:00
if [ "${runBehindTor}" = "on" ] && [ ${#toraddress} -gt 0 ]; then
2020-02-10 12:33:03 +01:00
/home/admin/config.scripts/blitz.lcd.sh qr "${toraddress}"
2020-07-14 21:04:14 +02:00
text="${text}\n
2020-07-14 21:17:05 +02:00
TOR Browser Hidden Service address (QR see LCD):
2020-07-14 21:04:14 +02:00
${toraddress}"
fi
if [ ${#ip2torDomain} -gt 0 ]; then
2020-07-14 23:06:09 +02:00
text="${text}\n
2020-07-14 21:04:14 +02:00
IP2TOR+LetsEncrypt: https://${ip2torDomain}:${ip2torPort}
2020-07-14 23:06:09 +02:00
SHA1 ${sslFingerprintTOR}"
2020-07-14 21:04:14 +02:00
elif [ ${#ip2torIP} -gt 0 ]; then
2020-07-14 22:56:20 +02:00
text="${text}\n
2020-07-14 21:04:14 +02:00
IP2TOR: https://${ip2torIP}:${ip2torPort}
2020-07-14 21:11:59 +02:00
SHA1 ${sslFingerprintTOR}
2020-07-14 21:04:14 +02:00
go MAINMENU > SUBSCRIBE and add LetsEncrypt HTTPS Domain"
elif [ ${#publicDomain} -eq 0 ]; then
2020-07-14 21:10:19 +02:00
text="${text}\n
2020-08-12 09:44:49 +02:00
To enable easy reachability with normal browser from the outside
2020-07-14 21:17:05 +02:00
consider adding a IP2TOR Bridge (MAINMENU > SUBSCRIBE)."
2020-02-10 12:33:03 +01:00
fi
2020-07-14 22:56:20 +02:00
whiptail --title " LNbits " --msgbox "${text}" 15 69
2020-07-14 21:04:14 +02:00
/home/admin/config.scripts/blitz.lcd.sh hide
2020-02-10 12:33:03 +01:00
echo "please wait ..."
exit 0
fi
# add default value to raspi config if needed
2020-04-23 23:21:32 +02:00
if ! grep -Eq "^LNBits=" /mnt/hdd/raspiblitz.conf; then
echo "LNBits=off" >> /mnt/hdd/raspiblitz.conf
2020-02-10 12:33:03 +01:00
fi
# status
if [ "$1" = "status" ]; then
2020-04-23 23:08:14 +02:00
if [ "${LNBits}" = "on" ]; then
2020-02-10 12:33:03 +01:00
echo "installed=1"
localIP=$(ip addr | grep 'state UP' -A2 | egrep -v 'docker0' | grep 'eth0\|wlan0' | tail -n1 | awk '{print $2}' | cut -f1 -d'/')
2020-07-14 20:29:33 +02:00
echo "localIP='${localIP}'"
echo "httpsPort='5001'"
echo "publicIP='${publicIP}'"
# check for LetsEnryptDomain for DynDns
error=""
source <(sudo /home/admin/config.scripts/blitz.subscriptions.ip2tor.py ip-by-tor $publicIP)
if [ ${#error} -eq 0 ]; then
echo "publicDomain='${domain}'"
fi
2020-07-14 20:35:37 +02:00
sslFingerprintIP=$(openssl x509 -in /mnt/hdd/app-data/nginx/tls.cert -fingerprint -noout 2>/dev/null | cut -d"=" -f2)
2020-07-14 20:29:33 +02:00
echo "sslFingerprintIP='${sslFingerprintIP}'"
toraddress=$(sudo cat /mnt/hdd/tor/lnbits/hostname 2>/dev/null)
echo "toraddress='${toraddress}'"
2020-07-14 20:35:37 +02:00
sslFingerprintTOR=$(openssl x509 -in /mnt/hdd/app-data/nginx/tor_tls.cert -fingerprint -noout 2>/dev/null | cut -d"=" -f2)
2020-07-14 20:29:33 +02:00
echo "sslFingerprintTOR='${sslFingerprintTOR}'"
# check for IP2TOR
error=""
source <(sudo /home/admin/config.scripts/blitz.subscriptions.ip2tor.py ip-by-tor $toraddress)
if [ ${#error} -eq 0 ]; then
echo "ip2torType='${ip2tor-v1}'"
2020-07-14 20:36:04 +02:00
echo "ip2torID='${id}'"
2020-07-14 20:29:33 +02:00
echo "ip2torIP='${ip}'"
echo "ip2torPort='${port}'"
# check for LetsEnryptDomain on IP2TOR
error=""
source <(sudo /home/admin/config.scripts/blitz.subscriptions.letsencrypt.py domain-by-ip $ip)
if [ ${#error} -eq 0 ]; then
echo "ip2torDomain='${domain}'"
domainWarning=$(sudo /home/admin/config.scripts/blitz.subscriptions.letsencrypt.py subscription-detail ${domain} ${port} | jq -r ".warning")
if [ ${#domainWarning} -gt 0 ]; then
echo "ip2torWarn='${domainWarning}'"
fi
2020-07-14 20:29:33 +02:00
fi
fi
2020-02-10 12:33:03 +01:00
# check for error
isDead=$(sudo systemctl status lnbits | grep -c 'inactive (dead)')
if [ ${isDead} -eq 1 ]; then
echo "error='Service Failed'"
exit 1
fi
else
echo "installed=0"
fi
exit 0
fi
# status
if [ "$1" = "write-macaroons" ]; then
2020-02-10 12:33:03 +01:00
# make sure its run as user admin
adminUserId=$(id -u admin)
2020-02-10 12:47:33 +01:00
if [ "${EUID}" != "${adminUserId}" ]; then
2020-02-10 12:33:03 +01:00
echo "error='please run as admin user'"
exit 1
fi
2020-05-01 11:15:32 +02:00
echo "make sure symlink to central app-data directory exists"
if ! [[ -L "/home/lnbits/.lnd" ]]; then
sudo rm -rf "/home/lnbits/.lnd" # not a symlink.. delete it silently
sudo ln -s "/mnt/hdd/app-data/lnd/" "/home/lnbits/.lnd" # and create symlink
fi
# set tls.cert path (use | as separator to avoid escaping file path slashes)
sudo -u lnbits sed -i "s|^LND_REST_CERT=.*|LND_REST_CERT=/home/lnbits/.lnd/tls.cert|g" /home/lnbits/lnbits/.env
2020-05-01 11:15:32 +02:00
# set macaroon path info in .env - USING HEX IMPORT
sudo chmod 600 /home/lnbits/lnbits/.env
2020-05-04 15:32:18 +02:00
macaroonAdminHex=$(sudo xxd -ps -u -c 1000 /home/lnbits/.lnd/data/chain/${network}/${chain}net/admin.macaroon)
macaroonInvoiceHex=$(sudo xxd -ps -u -c 1000 /home/lnbits/.lnd/data/chain/${network}/${chain}net/invoice.macaroon)
macaroonReadHex=$(sudo xxd -ps -u -c 1000 /home/lnbits/.lnd/data/chain/${network}/${chain}net/readonly.macaroon)
sudo sed -i "s/^LND_REST_ADMIN_MACAROON=.*/LND_REST_ADMIN_MACAROON=${macaroonAdminHex}/g" /home/lnbits/lnbits/.env
sudo sed -i "s/^LND_REST_INVOICE_MACAROON=.*/LND_REST_INVOICE_MACAROON=${macaroonInvoiceHex}/g" /home/lnbits/lnbits/.env
sudo sed -i "s/^LND_REST_READ_MACAROON=.*/LND_REST_READ_MACAROON=${macaroonReadHex}/g" /home/lnbits/lnbits/.env
#echo "make sure lnbits is member of lndreadonly, lndinvoice, lndadmin"
#sudo /usr/sbin/usermod --append --groups lndinvoice lnbits
#sudo /usr/sbin/usermod --append --groups lndreadonly lnbits
#sudo /usr/sbin/usermod --append --groups lndadmin lnbits
2020-05-04 15:32:18 +02:00
# set macaroon path info in .env - USING PATH
#sudo sed -i "s|^LND_REST_ADMIN_MACAROON=.*|LND_REST_ADMIN_MACAROON=/home/lnbits/.lnd/data/chain/${network}/${chain}net/admin.macaroon|g" /home/lnbits/lnbits/.env
#sudo sed -i "s|^LND_REST_INVOICE_MACAROON=.*|LND_REST_INVOICE_MACAROON=/home/lnbits/.lnd/data/chain/${network}/${chain}net/invoice.macaroon|g" /home/lnbits/lnbits/.env
#sudo sed -i "s|^LND_REST_READ_MACAROON=.*|LND_REST_READ_MACAROON=/home/lnbits/.lnd/data/chain/${network}/${chain}net/read.macaroon|g" /home/lnbits/lnbits/.env
echo "# OK - macaroons written to /home/lnbits/lnbits/.env"
2020-02-10 12:33:03 +01:00
exit 0
fi
# stop service
echo "making sure services are not running"
sudo systemctl stop lnbits 2>/dev/null
# switch on
if [ "$1" = "1" ] || [ "$1" = "on" ]; then
echo "*** INSTALL LNbits ***"
2020-02-10 12:33:03 +01:00
isInstalled=$(sudo ls /etc/systemd/system/lnbits.service 2>/dev/null | grep -c 'lnbits.service')
if [ ${isInstalled} -eq 0 ]; then
2020-04-28 09:27:03 +01:00
echo "*** Add the 'lnbits' user ***"
sudo adduser --disabled-password --gecos "" lnbits
2020-02-10 12:33:03 +01:00
# make sure needed debian packages are installed
echo "# installing needed packages"
sudo apt-get install -y pipenv 2>/dev/null
# install from GitHub
2020-02-10 12:33:03 +01:00
echo "# get the github code"
2020-04-28 09:27:03 +01:00
sudo rm -r /home/lnbits/lnbits 2>/dev/null
cd /home/lnbits
2020-07-14 22:56:20 +02:00
sudo -u lnbits git clone https://github.com/lnbits/lnbits.git
2020-04-28 09:27:03 +01:00
cd /home/lnbits/lnbits
2020-07-19 17:06:36 +02:00
if [ "$2" == "master" ]; then
2020-07-19 17:05:35 +02:00
echo "# checking out master branch"
sudo -u lnbits git checkout
else
echo "# checking out tag 'raspiblitz'"
sudo -u lnbits git checkout tags/raspiblitz
fi
# prepare .env file
2020-02-10 12:33:03 +01:00
echo "# preparing env file"
2020-04-28 09:27:03 +01:00
sudo rm /home/lnbits/lnbits/.env 2>/dev/null
sudo -u lnbits touch /home/lnbits/lnbits/.env
sudo bash -c "echo 'FLASK_APP=lnbits' >> /home/lnbits/lnbits/.env"
sudo bash -c "echo 'FLASK_ENV=production' >> /home/lnbits/lnbits/.env"
sudo bash -c "echo 'LNBITS_FORCE_HTTPS=0' >> /home/lnbits/lnbits/.env"
2020-05-02 19:56:30 +02:00
sudo bash -c "echo 'LNBITS_BACKEND_WALLET_CLASS=LndRestWallet' >> /home/lnbits/lnbits/.env"
2020-05-02 19:54:57 +01:00
sudo bash -c "echo 'LND_REST_ENDPOINT=https://127.0.0.1:8080' >> /home/lnbits/lnbits/.env"
2020-05-02 19:56:30 +02:00
sudo bash -c "echo 'LND_REST_CERT=' >> /home/lnbits/lnbits/.env"
sudo bash -c "echo 'LND_REST_ADMIN_MACAROON=' >> /home/lnbits/lnbits/.env"
sudo bash -c "echo 'LND_REST_INVOICE_MACAROON=' >> /home/lnbits/lnbits/.env"
sudo bash -c "echo 'LND_REST_READ_MACAROON=' >> /home/lnbits/lnbits/.env"
2020-04-28 09:27:03 +01:00
/home/admin/config.scripts/bonus.lnbits.sh write-macaroons
2020-02-10 21:07:24 +01:00
# set database path to HDD data so that its survives updates and migrations
2020-04-23 08:07:49 +02:00
sudo mkdir /mnt/hdd/app-data/LNBits 2>/dev/null
2020-04-28 09:27:03 +01:00
sudo chown lnbits:lnbits -R /mnt/hdd/app-data/LNBits
sudo bash -c "echo 'LNBITS_DATA_FOLDER=/mnt/hdd/app-data/LNBits' >> /home/lnbits/lnbits/.env"
2020-02-10 12:33:03 +01:00
2020-02-10 14:29:28 +01:00
# to the install
2020-04-24 06:52:33 +02:00
echo "# installing application dependencies"
2020-04-28 09:27:03 +01:00
cd /home/lnbits/lnbits
# do install like this
sudo -u lnbits pipenv run pip install python-dotenv
sudo -u lnbits pipenv run pip install -r requirements.txt
# instead of this
#sudo -u lnbits pipenv install
#sudo -u lnbits /usr/bin/pipenv run pip install python-dotenv
2020-07-22 21:20:34 +02:00
# update databases (if needed)
2020-04-24 06:52:33 +02:00
echo "# updating databases"
sudo -u lnbits pipenv run flask migrate
2020-04-24 06:52:33 +02:00
2020-02-10 12:33:03 +01:00
# open firewall
2020-02-10 13:15:18 +01:00
echo
2020-02-10 12:33:03 +01:00
echo "*** Updating Firewall ***"
2020-05-23 22:17:00 +01:00
sudo ufw allow 5001 comment 'lnbits'
2020-02-10 12:33:03 +01:00
echo ""
# install service
2020-02-10 14:29:28 +01:00
echo "*** Install systemd ***"
2020-05-23 22:17:00 +01:00
cat <<EOF | sudo tee /etc/systemd/system/lnbits.service >/dev/null
2020-02-10 18:16:55 +01:00
# systemd unit for lnbits
2020-02-10 12:33:03 +01:00
[Unit]
Description=lnbits
Wants=lnd.service
After=lnd.service
[Service]
2020-04-28 09:27:03 +01:00
WorkingDirectory=/home/lnbits/lnbits
2020-05-23 22:17:00 +01:00
ExecStart=/bin/sh -c 'cd /home/lnbits/lnbits && pipenv run gunicorn -b 127.0.0.1:5000 lnbits:app -k gevent'
2020-04-28 09:27:03 +01:00
User=lnbits
2020-02-10 12:33:03 +01:00
Restart=always
TimeoutSec=120
RestartSec=30
StandardOutput=null
StandardError=journal
2020-02-10 18:40:53 +01:00
[Install]
WantedBy=multi-user.target
2020-02-10 12:33:03 +01:00
EOF
sudo systemctl enable lnbits
2020-07-19 17:56:00 +02:00
source /home/admin/raspiblitz.info
if [ "${state}" == "ready" ]; then
2020-07-19 17:56:39 +02:00
echo "# OK - lnbits service is enabled, system is on ready so starting lnbits service"
2020-07-19 17:56:00 +02:00
sudo systemctl start lnbits
else
echo "# OK - lnbits service is enabled, but needs reboot or manual starting: sudo systemctl start lnbits"
fi
2020-02-10 12:33:03 +01:00
else
echo "LNbits already installed."
2020-02-10 12:33:03 +01:00
fi
2020-05-23 22:34:03 +01:00
# setup nginx symlinks
if ! [ -f /etc/nginx/sites-available/lnbits_ssl.conf ]; then
sudo cp /home/admin/assets/nginx/sites-available/lnbits_ssl.conf /etc/nginx/sites-available/lnbits_ssl.conf
fi
if ! [ -f /etc/nginx/sites-available/lnbits_tor.conf ]; then
sudo cp /home/admin/assets/nginx/sites-available/lnbits_tor.conf /etc/nginx/sites-available/lnbits_tor.conf
fi
if ! [ -f /etc/nginx/sites-available/lnbits_tor_ssl.conf ]; then
sudo cp /home/admin/assets/nginx/sites-available/lnbits_tor_ssl.conf /etc/nginx/sites-available/lnbits_tor_ssl.conf
fi
sudo ln -sf /etc/nginx/sites-available/lnbits_ssl.conf /etc/nginx/sites-enabled/
sudo ln -sf /etc/nginx/sites-available/lnbits_tor.conf /etc/nginx/sites-enabled/
sudo ln -sf /etc/nginx/sites-available/lnbits_tor_ssl.conf /etc/nginx/sites-enabled/
2020-05-23 22:34:03 +01:00
sudo nginx -t
sudo systemctl reload nginx
2020-02-10 12:33:03 +01:00
# setting value in raspi blitz config
2020-04-23 23:08:14 +02:00
sudo sed -i "s/^LNBits=.*/LNBits=on/g" /mnt/hdd/raspiblitz.conf
2020-02-10 14:29:28 +01:00
# Hidden Service if Tor is active
2020-02-10 12:33:03 +01:00
source /mnt/hdd/raspiblitz.conf
if [ "${runBehindTor}" = "on" ]; then
2020-07-18 15:26:56 +02:00
# make sure to keep in sync with internet.tor.sh script
2020-05-24 17:55:48 +01:00
/home/admin/config.scripts/internet.hiddenservice.sh lnbits 80 5002 443 5003
2020-02-10 12:33:03 +01:00
fi
exit 0
fi
# switch off
if [ "$1" = "0" ] || [ "$1" = "off" ]; then
2020-07-24 02:00:47 +02:00
# check for second parameter: should data be deleted?
deleteData=0
if [ "$2" = "--delete-data" ]; then
deleteData=1
elif [ "$2" = "--keep-data" ]; then
deleteData=0
else
2020-07-24 23:13:23 +02:00
if (whiptail --title " DELETE DATA? " --yesno "Do you want want to delete\nthe LNbits Server Data?" 8 30); then
2020-07-24 02:00:47 +02:00
deleteData=1
else
deleteData=0
fi
fi
echo "# deleteData(${deleteData})"
2020-02-10 12:33:03 +01:00
# setting value in raspi blitz config
2020-04-23 23:08:14 +02:00
sudo sed -i "s/^LNBits=.*/LNBits=off/g" /mnt/hdd/raspiblitz.conf
2020-02-10 12:33:03 +01:00
2020-05-23 22:34:03 +01:00
# remove nginx symlinks
sudo rm -f /etc/nginx/sites-enabled/lnbits_ssl.conf
sudo rm -f /etc/nginx/sites-enabled/lnbits_tor.conf
sudo rm -f /etc/nginx/sites-enabled/lnbits_tor_ssl.conf
2020-06-26 22:57:12 +02:00
sudo rm -f /etc/nginx/sites-available/lnbits_ssl.conf
sudo rm -f /etc/nginx/sites-available/lnbits_tor.conf
sudo rm -f /etc/nginx/sites-available/lnbits_tor_ssl.conf
2020-05-23 22:34:03 +01:00
sudo nginx -t
sudo systemctl reload nginx
# Hidden Service if Tor is active
if [ "${runBehindTor}" = "on" ]; then
/home/admin/config.scripts/internet.hiddenservice.sh off lnbits
fi
2020-02-10 12:33:03 +01:00
isInstalled=$(sudo ls /etc/systemd/system/lnbits.service 2>/dev/null | grep -c 'lnbits.service')
2020-04-23 23:08:14 +02:00
if [ ${isInstalled} -eq 1 ] || [ "${LNBits}" == "on" ]; then
echo "*** REMOVING LNbits ***"
2020-02-10 12:33:03 +01:00
sudo systemctl stop lnbits
sudo systemctl disable lnbits
sudo rm /etc/systemd/system/lnbits.service
2020-04-28 09:27:03 +01:00
sudo userdel -rf lnbits
2020-07-24 02:00:47 +02:00
if [ ${deleteData} -eq 1 ]; then
echo "# deleting data"
sudo rm -R /mnt/hdd/app-data/LNBits
else
echo "# keeping data"
fi
echo "OK LNbits removed."
else
echo "LNbits is not installed."
2020-02-10 12:33:03 +01:00
fi
exit 0
fi
echo "FAIL - Unknown Parameter $1"
exit 1