Skip to content

Commit

Permalink
add get_ip function
Browse files Browse the repository at this point in the history
Signed-off-by: nachoparker <[email protected]>
  • Loading branch information
nachoparker committed Sep 28, 2021
1 parent 6ad96ed commit b067844
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 21 deletions.
6 changes: 4 additions & 2 deletions bin/ncp-diag
Original file line number Diff line number Diff line change
Expand Up @@ -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 )"
Expand Down Expand Up @@ -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.*\"")

Expand All @@ -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"
Expand Down
7 changes: 4 additions & 3 deletions bin/ncp/NETWORKING/dnsmasq.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand Down
8 changes: 4 additions & 4 deletions bin/ncp/NETWORKING/nc-forward-ports.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}

Expand Down
6 changes: 1 addition & 5 deletions bin/ncp/UPDATES/nc-notify-updates.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

# check every hour
CHECKINTERVAL=1
NCDIR=/var/www/nextcloud

configure()
{
Expand Down Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions bin/nextcloud-domain.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 3 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
7 changes: 7 additions & 0 deletions etc/library.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
3 changes: 1 addition & 2 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit b067844

Please sign in to comment.