From cfd9128e3f0210881e7469a10ccb46497425e715 Mon Sep 17 00:00:00 2001 From: hwdsl2 Date: Fri, 13 Aug 2021 22:15:11 -0500 Subject: [PATCH] Improve VPN setup - Wait for apt to be available - Check for Wget and install it if not found - Fallback to cURL if Wget fails - Cleanup --- extras/quickstart.sh | 51 +++++++++++++++++++++++++++++--------------- vpnsetup.sh | 51 +++++++++++++++++++++++++++++--------------- 2 files changed, 68 insertions(+), 34 deletions(-) diff --git a/extras/quickstart.sh b/extras/quickstart.sh index d8c7fd2..f0c793d 100755 --- a/extras/quickstart.sh +++ b/extras/quickstart.sh @@ -106,22 +106,39 @@ check_iptables() { fi } +wait_for_apt() { + count=0 + apt_lk=/var/lib/apt/lists/lock + pkg_lk=/var/lib/dpkg/lock + while fuser "$apt_lk" "$pkg_lk" >/dev/null 2>&1 \ + || lsof "$apt_lk" >/dev/null 2>&1 || lsof "$pkg_lk" >/dev/null 2>&1; do + [ "$count" = "0" ] && echo "## Waiting for apt to be available..." + [ "$count" -ge "100" ] && exiterr "Could not get apt/dpkg lock." + count=$((count+1)) + printf '%s' '.' + sleep 3 + done +} + install_wget() { - if [ "$os_type" = "ubuntu" ] || [ "$os_type" = "debian" ] || [ "$os_type" = "raspbian" ]; then - export DEBIAN_FRONTEND=noninteractive - ( - set -x - apt-get -yqq update - ) || exiterr "'apt-get update' failed." - ( - set -x - apt-get -yqq install wget >/dev/null - ) || exiterr "'apt-get install wget' failed." - else - ( - set -x - yum -y -q install wget >/dev/null - ) || exiterr "'yum install wget' failed." + if ! command -v wget >/dev/null 2>&1; then + if [ "$os_type" = "ubuntu" ] || [ "$os_type" = "debian" ] || [ "$os_type" = "raspbian" ]; then + wait_for_apt + export DEBIAN_FRONTEND=noninteractive + ( + set -x + apt-get -yqq update + ) || exiterr "'apt-get update' failed." + ( + set -x + apt-get -yqq install wget >/dev/null + ) || exiterr "'apt-get install wget' failed." + else + ( + set -x + yum -y -q install wget >/dev/null + ) || exiterr "'yum install wget' failed." + fi fi } @@ -138,10 +155,10 @@ get_setup_url() { run_setup() { status=0 - get_setup_url TMPDIR=$(mktemp -d /tmp/vpnsetup.XXXXX 2>/dev/null) if [ -d "$TMPDIR" ]; then - if ( set -x; wget -t 3 -T 30 -q -O "$TMPDIR/vpn.sh" "$setup_url" ); then + if ( set -x; wget -t 3 -T 30 -q -O "$TMPDIR/vpn.sh" "$setup_url" \ + || curl -fsL "$setup_url" -o "$TMPDIR/vpn.sh" 2>/dev/null ); then if /bin/sh "$TMPDIR/vpn.sh"; then if [ -s /opt/src/ikev2.sh ] && [ ! -f /etc/ipsec.d/ikev2.conf ]; then sleep 1 diff --git a/vpnsetup.sh b/vpnsetup.sh index 3a0a4d6..a7ad2e9 100755 --- a/vpnsetup.sh +++ b/vpnsetup.sh @@ -160,22 +160,39 @@ check_iptables() { fi } +wait_for_apt() { + count=0 + apt_lk=/var/lib/apt/lists/lock + pkg_lk=/var/lib/dpkg/lock + while fuser "$apt_lk" "$pkg_lk" >/dev/null 2>&1 \ + || lsof "$apt_lk" >/dev/null 2>&1 || lsof "$pkg_lk" >/dev/null 2>&1; do + [ "$count" = "0" ] && echo "## Waiting for apt to be available..." + [ "$count" -ge "100" ] && exiterr "Could not get apt/dpkg lock." + count=$((count+1)) + printf '%s' '.' + sleep 3 + done +} + install_wget() { - if [ "$os_type" = "ubuntu" ] || [ "$os_type" = "debian" ] || [ "$os_type" = "raspbian" ]; then - export DEBIAN_FRONTEND=noninteractive - ( - set -x - apt-get -yqq update - ) || exiterr "'apt-get update' failed." - ( - set -x - apt-get -yqq install wget >/dev/null - ) || exiterr "'apt-get install wget' failed." - else - ( - set -x - yum -y -q install wget >/dev/null - ) || exiterr "'yum install wget' failed." + if ! command -v wget >/dev/null 2>&1; then + if [ "$os_type" = "ubuntu" ] || [ "$os_type" = "debian" ] || [ "$os_type" = "raspbian" ]; then + wait_for_apt + export DEBIAN_FRONTEND=noninteractive + ( + set -x + apt-get -yqq update + ) || exiterr "'apt-get update' failed." + ( + set -x + apt-get -yqq install wget >/dev/null + ) || exiterr "'apt-get install wget' failed." + else + ( + set -x + yum -y -q install wget >/dev/null + ) || exiterr "'yum install wget' failed." + fi fi } @@ -192,10 +209,10 @@ get_setup_url() { run_setup() { status=0 - get_setup_url TMPDIR=$(mktemp -d /tmp/vpnsetup.XXXXX 2>/dev/null) if [ -d "$TMPDIR" ]; then - if ( set -x; wget -t 3 -T 30 -q -O "$TMPDIR/vpn.sh" "$setup_url" ); then + if ( set -x; wget -t 3 -T 30 -q -O "$TMPDIR/vpn.sh" "$setup_url" \ + || curl -fsL "$setup_url" -o "$TMPDIR/vpn.sh" 2>/dev/null ); then VPN_IPSEC_PSK="$VPN_IPSEC_PSK" VPN_USER="$VPN_USER" VPN_PASSWORD="$VPN_PASSWORD" \ VPN_PUBLIC_IP="$VPN_PUBLIC_IP" VPN_L2TP_NET="$VPN_L2TP_NET" \ VPN_L2TP_LOCAL="$VPN_L2TP_LOCAL" VPN_L2TP_POOL="$VPN_L2TP_POOL" \