Improve VPN setup
- Refactor VPN setup scripts into functions - Cleanup
This commit is contained in:
parent
fc33e1c451
commit
9336c1c2c2
158
vpnsetup_amzn.sh
158
vpnsetup_amzn.sh
@ -46,17 +46,20 @@ check_ip() {
|
|||||||
printf '%s' "$1" | tr -d '\n' | grep -Eq "$IP_REGEX"
|
printf '%s' "$1" | tr -d '\n' | grep -Eq "$IP_REGEX"
|
||||||
}
|
}
|
||||||
|
|
||||||
vpnsetup() {
|
check_root() {
|
||||||
|
if [ "$(id -u)" != 0 ]; then
|
||||||
|
exiterr "Script must be run as root. Try 'sudo sh $0'"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
check_os() {
|
||||||
os_arch=$(uname -m | tr -dc 'A-Za-z0-9_-')
|
os_arch=$(uname -m | tr -dc 'A-Za-z0-9_-')
|
||||||
if ! grep -qs "Amazon Linux release 2" /etc/system-release; then
|
if ! grep -qs "Amazon Linux release 2" /etc/system-release; then
|
||||||
exiterr "This script only supports Amazon Linux 2."
|
exiterr "This script only supports Amazon Linux 2."
|
||||||
fi
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
if [ "$(id -u)" != 0 ]; then
|
check_iface() {
|
||||||
exiterr "Script must be run as root. Try 'sudo sh $0'"
|
|
||||||
fi
|
|
||||||
|
|
||||||
def_iface=$(route 2>/dev/null | grep -m 1 '^default' | grep -o '[^ ]*$')
|
def_iface=$(route 2>/dev/null | grep -m 1 '^default' | grep -o '[^ ]*$')
|
||||||
[ -z "$def_iface" ] && def_iface=$(ip -4 route list 0/0 2>/dev/null | grep -m 1 -Po '(?<=dev )(\S+)')
|
[ -z "$def_iface" ] && def_iface=$(ip -4 route list 0/0 2>/dev/null | grep -m 1 -Po '(?<=dev )(\S+)')
|
||||||
def_state=$(cat "/sys/class/net/$def_iface/operstate" 2>/dev/null)
|
def_state=$(cat "/sys/class/net/$def_iface/operstate" 2>/dev/null)
|
||||||
@ -74,7 +77,9 @@ else
|
|||||||
fi
|
fi
|
||||||
NET_IFACE=eth0
|
NET_IFACE=eth0
|
||||||
fi
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
check_creds() {
|
||||||
[ -n "$YOUR_IPSEC_PSK" ] && VPN_IPSEC_PSK="$YOUR_IPSEC_PSK"
|
[ -n "$YOUR_IPSEC_PSK" ] && VPN_IPSEC_PSK="$YOUR_IPSEC_PSK"
|
||||||
[ -n "$YOUR_USERNAME" ] && VPN_USER="$YOUR_USERNAME"
|
[ -n "$YOUR_USERNAME" ] && VPN_USER="$YOUR_USERNAME"
|
||||||
[ -n "$YOUR_PASSWORD" ] && VPN_PASSWORD="$YOUR_PASSWORD"
|
[ -n "$YOUR_PASSWORD" ] && VPN_PASSWORD="$YOUR_PASSWORD"
|
||||||
@ -99,41 +104,51 @@ case "$VPN_IPSEC_PSK $VPN_USER $VPN_PASSWORD" in
|
|||||||
exiterr "VPN credentials must not contain these special characters: \\ \" '"
|
exiterr "VPN credentials must not contain these special characters: \\ \" '"
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
|
check_dns() {
|
||||||
if { [ -n "$VPN_DNS_SRV1" ] && ! check_ip "$VPN_DNS_SRV1"; } \
|
if { [ -n "$VPN_DNS_SRV1" ] && ! check_ip "$VPN_DNS_SRV1"; } \
|
||||||
|| { [ -n "$VPN_DNS_SRV2" ] && ! check_ip "$VPN_DNS_SRV2"; } then
|
|| { [ -n "$VPN_DNS_SRV2" ] && ! check_ip "$VPN_DNS_SRV2"; } then
|
||||||
exiterr "The DNS server specified is invalid."
|
exiterr "The DNS server specified is invalid."
|
||||||
fi
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# shellcheck disable=SC2154,SC2039,SC3047
|
||||||
|
start_setup() {
|
||||||
bigecho "VPN setup in progress... Please be patient."
|
bigecho "VPN setup in progress... Please be patient."
|
||||||
|
trap 'dlo=$dl;dl=$LINENO' DEBUG 2>/dev/null
|
||||||
|
trap 'finish $? $((dlo+1))' EXIT
|
||||||
mkdir -p /opt/src
|
mkdir -p /opt/src
|
||||||
cd /opt/src || exit 1
|
cd /opt/src || exit 1
|
||||||
|
}
|
||||||
|
|
||||||
|
install_setup_pkgs() {
|
||||||
bigecho "Installing packages required for setup..."
|
bigecho "Installing packages required for setup..."
|
||||||
|
|
||||||
(
|
(
|
||||||
set -x
|
set -x
|
||||||
yum -y -q install wget bind-utils openssl tar \
|
yum -y -q install wget bind-utils openssl tar \
|
||||||
iptables iproute gawk grep sed net-tools >/dev/null
|
iptables iproute gawk grep sed net-tools >/dev/null
|
||||||
) || exiterr2
|
) || exiterr2
|
||||||
|
}
|
||||||
|
|
||||||
|
detect_ip() {
|
||||||
bigecho "Trying to auto discover IP of this server..."
|
bigecho "Trying to auto discover IP of this server..."
|
||||||
|
|
||||||
public_ip=${VPN_PUBLIC_IP:-''}
|
public_ip=${VPN_PUBLIC_IP:-''}
|
||||||
check_ip "$public_ip" || public_ip=$(dig @resolver1.opendns.com -t A -4 myip.opendns.com +short)
|
check_ip "$public_ip" || public_ip=$(dig @resolver1.opendns.com -t A -4 myip.opendns.com +short)
|
||||||
check_ip "$public_ip" || public_ip=$(wget -t 3 -T 15 -qO- http://ipv4.icanhazip.com)
|
check_ip "$public_ip" || public_ip=$(wget -t 3 -T 15 -qO- http://ipv4.icanhazip.com)
|
||||||
check_ip "$public_ip" || exiterr "Cannot detect this server's public IP. Define it as variable 'VPN_PUBLIC_IP' and re-run this script."
|
check_ip "$public_ip" || exiterr "Cannot detect this server's public IP. Define it as variable 'VPN_PUBLIC_IP' and re-run this script."
|
||||||
|
}
|
||||||
|
|
||||||
|
add_epel_repo() {
|
||||||
bigecho "Adding the EPEL repository..."
|
bigecho "Adding the EPEL repository..."
|
||||||
|
|
||||||
(
|
(
|
||||||
set -x
|
set -x
|
||||||
amazon-linux-extras install epel -y >/dev/null
|
amazon-linux-extras install epel -y >/dev/null
|
||||||
) || exiterr2
|
) || exiterr2
|
||||||
|
}
|
||||||
|
|
||||||
|
install_vpn_pkgs_1() {
|
||||||
bigecho "Installing packages required for the VPN..."
|
bigecho "Installing packages required for the VPN..."
|
||||||
|
|
||||||
(
|
(
|
||||||
set -x
|
set -x
|
||||||
yum -y -q install nss-devel nspr-devel pkgconfig pam-devel \
|
yum -y -q install nss-devel nspr-devel pkgconfig pam-devel \
|
||||||
@ -142,29 +157,35 @@ bigecho "Installing packages required for the VPN..."
|
|||||||
systemd-devel iptables-services \
|
systemd-devel iptables-services \
|
||||||
libevent-devel fipscheck-devel >/dev/null
|
libevent-devel fipscheck-devel >/dev/null
|
||||||
) || exiterr2
|
) || exiterr2
|
||||||
|
}
|
||||||
|
|
||||||
|
install_vpn_pkgs_2() {
|
||||||
(
|
(
|
||||||
set -x
|
set -x
|
||||||
yum --enablerepo=epel -y -q install xl2tpd >/dev/null 2>&1
|
yum --enablerepo=epel -y -q install xl2tpd >/dev/null 2>&1
|
||||||
) || exiterr2
|
) || exiterr2
|
||||||
|
}
|
||||||
|
|
||||||
|
install_fail2ban() {
|
||||||
bigecho "Installing Fail2Ban to protect SSH..."
|
bigecho "Installing Fail2Ban to protect SSH..."
|
||||||
|
|
||||||
(
|
(
|
||||||
set -x
|
set -x
|
||||||
yum --enablerepo=epel -y -q install fail2ban >/dev/null
|
yum --enablerepo=epel -y -q install fail2ban >/dev/null
|
||||||
) || exiterr2
|
) || exiterr2
|
||||||
|
}
|
||||||
|
|
||||||
|
get_ikev2_script() {
|
||||||
bigecho "Downloading IKEv2 script..."
|
bigecho "Downloading IKEv2 script..."
|
||||||
|
|
||||||
ikev2_url="https://github.com/hwdsl2/setup-ipsec-vpn/raw/master/extras/ikev2setup.sh"
|
ikev2_url="https://github.com/hwdsl2/setup-ipsec-vpn/raw/master/extras/ikev2setup.sh"
|
||||||
(
|
(
|
||||||
set -x
|
set -x
|
||||||
wget -t 3 -T 30 -q -O ikev2.sh "$ikev2_url"
|
wget -t 3 -T 30 -q -O ikev2.sh "$ikev2_url"
|
||||||
) || /bin/rm -f ikev2.sh
|
) || /bin/rm -f ikev2.sh
|
||||||
[ -s ikev2.sh ] && chmod +x ikev2.sh && ln -s /opt/src/ikev2.sh /usr/bin 2>/dev/null
|
[ -s ikev2.sh ] && chmod +x ikev2.sh && ln -s /opt/src/ikev2.sh /usr/bin 2>/dev/null
|
||||||
|
}
|
||||||
|
|
||||||
|
get_libreswan() {
|
||||||
bigecho "Downloading Libreswan..."
|
bigecho "Downloading Libreswan..."
|
||||||
|
|
||||||
SWAN_VER=4.4
|
SWAN_VER=4.4
|
||||||
swan_file="libreswan-$SWAN_VER.tar.gz"
|
swan_file="libreswan-$SWAN_VER.tar.gz"
|
||||||
swan_url1="https://github.com/libreswan/libreswan/archive/v$SWAN_VER.tar.gz"
|
swan_url1="https://github.com/libreswan/libreswan/archive/v$SWAN_VER.tar.gz"
|
||||||
@ -175,9 +196,10 @@ swan_url2="https://download.libreswan.org/$swan_file"
|
|||||||
) || exit 1
|
) || exit 1
|
||||||
/bin/rm -rf "/opt/src/libreswan-$SWAN_VER"
|
/bin/rm -rf "/opt/src/libreswan-$SWAN_VER"
|
||||||
tar xzf "$swan_file" && /bin/rm -f "$swan_file"
|
tar xzf "$swan_file" && /bin/rm -f "$swan_file"
|
||||||
|
}
|
||||||
|
|
||||||
|
install_libreswan() {
|
||||||
bigecho "Compiling and installing Libreswan, please wait..."
|
bigecho "Compiling and installing Libreswan, please wait..."
|
||||||
|
|
||||||
cd "libreswan-$SWAN_VER" || exit 1
|
cd "libreswan-$SWAN_VER" || exit 1
|
||||||
cat > Makefile.inc.local <<'EOF'
|
cat > Makefile.inc.local <<'EOF'
|
||||||
WERROR_CFLAGS=-w -s
|
WERROR_CFLAGS=-w -s
|
||||||
@ -201,7 +223,9 @@ cd /opt/src || exit 1
|
|||||||
if ! /usr/local/sbin/ipsec --version 2>/dev/null | grep -qF "$SWAN_VER"; then
|
if ! /usr/local/sbin/ipsec --version 2>/dev/null | grep -qF "$SWAN_VER"; then
|
||||||
exiterr "Libreswan $SWAN_VER failed to build."
|
exiterr "Libreswan $SWAN_VER failed to build."
|
||||||
fi
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
create_vpn_config() {
|
||||||
bigecho "Creating VPN configuration..."
|
bigecho "Creating VPN configuration..."
|
||||||
|
|
||||||
L2TP_NET=${VPN_L2TP_NET:-'192.168.42.0/24'}
|
L2TP_NET=${VPN_L2TP_NET:-'192.168.42.0/24'}
|
||||||
@ -322,9 +346,24 @@ VPN_PASSWORD_ENC=$(openssl passwd -1 "$VPN_PASSWORD")
|
|||||||
cat > /etc/ipsec.d/passwd <<EOF
|
cat > /etc/ipsec.d/passwd <<EOF
|
||||||
$VPN_USER:$VPN_PASSWORD_ENC:xauth-psk
|
$VPN_USER:$VPN_PASSWORD_ENC:xauth-psk
|
||||||
EOF
|
EOF
|
||||||
|
}
|
||||||
|
|
||||||
|
create_f2b_config() {
|
||||||
|
F2B_FILE=/etc/fail2ban/jail.local
|
||||||
|
if [ ! -f "$F2B_FILE" ]; then
|
||||||
|
bigecho "Creating basic Fail2Ban rules..."
|
||||||
|
cat > "$F2B_FILE" <<'EOF'
|
||||||
|
[ssh-iptables]
|
||||||
|
enabled = true
|
||||||
|
filter = sshd
|
||||||
|
logpath = /var/log/secure
|
||||||
|
action = iptables[name=SSH, port=ssh, protocol=tcp]
|
||||||
|
EOF
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
update_sysctl() {
|
||||||
bigecho "Updating sysctl settings..."
|
bigecho "Updating sysctl settings..."
|
||||||
|
|
||||||
if ! grep -qs "hwdsl2 VPN script" /etc/sysctl.conf; then
|
if ! grep -qs "hwdsl2 VPN script" /etc/sysctl.conf; then
|
||||||
conf_bk "/etc/sysctl.conf"
|
conf_bk "/etc/sysctl.conf"
|
||||||
cat >> /etc/sysctl.conf <<EOF
|
cat >> /etc/sysctl.conf <<EOF
|
||||||
@ -349,21 +388,10 @@ net.ipv4.tcp_rmem = 10240 87380 12582912
|
|||||||
net.ipv4.tcp_wmem = 10240 87380 12582912
|
net.ipv4.tcp_wmem = 10240 87380 12582912
|
||||||
EOF
|
EOF
|
||||||
fi
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
F2B_FILE=/etc/fail2ban/jail.local
|
update_iptables() {
|
||||||
if [ ! -f "$F2B_FILE" ]; then
|
|
||||||
bigecho "Creating basic Fail2Ban rules..."
|
|
||||||
cat > "$F2B_FILE" <<'EOF'
|
|
||||||
[ssh-iptables]
|
|
||||||
enabled = true
|
|
||||||
filter = sshd
|
|
||||||
logpath = /var/log/secure
|
|
||||||
action = iptables[name=SSH, port=ssh, protocol=tcp]
|
|
||||||
EOF
|
|
||||||
fi
|
|
||||||
|
|
||||||
bigecho "Updating IPTables rules..."
|
bigecho "Updating IPTables rules..."
|
||||||
|
|
||||||
IPT_FILE=/etc/sysconfig/iptables
|
IPT_FILE=/etc/sysconfig/iptables
|
||||||
ipt_flag=0
|
ipt_flag=0
|
||||||
if ! grep -qs "hwdsl2 VPN script" "$IPT_FILE"; then
|
if ! grep -qs "hwdsl2 VPN script" "$IPT_FILE"; then
|
||||||
@ -396,9 +424,10 @@ if [ "$ipt_flag" = "1" ]; then
|
|||||||
echo "# Modified by hwdsl2 VPN script" > "$IPT_FILE"
|
echo "# Modified by hwdsl2 VPN script" > "$IPT_FILE"
|
||||||
iptables-save >> "$IPT_FILE"
|
iptables-save >> "$IPT_FILE"
|
||||||
fi
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
enable_on_boot() {
|
||||||
bigecho "Enabling services on boot..."
|
bigecho "Enabling services on boot..."
|
||||||
|
|
||||||
systemctl --now mask firewalld 2>/dev/null
|
systemctl --now mask firewalld 2>/dev/null
|
||||||
systemctl enable iptables fail2ban 2>/dev/null
|
systemctl enable iptables fail2ban 2>/dev/null
|
||||||
|
|
||||||
@ -417,18 +446,19 @@ service xl2tpd restart
|
|||||||
echo 1 > /proc/sys/net/ipv4/ip_forward)&
|
echo 1 > /proc/sys/net/ipv4/ip_forward)&
|
||||||
EOF
|
EOF
|
||||||
fi
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
start_services() {
|
||||||
bigecho "Starting services..."
|
bigecho "Starting services..."
|
||||||
|
|
||||||
restorecon /etc/ipsec.d/*db 2>/dev/null
|
|
||||||
restorecon /usr/local/sbin -Rv 2>/dev/null
|
|
||||||
restorecon /usr/local/libexec/ipsec -Rv 2>/dev/null
|
|
||||||
|
|
||||||
sysctl -e -q -p
|
sysctl -e -q -p
|
||||||
|
|
||||||
chmod +x /etc/rc.local
|
chmod +x /etc/rc.local
|
||||||
chmod 600 /etc/ipsec.secrets* /etc/ppp/chap-secrets* /etc/ipsec.d/passwd*
|
chmod 600 /etc/ipsec.secrets* /etc/ppp/chap-secrets* /etc/ipsec.d/passwd*
|
||||||
|
|
||||||
|
restorecon /etc/ipsec.d/*db 2>/dev/null
|
||||||
|
restorecon /usr/local/sbin -Rv 2>/dev/null
|
||||||
|
restorecon /usr/local/libexec/ipsec -Rv 2>/dev/null
|
||||||
|
|
||||||
iptables-restore < "$IPT_FILE"
|
iptables-restore < "$IPT_FILE"
|
||||||
|
|
||||||
# Fix xl2tpd if l2tp_ppp is unavailable
|
# Fix xl2tpd if l2tp_ppp is unavailable
|
||||||
@ -441,20 +471,9 @@ mkdir -p /run/pluto
|
|||||||
service fail2ban restart 2>/dev/null
|
service fail2ban restart 2>/dev/null
|
||||||
service ipsec restart 2>/dev/null
|
service ipsec restart 2>/dev/null
|
||||||
service xl2tpd restart 2>/dev/null
|
service xl2tpd restart 2>/dev/null
|
||||||
|
}
|
||||||
|
|
||||||
swan_ver_url="https://dl.ls20.com/v1/amzn/2/swanver?arch=$os_arch&ver=$SWAN_VER"
|
show_vpn_info() {
|
||||||
swan_ver_latest=$(wget -t 3 -T 15 -qO- "$swan_ver_url")
|
|
||||||
if printf '%s' "$swan_ver_latest" | grep -Eq '^([3-9]|[1-9][0-9]{1,2})(\.([0-9]|[1-9][0-9]{1,2})){1,2}$' \
|
|
||||||
&& [ -n "$SWAN_VER" ] && [ "$SWAN_VER" != "$swan_ver_latest" ] \
|
|
||||||
&& printf '%s\n%s' "$SWAN_VER" "$swan_ver_latest" | sort -C -V; then
|
|
||||||
cat <<EOF
|
|
||||||
|
|
||||||
Note: A newer version of Libreswan ($swan_ver_latest) is available.
|
|
||||||
To update, run:
|
|
||||||
wget https://git.io/vpnupgrade -O vpnup.sh && sudo sh vpnup.sh
|
|
||||||
EOF
|
|
||||||
fi
|
|
||||||
|
|
||||||
cat <<EOF
|
cat <<EOF
|
||||||
|
|
||||||
================================================
|
================================================
|
||||||
@ -477,7 +496,52 @@ IKEv2 guide: https://git.io/ikev2
|
|||||||
================================================
|
================================================
|
||||||
|
|
||||||
EOF
|
EOF
|
||||||
|
}
|
||||||
|
|
||||||
|
check_swan_ver() {
|
||||||
|
swan_ver_url="https://dl.ls20.com/v1/amzn/2/swanver?arch=$os_arch&ver=$SWAN_VER"
|
||||||
|
[ "$1" != "0" ] && swan_ver_url="$swan_ver_url&e=$2"
|
||||||
|
swan_ver_latest=$(wget -t 3 -T 15 -qO- "$swan_ver_url")
|
||||||
|
if printf '%s' "$swan_ver_latest" | grep -Eq '^([3-9]|[1-9][0-9]{1,2})(\.([0-9]|[1-9][0-9]{1,2})){1,2}$' \
|
||||||
|
&& [ "$1" = "0" ] && [ -n "$SWAN_VER" ] && [ "$SWAN_VER" != "$swan_ver_latest" ] \
|
||||||
|
&& printf '%s\n%s' "$SWAN_VER" "$swan_ver_latest" | sort -C -V; then
|
||||||
|
cat <<EOF
|
||||||
|
Note: A newer version of Libreswan ($swan_ver_latest) is available.
|
||||||
|
To update, run:
|
||||||
|
wget https://git.io/vpnupgrade -O vpnup.sh && sudo sh vpnup.sh
|
||||||
|
|
||||||
|
EOF
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
finish() {
|
||||||
|
check_swan_ver "$1" "$2"
|
||||||
|
exit "$1"
|
||||||
|
}
|
||||||
|
|
||||||
|
vpnsetup() {
|
||||||
|
check_root
|
||||||
|
check_os
|
||||||
|
check_iface
|
||||||
|
check_creds
|
||||||
|
check_dns
|
||||||
|
start_setup
|
||||||
|
install_setup_pkgs
|
||||||
|
detect_ip
|
||||||
|
add_epel_repo
|
||||||
|
install_vpn_pkgs_1
|
||||||
|
install_vpn_pkgs_2
|
||||||
|
install_fail2ban
|
||||||
|
get_ikev2_script
|
||||||
|
get_libreswan
|
||||||
|
install_libreswan
|
||||||
|
create_vpn_config
|
||||||
|
create_f2b_config
|
||||||
|
update_sysctl
|
||||||
|
update_iptables
|
||||||
|
enable_on_boot
|
||||||
|
start_services
|
||||||
|
show_vpn_info
|
||||||
}
|
}
|
||||||
|
|
||||||
## Defer setup until we have the complete script
|
## Defer setup until we have the complete script
|
||||||
|
@ -47,8 +47,19 @@ check_ip() {
|
|||||||
printf '%s' "$1" | tr -d '\n' | grep -Eq "$IP_REGEX"
|
printf '%s' "$1" | tr -d '\n' | grep -Eq "$IP_REGEX"
|
||||||
}
|
}
|
||||||
|
|
||||||
vpnsetup() {
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
check_os() {
|
||||||
os_type=centos
|
os_type=centos
|
||||||
os_arch=$(uname -m | tr -dc 'A-Za-z0-9_-')
|
os_arch=$(uname -m | tr -dc 'A-Za-z0-9_-')
|
||||||
rh_file="/etc/redhat-release"
|
rh_file="/etc/redhat-release"
|
||||||
@ -65,15 +76,9 @@ elif grep -qs "release 8" "$rh_file"; then
|
|||||||
else
|
else
|
||||||
exiterr "This script only supports CentOS/RHEL 7 and 8."
|
exiterr "This script only supports CentOS/RHEL 7 and 8."
|
||||||
fi
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
if [ -f /proc/user_beancounters ]; then
|
check_iface() {
|
||||||
exiterr "OpenVZ VPS is not supported."
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ "$(id -u)" != 0 ]; then
|
|
||||||
exiterr "Script must be run as root. Try 'sudo sh $0'"
|
|
||||||
fi
|
|
||||||
|
|
||||||
def_iface=$(route 2>/dev/null | grep -m 1 '^default' | grep -o '[^ ]*$')
|
def_iface=$(route 2>/dev/null | grep -m 1 '^default' | grep -o '[^ ]*$')
|
||||||
[ -z "$def_iface" ] && def_iface=$(ip -4 route list 0/0 2>/dev/null | grep -m 1 -Po '(?<=dev )(\S+)')
|
[ -z "$def_iface" ] && def_iface=$(ip -4 route list 0/0 2>/dev/null | grep -m 1 -Po '(?<=dev )(\S+)')
|
||||||
def_state=$(cat "/sys/class/net/$def_iface/operstate" 2>/dev/null)
|
def_state=$(cat "/sys/class/net/$def_iface/operstate" 2>/dev/null)
|
||||||
@ -91,7 +96,9 @@ else
|
|||||||
fi
|
fi
|
||||||
NET_IFACE=eth0
|
NET_IFACE=eth0
|
||||||
fi
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
check_creds() {
|
||||||
[ -n "$YOUR_IPSEC_PSK" ] && VPN_IPSEC_PSK="$YOUR_IPSEC_PSK"
|
[ -n "$YOUR_IPSEC_PSK" ] && VPN_IPSEC_PSK="$YOUR_IPSEC_PSK"
|
||||||
[ -n "$YOUR_USERNAME" ] && VPN_USER="$YOUR_USERNAME"
|
[ -n "$YOUR_USERNAME" ] && VPN_USER="$YOUR_USERNAME"
|
||||||
[ -n "$YOUR_PASSWORD" ] && VPN_PASSWORD="$YOUR_PASSWORD"
|
[ -n "$YOUR_PASSWORD" ] && VPN_PASSWORD="$YOUR_PASSWORD"
|
||||||
@ -116,42 +123,52 @@ case "$VPN_IPSEC_PSK $VPN_USER $VPN_PASSWORD" in
|
|||||||
exiterr "VPN credentials must not contain these special characters: \\ \" '"
|
exiterr "VPN credentials must not contain these special characters: \\ \" '"
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
|
check_dns() {
|
||||||
if { [ -n "$VPN_DNS_SRV1" ] && ! check_ip "$VPN_DNS_SRV1"; } \
|
if { [ -n "$VPN_DNS_SRV1" ] && ! check_ip "$VPN_DNS_SRV1"; } \
|
||||||
|| { [ -n "$VPN_DNS_SRV2" ] && ! check_ip "$VPN_DNS_SRV2"; } then
|
|| { [ -n "$VPN_DNS_SRV2" ] && ! check_ip "$VPN_DNS_SRV2"; } then
|
||||||
exiterr "The DNS server specified is invalid."
|
exiterr "The DNS server specified is invalid."
|
||||||
fi
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# shellcheck disable=SC2154,SC2039,SC3047
|
||||||
|
start_setup() {
|
||||||
bigecho "VPN setup in progress... Please be patient."
|
bigecho "VPN setup in progress... Please be patient."
|
||||||
|
trap 'dlo=$dl;dl=$LINENO' DEBUG 2>/dev/null
|
||||||
|
trap 'finish $? $((dlo+1))' EXIT
|
||||||
mkdir -p /opt/src
|
mkdir -p /opt/src
|
||||||
cd /opt/src || exit 1
|
cd /opt/src || exit 1
|
||||||
|
}
|
||||||
|
|
||||||
|
install_setup_pkgs() {
|
||||||
bigecho "Installing packages required for setup..."
|
bigecho "Installing packages required for setup..."
|
||||||
|
|
||||||
(
|
(
|
||||||
set -x
|
set -x
|
||||||
yum -y -q install wget bind-utils openssl tar \
|
yum -y -q install wget bind-utils openssl tar \
|
||||||
iptables iproute gawk grep sed net-tools >/dev/null
|
iptables iproute gawk grep sed net-tools >/dev/null
|
||||||
) || exiterr2
|
) || exiterr2
|
||||||
|
}
|
||||||
|
|
||||||
|
detect_ip() {
|
||||||
bigecho "Trying to auto discover IP of this server..."
|
bigecho "Trying to auto discover IP of this server..."
|
||||||
|
|
||||||
public_ip=${VPN_PUBLIC_IP:-''}
|
public_ip=${VPN_PUBLIC_IP:-''}
|
||||||
check_ip "$public_ip" || public_ip=$(dig @resolver1.opendns.com -t A -4 myip.opendns.com +short)
|
check_ip "$public_ip" || public_ip=$(dig @resolver1.opendns.com -t A -4 myip.opendns.com +short)
|
||||||
check_ip "$public_ip" || public_ip=$(wget -t 3 -T 15 -qO- http://ipv4.icanhazip.com)
|
check_ip "$public_ip" || public_ip=$(wget -t 3 -T 15 -qO- http://ipv4.icanhazip.com)
|
||||||
check_ip "$public_ip" || exiterr "Cannot detect this server's public IP. Define it as variable 'VPN_PUBLIC_IP' and re-run this script."
|
check_ip "$public_ip" || exiterr "Cannot detect this server's public IP. Define it as variable 'VPN_PUBLIC_IP' and re-run this script."
|
||||||
|
}
|
||||||
|
|
||||||
|
add_epel_repo() {
|
||||||
bigecho "Adding the EPEL repository..."
|
bigecho "Adding the EPEL repository..."
|
||||||
|
|
||||||
epel_url="https://dl.fedoraproject.org/pub/epel/epel-release-latest-$(rpm -E '%{rhel}').noarch.rpm"
|
epel_url="https://dl.fedoraproject.org/pub/epel/epel-release-latest-$(rpm -E '%{rhel}').noarch.rpm"
|
||||||
(
|
(
|
||||||
set -x
|
set -x
|
||||||
yum -y -q install epel-release >/dev/null 2>&1 || yum -y -q install "$epel_url" >/dev/null
|
yum -y -q install epel-release >/dev/null 2>&1 || yum -y -q install "$epel_url" >/dev/null
|
||||||
) || exiterr2
|
) || exiterr2
|
||||||
|
}
|
||||||
|
|
||||||
|
install_vpn_pkgs_1() {
|
||||||
bigecho "Installing packages required for the VPN..."
|
bigecho "Installing packages required for the VPN..."
|
||||||
|
|
||||||
erp="--enablerepo"
|
erp="--enablerepo"
|
||||||
rp1="$erp=epel"
|
rp1="$erp=epel"
|
||||||
rp2="$erp=*server-*optional*"
|
rp2="$erp=*server-*optional*"
|
||||||
@ -165,11 +182,16 @@ rp4="$erp=[Pp]ower[Tt]ools"
|
|||||||
libcap-ng-devel libselinux-devel curl-devel nss-tools \
|
libcap-ng-devel libselinux-devel curl-devel nss-tools \
|
||||||
flex bison gcc make util-linux ppp >/dev/null
|
flex bison gcc make util-linux ppp >/dev/null
|
||||||
) || exiterr2
|
) || exiterr2
|
||||||
|
}
|
||||||
|
|
||||||
|
install_vpn_pkgs_2() {
|
||||||
(
|
(
|
||||||
set -x
|
set -x
|
||||||
yum "$rp1" -y -q install xl2tpd >/dev/null 2>&1
|
yum "$rp1" -y -q install xl2tpd >/dev/null 2>&1
|
||||||
) || exiterr2
|
) || exiterr2
|
||||||
|
}
|
||||||
|
|
||||||
|
install_vpn_pkgs_3() {
|
||||||
use_nft=0
|
use_nft=0
|
||||||
p1=systemd-devel
|
p1=systemd-devel
|
||||||
p2=libevent-devel
|
p2=libevent-devel
|
||||||
@ -196,25 +218,28 @@ else
|
|||||||
yum -y -q install $p4 >/dev/null
|
yum -y -q install $p4 >/dev/null
|
||||||
) || exiterr2
|
) || exiterr2
|
||||||
fi
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
install_fail2ban() {
|
||||||
bigecho "Installing Fail2Ban to protect SSH..."
|
bigecho "Installing Fail2Ban to protect SSH..."
|
||||||
|
|
||||||
(
|
(
|
||||||
set -x
|
set -x
|
||||||
yum "$rp1" -y -q install fail2ban >/dev/null
|
yum "$rp1" -y -q install fail2ban >/dev/null
|
||||||
) || exiterr2
|
) || exiterr2
|
||||||
|
}
|
||||||
|
|
||||||
|
get_ikev2_script() {
|
||||||
bigecho "Downloading IKEv2 script..."
|
bigecho "Downloading IKEv2 script..."
|
||||||
|
|
||||||
ikev2_url="https://github.com/hwdsl2/setup-ipsec-vpn/raw/master/extras/ikev2setup.sh"
|
ikev2_url="https://github.com/hwdsl2/setup-ipsec-vpn/raw/master/extras/ikev2setup.sh"
|
||||||
(
|
(
|
||||||
set -x
|
set -x
|
||||||
wget -t 3 -T 30 -q -O ikev2.sh "$ikev2_url"
|
wget -t 3 -T 30 -q -O ikev2.sh "$ikev2_url"
|
||||||
) || /bin/rm -f ikev2.sh
|
) || /bin/rm -f ikev2.sh
|
||||||
[ -s ikev2.sh ] && chmod +x ikev2.sh && ln -s /opt/src/ikev2.sh /usr/bin 2>/dev/null
|
[ -s ikev2.sh ] && chmod +x ikev2.sh && ln -s /opt/src/ikev2.sh /usr/bin 2>/dev/null
|
||||||
|
}
|
||||||
|
|
||||||
|
get_libreswan() {
|
||||||
bigecho "Downloading Libreswan..."
|
bigecho "Downloading Libreswan..."
|
||||||
|
|
||||||
SWAN_VER=4.4
|
SWAN_VER=4.4
|
||||||
swan_file="libreswan-$SWAN_VER.tar.gz"
|
swan_file="libreswan-$SWAN_VER.tar.gz"
|
||||||
swan_url1="https://github.com/libreswan/libreswan/archive/v$SWAN_VER.tar.gz"
|
swan_url1="https://github.com/libreswan/libreswan/archive/v$SWAN_VER.tar.gz"
|
||||||
@ -225,9 +250,10 @@ swan_url2="https://download.libreswan.org/$swan_file"
|
|||||||
) || exit 1
|
) || exit 1
|
||||||
/bin/rm -rf "/opt/src/libreswan-$SWAN_VER"
|
/bin/rm -rf "/opt/src/libreswan-$SWAN_VER"
|
||||||
tar xzf "$swan_file" && /bin/rm -f "$swan_file"
|
tar xzf "$swan_file" && /bin/rm -f "$swan_file"
|
||||||
|
}
|
||||||
|
|
||||||
|
install_libreswan() {
|
||||||
bigecho "Compiling and installing Libreswan, please wait..."
|
bigecho "Compiling and installing Libreswan, please wait..."
|
||||||
|
|
||||||
cd "libreswan-$SWAN_VER" || exit 1
|
cd "libreswan-$SWAN_VER" || exit 1
|
||||||
cat > Makefile.inc.local <<'EOF'
|
cat > Makefile.inc.local <<'EOF'
|
||||||
WERROR_CFLAGS=-w -s
|
WERROR_CFLAGS=-w -s
|
||||||
@ -251,7 +277,9 @@ cd /opt/src || exit 1
|
|||||||
if ! /usr/local/sbin/ipsec --version 2>/dev/null | grep -qF "$SWAN_VER"; then
|
if ! /usr/local/sbin/ipsec --version 2>/dev/null | grep -qF "$SWAN_VER"; then
|
||||||
exiterr "Libreswan $SWAN_VER failed to build."
|
exiterr "Libreswan $SWAN_VER failed to build."
|
||||||
fi
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
create_vpn_config() {
|
||||||
bigecho "Creating VPN configuration..."
|
bigecho "Creating VPN configuration..."
|
||||||
|
|
||||||
L2TP_NET=${VPN_L2TP_NET:-'192.168.42.0/24'}
|
L2TP_NET=${VPN_L2TP_NET:-'192.168.42.0/24'}
|
||||||
@ -372,9 +400,34 @@ VPN_PASSWORD_ENC=$(openssl passwd -1 "$VPN_PASSWORD")
|
|||||||
cat > /etc/ipsec.d/passwd <<EOF
|
cat > /etc/ipsec.d/passwd <<EOF
|
||||||
$VPN_USER:$VPN_PASSWORD_ENC:xauth-psk
|
$VPN_USER:$VPN_PASSWORD_ENC:xauth-psk
|
||||||
EOF
|
EOF
|
||||||
|
}
|
||||||
|
|
||||||
|
create_f2b_config() {
|
||||||
|
F2B_FILE=/etc/fail2ban/jail.local
|
||||||
|
if [ ! -f "$F2B_FILE" ]; then
|
||||||
|
bigecho "Creating basic Fail2Ban rules..."
|
||||||
|
cat > "$F2B_FILE" <<'EOF'
|
||||||
|
[ssh-iptables]
|
||||||
|
enabled = true
|
||||||
|
filter = sshd
|
||||||
|
logpath = /var/log/secure
|
||||||
|
EOF
|
||||||
|
|
||||||
|
if [ "$use_nft" = "1" ]; then
|
||||||
|
cat >> "$F2B_FILE" <<'EOF'
|
||||||
|
port = ssh
|
||||||
|
banaction = nftables-multiport[blocktype=drop]
|
||||||
|
EOF
|
||||||
|
else
|
||||||
|
cat >> "$F2B_FILE" <<'EOF'
|
||||||
|
action = iptables[name=SSH, port=ssh, protocol=tcp]
|
||||||
|
EOF
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
update_sysctl() {
|
||||||
bigecho "Updating sysctl settings..."
|
bigecho "Updating sysctl settings..."
|
||||||
|
|
||||||
if ! grep -qs "hwdsl2 VPN script" /etc/sysctl.conf; then
|
if ! grep -qs "hwdsl2 VPN script" /etc/sysctl.conf; then
|
||||||
conf_bk "/etc/sysctl.conf"
|
conf_bk "/etc/sysctl.conf"
|
||||||
cat >> /etc/sysctl.conf <<EOF
|
cat >> /etc/sysctl.conf <<EOF
|
||||||
@ -399,31 +452,10 @@ net.ipv4.tcp_rmem = 10240 87380 12582912
|
|||||||
net.ipv4.tcp_wmem = 10240 87380 12582912
|
net.ipv4.tcp_wmem = 10240 87380 12582912
|
||||||
EOF
|
EOF
|
||||||
fi
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
F2B_FILE=/etc/fail2ban/jail.local
|
update_iptables() {
|
||||||
if [ ! -f "$F2B_FILE" ]; then
|
|
||||||
bigecho "Creating basic Fail2Ban rules..."
|
|
||||||
cat > "$F2B_FILE" <<'EOF'
|
|
||||||
[ssh-iptables]
|
|
||||||
enabled = true
|
|
||||||
filter = sshd
|
|
||||||
logpath = /var/log/secure
|
|
||||||
EOF
|
|
||||||
|
|
||||||
if [ "$use_nft" = "1" ]; then
|
|
||||||
cat >> "$F2B_FILE" <<'EOF'
|
|
||||||
port = ssh
|
|
||||||
banaction = nftables-multiport[blocktype=drop]
|
|
||||||
EOF
|
|
||||||
else
|
|
||||||
cat >> "$F2B_FILE" <<'EOF'
|
|
||||||
action = iptables[name=SSH, port=ssh, protocol=tcp]
|
|
||||||
EOF
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
||||||
bigecho "Updating IPTables rules..."
|
bigecho "Updating IPTables rules..."
|
||||||
|
|
||||||
IPT_FILE=/etc/sysconfig/iptables
|
IPT_FILE=/etc/sysconfig/iptables
|
||||||
[ "$use_nft" = "1" ] && IPT_FILE=/etc/sysconfig/nftables.conf
|
[ "$use_nft" = "1" ] && IPT_FILE=/etc/sysconfig/nftables.conf
|
||||||
ipt_flag=0
|
ipt_flag=0
|
||||||
@ -479,9 +511,10 @@ if [ "$ipt_flag" = "1" ]; then
|
|||||||
iptables-save >> "$IPT_FILE"
|
iptables-save >> "$IPT_FILE"
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
enable_on_boot() {
|
||||||
bigecho "Enabling services on boot..."
|
bigecho "Enabling services on boot..."
|
||||||
|
|
||||||
systemctl --now mask firewalld 2>/dev/null
|
systemctl --now mask firewalld 2>/dev/null
|
||||||
if [ "$use_nft" = "1" ]; then
|
if [ "$use_nft" = "1" ]; then
|
||||||
systemctl enable nftables fail2ban 2>/dev/null
|
systemctl enable nftables fail2ban 2>/dev/null
|
||||||
@ -504,18 +537,19 @@ service xl2tpd restart
|
|||||||
echo 1 > /proc/sys/net/ipv4/ip_forward)&
|
echo 1 > /proc/sys/net/ipv4/ip_forward)&
|
||||||
EOF
|
EOF
|
||||||
fi
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
start_services() {
|
||||||
bigecho "Starting services..."
|
bigecho "Starting services..."
|
||||||
|
|
||||||
restorecon /etc/ipsec.d/*db 2>/dev/null
|
|
||||||
restorecon /usr/local/sbin -Rv 2>/dev/null
|
|
||||||
restorecon /usr/local/libexec/ipsec -Rv 2>/dev/null
|
|
||||||
|
|
||||||
sysctl -e -q -p
|
sysctl -e -q -p
|
||||||
|
|
||||||
chmod +x /etc/rc.local
|
chmod +x /etc/rc.local
|
||||||
chmod 600 /etc/ipsec.secrets* /etc/ppp/chap-secrets* /etc/ipsec.d/passwd*
|
chmod 600 /etc/ipsec.secrets* /etc/ppp/chap-secrets* /etc/ipsec.d/passwd*
|
||||||
|
|
||||||
|
restorecon /etc/ipsec.d/*db 2>/dev/null
|
||||||
|
restorecon /usr/local/sbin -Rv 2>/dev/null
|
||||||
|
restorecon /usr/local/libexec/ipsec -Rv 2>/dev/null
|
||||||
|
|
||||||
if [ "$use_nft" = "1" ]; then
|
if [ "$use_nft" = "1" ]; then
|
||||||
nft -f "$IPT_FILE"
|
nft -f "$IPT_FILE"
|
||||||
else
|
else
|
||||||
@ -532,20 +566,9 @@ mkdir -p /run/pluto
|
|||||||
service fail2ban restart 2>/dev/null
|
service fail2ban restart 2>/dev/null
|
||||||
service ipsec restart 2>/dev/null
|
service ipsec restart 2>/dev/null
|
||||||
service xl2tpd restart 2>/dev/null
|
service xl2tpd restart 2>/dev/null
|
||||||
|
}
|
||||||
|
|
||||||
swan_ver_url="https://dl.ls20.com/v1/$os_type/$os_ver/swanver?arch=$os_arch&ver=$SWAN_VER"
|
show_vpn_info() {
|
||||||
swan_ver_latest=$(wget -t 3 -T 15 -qO- "$swan_ver_url")
|
|
||||||
if printf '%s' "$swan_ver_latest" | grep -Eq '^([3-9]|[1-9][0-9]{1,2})(\.([0-9]|[1-9][0-9]{1,2})){1,2}$' \
|
|
||||||
&& [ -n "$SWAN_VER" ] && [ "$SWAN_VER" != "$swan_ver_latest" ] \
|
|
||||||
&& printf '%s\n%s' "$SWAN_VER" "$swan_ver_latest" | sort -C -V; then
|
|
||||||
cat <<EOF
|
|
||||||
|
|
||||||
Note: A newer version of Libreswan ($swan_ver_latest) is available.
|
|
||||||
To update, run:
|
|
||||||
wget https://git.io/vpnupgrade -O vpnup.sh && sudo sh vpnup.sh
|
|
||||||
EOF
|
|
||||||
fi
|
|
||||||
|
|
||||||
cat <<EOF
|
cat <<EOF
|
||||||
|
|
||||||
================================================
|
================================================
|
||||||
@ -568,7 +591,54 @@ IKEv2 guide: https://git.io/ikev2
|
|||||||
================================================
|
================================================
|
||||||
|
|
||||||
EOF
|
EOF
|
||||||
|
}
|
||||||
|
|
||||||
|
check_swan_ver() {
|
||||||
|
swan_ver_url="https://dl.ls20.com/v1/$os_type/$os_ver/swanver?arch=$os_arch&ver=$SWAN_VER"
|
||||||
|
[ "$1" != "0" ] && swan_ver_url="$swan_ver_url&e=$2"
|
||||||
|
swan_ver_latest=$(wget -t 3 -T 15 -qO- "$swan_ver_url")
|
||||||
|
if printf '%s' "$swan_ver_latest" | grep -Eq '^([3-9]|[1-9][0-9]{1,2})(\.([0-9]|[1-9][0-9]{1,2})){1,2}$' \
|
||||||
|
&& [ "$1" = "0" ] && [ -n "$SWAN_VER" ] && [ "$SWAN_VER" != "$swan_ver_latest" ] \
|
||||||
|
&& printf '%s\n%s' "$SWAN_VER" "$swan_ver_latest" | sort -C -V; then
|
||||||
|
cat <<EOF
|
||||||
|
Note: A newer version of Libreswan ($swan_ver_latest) is available.
|
||||||
|
To update, run:
|
||||||
|
wget https://git.io/vpnupgrade -O vpnup.sh && sudo sh vpnup.sh
|
||||||
|
|
||||||
|
EOF
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
finish() {
|
||||||
|
check_swan_ver "$1" "$2"
|
||||||
|
exit "$1"
|
||||||
|
}
|
||||||
|
|
||||||
|
vpnsetup() {
|
||||||
|
check_root
|
||||||
|
check_vz
|
||||||
|
check_os
|
||||||
|
check_iface
|
||||||
|
check_creds
|
||||||
|
check_dns
|
||||||
|
start_setup
|
||||||
|
install_setup_pkgs
|
||||||
|
detect_ip
|
||||||
|
add_epel_repo
|
||||||
|
install_vpn_pkgs_1
|
||||||
|
install_vpn_pkgs_2
|
||||||
|
install_vpn_pkgs_3
|
||||||
|
install_fail2ban
|
||||||
|
get_ikev2_script
|
||||||
|
get_libreswan
|
||||||
|
install_libreswan
|
||||||
|
create_vpn_config
|
||||||
|
create_f2b_config
|
||||||
|
update_sysctl
|
||||||
|
update_iptables
|
||||||
|
enable_on_boot
|
||||||
|
start_services
|
||||||
|
show_vpn_info
|
||||||
}
|
}
|
||||||
|
|
||||||
## Defer setup until we have the complete script
|
## Defer setup until we have the complete script
|
||||||
|
@ -47,8 +47,19 @@ check_ip() {
|
|||||||
printf '%s' "$1" | tr -d '\n' | grep -Eq "$IP_REGEX"
|
printf '%s' "$1" | tr -d '\n' | grep -Eq "$IP_REGEX"
|
||||||
}
|
}
|
||||||
|
|
||||||
vpnsetup() {
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
check_os() {
|
||||||
os_type=$(lsb_release -si 2>/dev/null)
|
os_type=$(lsb_release -si 2>/dev/null)
|
||||||
os_arch=$(uname -m | tr -dc 'A-Za-z0-9_-')
|
os_arch=$(uname -m | tr -dc 'A-Za-z0-9_-')
|
||||||
[ -z "$os_type" ] && [ -f /etc/os-release ] && os_type=$(. /etc/os-release && printf '%s' "$ID")
|
[ -z "$os_type" ] && [ -f /etc/os-release ] && os_type=$(. /etc/os-release && printf '%s' "$ID")
|
||||||
@ -74,15 +85,9 @@ fi
|
|||||||
if { [ "$os_ver" = "10" ] || [ "$os_ver" = "11" ]; } && [ ! -e /dev/ppp ]; then
|
if { [ "$os_ver" = "10" ] || [ "$os_ver" = "11" ]; } && [ ! -e /dev/ppp ]; then
|
||||||
exiterr "/dev/ppp is missing. Debian 11 or 10 users, see: https://git.io/vpndebian10"
|
exiterr "/dev/ppp is missing. Debian 11 or 10 users, see: https://git.io/vpndebian10"
|
||||||
fi
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
if [ -f /proc/user_beancounters ]; then
|
check_iface() {
|
||||||
exiterr "OpenVZ VPS is not supported."
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ "$(id -u)" != 0 ]; then
|
|
||||||
exiterr "Script must be run as root. Try 'sudo sh $0'"
|
|
||||||
fi
|
|
||||||
|
|
||||||
def_iface=$(route 2>/dev/null | grep -m 1 '^default' | grep -o '[^ ]*$')
|
def_iface=$(route 2>/dev/null | grep -m 1 '^default' | grep -o '[^ ]*$')
|
||||||
[ -z "$def_iface" ] && def_iface=$(ip -4 route list 0/0 2>/dev/null | grep -m 1 -Po '(?<=dev )(\S+)')
|
[ -z "$def_iface" ] && def_iface=$(ip -4 route list 0/0 2>/dev/null | grep -m 1 -Po '(?<=dev )(\S+)')
|
||||||
def_state=$(cat "/sys/class/net/$def_iface/operstate" 2>/dev/null)
|
def_state=$(cat "/sys/class/net/$def_iface/operstate" 2>/dev/null)
|
||||||
@ -102,7 +107,9 @@ else
|
|||||||
fi
|
fi
|
||||||
NET_IFACE=eth0
|
NET_IFACE=eth0
|
||||||
fi
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
check_creds() {
|
||||||
[ -n "$YOUR_IPSEC_PSK" ] && VPN_IPSEC_PSK="$YOUR_IPSEC_PSK"
|
[ -n "$YOUR_IPSEC_PSK" ] && VPN_IPSEC_PSK="$YOUR_IPSEC_PSK"
|
||||||
[ -n "$YOUR_USERNAME" ] && VPN_USER="$YOUR_USERNAME"
|
[ -n "$YOUR_USERNAME" ] && VPN_USER="$YOUR_USERNAME"
|
||||||
[ -n "$YOUR_PASSWORD" ] && VPN_PASSWORD="$YOUR_PASSWORD"
|
[ -n "$YOUR_PASSWORD" ] && VPN_PASSWORD="$YOUR_PASSWORD"
|
||||||
@ -127,55 +134,71 @@ case "$VPN_IPSEC_PSK $VPN_USER $VPN_PASSWORD" in
|
|||||||
exiterr "VPN credentials must not contain these special characters: \\ \" '"
|
exiterr "VPN credentials must not contain these special characters: \\ \" '"
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
|
check_dns() {
|
||||||
if { [ -n "$VPN_DNS_SRV1" ] && ! check_ip "$VPN_DNS_SRV1"; } \
|
if { [ -n "$VPN_DNS_SRV1" ] && ! check_ip "$VPN_DNS_SRV1"; } \
|
||||||
|| { [ -n "$VPN_DNS_SRV2" ] && ! check_ip "$VPN_DNS_SRV2"; } then
|
|| { [ -n "$VPN_DNS_SRV2" ] && ! check_ip "$VPN_DNS_SRV2"; } then
|
||||||
exiterr "The DNS server specified is invalid."
|
exiterr "The DNS server specified is invalid."
|
||||||
fi
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
check_iptables() {
|
||||||
if [ -x /sbin/iptables ] && ! iptables -nL INPUT >/dev/null 2>&1; then
|
if [ -x /sbin/iptables ] && ! iptables -nL INPUT >/dev/null 2>&1; then
|
||||||
exiterr "IPTables check failed. Reboot and re-run this script."
|
exiterr "IPTables check failed. Reboot and re-run this script."
|
||||||
fi
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# shellcheck disable=SC2154,SC2039,SC3047
|
||||||
|
start_setup() {
|
||||||
bigecho "VPN setup in progress... Please be patient."
|
bigecho "VPN setup in progress... Please be patient."
|
||||||
|
trap 'dlo=$dl;dl=$LINENO' DEBUG 2>/dev/null
|
||||||
|
trap 'finish $? $((dlo+1))' EXIT
|
||||||
|
mkdir -p /opt/src
|
||||||
|
cd /opt/src || exit 1
|
||||||
|
}
|
||||||
|
|
||||||
|
wait_for_apt() {
|
||||||
count=0
|
count=0
|
||||||
apt_lk=/var/lib/apt/lists/lock
|
apt_lk=/var/lib/apt/lists/lock
|
||||||
pkg_lk=/var/lib/dpkg/lock
|
pkg_lk=/var/lib/dpkg/lock
|
||||||
while fuser "$apt_lk" "$pkg_lk" >/dev/null 2>&1 \
|
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
|
|| lsof "$apt_lk" >/dev/null 2>&1 || lsof "$pkg_lk" >/dev/null 2>&1; do
|
||||||
[ "$count" = "0" ] && bigecho "Waiting for apt to be available..."
|
[ "$count" = "0" ] && echo "## Waiting for apt to be available..."
|
||||||
[ "$count" -ge "100" ] && exiterr "Could not get apt/dpkg lock."
|
[ "$count" -ge "100" ] && exiterr "Could not get apt/dpkg lock."
|
||||||
count=$((count+1))
|
count=$((count+1))
|
||||||
printf '%s' '.'
|
printf '%s' '.'
|
||||||
sleep 3
|
sleep 3
|
||||||
done
|
done
|
||||||
|
}
|
||||||
|
|
||||||
mkdir -p /opt/src
|
install_setup_pkgs_1() {
|
||||||
cd /opt/src || exit 1
|
|
||||||
|
|
||||||
bigecho "Installing packages required for setup..."
|
bigecho "Installing packages required for setup..."
|
||||||
|
|
||||||
export DEBIAN_FRONTEND=noninteractive
|
export DEBIAN_FRONTEND=noninteractive
|
||||||
(
|
(
|
||||||
set -x
|
set -x
|
||||||
apt-get -yqq update
|
apt-get -yqq update
|
||||||
) || exiterr "'apt-get update' failed."
|
) || exiterr "'apt-get update' failed."
|
||||||
|
}
|
||||||
|
|
||||||
|
install_setup_pkgs_2() {
|
||||||
(
|
(
|
||||||
set -x
|
set -x
|
||||||
apt-get -yqq install wget dnsutils openssl \
|
apt-get -yqq install wget dnsutils openssl \
|
||||||
iptables iproute2 gawk grep sed net-tools >/dev/null
|
iptables iproute2 gawk grep sed net-tools >/dev/null
|
||||||
) || exiterr2
|
) || exiterr2
|
||||||
|
}
|
||||||
|
|
||||||
|
detect_ip() {
|
||||||
bigecho "Trying to auto discover IP of this server..."
|
bigecho "Trying to auto discover IP of this server..."
|
||||||
|
|
||||||
public_ip=${VPN_PUBLIC_IP:-''}
|
public_ip=${VPN_PUBLIC_IP:-''}
|
||||||
check_ip "$public_ip" || public_ip=$(dig @resolver1.opendns.com -t A -4 myip.opendns.com +short)
|
check_ip "$public_ip" || public_ip=$(dig @resolver1.opendns.com -t A -4 myip.opendns.com +short)
|
||||||
check_ip "$public_ip" || public_ip=$(wget -t 3 -T 15 -qO- http://ipv4.icanhazip.com)
|
check_ip "$public_ip" || public_ip=$(wget -t 3 -T 15 -qO- http://ipv4.icanhazip.com)
|
||||||
check_ip "$public_ip" || exiterr "Cannot detect this server's public IP. Define it as variable 'VPN_PUBLIC_IP' and re-run this script."
|
check_ip "$public_ip" || exiterr "Cannot detect this server's public IP. Define it as variable 'VPN_PUBLIC_IP' and re-run this script."
|
||||||
|
}
|
||||||
|
|
||||||
|
install_vpn_pkgs() {
|
||||||
bigecho "Installing packages required for the VPN..."
|
bigecho "Installing packages required for the VPN..."
|
||||||
|
|
||||||
(
|
(
|
||||||
set -x
|
set -x
|
||||||
apt-get -yqq install libnss3-dev libnspr4-dev pkg-config \
|
apt-get -yqq install libnss3-dev libnspr4-dev pkg-config \
|
||||||
@ -183,25 +206,28 @@ bigecho "Installing packages required for the VPN..."
|
|||||||
libcurl4-nss-dev flex bison gcc make libnss3-tools \
|
libcurl4-nss-dev flex bison gcc make libnss3-tools \
|
||||||
libevent-dev libsystemd-dev uuid-runtime ppp xl2tpd >/dev/null
|
libevent-dev libsystemd-dev uuid-runtime ppp xl2tpd >/dev/null
|
||||||
) || exiterr2
|
) || exiterr2
|
||||||
|
}
|
||||||
|
|
||||||
|
install_fail2ban() {
|
||||||
bigecho "Installing Fail2Ban to protect SSH..."
|
bigecho "Installing Fail2Ban to protect SSH..."
|
||||||
|
|
||||||
(
|
(
|
||||||
set -x
|
set -x
|
||||||
apt-get -yqq install fail2ban >/dev/null
|
apt-get -yqq install fail2ban >/dev/null
|
||||||
) || exiterr2
|
) || exiterr2
|
||||||
|
}
|
||||||
|
|
||||||
|
get_ikev2_script() {
|
||||||
bigecho "Downloading IKEv2 script..."
|
bigecho "Downloading IKEv2 script..."
|
||||||
|
|
||||||
ikev2_url="https://github.com/hwdsl2/setup-ipsec-vpn/raw/master/extras/ikev2setup.sh"
|
ikev2_url="https://github.com/hwdsl2/setup-ipsec-vpn/raw/master/extras/ikev2setup.sh"
|
||||||
(
|
(
|
||||||
set -x
|
set -x
|
||||||
wget -t 3 -T 30 -q -O ikev2.sh "$ikev2_url"
|
wget -t 3 -T 30 -q -O ikev2.sh "$ikev2_url"
|
||||||
) || /bin/rm -f ikev2.sh
|
) || /bin/rm -f ikev2.sh
|
||||||
[ -s ikev2.sh ] && chmod +x ikev2.sh && ln -s /opt/src/ikev2.sh /usr/bin 2>/dev/null
|
[ -s ikev2.sh ] && chmod +x ikev2.sh && ln -s /opt/src/ikev2.sh /usr/bin 2>/dev/null
|
||||||
|
}
|
||||||
|
|
||||||
|
get_libreswan() {
|
||||||
bigecho "Downloading Libreswan..."
|
bigecho "Downloading Libreswan..."
|
||||||
|
|
||||||
SWAN_VER=4.4
|
SWAN_VER=4.4
|
||||||
swan_file="libreswan-$SWAN_VER.tar.gz"
|
swan_file="libreswan-$SWAN_VER.tar.gz"
|
||||||
swan_url1="https://github.com/libreswan/libreswan/archive/v$SWAN_VER.tar.gz"
|
swan_url1="https://github.com/libreswan/libreswan/archive/v$SWAN_VER.tar.gz"
|
||||||
@ -212,9 +238,10 @@ swan_url2="https://download.libreswan.org/$swan_file"
|
|||||||
) || exit 1
|
) || exit 1
|
||||||
/bin/rm -rf "/opt/src/libreswan-$SWAN_VER"
|
/bin/rm -rf "/opt/src/libreswan-$SWAN_VER"
|
||||||
tar xzf "$swan_file" && /bin/rm -f "$swan_file"
|
tar xzf "$swan_file" && /bin/rm -f "$swan_file"
|
||||||
|
}
|
||||||
|
|
||||||
|
install_libreswan() {
|
||||||
bigecho "Compiling and installing Libreswan, please wait..."
|
bigecho "Compiling and installing Libreswan, please wait..."
|
||||||
|
|
||||||
cd "libreswan-$SWAN_VER" || exit 1
|
cd "libreswan-$SWAN_VER" || exit 1
|
||||||
cat > Makefile.inc.local <<'EOF'
|
cat > Makefile.inc.local <<'EOF'
|
||||||
WERROR_CFLAGS=-w -s
|
WERROR_CFLAGS=-w -s
|
||||||
@ -246,7 +273,9 @@ cd /opt/src || exit 1
|
|||||||
if ! /usr/local/sbin/ipsec --version 2>/dev/null | grep -qF "$SWAN_VER"; then
|
if ! /usr/local/sbin/ipsec --version 2>/dev/null | grep -qF "$SWAN_VER"; then
|
||||||
exiterr "Libreswan $SWAN_VER failed to build."
|
exiterr "Libreswan $SWAN_VER failed to build."
|
||||||
fi
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
create_vpn_config() {
|
||||||
bigecho "Creating VPN configuration..."
|
bigecho "Creating VPN configuration..."
|
||||||
|
|
||||||
L2TP_NET=${VPN_L2TP_NET:-'192.168.42.0/24'}
|
L2TP_NET=${VPN_L2TP_NET:-'192.168.42.0/24'}
|
||||||
@ -373,9 +402,10 @@ VPN_PASSWORD_ENC=$(openssl passwd -1 "$VPN_PASSWORD")
|
|||||||
cat > /etc/ipsec.d/passwd <<EOF
|
cat > /etc/ipsec.d/passwd <<EOF
|
||||||
$VPN_USER:$VPN_PASSWORD_ENC:xauth-psk
|
$VPN_USER:$VPN_PASSWORD_ENC:xauth-psk
|
||||||
EOF
|
EOF
|
||||||
|
}
|
||||||
|
|
||||||
|
update_sysctl() {
|
||||||
bigecho "Updating sysctl settings..."
|
bigecho "Updating sysctl settings..."
|
||||||
|
|
||||||
if ! grep -qs "hwdsl2 VPN script" /etc/sysctl.conf; then
|
if ! grep -qs "hwdsl2 VPN script" /etc/sysctl.conf; then
|
||||||
conf_bk "/etc/sysctl.conf"
|
conf_bk "/etc/sysctl.conf"
|
||||||
cat >> /etc/sysctl.conf <<EOF
|
cat >> /etc/sysctl.conf <<EOF
|
||||||
@ -400,9 +430,10 @@ net.ipv4.tcp_rmem = 10240 87380 12582912
|
|||||||
net.ipv4.tcp_wmem = 10240 87380 12582912
|
net.ipv4.tcp_wmem = 10240 87380 12582912
|
||||||
EOF
|
EOF
|
||||||
fi
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
update_iptables() {
|
||||||
bigecho "Updating IPTables rules..."
|
bigecho "Updating IPTables rules..."
|
||||||
|
|
||||||
IPT_FILE=/etc/iptables.rules
|
IPT_FILE=/etc/iptables.rules
|
||||||
IPT_FILE2=/etc/iptables/rules.v4
|
IPT_FILE2=/etc/iptables/rules.v4
|
||||||
ipt_flag=0
|
ipt_flag=0
|
||||||
@ -441,9 +472,10 @@ if [ "$ipt_flag" = "1" ]; then
|
|||||||
/bin/cp -f "$IPT_FILE" "$IPT_FILE2"
|
/bin/cp -f "$IPT_FILE" "$IPT_FILE2"
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
enable_on_boot() {
|
||||||
bigecho "Enabling services on boot..."
|
bigecho "Enabling services on boot..."
|
||||||
|
|
||||||
IPT_PST=/etc/init.d/iptables-persistent
|
IPT_PST=/etc/init.d/iptables-persistent
|
||||||
IPT_PST2=/usr/share/netfilter-persistent/plugins.d/15-ip4tables
|
IPT_PST2=/usr/share/netfilter-persistent/plugins.d/15-ip4tables
|
||||||
ipt_load=1
|
ipt_load=1
|
||||||
@ -506,9 +538,10 @@ echo 1 > /proc/sys/net/ipv4/ip_forward)&
|
|||||||
exit 0
|
exit 0
|
||||||
EOF
|
EOF
|
||||||
fi
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
start_services() {
|
||||||
bigecho "Starting services..."
|
bigecho "Starting services..."
|
||||||
|
|
||||||
sysctl -e -q -p
|
sysctl -e -q -p
|
||||||
|
|
||||||
chmod +x /etc/rc.local
|
chmod +x /etc/rc.local
|
||||||
@ -518,20 +551,9 @@ mkdir -p /run/pluto
|
|||||||
service fail2ban restart 2>/dev/null
|
service fail2ban restart 2>/dev/null
|
||||||
service ipsec restart 2>/dev/null
|
service ipsec restart 2>/dev/null
|
||||||
service xl2tpd restart 2>/dev/null
|
service xl2tpd restart 2>/dev/null
|
||||||
|
}
|
||||||
|
|
||||||
swan_ver_url="https://dl.ls20.com/v1/$os_type/$os_ver/swanver?arch=$os_arch&ver=$SWAN_VER"
|
show_vpn_info() {
|
||||||
swan_ver_latest=$(wget -t 3 -T 15 -qO- "$swan_ver_url")
|
|
||||||
if printf '%s' "$swan_ver_latest" | grep -Eq '^([3-9]|[1-9][0-9]{1,2})(\.([0-9]|[1-9][0-9]{1,2})){1,2}$' \
|
|
||||||
&& [ -n "$SWAN_VER" ] && [ "$SWAN_VER" != "$swan_ver_latest" ] \
|
|
||||||
&& printf '%s\n%s' "$SWAN_VER" "$swan_ver_latest" | sort -C -V; then
|
|
||||||
cat <<EOF
|
|
||||||
|
|
||||||
Note: A newer version of Libreswan ($swan_ver_latest) is available.
|
|
||||||
To update, run:
|
|
||||||
wget https://git.io/vpnupgrade -O vpnup.sh && sudo sh vpnup.sh
|
|
||||||
EOF
|
|
||||||
fi
|
|
||||||
|
|
||||||
cat <<EOF
|
cat <<EOF
|
||||||
|
|
||||||
================================================
|
================================================
|
||||||
@ -554,7 +576,53 @@ IKEv2 guide: https://git.io/ikev2
|
|||||||
================================================
|
================================================
|
||||||
|
|
||||||
EOF
|
EOF
|
||||||
|
}
|
||||||
|
|
||||||
|
check_swan_ver() {
|
||||||
|
swan_ver_url="https://dl.ls20.com/v1/$os_type/$os_ver/swanver?arch=$os_arch&ver=$SWAN_VER"
|
||||||
|
[ "$1" != "0" ] && swan_ver_url="$swan_ver_url&e=$2"
|
||||||
|
swan_ver_latest=$(wget -t 3 -T 15 -qO- "$swan_ver_url")
|
||||||
|
if printf '%s' "$swan_ver_latest" | grep -Eq '^([3-9]|[1-9][0-9]{1,2})(\.([0-9]|[1-9][0-9]{1,2})){1,2}$' \
|
||||||
|
&& [ "$1" = "0" ] && [ -n "$SWAN_VER" ] && [ "$SWAN_VER" != "$swan_ver_latest" ] \
|
||||||
|
&& printf '%s\n%s' "$SWAN_VER" "$swan_ver_latest" | sort -C -V; then
|
||||||
|
cat <<EOF
|
||||||
|
Note: A newer version of Libreswan ($swan_ver_latest) is available.
|
||||||
|
To update, run:
|
||||||
|
wget https://git.io/vpnupgrade -O vpnup.sh && sudo sh vpnup.sh
|
||||||
|
|
||||||
|
EOF
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
finish() {
|
||||||
|
check_swan_ver "$1" "$2"
|
||||||
|
exit "$1"
|
||||||
|
}
|
||||||
|
|
||||||
|
vpnsetup() {
|
||||||
|
check_root
|
||||||
|
check_vz
|
||||||
|
check_os
|
||||||
|
check_iface
|
||||||
|
check_creds
|
||||||
|
check_dns
|
||||||
|
check_iptables
|
||||||
|
start_setup
|
||||||
|
wait_for_apt
|
||||||
|
install_setup_pkgs_1
|
||||||
|
install_setup_pkgs_2
|
||||||
|
detect_ip
|
||||||
|
install_vpn_pkgs
|
||||||
|
install_fail2ban
|
||||||
|
get_ikev2_script
|
||||||
|
get_libreswan
|
||||||
|
install_libreswan
|
||||||
|
create_vpn_config
|
||||||
|
update_sysctl
|
||||||
|
update_iptables
|
||||||
|
enable_on_boot
|
||||||
|
start_services
|
||||||
|
show_vpn_info
|
||||||
}
|
}
|
||||||
|
|
||||||
## Defer setup until we have the complete script
|
## Defer setup until we have the complete script
|
||||||
|
Loading…
Reference in New Issue
Block a user