diff --git a/openvpn-install.sh b/openvpn-install.sh index 45c2bce..9ebcdd6 100644 --- a/openvpn-install.sh +++ b/openvpn-install.sh @@ -21,6 +21,11 @@ check_ip() { printf '%s' "$1" | tr -d '\n' | grep -Eq "$IP_REGEX" } +check_dns_name() { + FQDN_REGEX='^([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$' + printf '%s' "$1" | tr -d '\n' | grep -Eq "$FQDN_REGEX" +} + check_root() { if [ "$(id -u)" != 0 ]; then exiterr "This installer must be run as root. Try 'sudo bash $0'" @@ -79,12 +84,10 @@ check_os_ver() { exiterr "Ubuntu 20.04 or higher is required to use this installer. This version of Ubuntu is too old and unsupported." fi - if [[ "$os" == "debian" && "$os_version" -lt 10 ]]; then exiterr "Debian 10 or higher is required to use this installer. This version of Debian is too old and unsupported." fi - if [[ "$os" == "centos" && "$os_version" -lt 7 ]]; then exiterr "CentOS 7 or higher is required to use this installer. This version of CentOS is too old and unsupported." @@ -124,11 +127,6 @@ check_nftables() { fi } -check_dns_name() { - FQDN_REGEX='^([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$' - printf '%s' "$1" | tr -d '\n' | grep -Eq "$FQDN_REGEX" -} - install_wget() { # Detect some Debian minimal setups where neither wget nor curl are installed if ! hash wget 2>/dev/null && ! hash curl 2>/dev/null; then @@ -172,11 +170,52 @@ install_iproute() { fi } -show_start_setup() { +show_header() { +cat <<'EOF' + +OpenVPN Script +https://github.com/hwdsl2/openvpn-install +EOF +} + +show_header2() { +cat <<'EOF' + +Welcome to this OpenVPN server installer! +GitHub: https://github.com/hwdsl2/openvpn-install +EOF +} + +show_header3() { +cat <<'EOF' + +Copyright (c) 2022-2024 Lin Song +Copyright (c) 2013-2023 Nyr +EOF +} + +show_usage() { + if [ -n "$1" ]; then + echo "Error: $1" >&2 + fi + show_header + show_header3 +cat 1>&2 </dev/null; then @@ -447,6 +493,11 @@ confirm_setup() { fi } +show_start_setup() { + echo + echo "Installing OpenVPN, please wait..." +} + disable_limitnproc() { # If running inside a container, disable LimitNPROC to prevent conflicts if systemd-detect-virt -cq; then @@ -742,10 +793,6 @@ crl-verify crl.pem" >> "$OVPN_CONF" fi } -show_clients() { - tail -n +2 /etc/openvpn/server/easy-rsa/pki/index.txt | grep "^V" | cut -d '=' -f 2 | nl -s ') ' -} - get_export_dir() { export_to_home_dir=0 export_dir=~/ @@ -858,6 +905,8 @@ update_selinux() { } create_client_common() { + # If the server is behind NAT, use the correct IP address + [[ -n "$public_ip" ]] && ip="$public_ip" # client-common.txt is created so we have a template to add further users later echo "client dev tun @@ -875,7 +924,6 @@ verb 3" > /etc/openvpn/server/client-common.txt } start_openvpn_service() { - # Enable and start the OpenVPN service if [ "$os" != "openSUSE" ]; then ( set -x @@ -898,22 +946,6 @@ finish_setup() { echo "New clients can be added by running this script again." } -show_header() { -cat <<'EOF' - -OpenVPN Script -https://github.com/hwdsl2/openvpn-install -EOF -} - -show_header2() { -cat <<'EOF' - -Copyright (c) 2022-2024 Lin Song -Copyright (c) 2013-2023 Nyr -EOF -} - select_menu_option() { echo echo "OpenVPN is already installed." @@ -932,23 +964,167 @@ select_menu_option() { done } -show_usage() { - if [ -n "$1" ]; then - echo "Error: $1" >&2 +show_clients() { + tail -n +2 /etc/openvpn/server/easy-rsa/pki/index.txt | grep "^V" | cut -d '=' -f 2 | nl -s ') ' +} + +enter_client_name() { + echo + echo "Provide a name for the client:" + read -rp "Name: " unsanitized_client + [ -z "$unsanitized_client" ] && abort_and_exit + set_client_name + while [[ -z "$client" || -e /etc/openvpn/server/easy-rsa/pki/issued/"$client".crt ]]; do + echo "$client: invalid name." + read -rp "Name: " unsanitized_client + [ -z "$unsanitized_client" ] && abort_and_exit + set_client_name + done +} + +build_client_config() { + cd /etc/openvpn/server/easy-rsa/ || exit 1 + ( + set -x + ./easyrsa --batch --days=3650 build-client-full "$client" nopass >/dev/null 2>&1 + ) +} + +print_client_action() { + echo + echo "$client $1. Configuration available in: $export_dir$client.ovpn" +} + +print_check_clients() { + echo + echo "Checking for existing client(s)..." +} + +check_clients() { + num_of_clients=$(tail -n +2 /etc/openvpn/server/easy-rsa/pki/index.txt | grep -c "^V") + if [[ "$num_of_clients" = 0 ]]; then + echo + echo "There are no existing clients!" + exit fi - show_header - show_header2 -cat 1>&2 <