Skip to content

Commit

Permalink
Improve NAT detection
Browse files Browse the repository at this point in the history
Cleaner and better:
- Not relying in an external service
- Avoids a false positive when the server has multiple public IPv4
addresses and the user selects one which is not the default gateway
  • Loading branch information
Nyr committed Apr 21, 2018
1 parent ff254ae commit 83234dd
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions openvpn-install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,11 @@ else
# Autodetect IP address and pre-fill for the user
IP=$(ip addr | grep 'inet' | grep -v inet6 | grep -vE '127\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | head -1)
read -p "IP address: " -e -i $IP IP
# Try to detect a NATed connection and ask the user about it
EXTERNALIP=$(wget -4qO- "http://whatismyip.akamai.com/" 2>/dev/null || curl -4s "http://whatismyip.akamai.com/")
if [[ "$IP" != "$EXTERNALIP" ]]; then
# If $IP is a private IP address, the server must be behind NAT
if echo "$IP" | grep -qE '^(10\.|172\.1[6789]\.|172\.2[0-9]\.|172\.3[01]\.|192\.168)'; then
echo
echo "If your server is behind NAT, please provide the public IP address or hostname."
echo "If that's not the case, just ignore this and leave the next field blank."
read -p "Public IP address / Hostname: " -e PUBLICIP
echo "This server is behind NAT. What is the public IPv4 address or hostname?"
read -p "Public IP address / hostname: " -e PUBLICIP
fi
echo
echo "Which protocol do you want for OpenVPN connections?"
Expand Down

0 comments on commit 83234dd

Please sign in to comment.