|
|
@@ -1,26 +1,49 @@
|
|
|
#!/bin/sh
|
|
|
|
|
|
-LOG_FILE=/var/log/vpn_check.log
|
|
|
-now=$(date)
|
|
|
+# Configuration
|
|
|
+LOG_FILE="/var/log/vpn_check.log"
|
|
|
+VPN_SERVICE="vpnc@fb"
|
|
|
+ROUTER_IP="192.168.178.1"
|
|
|
+VPN_CHECK_URL="https://tbsgl.xyz/vpn"
|
|
|
+RESTART_CHECK_URL="https://tbsgl.xyz/restart"
|
|
|
|
|
|
+# Log function
|
|
|
+log() {
|
|
|
+ now=$(date)
|
|
|
+ echo "${now} - $1" | tee -a ${LOG_FILE}
|
|
|
+}
|
|
|
|
|
|
-echo "${now} - Running Check" | tee -a ${LOG_FILE}
|
|
|
+# Check URL function
|
|
|
+check_url() {
|
|
|
+ curl -sI $1 | grep "HTTP/2 200" > /dev/null
|
|
|
+ return $?
|
|
|
+}
|
|
|
+
|
|
|
+# Check VPN Connectivity
|
|
|
+check_vpn_connectivity() {
|
|
|
+ ping -c 1 ${ROUTER_IP} | grep "64 bytes from" > /dev/null
|
|
|
+ if [ $? != 0 ]; then
|
|
|
+ systemctl restart ${VPN_SERVICE} > /dev/null
|
|
|
+ log "Ping not successful - Restarting VPN"
|
|
|
+ else
|
|
|
+ log "Ping reached router, doing nothing"
|
|
|
+ fi
|
|
|
+}
|
|
|
+
|
|
|
+# Main
|
|
|
+log "Running Check"
|
|
|
|
|
|
-curl -sI https://tbsgl.xyz/vpn | grep "HTTP/2 200" | grep -v grep > /dev/null
|
|
|
# Check if VPN-On Flag is set
|
|
|
-if [ $? = 0 ]
|
|
|
- then
|
|
|
- ping -c 1 192.168.178.1 | grep "64 bytes from" | grep -v grep > /dev/null
|
|
|
- # Check if VPN is already connected
|
|
|
- if [ $? != 0 ]
|
|
|
- then
|
|
|
- # If not, then Restart VPN
|
|
|
- systemctl restart vpnc@fb > /dev/null
|
|
|
- echo "Ping not successful - Restarting VPN\n" | tee -a ${LOG_FILE}
|
|
|
- else
|
|
|
- echo "Ping reached router, doing nothing\n" | tee -a ${LOG_FILE}
|
|
|
- fi
|
|
|
+if check_url ${VPN_CHECK_URL}; then
|
|
|
+ check_vpn_connectivity
|
|
|
else
|
|
|
- systemctl stop vpnc@fb
|
|
|
- echo "VPN Flag seems to be disabled or no Internet, stopping service\n" | tee -a ${LOG_FILE}
|
|
|
+ systemctl stop ${VPN_SERVICE}
|
|
|
+ log "VPN Flag seems to be disabled or no Internet, stopping service"
|
|
|
fi
|
|
|
+
|
|
|
+# Additional Functionality: Check for Restart Condition
|
|
|
+if check_url ${RESTART_CHECK_URL}; then
|
|
|
+ log "Restart condition met - Restarting device"
|
|
|
+ reboot now
|
|
|
+fi
|
|
|
+
|