#869 Adding IPv6 option to config (#1561)

This commit is contained in:
Christian Rotzoll 2020-09-18 00:54:34 +02:00 committed by GitHub
parent 0dd99df235
commit 0a3b3a726b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 206 additions and 123 deletions

View file

@ -19,7 +19,10 @@ source /mnt/hdd/raspiblitz.conf 2>/dev/null
source <(sudo /home/admin/config.scripts/blitz.datadrive.sh status)
hdd="${hddUsedInfo}"
# get UPS info
## get internet info
source <(sudo /home/admin/config.scripts/internet.sh status global)
## get UPS info
source <(/home/admin/config.scripts/blitz.ups.sh status)
upsInfo=""
if [ "${upsStatus}" = "ONLINE" ]; then
@ -84,20 +87,6 @@ else
color_ram=${color_green}
fi
# get name of active interface (eth0 or wlan0)
network_active_if=$(ip route get 255.255.255.255 | awk -- '{print $4}' | head -n 1)
# get network traffic
# ifconfig does not show eth0 on Armbian or in a VM - get first traffic info
isArmbian=$(cat /etc/os-release 2>/dev/null | grep -c 'Debian')
if [ ${isArmbian} -gt 0 ] || [ ! -d "/sys/class/thermal/thermal_zone0/" ]; then
network_rx=$(ifconfig | grep -m1 'RX packets' | awk '{ print $6$7 }' | sed 's/[()]//g')
network_tx=$(ifconfig | grep -m1 'TX packets' | awk '{ print $6$7 }' | sed 's/[()]//g')
else
network_rx=$(ifconfig ${network_active_if} | grep 'RX packets' | awk '{ print $6$7 }' | sed 's/[()]//g')
network_tx=$(ifconfig ${network_active_if} | grep 'TX packets' | awk '{ print $6$7 }' | sed 's/[()]//g')
fi
# Bitcoin blockchain
btc_path=$(command -v ${network}-cli)
if [ -n ${btc_path} ]; then
@ -148,9 +137,8 @@ fi
# get IP address & port
networkInfo=$(${network}-cli -datadir=${bitcoin_dir} getnetworkinfo 2>/dev/null)
source <(sudo /home/admin/config.scripts/internet.sh status)
local_ip="${localip}"
public_ip="${publicIP}"
local_ip="${localip}" # from internet.sh
public_ip="${cleanip}"
public_port="$(echo ${networkInfo} | jq -r '.localaddresses [0] .port')"
if [ "${public_port}" = "null" ]; then
if [ "${chain}" = "test" ]; then
@ -192,9 +180,9 @@ else
# IP address
networkConnectionsInfo="${color_purple}${networkConnections} ${color_gray}connections"
public_addr="${public_ip}:${public_port}"
public_check=$(nc -z -w6 ${public_ip} ${public_port} 2>/dev/null; echo $?)
if [ $public_check = "0" ]; then
public_addr="${publicip}:${public_port}"
public_check=$(nc -z -w6 ${cleanip} ${public_port} 2>/dev/null; echo $?)
if [ $public_check = "0" ] || [ "${ipv6}" == "on" ] ; then
public=""
# only set yellow/normal because netcat can only say that the port is open - not that it points to this device for sure
public_color="${color_amber}"
@ -255,7 +243,7 @@ else
ln_publicColor="${color_green}"
else
public_check=$(nc -z -w6 ${public_ip} ${ln_port} 2>/dev/null; echo $?)
if [ $public_check = "0" ]; then
if [ $public_check = "0" ] || [ "${ipv6}" == "on" ]; then
# only set yellow/normal because netcat can only say that the port is open - not that it points to this device for sure
ln_publicColor="${color_amber}"
else

View file

