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:51:37 -05:00
parent 05f9f80095
commit 10a1c7dfe1

View File

@ -202,7 +202,7 @@ EOF
show_header() {
cat <<'EOF'
WireGuard Script 3 Oct 2022
WireGuard Script
https://github.com/hwdsl2/wireguard-install
EOF
}
@ -338,6 +338,25 @@ if [[ ! -e /etc/wireguard/wg0.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 WireGuard server installer!'
@ -387,6 +406,11 @@ if [[ ! -e /etc/wireguard/wg0.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