1
0
mirror of synced 2024-11-25 14:26:09 +03:00

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
This commit is contained in:
hwdsl2 2021-08-13 22:15:11 -05:00
parent 6ce1be392d
commit cfd9128e3f
2 changed files with 68 additions and 34 deletions

View File

@ -106,8 +106,24 @@ check_iptables() {
fi 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() { install_wget() {
if ! command -v wget >/dev/null 2>&1; then
if [ "$os_type" = "ubuntu" ] || [ "$os_type" = "debian" ] || [ "$os_type" = "raspbian" ]; then if [ "$os_type" = "ubuntu" ] || [ "$os_type" = "debian" ] || [ "$os_type" = "raspbian" ]; then
wait_for_apt
export DEBIAN_FRONTEND=noninteractive export DEBIAN_FRONTEND=noninteractive
( (
set -x set -x
@ -123,6 +139,7 @@ install_wget() {
yum -y -q install wget >/dev/null yum -y -q install wget >/dev/null
) || exiterr "'yum install wget' failed." ) || exiterr "'yum install wget' failed."
fi fi
fi
} }
get_setup_url() { get_setup_url() {
@ -138,10 +155,10 @@ get_setup_url() {
run_setup() { run_setup() {
status=0 status=0
get_setup_url
TMPDIR=$(mktemp -d /tmp/vpnsetup.XXXXX 2>/dev/null) TMPDIR=$(mktemp -d /tmp/vpnsetup.XXXXX 2>/dev/null)
if [ -d "$TMPDIR" ]; then 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 /bin/sh "$TMPDIR/vpn.sh"; then
if [ -s /opt/src/ikev2.sh ] && [ ! -f /etc/ipsec.d/ikev2.conf ]; then if [ -s /opt/src/ikev2.sh ] && [ ! -f /etc/ipsec.d/ikev2.conf ]; then
sleep 1 sleep 1

View File

@ -160,8 +160,24 @@ check_iptables() {
fi 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() { install_wget() {
if ! command -v wget >/dev/null 2>&1; then
if [ "$os_type" = "ubuntu" ] || [ "$os_type" = "debian" ] || [ "$os_type" = "raspbian" ]; then if [ "$os_type" = "ubuntu" ] || [ "$os_type" = "debian" ] || [ "$os_type" = "raspbian" ]; then
wait_for_apt
export DEBIAN_FRONTEND=noninteractive export DEBIAN_FRONTEND=noninteractive
( (
set -x set -x
@ -177,6 +193,7 @@ install_wget() {
yum -y -q install wget >/dev/null yum -y -q install wget >/dev/null
) || exiterr "'yum install wget' failed." ) || exiterr "'yum install wget' failed."
fi fi
fi
} }
get_setup_url() { get_setup_url() {
@ -192,10 +209,10 @@ get_setup_url() {
run_setup() { run_setup() {
status=0 status=0
get_setup_url
TMPDIR=$(mktemp -d /tmp/vpnsetup.XXXXX 2>/dev/null) TMPDIR=$(mktemp -d /tmp/vpnsetup.XXXXX 2>/dev/null)
if [ -d "$TMPDIR" ]; then 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_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_PUBLIC_IP="$VPN_PUBLIC_IP" VPN_L2TP_NET="$VPN_L2TP_NET" \
VPN_L2TP_LOCAL="$VPN_L2TP_LOCAL" VPN_L2TP_POOL="$VPN_L2TP_POOL" \ VPN_L2TP_LOCAL="$VPN_L2TP_LOCAL" VPN_L2TP_POOL="$VPN_L2TP_POOL" \