2021-08-10 10:57:18 +03:00
|
|
|
#!/bin/sh
|
|
|
|
#
|
2022-04-27 08:05:45 +03:00
|
|
|
# Script for automatic setup of an IPsec VPN server on Ubuntu, Debian, CentOS/RHEL,
|
2022-03-21 07:10:11 +03:00
|
|
|
# Rocky Linux, AlmaLinux, Oracle Linux, Amazon Linux 2 and Alpine Linux
|
2021-08-10 10:57:18 +03:00
|
|
|
#
|
|
|
|
# DO NOT RUN THIS SCRIPT ON YOUR PC OR MAC!
|
|
|
|
#
|
|
|
|
# The latest version of this script is available at:
|
|
|
|
# https://github.com/hwdsl2/setup-ipsec-vpn
|
|
|
|
#
|
2023-01-05 03:58:29 +03:00
|
|
|
# Copyright (C) 2021-2023 Lin Song <linsongui@gmail.com>
|
2021-08-10 10:57:18 +03:00
|
|
|
#
|
|
|
|
# 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!
|
|
|
|
|
2022-02-26 08:41:49 +03:00
|
|
|
# =====================================================
|
|
|
|
|
|
|
|
# Define your own values for these variables
|
|
|
|
# - IPsec pre-shared key, VPN username and password
|
|
|
|
# - All values MUST be placed inside 'single quotes'
|
|
|
|
# - DO NOT use these special characters within values: \ " '
|
|
|
|
|
|
|
|
YOUR_IPSEC_PSK=''
|
|
|
|
YOUR_USERNAME=''
|
|
|
|
YOUR_PASSWORD=''
|
|
|
|
|
|
|
|
# =====================================================
|
|
|
|
|
2021-08-10 10:57:18 +03:00
|
|
|
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
|
|
|
|
|
|
|
|
exiterr() { echo "Error: $1" >&2; exit 1; }
|
|
|
|
|
2022-02-26 08:41:49 +03:00
|
|
|
check_ip() {
|
|
|
|
IP_REGEX='^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$'
|
|
|
|
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"
|
|
|
|
}
|
|
|
|
|
2021-08-10 10:57:18 +03:00
|
|
|
check_root() {
|
|
|
|
if [ "$(id -u)" != 0 ]; then
|
|
|
|
exiterr "Script must be run as root. Try 'sudo sh $0'"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
check_vz() {
|
|
|
|
if [ -f /proc/user_beancounters ]; then
|
|
|
|
exiterr "OpenVZ VPS is not supported."
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2021-09-18 08:53:15 +03:00
|
|
|
check_lxc() {
|
|
|
|
# shellcheck disable=SC2154
|
|
|
|
if [ "$container" = "lxc" ] && [ ! -e /dev/ppp ]; then
|
2021-09-18 22:58:06 +03:00
|
|
|
cat 1>&2 <<'EOF'
|
2021-09-18 08:53:15 +03:00
|
|
|
Error: /dev/ppp is missing. LXC containers require configuration.
|
|
|
|
See: https://github.com/hwdsl2/setup-ipsec-vpn/issues/1014
|
|
|
|
EOF
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2021-08-10 10:57:18 +03:00
|
|
|
check_os() {
|
|
|
|
rh_file="/etc/redhat-release"
|
2022-08-11 06:41:55 +03:00
|
|
|
if [ -f "$rh_file" ]; then
|
|
|
|
os_type=centos
|
|
|
|
if grep -q "Red Hat" "$rh_file"; then
|
|
|
|
os_type=rhel
|
|
|
|
fi
|
|
|
|
[ -f /etc/oracle-release ] && os_type=ol
|
|
|
|
grep -qi rocky "$rh_file" && os_type=rocky
|
|
|
|
grep -qi alma "$rh_file" && os_type=alma
|
|
|
|
if grep -q "release 7" "$rh_file"; then
|
|
|
|
os_ver=7
|
|
|
|
elif grep -q "release 8" "$rh_file"; then
|
|
|
|
os_ver=8
|
|
|
|
grep -qi stream "$rh_file" && os_ver=8s
|
|
|
|
if [ "$os_type$os_ver" = "centos8" ]; then
|
|
|
|
exiterr "CentOS Linux 8 is EOL and not supported."
|
|
|
|
fi
|
|
|
|
elif grep -q "release 9" "$rh_file"; then
|
|
|
|
os_ver=9
|
|
|
|
grep -qi stream "$rh_file" && os_ver=9s
|
|
|
|
else
|
|
|
|
exiterr "This script only supports CentOS/RHEL 7-9."
|
2022-03-09 06:39:19 +03:00
|
|
|
fi
|
2021-08-10 10:57:18 +03:00
|
|
|
elif grep -qs "Amazon Linux release 2" /etc/system-release; then
|
|
|
|
os_type=amzn
|
|
|
|
os_ver=2
|
|
|
|
else
|
|
|
|
os_type=$(lsb_release -si 2>/dev/null)
|
|
|
|
[ -z "$os_type" ] && [ -f /etc/os-release ] && os_type=$(. /etc/os-release && printf '%s' "$ID")
|
|
|
|
case $os_type in
|
|
|
|
[Uu]buntu)
|
|
|
|
os_type=ubuntu
|
|
|
|
;;
|
2022-08-11 07:25:58 +03:00
|
|
|
[Dd]ebian|[Kk]ali)
|
2021-08-10 10:57:18 +03:00
|
|
|
os_type=debian
|
|
|
|
;;
|
|
|
|
[Rr]aspbian)
|
|
|
|
os_type=raspbian
|
|
|
|
;;
|
2021-09-11 23:00:29 +03:00
|
|
|
[Aa]lpine)
|
|
|
|
os_type=alpine
|
|
|
|
;;
|
2021-08-10 10:57:18 +03:00
|
|
|
*)
|
2021-09-20 05:51:14 +03:00
|
|
|
cat 1>&2 <<'EOF'
|
|
|
|
Error: This script only supports one of the following OS:
|
2022-03-21 07:10:11 +03:00
|
|
|
Ubuntu, Debian, CentOS/RHEL, Rocky Linux, AlmaLinux,
|
|
|
|
Oracle Linux, Amazon Linux 2 or Alpine Linux
|
2021-09-20 05:51:14 +03:00
|
|
|
EOF
|
2021-09-11 23:00:29 +03:00
|
|
|
exit 1
|
2021-08-10 10:57:18 +03:00
|
|
|
;;
|
|
|
|
esac
|
2021-09-11 23:00:29 +03:00
|
|
|
if [ "$os_type" = "alpine" ]; then
|
|
|
|
os_ver=$(. /etc/os-release && printf '%s' "$VERSION_ID" | cut -d '.' -f 1,2)
|
2022-06-20 06:41:21 +03:00
|
|
|
if [ "$os_ver" != "3.15" ] && [ "$os_ver" != "3.16" ]; then
|
|
|
|
exiterr "This script only supports Alpine Linux 3.15/3.16."
|
2021-09-11 23:00:29 +03:00
|
|
|
fi
|
|
|
|
else
|
|
|
|
os_ver=$(sed 's/\..*//' /etc/debian_version | tr -dc 'A-Za-z0-9')
|
2022-09-25 02:56:27 +03:00
|
|
|
if [ "$os_ver" = 8 ] || [ "$os_ver" = "jessiesid" ]; then
|
2021-09-11 23:00:29 +03:00
|
|
|
exiterr "Debian 8 or Ubuntu < 16.04 is not supported."
|
|
|
|
fi
|
2021-08-10 10:57:18 +03:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
check_iface() {
|
|
|
|
def_iface=$(route 2>/dev/null | grep -m 1 '^default' | grep -o '[^ ]*$')
|
2021-09-11 23:00:29 +03:00
|
|
|
if [ "$os_type" != "alpine" ]; then
|
|
|
|
[ -z "$def_iface" ] && def_iface=$(ip -4 route list 0/0 2>/dev/null | grep -m 1 -Po '(?<=dev )(\S+)')
|
|
|
|
fi
|
2021-08-10 10:57:18 +03:00
|
|
|
def_state=$(cat "/sys/class/net/$def_iface/operstate" 2>/dev/null)
|
2021-08-11 07:09:48 +03:00
|
|
|
check_wl=0
|
2021-08-10 10:57:18 +03:00
|
|
|
if [ -n "$def_state" ] && [ "$def_state" != "down" ]; then
|
2021-08-11 07:09:48 +03:00
|
|
|
if [ "$os_type" = "ubuntu" ] || [ "$os_type" = "debian" ] || [ "$os_type" = "raspbian" ]; then
|
|
|
|
if ! uname -m | grep -qi -e '^arm' -e '^aarch64'; then
|
|
|
|
check_wl=1
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
check_wl=1
|
|
|
|
fi
|
|
|
|
fi
|
2022-09-25 02:56:27 +03:00
|
|
|
if [ "$check_wl" = 1 ]; then
|
2021-08-10 10:57:18 +03:00
|
|
|
case $def_iface in
|
|
|
|
wl*)
|
|
|
|
exiterr "Wireless interface '$def_iface' detected. DO NOT run this script on your PC or Mac!"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2022-02-26 08:41:49 +03:00
|
|
|
check_creds() {
|
|
|
|
[ -n "$YOUR_IPSEC_PSK" ] && VPN_IPSEC_PSK="$YOUR_IPSEC_PSK"
|
|
|
|
[ -n "$YOUR_USERNAME" ] && VPN_USER="$YOUR_USERNAME"
|
|
|
|
[ -n "$YOUR_PASSWORD" ] && VPN_PASSWORD="$YOUR_PASSWORD"
|
|
|
|
if [ -z "$VPN_IPSEC_PSK" ] && [ -z "$VPN_USER" ] && [ -z "$VPN_PASSWORD" ]; then
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
if [ -z "$VPN_IPSEC_PSK" ] || [ -z "$VPN_USER" ] || [ -z "$VPN_PASSWORD" ]; then
|
|
|
|
exiterr "All VPN credentials must be specified. Edit the script and re-enter them."
|
|
|
|
fi
|
|
|
|
if printf '%s' "$VPN_IPSEC_PSK $VPN_USER $VPN_PASSWORD" | LC_ALL=C grep -q '[^ -~]\+'; then
|
|
|
|
exiterr "VPN credentials must not contain non-ASCII characters."
|
|
|
|
fi
|
|
|
|
case "$VPN_IPSEC_PSK $VPN_USER $VPN_PASSWORD" in
|
|
|
|
*[\\\"\']*)
|
|
|
|
exiterr "VPN credentials must not contain these special characters: \\ \" '"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
|
|
|
check_dns() {
|
|
|
|
if { [ -n "$VPN_DNS_SRV1" ] && ! check_ip "$VPN_DNS_SRV1"; } \
|
|
|
|
|| { [ -n "$VPN_DNS_SRV2" ] && ! check_ip "$VPN_DNS_SRV2"; }; then
|
|
|
|
exiterr "The DNS server specified is invalid."
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
check_server_dns() {
|
|
|
|
if [ -n "$VPN_DNS_NAME" ] && ! check_dns_name "$VPN_DNS_NAME"; then
|
2022-08-28 05:51:19 +03:00
|
|
|
exiterr "Invalid DNS name. 'VPN_DNS_NAME' must be a fully qualified domain name (FQDN)."
|
2022-02-26 08:41:49 +03:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
check_client_name() {
|
|
|
|
if [ -n "$VPN_CLIENT_NAME" ]; then
|
|
|
|
name_len="$(printf '%s' "$VPN_CLIENT_NAME" | wc -m)"
|
|
|
|
if [ "$name_len" -gt "64" ] || printf '%s' "$VPN_CLIENT_NAME" | LC_ALL=C grep -q '[^A-Za-z0-9_-]\+' \
|
2022-04-27 08:05:45 +03:00
|
|
|
|| case $VPN_CLIENT_NAME in -*) true ;; *) false ;; esac; then
|
2022-02-26 08:41:49 +03:00
|
|
|
exiterr "Invalid client name. Use one word only, no special characters except '-' and '_'."
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2021-08-14 06:15:11 +03:00
|
|
|
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
|
2022-09-25 02:56:27 +03:00
|
|
|
[ "$count" = 0 ] && echo "## Waiting for apt to be available..."
|
|
|
|
[ "$count" -ge 100 ] && exiterr "Could not get apt/dpkg lock."
|
2021-08-14 06:15:11 +03:00
|
|
|
count=$((count+1))
|
|
|
|
printf '%s' '.'
|
|
|
|
sleep 3
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2021-09-11 23:00:29 +03:00
|
|
|
install_pkgs() {
|
2021-08-14 06:15:11 +03:00
|
|
|
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
|
2022-02-09 08:24:46 +03:00
|
|
|
apt-get -yqq update || apt-get -yqq update
|
2021-08-14 06:15:11 +03:00
|
|
|
) || exiterr "'apt-get update' failed."
|
|
|
|
(
|
|
|
|
set -x
|
2022-02-09 08:24:46 +03:00
|
|
|
apt-get -yqq install wget >/dev/null || apt-get -yqq install wget >/dev/null
|
2021-08-14 06:15:11 +03:00
|
|
|
) || exiterr "'apt-get install wget' failed."
|
2021-09-11 23:00:29 +03:00
|
|
|
elif [ "$os_type" != "alpine" ]; then
|
2021-08-14 06:15:11 +03:00
|
|
|
(
|
|
|
|
set -x
|
2022-02-09 08:24:46 +03:00
|
|
|
yum -y -q install wget >/dev/null || yum -y -q install wget >/dev/null
|
2021-08-14 06:15:11 +03:00
|
|
|
) || exiterr "'yum install wget' failed."
|
|
|
|
fi
|
2021-08-10 10:57:18 +03:00
|
|
|
fi
|
2021-09-11 23:00:29 +03:00
|
|
|
if [ "$os_type" = "alpine" ]; then
|
|
|
|
(
|
|
|
|
set -x
|
2021-09-14 08:49:13 +03:00
|
|
|
apk add -U -q bash coreutils grep net-tools sed wget
|
2021-09-11 23:00:29 +03:00
|
|
|
) || exiterr "'apk add' failed."
|
|
|
|
fi
|
2021-08-10 10:57:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
get_setup_url() {
|
2022-10-23 22:05:57 +03:00
|
|
|
base_url1="https://raw.githubusercontent.com/hwdsl2/setup-ipsec-vpn/master"
|
2022-04-12 06:36:43 +03:00
|
|
|
base_url2="https://gitlab.com/hwdsl2/setup-ipsec-vpn/-/raw/master"
|
2021-08-13 10:11:31 +03:00
|
|
|
sh_file="vpnsetup_ubuntu.sh"
|
2022-03-21 07:10:11 +03:00
|
|
|
if [ "$os_type" = "centos" ] || [ "$os_type" = "rhel" ] || [ "$os_type" = "rocky" ] \
|
|
|
|
|| [ "$os_type" = "alma" ] || [ "$os_type" = "ol" ]; then
|
2021-08-10 10:57:18 +03:00
|
|
|
sh_file="vpnsetup_centos.sh"
|
|
|
|
elif [ "$os_type" = "amzn" ]; then
|
|
|
|
sh_file="vpnsetup_amzn.sh"
|
2021-09-11 23:00:29 +03:00
|
|
|
elif [ "$os_type" = "alpine" ]; then
|
|
|
|
sh_file="vpnsetup_alpine.sh"
|
2021-08-10 10:57:18 +03:00
|
|
|
fi
|
2022-04-12 06:36:43 +03:00
|
|
|
setup_url1="$base_url1/$sh_file"
|
|
|
|
setup_url2="$base_url2/$sh_file"
|
2021-08-10 10:57:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
run_setup() {
|
|
|
|
status=0
|
2021-08-29 00:07:52 +03:00
|
|
|
if tmpdir=$(mktemp --tmpdir -d vpn.XXXXX 2>/dev/null); then
|
2022-04-12 06:36:43 +03:00
|
|
|
if ( set -x; wget -t 3 -T 30 -q -O "$tmpdir/vpn.sh" "$setup_url1" \
|
|
|
|
|| wget -t 3 -T 30 -q -O "$tmpdir/vpn.sh" "$setup_url2" \
|
2022-09-24 08:58:16 +03:00
|
|
|
|| curl -m 30 -fsL "$setup_url1" -o "$tmpdir/vpn.sh" 2>/dev/null ); then
|
2022-08-28 05:51:19 +03:00
|
|
|
VPN_IPSEC_PSK="$VPN_IPSEC_PSK" VPN_USER="$VPN_USER" \
|
|
|
|
VPN_PASSWORD="$VPN_PASSWORD" \
|
2022-03-04 07:05:09 +03:00
|
|
|
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_XAUTH_NET="$VPN_XAUTH_NET" VPN_XAUTH_POOL="$VPN_XAUTH_POOL" \
|
|
|
|
VPN_DNS_SRV1="$VPN_DNS_SRV1" VPN_DNS_SRV2="$VPN_DNS_SRV2" \
|
|
|
|
VPN_DNS_NAME="$VPN_DNS_NAME" VPN_CLIENT_NAME="$VPN_CLIENT_NAME" \
|
|
|
|
VPN_PROTECT_CONFIG="$VPN_PROTECT_CONFIG" \
|
2022-10-16 08:45:45 +03:00
|
|
|
VPN_CLIENT_VALIDITY="$VPN_CLIENT_VALIDITY" \
|
2022-08-27 23:59:43 +03:00
|
|
|
VPN_SKIP_IKEV2="$VPN_SKIP_IKEV2" \
|
2022-03-04 07:05:09 +03:00
|
|
|
/bin/bash "$tmpdir/vpn.sh" || status=1
|
2021-08-10 10:57:18 +03:00
|
|
|
else
|
|
|
|
status=1
|
|
|
|
echo "Error: Could not download VPN setup script." >&2
|
|
|
|
fi
|
2021-08-29 00:07:52 +03:00
|
|
|
/bin/rm -f "$tmpdir/vpn.sh"
|
|
|
|
/bin/rmdir "$tmpdir"
|
2021-08-10 10:57:18 +03:00
|
|
|
else
|
|
|
|
exiterr "Could not create temporary directory."
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2022-04-27 08:05:45 +03:00
|
|
|
vpnsetup() {
|
2021-08-10 10:57:18 +03:00
|
|
|
check_root
|
|
|
|
check_vz
|
2021-09-18 08:53:15 +03:00
|
|
|
check_lxc
|
2021-08-10 10:57:18 +03:00
|
|
|
check_os
|
|
|
|
check_iface
|
2022-02-26 08:41:49 +03:00
|
|
|
check_creds
|
|
|
|
check_dns
|
|
|
|
check_server_dns
|
|
|
|
check_client_name
|
2021-09-11 23:00:29 +03:00
|
|
|
install_pkgs
|
2021-08-10 10:57:18 +03:00
|
|
|
get_setup_url
|
|
|
|
run_setup
|
|
|
|
}
|
|
|
|
|
|
|
|
## Defer setup until we have the complete script
|
2022-04-27 08:05:45 +03:00
|
|
|
vpnsetup "$@"
|
2021-08-10 10:57:18 +03:00
|
|
|
|
|
|
|
exit "$status"
|