diff --git a/openvpn-install.sh b/openvpn-install.sh index 0e26ba8..ed7d55e 100644 --- a/openvpn-install.sh +++ b/openvpn-install.sh @@ -19,6 +19,7 @@ if grep -qs "CentOS release 5" "/etc/redhat-release"; then exit 3 fi +## Global variables dir_openvpn='/etc/openvpn' dir_easy="${dir_openvpn}/easy-rsa" dir_pki="${dir_easy}/pki" @@ -28,7 +29,10 @@ bin_easy="${dir_easy}/easyrsa" file_client_tpl="${dir_openvpn}/client-template.txt" file_openvpn_conf="${dir_openvpn}/server.conf" file_iptables='/etc/iptables/iptables.rules' - + +## function determine the operating system version +detect_os_ver(){ + if [[ -e /etc/debian_version ]]; then OS="debian" # Getting the version number, to verify that a recent version of OpenVPN is available @@ -62,6 +66,8 @@ else echo "Looks like you aren't running this installer on a Debian, Ubuntu, CentOS or ArchLinux system" exit 4 fi +} + install_easyrsa(){ # An old version of easy-rsa was available by default in some openvpn packages @@ -155,130 +161,9 @@ newclient () { echo "" >> ${file_client} } -# Try to get our IP from the system and fallback to the Internet. -# I do this to make the script compatible with NATed servers (LowEndSpirit/Scaleway) -# and to avoid getting an IPv6. -IP=$(ip addr | grep 'inet' | grep -v inet6 | grep -vE '127\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | grep -o -E '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | head -1) -if [[ "$IP" = "" ]]; then - IP=$(wget -qO- ipv4.icanhazip.com) -fi -# Get Internet network interface with default route -NIC=$(ip -4 route ls | grep default | grep -Po '(?<=dev )(\S+)' | head -1) -#### server.conf exist. -if [[ -e ${file_openvpn_conf} ]]; then - while : - do - clear -cat < $file_iptables - fi - iptables -t nat -D POSTROUTING -o $NIC -s 10.8.0.0/24 -j MASQUERADE - iptables-save > $file_iptables - if hash sestatus 2>/dev/null; then - if sestatus | grep "Current mode" | grep -qs "enforcing"; then - if [[ "$PORT" != '1194' ]]; then - semanage port -d -t openvpn_port_t -p ${PROTOCOL} $PORT - fi - fi - fi - if [[ "$OS" = 'debian' ]]; then - apt-get autoremove --purge -y openvpn - elif [[ "$OS" = 'arch' ]]; then - pacman -R openvpn --noconfirm - else - yum remove openvpn -y - fi - rm -rf ${dir_openvpn} /usr/share/doc/openvpn* - echo "" - echo "OpenVPN removed!" - else - echo "" - echo "Removal aborted!" - fi - exit - ;; - 4) exit;; - esac - done -fi - - - -## OpenVPN setup and first user creation -if [[ ! -e ${file_openvpn_conf} ]]; then +## function for install openvpn server +install_openvpn(){ clear cat < /etc/iptables/iptables.rules + iptables-save > ${file_iptables} echo "#!/bin/sh iptables -F iptables -X @@ -457,8 +344,8 @@ Before=network-pre.target Wants=network-pre.target [Service] Type=oneshot -ExecStart=/sbin/iptables-restore /etc/iptables/iptables.rules -ExecReload=/sbin/iptables-restore /etc/iptables/iptables.rules +ExecStart=/sbin/iptables-restore ${file_iptables} +ExecReload=/sbin/iptables-restore ${file_iptables} ExecStop=/etc/iptables/flush-iptables.sh RemainAfterExit=yes [Install] @@ -474,7 +361,7 @@ WantedBy=multi-user.target" > /etc/systemd/system/iptables.service # Install iptables service if [[ ! -e /etc/systemd/system/iptables.service ]]; then mkdir /etc/iptables - iptables-save > /etc/iptables/iptables.rules + iptables-save > ${file_iptables} echo "#!/bin/sh iptables -F iptables -X @@ -493,8 +380,8 @@ Before=network-pre.target Wants=network-pre.target [Service] Type=oneshot -ExecStart=/sbin/iptables-restore /etc/iptables/iptables.rules -ExecReload=/sbin/iptables-restore /etc/iptables/iptables.rules +ExecStart=/sbin/iptables-restore ${file_iptables} +ExecReload=/sbin/iptables-restore ${file_iptables} ExecStop=/etc/iptables/flush-iptables.sh RemainAfterExit=yes [Install] @@ -524,7 +411,7 @@ WantedBy=multi-user.target" > /etc/systemd/system/iptables.service if [[ "$OS" = 'arch' ]]; then # Install dependencies pacman -Syu openvpn iptables openssl wget ca-certificates curl --needed --noconfirm - iptables-save > /etc/iptables/iptables.rules # iptables won't start if this file does not exist + iptables-save > ${file_iptables} # iptables won't start if this file does not exist systemctl daemon-reload systemctl enable iptables systemctl start iptables @@ -698,5 +585,149 @@ verb 3" >> ${file_client_tpl} echo "" echo "Your client config is available at $homeDir/$CLIENT.ovpn" echo "If you want to add more clients, you simply need to run this script another time!" +} + +detect_IP_NIC(){ +# Try to get our IP from the system and fallback to the Internet. +# I do this to make the script compatible with NATed servers (LowEndSpirit/Scaleway) +# and to avoid getting an IPv6. +IP=$(ip addr | grep 'inet' | grep -v inet6 | grep -vE '127\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | grep -o -E '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | head -1) +if [[ "$IP" = "" ]]; then + IP=$(wget -qO- ipv4.icanhazip.com) fi +# Get Internet network interface with default route +NIC=$(ip -4 route ls | grep default | grep -Po '(?<=dev )(\S+)' | head -1) + + +} + +config_openvpn(){ + + + while : + do + clear +cat < $file_iptables + fi + iptables -t nat -D POSTROUTING -o $NIC -s 10.8.0.0/24 -j MASQUERADE + iptables-save > $file_iptables + if hash sestatus 2>/dev/null; then + if sestatus | grep "Current mode" | grep -qs "enforcing"; then + if [[ "$PORT" != '1194' ]]; then + semanage port -d -t openvpn_port_t -p ${PROTOCOL} $PORT + fi + fi + fi + if [[ "$OS" = 'debian' ]]; then + apt-get autoremove --purge -y openvpn + elif [[ "$OS" = 'arch' ]]; then + pacman -R openvpn --noconfirm + else + yum remove openvpn -y + fi + rm -rf ${dir_openvpn} /usr/share/doc/openvpn* + echo "" + echo "OpenVPN removed!" + else + echo "" + echo "Removal aborted!" + fi + exit + ;; + 4) exit;; + esac + done + +} + + +################################# main ################################# +detect_os_ver +detect_IP_NIC + +## OpenVPN setup and first user creation +if [[ ! -e ${file_openvpn_conf} ]]; then + install_openvpn +fi + +#### server.conf exist. +if [[ -e ${file_openvpn_conf} ]]; then + config_openvpn +fi + + + exit 0;