From c0a81ceb5bc08d08f0decb0e16ca303a3989580b Mon Sep 17 00:00:00 2001 From: hwdsl2 Date: Sun, 2 Jan 2022 01:01:02 -0600 Subject: [PATCH] Add IKEv2-only mode script - New: Helper script to enable or disable IKEv2-only mode --- extras/ikev2onlymode.sh | 178 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 178 insertions(+) create mode 100755 extras/ikev2onlymode.sh diff --git a/extras/ikev2onlymode.sh b/extras/ikev2onlymode.sh new file mode 100755 index 0000000..f15558f --- /dev/null +++ b/extras/ikev2onlymode.sh @@ -0,0 +1,178 @@ +#!/bin/bash +# +# Script to enable or disable IKEv2-only mode +# +# Copyright (C) 2022 Lin Song +# +# This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 +# Unported License: http://creativecommons.org/licenses/by-sa/3.0/ +# +# Attribution required: please include my name in any derivative and let me +# know how you have improved it! + +export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" + +exiterr() { echo "Error: $1" >&2; exit 1; } +bigecho() { echo "## $1"; } + +check_root() { + if [ "$(id -u)" != 0 ]; then + exiterr "Script must be run as root. Try 'sudo bash $0'" + fi +} + +abort_and_exit() { + echo "Abort. No changes were made." >&2 + exit 1 +} + +confirm_or_abort() { + printf '%s' "$1" + read -r response + case $response in + [yY][eE][sS]|[yY]) + echo + ;; + *) + abort_and_exit + ;; + esac +} + +check_ikev2_exists() { + grep -qs "conn ikev2-cp" /etc/ipsec.conf || [ -f /etc/ipsec.d/ikev2.conf ] +} + +check_libreswan() { + ipsec_ver=$(ipsec --version 2>/dev/null) + swan_ver=$(printf '%s' "$ipsec_ver" | sed -e 's/.*Libreswan U\?//' -e 's/\( (\|\/K\).*//') + if ! grep -qs "hwdsl2 VPN script" /etc/sysctl.conf \ + || ! grep -qs "config setup" /etc/ipsec.conf \ + || ! printf '%s' "$ipsec_ver" | grep -q "Libreswan"; then +cat 1>&2 <<'EOF' +Error: Your must first set up the IPsec VPN server before selecting IKEv2-only mode. + See: https://github.com/hwdsl2/setup-ipsec-vpn +EOF + exit 1 + fi + if ! check_ikev2_exists; then +cat 1>&2 <<'EOF' +Error: Your must first set up IKEv2 before selecting IKEv2-only mode. + See: https://git.io/ikev2 +EOF + exit 1 + fi + case $swan_ver in + 4.[2-9]|4.[1-9][0-9]) + true + ;; + *) +cat 1>&2 </dev/null +} + +print_complete() { +cat <<'EOF' +Done! + +EOF +} + +select_menu_option() { +cat <