diff --git a/vpnsetup.sh b/vpnsetup.sh index 8ede5cb..786ffd7 100644 --- a/vpnsetup.sh +++ b/vpnsetup.sh @@ -24,17 +24,16 @@ if [ "$(uname)" = "Darwin" ]; then fi # Please define your own values for these variables -# IMPORTANT: -# Escape all non-alphanumeric characters with a backslash (or 3 backslashes for \ and "). +# Escape *all* non-alphanumeric characters with a backslash (or 3 backslashes for \ and "). # Examples: \ --> \\\\, " --> \\\", ' --> \', $ --> \$, ` --> \`, [space] --> \[space] IPSEC_PSK=your_very_secure_key VPN_USER=your_username VPN_PASSWORD=your_very_secure_password -# -------------------- -# | IMPORTANT NOTES | -# -------------------- +# ----------------- +# IMPORTANT NOTES +# ----------------- # To support multiple VPN users with different credentials, just edit a few lines below. # See: https://gist.github.com/hwdsl2/123b886f29f4c689f531 @@ -44,8 +43,8 @@ VPN_PASSWORD=your_very_secure_password # https://documentation.meraki.com/MX-Z/Client_VPN/Troubleshooting_Client_VPN#Windows_Error_809 # **Android 6.0 users**: Edit /etc/ipsec.conf and append ",aes256-sha2_256" to the end of -# both "ike=" and "phase2alg=", then add a new line "sha2-truncbug=yes". Must start lines with -# two spaces. Finally, run "service ipsec restart". +# both "ike=" and "phase2alg=", then add a new line "sha2-truncbug=yes". Must start lines +# with two spaces. Finally, run "service ipsec restart". # **iPhone/iOS users**: In iOS settings, choose L2TP (instead of IPSec) for the VPN type. # In case you're unable to connect, try replacing this line in /etc/ipsec.conf: @@ -60,8 +59,8 @@ VPN_PASSWORD=your_very_secure_password # If your server uses a custom SSH port (not 22), or if you wish to allow other services # through IPTables, be sure to edit the IPTables rules below before running this script. -# This script will backup /etc/rc.local, /etc/sysctl.conf and /etc/iptables.rules -# before overwriting them. Backups can be found under the same folder with .old suffix. +# This script will backup your existing configuration files before overwriting them. +# Backups can be found in the same folder as the original, with .old-date/time suffix. if [ "$(lsb_release -si)" != "Ubuntu" ] && [ "$(lsb_release -si)" != "Debian" ]; then echo "Looks like you aren't running this script on a Ubuntu or Debian system." @@ -88,10 +87,10 @@ fi mkdir -p /opt/src cd /opt/src || { echo "Failed to change working directory to /opt/src. Aborting."; exit 1; } -# Update package index and install wget, dig (dnsutils) and nano +# Update package index and install wget, dig (dnsutils), sed and nano export DEBIAN_FRONTEND=noninteractive apt-get -y update -apt-get -y install wget dnsutils nano +apt-get -y install wget dnsutils sed nano echo echo 'Please wait... Trying to find Public IP and Private IP of this server.' @@ -150,6 +149,7 @@ cd "libreswan-${SWAN_VER}" || { echo "Failed to enter Libreswan source directory make programs && make install # Prepare various config files +/bin/cp -f /etc/ipsec.conf "/etc/ipsec.conf.old-$(date +%Y-%m-%d-%H:%M:%S)" 2>/dev/null cat > /etc/ipsec.conf </dev/null cat > /etc/ipsec.secrets </dev/null cat > /etc/xl2tpd/xl2tpd.conf </dev/null cat > /etc/ppp/options.xl2tpd </dev/null + cat > /etc/ppp/chap-secrets </dev/null -cat > /etc/sysctl.conf <> /etc/sysctl.conf </dev/null +/usr/sbin/service fail2ban stop >/dev/null 2>&1 +if [ "$(/sbin/iptables-save | grep -c '^\-')" = "0" ]; then + cat > /etc/iptables.rules < /etc/iptables.rules +echo "# Modified by hwdsl2 VPN script" >> /etc/iptables.rules + +fi +fi + cat > /etc/network/if-pre-up.d/iptablesload </dev/null -cat > /etc/rc.local </dev/null +/bin/sed --follow-symlinks -i -e '/^exit 0/d' /etc/rc.local +cat >> /etc/rc.local < /proc/sys/net/ipv4/ip_forward exit 0 EOF +fi + if [ ! -f /etc/ipsec.d/cert8.db ] ; then echo > /var/tmp/libreswan-nss-pwd /usr/bin/certutil -N -f /var/tmp/libreswan-nss-pwd -d /etc/ipsec.d @@ -346,7 +375,7 @@ fi /sbin/sysctl -p /bin/chmod +x /etc/rc.local /bin/chmod +x /etc/network/if-pre-up.d/iptablesload -/bin/chmod 600 /etc/ipsec.secrets /etc/ppp/chap-secrets +/bin/chmod 600 /etc/ipsec.secrets* /etc/ppp/chap-secrets* /sbin/iptables-restore < /etc/iptables.rules /usr/sbin/service fail2ban stop >/dev/null 2>&1 diff --git a/vpnsetup_centos.sh b/vpnsetup_centos.sh index 83a4930..322fc6f 100644 --- a/vpnsetup_centos.sh +++ b/vpnsetup_centos.sh @@ -23,17 +23,16 @@ if [ "$(uname)" = "Darwin" ]; then fi # Please define your own values for these variables -# IMPORTANT: -# Escape all non-alphanumeric characters with a backslash (or 3 backslashes for \ and "). +# Escape *all* non-alphanumeric characters with a backslash (or 3 backslashes for \ and "). # Examples: \ --> \\\\, " --> \\\", ' --> \', $ --> \$, ` --> \`, [space] --> \[space] IPSEC_PSK=your_very_secure_key VPN_USER=your_username VPN_PASSWORD=your_very_secure_password -# -------------------- -# | IMPORTANT NOTES | -# -------------------- +# ----------------- +# IMPORTANT NOTES +# ----------------- # To support multiple VPN users with different credentials, just edit a few lines below. # See: https://gist.github.com/hwdsl2/123b886f29f4c689f531 @@ -43,8 +42,8 @@ VPN_PASSWORD=your_very_secure_password # https://documentation.meraki.com/MX-Z/Client_VPN/Troubleshooting_Client_VPN#Windows_Error_809 # **Android 6.0 users**: Edit /etc/ipsec.conf and append ",aes256-sha2_256" to the end of -# both "ike=" and "phase2alg=", then add a new line "sha2-truncbug=yes". Must start lines with -# two spaces. Finally, run "service ipsec restart". +# both "ike=" and "phase2alg=", then add a new line "sha2-truncbug=yes". Must start lines +# with two spaces. Finally, run "service ipsec restart". # **iPhone/iOS users**: In iOS settings, choose L2TP (instead of IPSec) for the VPN type. # In case you're unable to connect, try replacing this line in /etc/ipsec.conf: @@ -59,8 +58,8 @@ VPN_PASSWORD=your_very_secure_password # If your server uses a custom SSH port (not 22), or if you wish to allow other services # through IPTables, be sure to edit the IPTables rules below before running this script. -# This script will backup /etc/rc.local, /etc/sysctl.conf and /etc/sysconfig/iptables -# before overwriting them. Backups can be found under the same folder with .old suffix. +# This script will backup your existing configuration files before overwriting them. +# Backups can be found in the same folder as the original, with .old-date/time suffix. if [ ! -f /etc/redhat-release ]; then echo "Looks like you aren't running this script on a CentOS/RHEL system." @@ -159,7 +158,7 @@ yum -y install ppp xl2tpd # Install Fail2Ban to protect SSH server yum -y install fail2ban -# Installed Libevent 2. Use backported version for CentOS 6. +# Installed Libevent2. Use backported version for CentOS 6. if grep -qs "release 6" /etc/redhat-release; then LE2_URL="https://people.redhat.com/pwouters/libreswan-rhel6" RPM1="libevent2-2.0.21-1.el6.x86_64.rpm" @@ -184,6 +183,7 @@ cd "libreswan-${SWAN_VER}" || { echo "Failed to enter Libreswan source directory make programs && make install # Prepare various config files +/bin/cp -f /etc/ipsec.conf "/etc/ipsec.conf.old-$(date +%Y-%m-%d-%H:%M:%S)" 2>/dev/null cat > /etc/ipsec.conf </dev/null cat > /etc/ipsec.secrets </dev/null cat > /etc/xl2tpd/xl2tpd.conf </dev/null cat > /etc/ppp/options.xl2tpd </dev/null + cat > /etc/ppp/chap-secrets </dev/null -cat > /etc/sysctl.conf <> /etc/sysctl.conf </dev/null +/sbin/service fail2ban stop >/dev/null 2>&1 +if [ "$(/sbin/iptables-save | grep -c '^\-')" = "0" ]; then + cat > /etc/sysconfig/iptables < /etc/sysconfig/iptables +echo "# Modified by hwdsl2 VPN script" >> /etc/sysconfig/iptables + +fi +fi + if [ ! -f /etc/fail2ban/jail.local ] ; then cat > /etc/fail2ban/jail.local </dev/null -cat > /etc/rc.local </dev/null +cat >> /etc/rc.local < /proc/sys/net/ipv4/ip_forward EOF +fi + if [ ! -f /etc/ipsec.d/cert8.db ] ; then echo > /var/tmp/libreswan-nss-pwd /usr/bin/certutil -N -f /var/tmp/libreswan-nss-pwd -d /etc/ipsec.d @@ -390,7 +424,7 @@ restorecon /usr/local/libexec/ipsec -Rv 2>/dev/null /sbin/sysctl -p /bin/chmod +x /etc/rc.local -/bin/chmod 600 /etc/ipsec.secrets /etc/ppp/chap-secrets +/bin/chmod 600 /etc/ipsec.secrets* /etc/ppp/chap-secrets* /sbin/iptables-restore < /etc/sysconfig/iptables /sbin/service fail2ban stop >/dev/null 2>&1