@ -108,43 +108,24 @@ do
# 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)
# sanity check on IP data
# see https://github.com/rootzoll/raspiblitz/issues/371#issuecomment-472416349
echo "-> sanity check of new IP data"
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
echo "OK IPv6"
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"
else
echo "FAIL - not an IPv4 or IPv6 address"
freshPublicIP=""
fi
if [ ${#freshPublicIP} -eq 0 ]; then
echo "freshPublicIP is ZERO - ignoring"
publicIPChanged=$(/home/admin/config.scripts/internet.sh update-publicip | grep -c 'ip_changed=1')
# check if changed
elif [ "${freshPublicIP}" != "${publicIP}" ]; then
elif [ ${publicIPChanged} -get 0 ]; then
# 1) update config file
echo "update config value"
sed -i "s/^publicIP=.*/publicIP='${freshPublicIP}'/g" ${configFile}
publicIP='${freshPublicIP}'
# refresh data
source /mnt/hdd/raspiblitz.conf
# 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
# only restart LND if auto-unlock is activated
if [ "${autoUnlock}" = "on" ]; then
echo "restart LND with to pickup up new publiIP"
sudo systemctl stop lnd
sudo systemctl start lnd
else
echo "new publicIP but no LND restart because no auto-unlock"
fi
# 2) trigger update if dnyamic domain (if set)
# trigger update if dnyamic domain (if set)
updateDynDomain=1
else
@ -372,7 +353,7 @@ do
# calling the update url
echo "calling: ${dynUpdateUrl}"
echo "to update domain: ${dynDomain}"
curl --connect-timeout 6 ${dynUpdateUrl}
curl -s --connect-timeout 6 ${dynUpdateUrl} 2>/dev/null
else
echo "'dynUpdateUrl' not set in ${configFile}"
fi

View file

@ -474,64 +474,10 @@ if [ ${configExists} -eq 1 ]; then
# load values
echo "load and update publicIP" >> $logFile
source ${configFile}
freshPublicIP=""
# determine the publicIP/domain that LND should announce
if [ ${#lndAddress} -gt 3 ]; then
# use domain as PUBLICIP
freshPublicIP="${lndAddress}"
else
# update public IP on boot
# wait otherwise looking for publicIP fails
sleep 5
freshPublicIP=$(curl -s http://v4.ipv6-test.com/api/myip.php)
# sanity check on IP data
# see https://github.com/rootzoll/raspiblitz/issues/371#issuecomment-472416349
echo "-> sanity check of IP data:"
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
echo "OK IPv6"
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"
else
echo "FAIL - not an IPv4 or IPv6 address"
freshPublicIP=""
fi
if [ ${#freshPublicIP} -eq 0 ]; then
# prevent having no publicIP set at all and LND getting stuck
# https://github.com/rootzoll/raspiblitz/issues/312#issuecomment-462675101
if [ ${#publicIP} -eq 0 ]; then
localIP=$(ip addr | grep 'state UP' -A2 | egrep -v 'docker0' | grep 'eth0\|wlan0' | tail -n1 | awk '{print $2}' | cut -f1 -d'/')
echo "WARNING: No publicIP information at all - working with placeholder: ${localIP}" >> $logFile
freshPublicIP="${localIP}"
fi
fi
fi
# set publicip value in raspiblitz.conf
if [ ${#freshPublicIP} -eq 0 ]; then
echo "WARNING: Was not able to determine external IP/domain on startup." >> $logFile
else
publicIPValueExists=$( sudo cat ${configFile} | grep -c 'publicIP=' )
if [ ${publicIPValueExists} -gt 1 ]; then
# remove one
echo "more then one publiIp entry - removing one" >> $logFile
sed -i "s/^publicIP=.*//g" ${configFile}
publicIPValueExists=$( sudo cat ${configFile} | grep -c 'publicIP=' )
fi
if [ ${publicIPValueExists} -eq 0 ]; then
echo "create value (${freshPublicIP})" >> $logFile
echo "publicIP='${freshPublicIP}'" >> $configFile
else
echo "update value (${freshPublicIP})" >> $logFile
sed -i "s/^publicIP=.*/publicIP='${freshPublicIP}'/g" ${configFile}
fi
fi
# update public IP on boot - set to domain is available
sleep 3
/home/admin/config.scripts/internet.sh update-publicip ${lndAddress}
fi

View file

@ -5,12 +5,16 @@
# command info
if [ $# -eq 0 ] || [ "$1" = "-h" ] || [ "$1" = "-help" ]; then
echo "config script to switch the Electrum Rust Server on or off"
echo "bonus.electrs.sh [on|off|status[showAddress]|menu]"
echo "bonus.electrs.sh status [?showAddress]"
echo "bonus.electrs.sh [on|off|menu]"
exit 1
fi
source /mnt/hdd/raspiblitz.conf
# get local and global internet info
source <(/home/admin/config.scripts/internet.sh status global)
# give status
if [ "$1" = "status" ]; then
@ -64,15 +68,15 @@ if [ "$1" = "status" ]; then
fi
# check local IPv4 port
localIP=$(ip addr | grep 'state UP' -A2 | egrep -v 'docker0' | grep 'eth0\|wlan0' | tail -n1 | awk '{print $2}' | cut -f1 -d'/')
echo "localIP='${localIP}'"
echo "localIP='${localip}'"
if [ "$2" = "showAddress" ]; then
echo "publicIP='${publicIP}'"
echo "publicIP='${cleanip}'"
fi
echo "portTCP='50001'"
localPortRunning=$(sudo netstat -a | grep -c '0.0.0.0:50001')
echo "localTCPPortActive=${localPortRunning}"
publicPortRunning=$(nc -z -w6 ${publicIP} 50001 2>/dev/null; echo $?)
publicPortRunning=$(nc -z -w6 ${publicip} 50001 2>/dev/null; echo $?)
if [ "${publicPortRunning}" == "0" ]; then
# OK looks good - but just means that something is answering on that port
echo "publicTCPPortAnswering=1"
@ -83,7 +87,7 @@ if [ "$1" = "status" ]; then
echo "portHTTP='50002'"
localPortRunning=$(sudo netstat -a | grep -c '0.0.0.0:50002')
echo "localHTTPPortActive=${localPortRunning}"
publicPortRunning=$(nc -z -w6 ${publicIP} 50002 2>/dev/null; echo $?)
publicPortRunning=$(nc -z -w6 ${publicip} 50002 2>/dev/null; echo $?)
if [ "${publicPortRunning}" == "0" ]; then
# OK looks good - but just means that something is answering on that port
echo "publicHTTPPortAnswering=1"
@ -182,11 +186,11 @@ Check 'sudo nginx -t' for a detailed error message.
echo
echo "On Network Settings > Server menu:"
echo "- deactivate automatic server selection"
echo "- as manual server set '${localIP}' & '${portHTTP}'"
echo "- as manual server set '${localip}' & '${portHTTP}'"
echo "- laptop and RaspiBlitz need to be within same local network"
echo
echo "To start directly from laptop terminal use:"
echo "electrum --oneserver --server ${localIP}:${portHTTP}:s"
echo "electrum --oneserver --server ${localip}:${portHTTP}:s"
if [ ${TORrunning} -eq 1 ]; then
echo ""
echo "The Tor Hidden Service address for electrs is (see LCD for QR code):"

View file

@ -3,12 +3,26 @@
# command info
if [ $# -eq 0 ] || [ "$1" = "-h" ] || [ "$1" = "-help" ]; then
echo "# handle the internet connection"
echo "# internet.sh status"
echo "# internet.sh status [local|global]"
echo "# internet.sh ipv6 [on|off]"
echo "# internet.sh update-publicip [?domain]"
exit 1
fi
# FUNCTIONS
# check when to global check
runGlobal=0
if [ "$2" == "global" ]; then
runGlobal=1
fi
if [ "$1" == "update-publicip" ]; then
runGlobal=1
fi
# load local config (but should also work if not available)
source /mnt/hdd/raspiblitz.conf 2>/dev/null
#############################################
# FUNCTIONS
isValidIP() {
if [ "$1" != "${1#*[0-9].[0-9]}" ]; then
# IPv4
@ -22,9 +36,27 @@ isValidIP() {
fi
}
# load local config (but should also work if not available)
source /mnt/hdd/raspiblitz.conf 2>/dev/null
#############################################
# by deafult ipv6 is off (for publicIP)
if [ "${ipv6}" = "" ]; then
ipv6="off"
fi
#############################################
# get active network device (eth0 or wlan0) & trafiic
networkDevice=$(ip addr | grep -v "lo:" | grep 'state UP' | tr -d " " | cut -d ":" -f2 | head -n 1)
# get network traffic
# ifconfig does not show eth0 on Armbian or in a VM - get first traffic info
isArmbian=$(cat /etc/os-release 2>/dev/null | grep -c 'Debian')
if [ ${isArmbian} -gt 0 ] || [ ! -d "/sys/class/thermal/thermal_zone0/" ]; then
network_rx=$(ifconfig | grep -m1 'RX packets' | awk '{ print $6$7 }' | sed 's/[()]//g')
network_tx=$(ifconfig | grep -m1 'TX packets' | awk '{ print $6$7 }' | sed 's/[()]//g')
else
network_rx=$(ifconfig ${networkDevice} | grep 'RX packets' | awk '{ print $6$7 }' | sed 's/[()]//g')
network_tx=$(ifconfig ${networkDevice} | grep 'TX packets' | awk '{ print $6$7 }' | sed 's/[()]//g')
fi
#############################################
# get local IP (from different sources)
localip_ALL=$(ip addr | grep 'state UP' -A2 | egrep -v 'docker0' | egrep -i '(*[eth|ens|enp|eno|wlan|wlp][0-9]$)' | tail -n1 | awk '{print $2}' | cut -f1 -d'/')
if [ $(isValidIP ${localip_ALL}) -eq 0 ]; then
@ -40,16 +72,18 @@ if [ ${#localip_LAN} -gt 0 ]; then
localip="${localip_LAN}"
fi
#############################################
# check DHCP
dhcp=1
if [ "${localip:0:4}" = "169." ]; then
dhcp=0
fi
#############################################
# check for internet connection
online=0
if [ ${#dnsServer} -gt 0 ]; then
# re-test with other server
# re-test with user set dns server
online=$(ping ${dnsServer} -c 1 -W 2 | grep -c '1 received')
fi
if [ ${online} -eq 0 ]; then
@ -60,22 +94,152 @@ if [ ${online} -eq 0 ]; then
# re-test with other server
online=$(ping 8.8.8.8 -c 1 -W 2 | grep -c '1 received')
fi
if [ ${online} -eq 0 ]; then
# re-test with other server (IPv6)
online=$(ping 2620:119:35::35 -c 1 -W 2 | grep -c '1 received')
fi
if [ ${online} -eq 0 ]; then
# re-test with other server
online=$(ping 208.67.222.222 -c 1 -W 2 | grep -c '1 received')
fi
if [ ${online} -eq 0 ]; then
# re-test with other server (IPv6)
online=$(ping 2001:4860:4860::8844 -c 1 -W 2 | grep -c '1 received')
fi
if [ ${online} -eq 0 ]; then
# re-test with other server
online=$(ping 1.1.1.1 -c 1 -W 2 | grep -c '1 received')
fi
#############################################
# check for internet connection
if [ ${runGlobal} -eq 1 ]; then
###########################################
# Global IP
# the public IP that can be detected from outside
if [ "${ipv6}" == "on" ]; then
globalIP=$(curl -s http://v6.ipv6-test.com/api/myip.php 2>/dev/null)
else
globalIP=$(curl -s http://v4.ipv6-test.com/api/myip.php 2>/dev/null)
fi
# prevent having no publicIP set at all and LND getting stuck
# https://github.com/rootzoll/raspiblitz/issues/312#issuecomment-462675101
if [ ${#globalIP} -eq 0 ]; then
if [ "${ipv6}" == "on" ]; then
globalIP="::1"
else
globalIP="127.0.0.1"
fi
fi
##########################################
# Public IP
# the public that is maybe set by raspibitz config file (overriding aut-detection)
if [ "${publicIP}" == "" ]; then
# if publicIP is not set by config ... use detected global IP
if [ "${ipv6}" == "on" ]; then
# use ipv6 with brackes so that it can be used in http addresses like a IPv4
publicIP="[${globalIP}]"
else
publicIP="${globalIP}"
fi
fi
##########################################
# Clean IP
# really just the IP value - without the default brackets if IPv6
has_brackets="$(echo "${publicIP}" | grep -c '\[')"
if [ ${has_brackets} -eq 0 ]; then
cleanIP="${publicIP}"
else
cleanIP="$(echo "${publicIP}" | cut -d'[' -f2 | cut -d']' -f1)"
fi
fi
#############################################
if [ "$1" == "status" ]; then
echo "### LOCAL INTERNET ###"
echo "localip=${localip}"
echo "dhcp=${dhcp}"
echo "network_device=${networkDevice}"
echo "network_rx='${network_rx}'"
echo "network_tx='${network_tx}'"
echo "### GLOBAL INTERNET ###"
echo "online=${online}"
if [ ${runGlobal} -eq 1 ]; then
echo "ipv6=${ipv6}"
echo "globalip=${globalIP}"
echo "publicip=${publicIP}"
echo "cleanip=${cleanIP}"
else
echo "# for more global internet info use 'status global'"
fi
exit 0
#############################################
elif [ "$1" == "update-publicip" ]; then
if [ "$2" != "" ]; then
echo "ip_changed=0"
publicIP="$2"
elif [ "${globalIP}" == "${cleanIP}" ]; then
echo "ip_changed=0"
exit 0
else
echo "ip_changed=1"
if [ "${ipv6}" == "on" ]; then
# use ipv6 with brackes so that it can be used in http addresses like a IPv4
publicIP="[${globalIP}]"
else
publicIP="${globalIP}"
fi
echo "publicip=${publicIP}"
fi
# store to raspiblitz.conf new publiciP
publicIPValueExists=$(sudo cat /mnt/hdd/raspiblitz.conf | grep -c 'publicIP=')
if [ ${publicIPValueExists} -gt 1 ]; then
# more then one publiIp entry - removing one
sudo sed -i "s/^publicIP=.*//g" /mnt/hdd/raspiblitz.conf
fi
if [ ${publicIPValueExists} -eq 0 ]; then
echo "publicIP='${publicIP}'" >> /mnt/hdd/raspiblitz.conf
else
sudo sed -i "s/^publicIP=.*/publicIP='${publicIP}'/g" /mnt/hdd/raspiblitz.conf
fi
exit 0
#############################################
elif [ "$1" == "ipv6" ]; then
if [ "$2" == "on" ]; then
echo "# Switching IPv6 ON"
# set config
if ! grep -Eq "^ipv6=" /mnt/hdd/raspiblitz.conf; then
echo "ipv6=on" >> /mnt/hdd/raspiblitz.conf
else
sudo sed -i "s/^ipv6=.*/ipv6=on/g" /mnt/hdd/raspiblitz.conf
fi
exit 0
elif [ "$2" == "off" ]; then
echo "# Switching IPv6 OFF"
# set config
sudo sed -i "s/^ipv6=.*/ipv6=off/g" /mnt/hdd/raspiblitz.conf
exit 0
else
echo "error='unknown second parameter'"
exit 1
fi
else
echo "err='parameter not known - run with -help'"
fi