From a424e43d6fc6e21677ce1b6df8b7632e040b4d18 Mon Sep 17 00:00:00 2001 From: bedefaced Date: Wed, 19 Apr 2017 21:14:15 +0300 Subject: [PATCH] ipsec cron jobs adding --- ipsec/autostart.sh | 26 ++++++++++++++++++++++++++ ipsec/checkserver.sh | 13 +++++++++++++ ipsec/env.sh | 1 + ipsec/install.sh | 7 +++++-- ipsec/iptables-setup.sh | 8 -------- 5 files changed, 45 insertions(+), 10 deletions(-) create mode 100644 ipsec/autostart.sh create mode 100644 ipsec/checkserver.sh diff --git a/ipsec/autostart.sh b/ipsec/autostart.sh new file mode 100644 index 0000000..3a4353d --- /dev/null +++ b/ipsec/autostart.sh @@ -0,0 +1,26 @@ +#!/usr/bin/env bash + +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +source $DIR/env.sh + +if [[ "$EUID" -ne 0 ]]; then + echo "Sorry, you need to run this as root" + exit 1 +fi + +TMPFILE=$(mktemp crontab.XXXXX) +crontab -l > $TMPFILE + +RESTOREPATH=$(which iptables-restore) +RESTORPRESENTS=$(grep iptables-restore $TMPFILE) +if [ $? -ne 0 ]; then + echo "@reboot $RESTOREPATH <$IPTABLES >/dev/null 2>&1" >> $TMPFILE +fi + +SERVERSPRESENTS=$(grep "$CHECKSERVER" $TMPFILE) +if [ $? -ne 0 ]; then + echo "*/5 * * * * $CHECKSERVER >/dev/null 2>&1" >> $TMPFILE +fi + +crontab $TMPFILE > /dev/null +rm $TMPFILE diff --git a/ipsec/checkserver.sh b/ipsec/checkserver.sh new file mode 100644 index 0000000..9f4010c --- /dev/null +++ b/ipsec/checkserver.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash + +RET=$(pgrep xl2tpd) + +if [ $? -eq 1 ]; then + /etc/init.d/xl2tpd restart +fi + +RET=$(pgrep strongswan) + +if [ $? -eq 1 ]; then + /etc/init.d/strongswan restart +fi diff --git a/ipsec/env.sh b/ipsec/env.sh index df153ef..8784278 100755 --- a/ipsec/env.sh +++ b/ipsec/env.sh @@ -8,6 +8,7 @@ CHAPSECRETS=/etc/ppp/chap-secrets IPTABLES=/etc/iptables.rules RCLOCAL=/etc/rc.local SECRETSFILE=/etc/ipsec.secrets +CHECKSERVER=/etc/xl2tpd/checkserver.sh LOCALPREFIX="172.18" LOCALIP="$LOCALPREFIX.0.0" diff --git a/ipsec/install.sh b/ipsec/install.sh index fa41ed9..104fa62 100755 --- a/ipsec/install.sh +++ b/ipsec/install.sh @@ -52,10 +52,13 @@ echo echo "Configuring DNS parameters..." $DIR/dns.sh +echo +echo "Adding cron jobs..." +yes | cp -rf $DIR/checkserver.sh $CHECKSERVER +$DIR/autostart.sh + echo echo "Starting strongSwan and xl2tp..." -systemctl enable xl2tpd -systemctl enable strongswan service xl2tpd restart service strongswan restart diff --git a/ipsec/iptables-setup.sh b/ipsec/iptables-setup.sh index 0665878..76a700a 100755 --- a/ipsec/iptables-setup.sh +++ b/ipsec/iptables-setup.sh @@ -73,13 +73,5 @@ iptables -A OUTPUT -o ppp+ -j ACCEPT iptables -A INPUT -p tcp --dport 1701 -j ACCEPT iptables-save | awk '($0 !~ /^-A/)||!($0 in a) {a[$0];print}' > $IPTABLES - -RESTORPRESENTS=$(grep iptables-restore $RCLOCAL) -if [ $? -ne 0 ]; then - sed -i -e "/exit 0/d" $RCLOCAL - echo "iptables-restore < $IPTABLES" >> $RCLOCAL - echo "exit 0" >> $RCLOCAL -fi - iptables -F iptables-restore < $IPTABLES