From 9458735d2c695b22ad79ecfd1eebea2274f827fd Mon Sep 17 00:00:00 2001 From: hwdsl2 Date: Sat, 23 Apr 2022 00:07:46 -0500 Subject: [PATCH] Improve Libreswan check --- vpnsetup_alpine.sh | 12 ++++++++---- vpnsetup_amzn.sh | 12 ++++++++---- vpnsetup_centos.sh | 12 ++++++++---- vpnsetup_ubuntu.sh | 12 ++++++++---- 4 files changed, 32 insertions(+), 16 deletions(-) diff --git a/vpnsetup_alpine.sh b/vpnsetup_alpine.sh index fbcd13b..507cebf 100755 --- a/vpnsetup_alpine.sh +++ b/vpnsetup_alpine.sh @@ -219,29 +219,32 @@ get_swan_ver() { SWAN_VER=4.6 base_url="https://github.com/hwdsl2/vpn-extras/releases/download/v1.0.0" swan_ver_url="$base_url/v1-$os_type-$os_ver-swanver" - swan_ver_latest=$(wget -t 3 -T 15 -qO- "$swan_ver_url" | head -n 1) + swan_ver_latest=$(wget -t 3 -T 15 -qO- "$swan_ver_url" 2>/dev/null | head -n 1) + [ -z "$swan_ver_latest" ] && swan_ver_latest=$(curl -fsL "$swan_ver_url" 2>/dev/null | head -n 1) if printf '%s' "$swan_ver_latest" | grep -Eq '^([3-9]|[1-9][0-9]{1,2})(\.([0-9]|[1-9][0-9]{1,2})){1,2}$'; then SWAN_VER="$swan_ver_latest" fi } check_libreswan() { + check_result=0 ipsec_ver=$(/usr/local/sbin/ipsec --version 2>/dev/null) swan_ver_old=$(printf '%s' "$ipsec_ver" | sed -e 's/.*Libreswan U\?//' -e 's/\( (\|\/K\).*//') ipsec_bin="/usr/local/sbin/ipsec" if [ -n "$swan_ver_old" ] && printf '%s' "$ipsec_ver" | grep -qi 'libreswan' \ && [ "$(find "$ipsec_bin" -mmin -10080)" ]; then + check_result=1 return 0 fi get_swan_ver if [ -s "$ipsec_bin" ] && [ "$swan_ver_old" = "$SWAN_VER" ]; then touch "$ipsec_bin" fi - [ "$swan_ver_old" = "$SWAN_VER" ] + [ "$swan_ver_old" = "$SWAN_VER" ] && check_result=1 } get_libreswan() { - if ! check_libreswan; then + if [ "$check_result" = "0" ]; then bigecho "Downloading Libreswan..." cd /opt/src || exit 1 swan_file="libreswan-$SWAN_VER.tar.gz" @@ -259,7 +262,7 @@ get_libreswan() { } install_libreswan() { - if ! check_libreswan; then + if [ "$check_result" = "0" ]; then bigecho "Compiling and installing Libreswan, please wait..." cd "libreswan-$SWAN_VER" || exit 1 sed -i '28s/stdlib\.h/sys\/types.h/' include/fd.h @@ -574,6 +577,7 @@ vpnsetup() { check_dns check_server_dns check_client_name + check_libreswan start_setup install_setup_pkgs detect_ip diff --git a/vpnsetup_amzn.sh b/vpnsetup_amzn.sh index cc01774..84b426f 100755 --- a/vpnsetup_amzn.sh +++ b/vpnsetup_amzn.sh @@ -219,29 +219,32 @@ get_swan_ver() { SWAN_VER=4.6 base_url="https://github.com/hwdsl2/vpn-extras/releases/download/v1.0.0" swan_ver_url="$base_url/v1-amzn-2-swanver" - swan_ver_latest=$(wget -t 3 -T 15 -qO- "$swan_ver_url" | head -n 1) + swan_ver_latest=$(wget -t 3 -T 15 -qO- "$swan_ver_url" 2>/dev/null | head -n 1) + [ -z "$swan_ver_latest" ] && swan_ver_latest=$(curl -fsL "$swan_ver_url" 2>/dev/null | head -n 1) if printf '%s' "$swan_ver_latest" | grep -Eq '^([3-9]|[1-9][0-9]{1,2})(\.([0-9]|[1-9][0-9]{1,2})){1,2}$'; then SWAN_VER="$swan_ver_latest" fi } check_libreswan() { + check_result=0 ipsec_ver=$(/usr/local/sbin/ipsec --version 2>/dev/null) swan_ver_old=$(printf '%s' "$ipsec_ver" | sed -e 's/.*Libreswan U\?//' -e 's/\( (\|\/K\).*//') ipsec_bin="/usr/local/sbin/ipsec" if [ -n "$swan_ver_old" ] && printf '%s' "$ipsec_ver" | grep -qi 'libreswan' \ && [ "$(find "$ipsec_bin" -mmin -10080)" ]; then + check_result=1 return 0 fi get_swan_ver if [ -s "$ipsec_bin" ] && [ "$swan_ver_old" = "$SWAN_VER" ]; then touch "$ipsec_bin" fi - [ "$swan_ver_old" = "$SWAN_VER" ] + [ "$swan_ver_old" = "$SWAN_VER" ] && check_result=1 } get_libreswan() { - if ! check_libreswan; then + if [ "$check_result" = "0" ]; then bigecho "Downloading Libreswan..." cd /opt/src || exit 1 swan_file="libreswan-$SWAN_VER.tar.gz" @@ -259,7 +262,7 @@ get_libreswan() { } install_libreswan() { - if ! check_libreswan; then + if [ "$check_result" = "0" ]; then bigecho "Compiling and installing Libreswan, please wait..." cd "libreswan-$SWAN_VER" || exit 1 cat > Makefile.inc.local <<'EOF' @@ -590,6 +593,7 @@ vpnsetup() { check_dns check_server_dns check_client_name + check_libreswan start_setup install_setup_pkgs detect_ip diff --git a/vpnsetup_centos.sh b/vpnsetup_centos.sh index 787fb11..59fab92 100755 --- a/vpnsetup_centos.sh +++ b/vpnsetup_centos.sh @@ -290,29 +290,32 @@ get_swan_ver() { SWAN_VER=4.6 base_url="https://github.com/hwdsl2/vpn-extras/releases/download/v1.0.0" swan_ver_url="$base_url/v1-$os_type-$os_ver-swanver" - swan_ver_latest=$(wget -t 3 -T 15 -qO- "$swan_ver_url" | head -n 1) + swan_ver_latest=$(wget -t 3 -T 15 -qO- "$swan_ver_url" 2>/dev/null | head -n 1) + [ -z "$swan_ver_latest" ] && swan_ver_latest=$(curl -fsL "$swan_ver_url" 2>/dev/null | head -n 1) if printf '%s' "$swan_ver_latest" | grep -Eq '^([3-9]|[1-9][0-9]{1,2})(\.([0-9]|[1-9][0-9]{1,2})){1,2}$'; then SWAN_VER="$swan_ver_latest" fi } check_libreswan() { + check_result=0 ipsec_ver=$(/usr/local/sbin/ipsec --version 2>/dev/null) swan_ver_old=$(printf '%s' "$ipsec_ver" | sed -e 's/.*Libreswan U\?//' -e 's/\( (\|\/K\).*//') ipsec_bin="/usr/local/sbin/ipsec" if [ -n "$swan_ver_old" ] && printf '%s' "$ipsec_ver" | grep -qi 'libreswan' \ && [ "$(find "$ipsec_bin" -mmin -10080)" ]; then + check_result=1 return 0 fi get_swan_ver if [ -s "$ipsec_bin" ] && [ "$swan_ver_old" = "$SWAN_VER" ]; then touch "$ipsec_bin" fi - [ "$swan_ver_old" = "$SWAN_VER" ] + [ "$swan_ver_old" = "$SWAN_VER" ] && check_result=1 } get_libreswan() { - if ! check_libreswan; then + if [ "$check_result" = "0" ]; then bigecho "Downloading Libreswan..." cd /opt/src || exit 1 swan_file="libreswan-$SWAN_VER.tar.gz" @@ -330,7 +333,7 @@ get_libreswan() { } install_libreswan() { - if ! check_libreswan; then + if [ "$check_result" = "0" ]; then bigecho "Compiling and installing Libreswan, please wait..." cd "libreswan-$SWAN_VER" || exit 1 cat > Makefile.inc.local <<'EOF' @@ -718,6 +721,7 @@ vpnsetup() { check_dns check_server_dns check_client_name + check_libreswan start_setup install_setup_pkgs detect_ip diff --git a/vpnsetup_ubuntu.sh b/vpnsetup_ubuntu.sh index fd73cd4..78de6c1 100755 --- a/vpnsetup_ubuntu.sh +++ b/vpnsetup_ubuntu.sh @@ -259,29 +259,32 @@ get_swan_ver() { SWAN_VER=4.6 base_url="https://github.com/hwdsl2/vpn-extras/releases/download/v1.0.0" swan_ver_url="$base_url/v1-$os_type-$os_ver-swanver" - swan_ver_latest=$(wget -t 3 -T 15 -qO- "$swan_ver_url" | head -n 1) + swan_ver_latest=$(wget -t 3 -T 15 -qO- "$swan_ver_url" 2>/dev/null | head -n 1) + [ -z "$swan_ver_latest" ] && swan_ver_latest=$(curl -fsL "$swan_ver_url" 2>/dev/null | head -n 1) if printf '%s' "$swan_ver_latest" | grep -Eq '^([3-9]|[1-9][0-9]{1,2})(\.([0-9]|[1-9][0-9]{1,2})){1,2}$'; then SWAN_VER="$swan_ver_latest" fi } check_libreswan() { + check_result=0 ipsec_ver=$(/usr/local/sbin/ipsec --version 2>/dev/null) swan_ver_old=$(printf '%s' "$ipsec_ver" | sed -e 's/.*Libreswan U\?//' -e 's/\( (\|\/K\).*//') ipsec_bin="/usr/local/sbin/ipsec" if [ -n "$swan_ver_old" ] && printf '%s' "$ipsec_ver" | grep -qi 'libreswan' \ && [ "$(find "$ipsec_bin" -mmin -10080)" ]; then + check_result=1 return 0 fi get_swan_ver if [ -s "$ipsec_bin" ] && [ "$swan_ver_old" = "$SWAN_VER" ]; then touch "$ipsec_bin" fi - [ "$swan_ver_old" = "$SWAN_VER" ] + [ "$swan_ver_old" = "$SWAN_VER" ] && check_result=1 } get_libreswan() { - if ! check_libreswan; then + if [ "$check_result" = "0" ]; then bigecho "Downloading Libreswan..." cd /opt/src || exit 1 swan_file="libreswan-$SWAN_VER.tar.gz" @@ -299,7 +302,7 @@ get_libreswan() { } install_libreswan() { - if ! check_libreswan; then + if [ "$check_result" = "0" ]; then bigecho "Compiling and installing Libreswan, please wait..." cd "libreswan-$SWAN_VER" || exit 1 cat > Makefile.inc.local <<'EOF' @@ -693,6 +696,7 @@ vpnsetup() { check_server_dns check_client_name check_iptables + check_libreswan start_setup wait_for_apt update_apt_cache