Improve IP check

- Install iproute (for the "ip" command) in the unlikely cases that
  it is not already installed.
- Abort and exit if server IP cannot be detected.
- Cleanup
This commit is contained in:
hwdsl2 2022-10-04 23:50:06 -05:00
parent c7de2ced33
commit d189d735e3

View File

@ -115,7 +115,7 @@ new_client() {
show_header() {
cat <<'EOF'
OpenVPN Script 3 Oct 2022
OpenVPN Script
https://github.com/hwdsl2/openvpn-install
EOF
}
@ -260,6 +260,25 @@ if [[ ! -e /etc/openvpn/server/server.conf ]]; then
apt-get -yqq install wget >/dev/null
) || exit 1
fi
if ! hash ip 2>/dev/null; then
if [ "$auto" = 0 ]; then
echo "iproute is required to use this installer."
read -n1 -r -p "Press any key to install iproute and continue..."
fi
if [ "$os" = "debian" ] || [ "$os" = "ubuntu" ]; then
export DEBIAN_FRONTEND=noninteractive
(
set -x
apt-get -yqq update || apt-get -yqq update
apt-get -yqq install iproute2 >/dev/null
) || exit 1
else
(
set -x
yum -y -q install iproute >/dev/null
) || exit 1
fi
fi
if [ "$auto" = 0 ]; then
echo
echo 'Welcome to this OpenVPN server installer!'
@ -309,6 +328,11 @@ if [[ ! -e /etc/openvpn/server/server.conf ]]; then
fi
fi
fi
if ! check_ip "$ip"; then
echo "Error: Could not detect this server's IP address." >&2
echo "Abort. No changes were made." >&2
exit 1
fi
# If $ip is a private IP address, the server must be behind NAT
if printf '%s' "$ip" | grep -qE '^(10|127|172\.(1[6-9]|2[0-9]|3[0-1])|192\.168|169\.254)\.'; then
find_public_ip