From d7cba3c2dca0166d9ad4536b7edcfc56e94780c6 Mon Sep 17 00:00:00 2001 From: ralphg6 Date: Sat, 10 Aug 2024 15:15:18 -0300 Subject: [PATCH 1/7] Fix Public IP detection --- openvpn-install.sh | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/openvpn-install.sh b/openvpn-install.sh index 6c6a6474..884c1c86 100755 --- a/openvpn-install.sh +++ b/openvpn-install.sh @@ -625,16 +625,40 @@ function installOpenVPN() { PASS=${PASS:-1} CONTINUE=${CONTINUE:-y} - # Behind NAT, we'll default to the publicly reachable IPv4/IPv6. - if [[ $IPV6_SUPPORT == "y" ]]; then - if ! PUBLIC_IP=$(curl -f --retry 5 --retry-connrefused https://ip.seeip.org); then - PUBLIC_IP=$(dig -6 TXT +short o-o.myaddr.l.google.com @ns1.google.com | tr -d '"') + if [[ -z $ENDPOINT ]]; then + + # IP version flags, we'll use as default the IPv4 + CURL_IP_VERSION_FLAG="-4" + DIG_IP_VERSION_FLAG="-4" + + # Behind NAT, we'll default to the publicly reachable IPv4/IPv6. + if [[ $IPV6_SUPPORT == "y" ]]; then + CURL_IP_VERSION_FLAG="" + DIG_IP_VERSION_FLAG="-6" fi - else - if ! PUBLIC_IP=$(curl -f --retry 5 --retry-connrefused -4 https://ip.seeip.org); then - PUBLIC_IP=$(dig -4 TXT +short o-o.myaddr.l.google.com @ns1.google.com | tr -d '"') + + # If there is no public ip yet, we'll try to solve it using: https://ip.seeip.org + if [[ -z $PUBLIC_IP ]]; then + PUBLIC_IP=$(curl -f -m 5 -sS --retry 5 --retry-connrefused $CURL_IP_VERSION_FLAG https://ip.seeip.org 2>/dev/null) + fi + + # If there is no public ip yet, we'll try to solve it using: https://ifconfig.me + if [[ -z $PUBLIC_IP ]]; then + PUBLIC_IP=$(curl -f -m 5 -sS --retry 5 --retry-connrefused $CURL_IP_VERSION_FLAG https://ifconfig.me 2>/dev/null) fi + + # If there is no public ip yet, we'll try to solve it using: https://api.ipify.org + if [[ -z $PUBLIC_IP ]]; then + PUBLIC_IP=$(curl -f -m 5 -sS --retry 5 --retry-connrefused $CURL_IP_VERSION_FLAG https://api.ipify.org 2>/dev/null) + fi + + # If there is no public ip yet, we'll try to solve it using: ns1.google.com + if [[ -z $PUBLIC_IP ]]; then + PUBLIC_IP=$(dig $DIG_IP_VERSION_FLAG TXT +short o-o.myaddr.l.google.com @ns1.google.com | tr -d '"') + fi + fi + ENDPOINT=${ENDPOINT:-$PUBLIC_IP} fi From de65f0299477b46b45a63d71446e2424270ae18e Mon Sep 17 00:00:00 2001 From: ralphg6 Date: Sat, 10 Aug 2024 17:22:44 -0300 Subject: [PATCH 2/7] feat: public ip as a function --- openvpn-install.sh | 81 +++++++++++++++++++++++++--------------------- 1 file changed, 44 insertions(+), 37 deletions(-) diff --git a/openvpn-install.sh b/openvpn-install.sh index 884c1c86..083ea7ab 100755 --- a/openvpn-install.sh +++ b/openvpn-install.sh @@ -216,6 +216,45 @@ access-control: fd42:42:42:42::/112 allow' >>/etc/unbound/openvpn.conf systemctl restart unbound } +function resolvePublicIP() { + # IP version flags, we'll use as default the IPv4 + CURL_IP_VERSION_FLAG="-4" + DIG_IP_VERSION_FLAG="-4" + + # Behind NAT, we'll default to the publicly reachable IPv4/IPv6. + if [[ $IPV6_SUPPORT == "y" ]]; then + CURL_IP_VERSION_FLAG="" + DIG_IP_VERSION_FLAG="-6" + fi + + # If there is no public ip yet, we'll try to solve it using: https://ip.seeip.org + if [[ -z $PUBLIC_IP ]]; then + PUBLIC_IP=$(curl -f -m 5 -sS --retry 5 --retry-connrefused $CURL_IP_VERSION_FLAG https://ip.seeip.org 2>/dev/null) + fi + + # If there is no public ip yet, we'll try to solve it using: https://ifconfig.me + if [[ -z $PUBLIC_IP ]]; then + PUBLIC_IP=$(curl -f -m 5 -sS --retry 5 --retry-connrefused $CURL_IP_VERSION_FLAG https://ifconfig.me 2>/dev/null) + fi + + # If there is no public ip yet, we'll try to solve it using: https://api.ipify.org + if [[ -z $PUBLIC_IP ]]; then + PUBLIC_IP=$(curl -f -m 5 -sS --retry 5 --retry-connrefused $CURL_IP_VERSION_FLAG https://api.ipify.org 2>/dev/null) + fi + + # If there is no public ip yet, we'll try to solve it using: ns1.google.com + if [[ -z $PUBLIC_IP ]]; then + PUBLIC_IP=$(dig $DIG_IP_VERSION_FLAG TXT +short o-o.myaddr.l.google.com @ns1.google.com | tr -d '"') + fi + + if [[ -z $PUBLIC_IP ]]; then + >&2 echo "Couldn't solve the public IP" + exit 1 + fi + + echo "$PUBLIC_IP" +} + function installQuestions() { echo "Welcome to the OpenVPN installer!" echo "The git repository is available at: https://github.com/angristan/openvpn-install" @@ -244,9 +283,9 @@ function installQuestions() { echo "It seems this server is behind NAT. What is its public IPv4 address or hostname?" echo "We need it for the clients to connect to the server." - PUBLICIP=$(curl -s https://api.ipify.org) until [[ $ENDPOINT != "" ]]; do - read -rp "Public IPv4 address or hostname: " -e -i "$PUBLICIP" ENDPOINT + PUBLIC_IP=$(resolvePublicIP) + read -rp "Public IPv4 address or hostname: " -e -i "$PUBLIC_IP" ENDPOINT done fi @@ -625,41 +664,9 @@ function installOpenVPN() { PASS=${PASS:-1} CONTINUE=${CONTINUE:-y} - if [[ -z $ENDPOINT ]]; then - - # IP version flags, we'll use as default the IPv4 - CURL_IP_VERSION_FLAG="-4" - DIG_IP_VERSION_FLAG="-4" - - # Behind NAT, we'll default to the publicly reachable IPv4/IPv6. - if [[ $IPV6_SUPPORT == "y" ]]; then - CURL_IP_VERSION_FLAG="" - DIG_IP_VERSION_FLAG="-6" - fi - - # If there is no public ip yet, we'll try to solve it using: https://ip.seeip.org - if [[ -z $PUBLIC_IP ]]; then - PUBLIC_IP=$(curl -f -m 5 -sS --retry 5 --retry-connrefused $CURL_IP_VERSION_FLAG https://ip.seeip.org 2>/dev/null) - fi - - # If there is no public ip yet, we'll try to solve it using: https://ifconfig.me - if [[ -z $PUBLIC_IP ]]; then - PUBLIC_IP=$(curl -f -m 5 -sS --retry 5 --retry-connrefused $CURL_IP_VERSION_FLAG https://ifconfig.me 2>/dev/null) - fi - - # If there is no public ip yet, we'll try to solve it using: https://api.ipify.org - if [[ -z $PUBLIC_IP ]]; then - PUBLIC_IP=$(curl -f -m 5 -sS --retry 5 --retry-connrefused $CURL_IP_VERSION_FLAG https://api.ipify.org 2>/dev/null) - fi - - # If there is no public ip yet, we'll try to solve it using: ns1.google.com - if [[ -z $PUBLIC_IP ]]; then - PUBLIC_IP=$(dig $DIG_IP_VERSION_FLAG TXT +short o-o.myaddr.l.google.com @ns1.google.com | tr -d '"') - fi - - fi - - ENDPOINT=${ENDPOINT:-$PUBLIC_IP} + until [[ $ENDPOINT != "" ]]; do + ENDPOINT=$(resolvePublicIP) + done fi # Run setup questions first, and set other variables if auto-install From 5fd223790d43b52c3b68e11046c6b5d286ddc3b0 Mon Sep 17 00:00:00 2001 From: ralphg6 Date: Sun, 11 Aug 2024 05:21:17 -0300 Subject: [PATCH 3/7] Fix IP Detection conditions --- openvpn-install.sh | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/openvpn-install.sh b/openvpn-install.sh index 083ea7ab..a18e9158 100755 --- a/openvpn-install.sh +++ b/openvpn-install.sh @@ -283,9 +283,12 @@ function installQuestions() { echo "It seems this server is behind NAT. What is its public IPv4 address or hostname?" echo "We need it for the clients to connect to the server." + if [[ -z $ENDPOINT ]]; then + DEFAULT_ENDPOINT=$(resolvePublicIP) + fi + until [[ $ENDPOINT != "" ]]; do - PUBLIC_IP=$(resolvePublicIP) - read -rp "Public IPv4 address or hostname: " -e -i "$PUBLIC_IP" ENDPOINT + read -rp "Public IPv4 address or hostname: " -e -i "$DEFAULT_ENDPOINT" ENDPOINT done fi @@ -664,9 +667,9 @@ function installOpenVPN() { PASS=${PASS:-1} CONTINUE=${CONTINUE:-y} - until [[ $ENDPOINT != "" ]]; do + if [[ -z $ENDPOINT ]]; then ENDPOINT=$(resolvePublicIP) - done + fi fi # Run setup questions first, and set other variables if auto-install From b793f531689fac4eaae4001b71cc7bfae5cfd64e Mon Sep 17 00:00:00 2001 From: Raphael Pinto Date: Wed, 25 Sep 2024 22:37:10 -0300 Subject: [PATCH 4/7] feat: decrease failed retry number --- openvpn-install.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/openvpn-install.sh b/openvpn-install.sh index a18e9158..7da2ddd5 100755 --- a/openvpn-install.sh +++ b/openvpn-install.sh @@ -229,17 +229,17 @@ function resolvePublicIP() { # If there is no public ip yet, we'll try to solve it using: https://ip.seeip.org if [[ -z $PUBLIC_IP ]]; then - PUBLIC_IP=$(curl -f -m 5 -sS --retry 5 --retry-connrefused $CURL_IP_VERSION_FLAG https://ip.seeip.org 2>/dev/null) + PUBLIC_IP=$(curl -f -m 5 -sS --retry 2 --retry-connrefused $CURL_IP_VERSION_FLAG https://ip.seeip.org 2>/dev/null) fi # If there is no public ip yet, we'll try to solve it using: https://ifconfig.me if [[ -z $PUBLIC_IP ]]; then - PUBLIC_IP=$(curl -f -m 5 -sS --retry 5 --retry-connrefused $CURL_IP_VERSION_FLAG https://ifconfig.me 2>/dev/null) + PUBLIC_IP=$(curl -f -m 5 -sS --retry 2 --retry-connrefused $CURL_IP_VERSION_FLAG https://ifconfig.me 2>/dev/null) fi # If there is no public ip yet, we'll try to solve it using: https://api.ipify.org if [[ -z $PUBLIC_IP ]]; then - PUBLIC_IP=$(curl -f -m 5 -sS --retry 5 --retry-connrefused $CURL_IP_VERSION_FLAG https://api.ipify.org 2>/dev/null) + PUBLIC_IP=$(curl -f -m 5 -sS --retry 2 --retry-connrefused $CURL_IP_VERSION_FLAG https://api.ipify.org 2>/dev/null) fi # If there is no public ip yet, we'll try to solve it using: ns1.google.com From 93cd4ed0892416e6bd287e53d4cf1a7c6759575f Mon Sep 17 00:00:00 2001 From: Stanislas Date: Thu, 7 Nov 2024 20:42:29 +0100 Subject: [PATCH 5/7] ip.seeip.org -> api.seeip.org --- openvpn-install.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/openvpn-install.sh b/openvpn-install.sh index 7da2ddd5..7a90a57b 100755 --- a/openvpn-install.sh +++ b/openvpn-install.sh @@ -227,9 +227,9 @@ function resolvePublicIP() { DIG_IP_VERSION_FLAG="-6" fi - # If there is no public ip yet, we'll try to solve it using: https://ip.seeip.org + # If there is no public ip yet, we'll try to solve it using: https://api.seeip.org if [[ -z $PUBLIC_IP ]]; then - PUBLIC_IP=$(curl -f -m 5 -sS --retry 2 --retry-connrefused $CURL_IP_VERSION_FLAG https://ip.seeip.org 2>/dev/null) + PUBLIC_IP=$(curl -f -m 5 -sS --retry 2 --retry-connrefused $CURL_IP_VERSION_FLAG https://api.seeip.org 2>/dev/null) fi # If there is no public ip yet, we'll try to solve it using: https://ifconfig.me From 1cb0504341cb55eaea83b36977f928d2c2ba49ae Mon Sep 17 00:00:00 2001 From: Stanislas Date: Thu, 7 Nov 2024 20:44:56 +0100 Subject: [PATCH 6/7] Fix lint issue --- openvpn-install.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openvpn-install.sh b/openvpn-install.sh index 7a90a57b..58b99ae8 100755 --- a/openvpn-install.sh +++ b/openvpn-install.sh @@ -248,7 +248,7 @@ function resolvePublicIP() { fi if [[ -z $PUBLIC_IP ]]; then - >&2 echo "Couldn't solve the public IP" + echo >&2 echo "Couldn't solve the public IP" exit 1 fi From 26194f5dd5bb568a4425198b653f83a6e5f58715 Mon Sep 17 00:00:00 2001 From: Stanislas Lange Date: Thu, 7 Nov 2024 20:48:27 +0100 Subject: [PATCH 7/7] Fix lint --- openvpn-install.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/openvpn-install.sh b/openvpn-install.sh index 58b99ae8..a0e04a49 100755 --- a/openvpn-install.sh +++ b/openvpn-install.sh @@ -229,17 +229,17 @@ function resolvePublicIP() { # If there is no public ip yet, we'll try to solve it using: https://api.seeip.org if [[ -z $PUBLIC_IP ]]; then - PUBLIC_IP=$(curl -f -m 5 -sS --retry 2 --retry-connrefused $CURL_IP_VERSION_FLAG https://api.seeip.org 2>/dev/null) + PUBLIC_IP=$(curl -f -m 5 -sS --retry 2 --retry-connrefused "$CURL_IP_VERSION_FLAG" https://api.seeip.org 2>/dev/null) fi # If there is no public ip yet, we'll try to solve it using: https://ifconfig.me if [[ -z $PUBLIC_IP ]]; then - PUBLIC_IP=$(curl -f -m 5 -sS --retry 2 --retry-connrefused $CURL_IP_VERSION_FLAG https://ifconfig.me 2>/dev/null) + PUBLIC_IP=$(curl -f -m 5 -sS --retry 2 --retry-connrefused "$CURL_IP_VERSION_FLAG" https://ifconfig.me 2>/dev/null) fi # If there is no public ip yet, we'll try to solve it using: https://api.ipify.org if [[ -z $PUBLIC_IP ]]; then - PUBLIC_IP=$(curl -f -m 5 -sS --retry 2 --retry-connrefused $CURL_IP_VERSION_FLAG https://api.ipify.org 2>/dev/null) + PUBLIC_IP=$(curl -f -m 5 -sS --retry 2 --retry-connrefused "$CURL_IP_VERSION_FLAG" https://api.ipify.org 2>/dev/null) fi # If there is no public ip yet, we'll try to solve it using: ns1.google.com