From 0e2fd697a6453922d16e1e57f830bbfd79a7044a Mon Sep 17 00:00:00 2001 From: hwdsl2 Date: Sat, 1 Oct 2022 23:27:52 -0500 Subject: [PATCH] Cleanup --- openvpn-install.sh | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/openvpn-install.sh b/openvpn-install.sh index 5c730fc..26b5ba0 100644 --- a/openvpn-install.sh +++ b/openvpn-install.sh @@ -32,16 +32,6 @@ check_ip() { printf '%s' "$1" | tr -d '\n' | grep -Eq "$IP_REGEX" } -find_public_ip() { - ip_url1="http://ipv4.icanhazip.com" - ip_url2="http://ip1.dynupdate.no-ip.com" - # Get public IP and sanitize with grep - get_public_ip=$(grep -m 1 -oE '^[0-9]{1,3}(\.[0-9]{1,3}){3}$' <<< "$(wget -T 10 -t 1 -4qO- "$ip_url1" || curl -m 10 -4Ls "$ip_url1")") - if [ -z "$get_public_ip" ]; then - get_public_ip=$(grep -m 1 -oE '^[0-9]{1,3}(\.[0-9]{1,3}){3}$' <<< "$(wget -T 10 -t 1 -4qO- "$ip_url2" || curl -m 10 -4Ls "$ip_url2")") - fi -} - abort_and_exit() { echo "Abort. No changes were made." >&2 exit 1 @@ -59,6 +49,16 @@ get_export_dir() { fi } +find_public_ip() { + ip_url1="http://ipv4.icanhazip.com" + ip_url2="http://ip1.dynupdate.no-ip.com" + # Get public IP and sanitize with grep + get_public_ip=$(grep -m 1 -oE '^[0-9]{1,3}(\.[0-9]{1,3}){3}$' <<< "$(wget -T 10 -t 1 -4qO- "$ip_url1" || curl -m 10 -4Ls "$ip_url1")") + if ! check_ip "$get_public_ip"; then + get_public_ip=$(grep -m 1 -oE '^[0-9]{1,3}(\.[0-9]{1,3}){3}$' <<< "$(wget -T 10 -t 1 -4qO- "$ip_url2" || curl -m 10 -4Ls "$ip_url2")") + fi +} + update_sysctl() { # Enable net.ipv4.ip_forward for the system echo 'net.ipv4.ip_forward=1' > /etc/sysctl.d/99-openvpn-forward.conf @@ -192,6 +192,7 @@ TUN needs to be enabled before running this installer." exit 1 fi +auto=0 if [[ ! -e /etc/openvpn/server/server.conf ]]; then if [ "$os" = "centos" ]; then if grep -qs "hwdsl2 VPN script" /etc/sysconfig/nftables.conf \ @@ -200,7 +201,6 @@ if [[ ! -e /etc/openvpn/server/server.conf ]]; then exit 1 fi fi - auto=0 while [ "$#" -gt 0 ]; do case $1 in --auto) @@ -276,7 +276,7 @@ if [[ ! -e /etc/openvpn/server/server.conf ]]; then # 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 - if [ -z "$get_public_ip" ]; then + if ! check_ip "$get_public_ip"; then if [ "$auto" = 0 ]; then echo echo "This server is behind NAT. What is the public IPv4 address?"