2018-12-11 13:15:15 +01:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
# This script runs on after start in background
|
|
|
|
# as a service and gets restarted on failure
|
|
|
|
# it runs ALMOST every seconds
|
|
|
|
|
|
|
|
# INFOFILE - state data from bootstrap
|
|
|
|
infoFile="/home/admin/raspiblitz.info"
|
|
|
|
|
|
|
|
# CONFIGFILE - configuration of RaspiBlitz
|
|
|
|
configFile="/mnt/hdd/raspiblitz.conf"
|
|
|
|
|
2018-12-23 14:20:13 +01:00
|
|
|
# LOGS see: sudo journalctl -f -u background
|
|
|
|
|
2018-12-11 13:15:15 +01:00
|
|
|
# Check if HDD contains configuration
|
|
|
|
configExists=$(ls ${configFile} | grep -c '.conf')
|
|
|
|
if [ ${configExists} -eq 1 ]; then
|
|
|
|
source ${configFile}
|
2019-02-02 23:49:04 +01:00
|
|
|
else
|
|
|
|
source ${infoFile}
|
2018-12-11 13:15:15 +01:00
|
|
|
fi
|
|
|
|
|
2018-12-23 14:31:58 +01:00
|
|
|
echo "_background.sh STARTED"
|
|
|
|
|
2019-04-15 13:31:28 +02:00
|
|
|
# monitor lost LND sync
|
|
|
|
syncedSince=0
|
|
|
|
lastSyncState=0
|
|
|
|
|
2018-12-11 13:15:15 +01:00
|
|
|
counter=0
|
|
|
|
while [ 1 ]
|
|
|
|
do
|
|
|
|
|
|
|
|
###############################
|
|
|
|
# Prepare this loop
|
|
|
|
###############################
|
|
|
|
|
|
|
|
# count up
|
|
|
|
counter=$(($counter+1))
|
|
|
|
|
2018-12-22 16:44:15 +01:00
|
|
|
# gather the uptime seconds
|
|
|
|
upSeconds=$(cat /proc/uptime | grep -o '^[0-9]\+')
|
|
|
|
|
2018-12-11 13:15:15 +01:00
|
|
|
####################################################
|
|
|
|
# RECHECK DHCP-SERVER
|
|
|
|
# https://github.com/rootzoll/raspiblitz/issues/160
|
|
|
|
####################################################
|
|
|
|
|
|
|
|
# every 5 minutes
|
2018-12-11 13:33:22 +01:00
|
|
|
recheckDHCP=$((($counter % 300)+1))
|
2018-12-11 13:35:00 +01:00
|
|
|
if [ ${recheckDHCP} -eq 1 ]; then
|
2018-12-11 13:15:15 +01:00
|
|
|
echo "*** RECHECK DHCP-SERVER ***"
|
|
|
|
|
|
|
|
# get the local network IP
|
|
|
|
localip=$(ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1 -d'/')
|
|
|
|
echo "localip(${localip})"
|
|
|
|
|
|
|
|
# detect a missing DHCP config
|
|
|
|
if [ "${localip:0:4}" = "169." ]; then
|
|
|
|
echo "Missing DHCP detected ... trying emergency reboot"
|
|
|
|
sudo shutdown -r now
|
|
|
|
else
|
|
|
|
echo "DHCP OK"
|
|
|
|
fi
|
|
|
|
|
|
|
|
fi
|
|
|
|
|
2019-03-15 15:43:16 +01:00
|
|
|
####################################################
|
|
|
|
# CHECK FOR UNDERVOLTAGE REPORTS
|
|
|
|
# every 1 hour scan for undervoltage reports
|
|
|
|
####################################################
|
|
|
|
recheckUndervoltage=$(($counter % 3600))
|
|
|
|
if [ ${recheckUndervoltage} -eq 1 ]; then
|
|
|
|
echo "*** RECHECK UNDERVOLTAGE ***"
|
|
|
|
countReports=$(sudo cat /var/log/syslog | grep -c "Under-voltage detected!")
|
|
|
|
echo "${countReports} undervoltage reports found in syslog"
|
|
|
|
if [ ${#undervoltageReports} -eq 0 ]; then
|
|
|
|
# write new value to info file
|
|
|
|
undervoltageReports="${countReports}"
|
|
|
|
echo "undervoltageReports=${undervoltageReports}" >> ${infoFile}
|
|
|
|
else
|
|
|
|
# update value in info file
|
|
|
|
sed -i "s/^undervoltageReports=.*/undervoltageReports=${countReports}/g" ${infoFile}
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2018-12-11 13:15:15 +01:00
|
|
|
####################################################
|
|
|
|
# RECHECK PUBLIC IP
|
|
|
|
# when public IP changes, restart LND with new IP
|
|
|
|
####################################################
|
|
|
|
|
|
|
|
# every 15min - not too often
|
|
|
|
# because its a ping to external service
|
2019-03-14 14:19:00 +01:00
|
|
|
recheckPublicIP=$((($counter % 900)+1))
|
2019-04-03 03:58:10 +02:00
|
|
|
# prevent when lndAddress is set
|
|
|
|
if [ ${#lndAddress} -gt 3 ]; then
|
|
|
|
recheckPublicIP=0
|
|
|
|
fi
|
2018-12-13 21:59:58 +01:00
|
|
|
updateDynDomain=0
|
2018-12-11 13:35:00 +01:00
|
|
|
if [ ${recheckPublicIP} -eq 1 ]; then
|
2018-12-11 13:15:15 +01:00
|
|
|
echo "*** RECHECK PUBLIC IP ***"
|
|
|
|
|
|
|
|
# execute only after setup when config exists
|
|
|
|
if [ ${configExists} -eq 1 ]; then
|
|
|
|
|
|
|
|
# get actual public IP
|
|
|
|
freshPublicIP=$(curl -s http://v4.ipv6-test.com/api/myip.php 2>/dev/null)
|
|
|
|
|
2019-03-14 13:54:45 +01:00
|
|
|
# sanity check on IP data
|
|
|
|
# see https://github.com/rootzoll/raspiblitz/issues/371#issuecomment-472416349
|
2019-04-10 00:32:14 +02:00
|
|
|
echo "-> sanity check of new IP data"
|
2019-03-14 14:08:20 +01:00
|
|
|
if [[ $freshPublicIP =~ ^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$ ]]; then
|
2019-03-14 13:54:45 +01:00
|
|
|
echo "OK IPv6"
|
2019-03-14 14:08:20 +01:00
|
|
|
elif [[ $freshPublicIP =~ ^([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$ ]]; then
|
|
|
|
echo "OK IPv4"
|
2019-03-14 13:54:45 +01:00
|
|
|
else
|
|
|
|
echo "FAIL - not an IPv4 or IPv6 address"
|
|
|
|
freshPublicIP=""
|
|
|
|
fi
|
|
|
|
|
2019-01-21 21:40:48 +01:00
|
|
|
if [ ${#freshPublicIP} -eq 0 ]; then
|
|
|
|
|
|
|
|
echo "freshPublicIP is ZERO - ignoring"
|
|
|
|
|
2018-12-11 13:15:15 +01:00
|
|
|
# check if changed
|
2019-01-21 21:40:48 +01:00
|
|
|
elif [ "${freshPublicIP}" != "${publicIP}" ]; then
|
2018-12-11 13:15:15 +01:00
|
|
|
|
|
|
|
# 1) update config file
|
|
|
|
echo "update config value"
|
2019-04-03 03:58:10 +02:00
|
|
|
sed -i "s/^publicIP=.*/publicIP='${freshPublicIP}'/g" ${configFile}
|
|
|
|
publicIP='${freshPublicIP}'
|
2018-12-11 13:15:15 +01:00
|
|
|
|
2018-12-22 16:44:15 +01:00
|
|
|
# 2) only restart LND if dynDNS is activated
|
|
|
|
# because this signals that user wants "public node"
|
|
|
|
if [ ${#dynDomain} -gt 0 ]; then
|
|
|
|
echo "restart LND with new environment config"
|
|
|
|
# restart and let to auto-unlock (if activated) do the rest
|
|
|
|
sudo systemctl restart lnd.service
|
|
|
|
fi
|
2018-12-11 13:15:15 +01:00
|
|
|
|
2018-12-22 16:44:15 +01:00
|
|
|
# 2) trigger update if dnyamic domain (if set)
|
2018-12-13 21:59:58 +01:00
|
|
|
updateDynDomain=1
|
|
|
|
|
2018-12-11 13:15:15 +01:00
|
|
|
else
|
|
|
|
echo "public IP has not changed"
|
|
|
|
fi
|
|
|
|
|
|
|
|
else
|
|
|
|
echo "skip - because setup is still running"
|
|
|
|
fi
|
|
|
|
|
|
|
|
fi
|
|
|
|
|
2019-04-15 13:31:28 +02:00
|
|
|
|
|
|
|
###############################
|
|
|
|
# LND MONITOR LOST SYNC
|
|
|
|
###############################
|
|
|
|
|
|
|
|
# check every 5min
|
|
|
|
recheckSync=$(($counter % 300))
|
|
|
|
if [ ${recheckSync} -eq 1 ]; then
|
|
|
|
source ${configFile}
|
|
|
|
echo "LND MONITOR LOST SYNC ..."
|
|
|
|
lndSynced=$(sudo -u bitcoin /usr/local/bin/lncli --chain=${network} --network=${chain}net getinfo 2>/dev/null | jq -r '.synced_to_chain' | grep -c true)
|
|
|
|
echo "lndSynced(${lndSynced})"
|
|
|
|
echo "syncedSince(${syncedSince})"
|
|
|
|
echo "lastSyncState(${lastSyncState})"
|
|
|
|
if [ ${lndSynced} -eq ${lastSyncState} ]; then
|
|
|
|
|
|
|
|
echo "no sync change"
|
|
|
|
if [ ${lndSynced} -eq 1 ]; then
|
|
|
|
echo "all is good - LND still in sync now for:"
|
|
|
|
actualSecondsTimestamp=$(date +%s)
|
|
|
|
secondsInSync=$(echo "${actualSecondsTimestamp}-"${syncedSince} | bc)
|
|
|
|
echo "${secondsInSync} seconds"
|
|
|
|
if [ "${backupTorrentSeeding}" == "on" ]; then
|
|
|
|
echo "Backup Torrent Seeding is ON - check if already running"
|
|
|
|
source <(sudo -u admin /home/admin/50torrentHDD.sh status)
|
|
|
|
if [ "${baseSeeding}" == "0" ] || [ "${updateSeeding}" == "0" ]; then
|
|
|
|
echo "---> STARTING Backup Torrent Seeding"
|
|
|
|
sudo -u admin /home/admin/50torrentHDD.sh backup-torrent-hosting
|
|
|
|
else
|
|
|
|
echo "Backup Torrent Seeding - already running"
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo "Backup Torrent Seeding is OFF"
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo "still not in sync"
|
|
|
|
if [ ${syncedSince} -gt 0 ]; then
|
|
|
|
|
|
|
|
echo "was in sync at least once since rinning but lost now for:"
|
|
|
|
actualSecondsTimestamp=$(date +%s)
|
|
|
|
secondsOutOfSync=$(echo "${actualSecondsTimestamp}-"${syncedSince} | bc)
|
|
|
|
echo "${secondsOutOfSync} seconds"
|
|
|
|
|
|
|
|
# when >10min out of sync
|
|
|
|
if [ ${secondsOutOfSync} -gt 600 ]; then
|
|
|
|
echo "! LND fell out of sync for longer then 10 minutes !"
|
|
|
|
if [ "${backupTorrentSeeding}" == "on" ]; then
|
|
|
|
echo "Backup Torrent Seeding is ON - check if still running"
|
|
|
|
source <(sudo -u admin /home/admin/50torrentHDD.sh status)
|
|
|
|
if [ "${baseSeeding}" == "1" ] || [ "${updateSeeding}" == "1" ]; then
|
|
|
|
echo "---> STOPPING Backup Torrent Seeding"
|
|
|
|
sudo -u admin /home/admin/50torrentHDD.sh stop
|
|
|
|
else
|
|
|
|
echo "No Backup Torrent Seeding - already stopped"
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo "Backup Torrent Seeding is OFF"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
# when >1h out of sync
|
|
|
|
if [ ${secondsOutOfSync} -gt 3600 ]; then
|
|
|
|
echo "!!!! LND fell out of sync for longer then 1 hour !!!"
|
|
|
|
# TODO: When auto-unlock is ON --> consider implementing restart (this sometimes help)
|
|
|
|
fi
|
|
|
|
|
|
|
|
else
|
|
|
|
echo "LND was never in sync since since started (could be multiple reasons - dont act)"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
else
|
|
|
|
echo "sync change detected"
|
|
|
|
if [ ${lastSyncState} -eq 1 ] && [ ${lndSynced} -eq 0 ]; then
|
|
|
|
echo "--> LND SNC LOST"
|
|
|
|
else
|
|
|
|
if [ ${syncedSince} -eq 0 ]; then
|
|
|
|
echo "--> LND SYNC GAINED"
|
|
|
|
else
|
|
|
|
echo "--> LND SYNC RECOVERED"
|
|
|
|
fi
|
|
|
|
syncedSince=$(date +%s)
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
lastSyncState=${lastSyncState}
|
|
|
|
fi
|
|
|
|
|
2018-12-13 21:59:58 +01:00
|
|
|
###############################
|
2018-12-22 16:44:15 +01:00
|
|
|
# LND AUTO-UNLOCK
|
|
|
|
###############################
|
|
|
|
|
|
|
|
# check every 10secs
|
|
|
|
recheckAutoUnlock=$((($counter % 10)+1))
|
|
|
|
if [ ${recheckAutoUnlock} -eq 1 ]; then
|
|
|
|
|
|
|
|
# check if auto-unlock feature if activated
|
|
|
|
if [ "${autoUnlock}" = "on" ]; then
|
|
|
|
|
|
|
|
# check if lnd is locked
|
|
|
|
locked=$(sudo -u bitcoin /usr/local/bin/lncli --chain=${network} --network=${chain}net getinfo 2>&1 | grep -c unlock)
|
|
|
|
if [ ${locked} -gt 0 ]; then
|
|
|
|
|
2018-12-23 15:47:01 +01:00
|
|
|
echo "STARTING AUTO-UNLOCK ..."
|
|
|
|
|
2018-12-23 22:11:21 +01:00
|
|
|
# building REST command
|
2019-01-18 00:46:32 +01:00
|
|
|
passwordC=$(sudo cat /root/lnd.autounlock.pwd)
|
2019-01-18 01:00:33 +01:00
|
|
|
command="sudo python /home/admin/config.scripts/lnd.unlock.py '${passwordC}'"
|
2019-01-18 01:55:38 +01:00
|
|
|
bash -c "${command}"
|
2019-04-03 04:01:11 +02:00
|
|
|
|
2018-12-22 16:44:15 +01:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
###############################
|
2018-12-13 21:59:58 +01:00
|
|
|
# UPDATE DYNAMIC DOMAIN
|
|
|
|
# like afraid.org
|
2018-12-13 23:09:16 +01:00
|
|
|
# ! experimental
|
2018-12-13 21:59:58 +01:00
|
|
|
###############################
|
|
|
|
|
2018-12-13 23:08:23 +01:00
|
|
|
# if not activated above, update every 6 hours
|
2018-12-13 22:33:02 +01:00
|
|
|
if [ ${updateDynDomain} -eq 0 ]; then
|
2018-12-13 21:59:58 +01:00
|
|
|
# dont +1 so that it gets executed on first loop
|
2018-12-13 23:08:23 +01:00
|
|
|
updateDynDomain=$(($counter % 21600))
|
2018-12-13 21:59:58 +01:00
|
|
|
fi
|
|
|
|
if [ ${updateDynDomain} -eq 1 ]; then
|
|
|
|
echo "*** UPDATE DYNAMIC DOMAIN ***"
|
|
|
|
# check if update URL for dyn Domain is set
|
2019-01-18 23:44:49 +01:00
|
|
|
if [ ${#dynUpdateUrl} -gt 6 ]; then
|
2018-12-13 21:59:58 +01:00
|
|
|
# calling the update url
|
|
|
|
echo "calling: ${dynUpdateUrl}"
|
|
|
|
echo "to update domain: ${dynDomain}"
|
|
|
|
curl --connect-timeout 6 ${dynUpdateUrl}
|
|
|
|
else
|
|
|
|
echo "'dynUpdateUrl' not set in ${configFile}"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2018-12-11 13:15:15 +01:00
|
|
|
###############################
|
|
|
|
# Prepare next loop
|
|
|
|
###############################
|
|
|
|
|
|
|
|
# sleep 1 sec
|
|
|
|
sleep 1
|
|
|
|
|
|
|
|
# limit counter to max seconds per week:
|
|
|
|
# 604800 = 60sec * 60min * 24hours * 7days
|
2018-12-13 23:08:23 +01:00
|
|
|
if [ ${counter} -gt 604800 ]; then
|
2018-12-11 13:15:15 +01:00
|
|
|
counter=0
|
|
|
|
echo "counter zero reset"
|
|
|
|
fi
|
|
|
|
|
|
|
|
done
|
|
|
|
|