add get_ip function

Signed-off-by: nachoparker <nacho@ownyourbits.com>
This commit is contained in:
nachoparker 2021-09-27 18:21:33 -06:00
parent 6ad96eddd3
commit b067844550
8 changed files with 27 additions and 21 deletions

View File

@ -11,6 +11,8 @@
# More at https://ownyourbits.com
#
source /usr/local/etc/library.sh
# Distro, NCP version and tag
echo "NextCloudPi version|$( cat /usr/local/etc/ncp-version )"
[[ -f /usr/local/etc/ncp-baseimage ]] && echo "NextCloudPi image|$( cat /usr/local/etc/ncp-baseimage )"
@ -67,7 +69,7 @@ function is_port_open()
{
local port=$1
local public_ip
public_ip="$(curl -4 https://icanhazip.com 2>/dev/null)" || { echo "closed"; return 1; }
public_ip="$(curl -m4 -4 https://icanhazip.com 2>/dev/null)" || { echo "closed"; return 1; }
local tmp_file=$(mktemp)
local token=$(wget -T2 -t1 -qO- --keep-session-cookies --save-cookies $tmp_file https://portchecker.co | grep -oP "_csrf\" value=\"\K.*\"")
@ -85,7 +87,7 @@ echo "port check 443|$( is_port_open 443 )"
# LAN
IFACE=$( ip r | grep "default via" | awk '{ print $5 }' | head -1 )
GW=$( ip r | grep "default via" | awk '{ print $3 }' | head -1 )
IP=$( ip a show dev "$IFACE" | grep global | grep -oP '\d{1,3}(.\d{1,3}){3}' | head -1 )
IP="$(get_ip)"
echo "IP|$IP"
echo "gateway|$GW"

View File

@ -48,9 +48,10 @@ configure()
return
}
local IFACE=$( ip r | grep "default via" | awk '{ print $5 }' | head -1 )
local IP=$( ncc config:system:get trusted_domains "${TRUSTED_DOMAINS[docker_overwrite]}" | grep -oP '\d{1,3}(.\d{1,3}){3}' )
[[ "$IP" == "" ]] && IP=$( ip a show dev "$IFACE" | grep global | grep -oP '\d{1,3}(.\d{1,3}){3}' | head -1 )
local IFACE IP
IFACE=$( ip r | grep "default via" | awk '{ print $5 }' | head -1 )
IP=$( ncc config:system:get trusted_domains "${TRUSTED_DOMAINS[docker_overwrite]}" | grep -oP '\d{1,3}(.\d{1,3}){3}' )
[[ "$IP" == "" ]] && IP="$(get_ip)"
[[ "$IP" == "" ]] && { echo "could not detect IP"; return 1; }

View File

@ -17,13 +17,13 @@ install()
configure()
{
local IFACE=$( ip r | grep "default via" | awk '{ print $5 }' | head -1 )
local IP=$( ip a show dev "$IFACE" | grep global | grep -oP '\d{1,3}(.\d{1,3}){3}' | head -1 )
local ip
ip="$(get_ip)"
upnpc -d "$HTTPSPORT" TCP
upnpc -d "$HTTPPORT" TCP
upnpc -a "$IP" 443 "$HTTPSPORT" TCP | tee >(cat - >&2) | grep -q "is redirected to internal" || \
upnpc -a "$ip" 443 "$HTTPSPORT" TCP | tee >(cat - >&2) | grep -q "is redirected to internal" || \
{ echo -e "\nCould not forward ports automatically.\nDo it manually, or activate UPnP in your router and try again"; return 1; }
upnpc -a "$IP" 80 "$HTTPPORT" TCP | tee >(cat - >&2) | grep -q "is redirected to internal" || \
upnpc -a "$ip" 80 "$HTTPPORT" TCP | tee >(cat - >&2) | grep -q "is redirected to internal" || \
{ echo -e "\nCould not forward ports automatically.\nDo it manually, or activate UPnP in your router and try again"; return 1; }
}

View File

@ -12,7 +12,6 @@
# check every hour
CHECKINTERVAL=1
NCDIR=/var/www/nextcloud
configure()
{
@ -41,12 +40,9 @@ test -e \$NOTIFIED && [[ "\$( cat \$LATEST )" == "\$( cat \$NOTIFIED )" ]] && {
echo "Found update from \$( cat \$VERFILE ) to \$( cat \$LATEST ). Sending notification..."
IFACE=\$( ip r | grep "default via" | awk '{ print \$5 }' | head -1 )
IP=\$( ip a show dev "\$IFACE" | grep global | grep -oP '\d{1,3}(\.\d{1,3}){3}' | head -1 )
notify_admin \
"NextCloudPi update" \
"Update from \$( cat \$VERFILE ) to \$( cat \$LATEST ) is available. Update from https://\$IP:4443"
"Update from \$( cat \$VERFILE ) to \$( cat \$LATEST ) is available. Update from https://\$(get_ip):4443"
cat \$LATEST > \$NOTIFIED
EOF

View File

@ -4,13 +4,12 @@ source /usr/local/etc/library.sh
# wicd service finishes before completing DHCP
while :; do
iface="$( ip r | grep "default via" | awk '{ print $5 }' | head -1 )"
ip="$( ip a show dev "$iface" | grep global | grep -oP '\d{1,3}(.\d{1,3}){3}' | head -1 )"
ip="$(get_ip)"
public_ip="$(curl -m4 icanhazip.com 2>/dev/null)"
public_ip="$(curl icanhazip.com 2>/dev/null)"
[[ "$public_ip" != "" ]] && ncc config:system:set trusted_domains 11 --value="$public_ip"
[[ "$ip" != "" ]] && break
sleep 3
done

View File

@ -1,5 +1,7 @@
[v1.39.14](https://github.com/nextcloud/nextcloudpi/commit/077c3bc) (2021-09-25) nc-https:only fix infinite redirects behind proxy
[v1.39.15](https://github.com/nextcloud/nextcloudpi/commit/3e408f1) (2021-09-27) add get_ip function
[v1.39.14](https://github.com/nextcloud/nextcloudpi/commit/6ad96ed) (2021-09-25) nc-https:only fix infinite redirects behind proxy
[v1.39.13](https://github.com/nextcloud/nextcloudpi/commit/eef7b09) (2021-09-23) ncp-web: make letsencrypt detection more robust

View File

@ -382,6 +382,13 @@ function nc_version()
ncc status | grep "version:" | awk '{ print $3 }'
}
function get_ip()
{
local iface
iface="$( ip r | grep "default via" | awk '{ print $5 }' | head -1 )"
ip a show dev "$iface" | grep global | grep -oP '\d{1,3}(.\d{1,3}){3}' | head -1
}
function is_an_ip()
{
local ip_or_domain="${1}"

View File

@ -67,8 +67,7 @@ rm /.ncp-image
cd -
rm -rf "${TMPDIR}"
IFACE="$( ip r | grep "default via" | awk '{ print $5 }' | head -1 )"
IP="$( ip a show dev "$IFACE" | grep global | grep -oP '\d{1,3}(.\d{1,3}){3}' | head -1 )"
IP="$(get_ip)"
echo "Done.