X-Git-Url: https://git.archive.openwrt.org/?p=project%2Fluci.git;a=blobdiff_plain;f=contrib%2Fpackage%2Ffreifunk-gwcheck%2Ffiles%2Fusr%2Fsbin%2Fff_olsr_test_gw.sh;h=a85ead3ce6aa0daae14fc577cfbb8102aaa13105;hp=a622a4fc4437aa0d0a141b50bf4696bcfdfae2fb;hb=c9525b9b2261a9e9b5c15525b1f3b485ec72a943;hpb=256daa259bcfb7f1b38bf9c6b2959a0aaa239b1c diff --git a/contrib/package/freifunk-gwcheck/files/usr/sbin/ff_olsr_test_gw.sh b/contrib/package/freifunk-gwcheck/files/usr/sbin/ff_olsr_test_gw.sh index a622a4fc4..a85ead3ce 100755 --- a/contrib/package/freifunk-gwcheck/files/usr/sbin/ff_olsr_test_gw.sh +++ b/contrib/package/freifunk-gwcheck/files/usr/sbin/ff_olsr_test_gw.sh @@ -1,19 +1,28 @@ #!/bin/sh +# Copyright 2013 Manuel Munz +# Licensed under the GNU General Public License (GPL) v3 +# This script monitors the local internet gateway -#check if dyngw_plain is installed and enabled, else exit -dyngwplainlib=`uci show olsrd |grep dyn_gw_plain |awk {' FS="."; print $1"."$2 '}` -if [ -n "$dyngwplainlib" ]; then - if [ "$(uci -q get $dyngwplainlib.ignore)" == 1 ]; then +. /lib/functions/network.sh + +# exit if dyngw_plain is not installed or enabled +dgwlib=`uci show olsrd |grep dyn_gw_plain |awk {' FS="."; print $1"."$2 '}` +if [ -n "$dgwlib" ]; then + if [ "$(uci -q get $dgwlib.ignore)" == 1 ]; then exit 1 fi else - echo "dyngw_plain not found in olsrd config, exit" exit 1 fi +#Exit if this script is already running +pid="$(pidof ff_olsr_test_gw.sh)" +if [ ${#pid} -gt 5 ]; then + logger -p debug -t gwcheck "Gateway check script is already running, exit now" + exit 1 +fi -# check if we have a defaultroute with metric=0 in one of these tables: main table and gw-check table. -# If not exit here. +# exit if there is no defaultroute with metric=0 in main or gw-check table. defroutemain="$(ip r s |grep default |grep -v metric)" defroutegwcheck="$(ip r s t gw-check |grep default |grep -v metric)" if [ -z "$defroutegwcheck" -a -z "$defroutemain" ]; then @@ -47,36 +56,65 @@ check_internet() { echo 0 break else - logger -t gw-check "Could not get test file from http://$t/conntest.html" + logger -p debug -t gw-check "Could not fetch http://$t/conntest.html" fi done } +resolve() { + echo "$(nslookup $1 2>/dev/null |grep 'Address' |grep -v '127.0.0.1' |awk '{ print $3 }')" +} + +get_dnsservers() { + # this gets all dns servers for the interface which has the default route + + dns="" + if [ ! -x /bin/ubus ]; then + # ubus not present (versions before Attitude): fallback to get these from /var/state/network. + # We always assume that wan is the default route interface here + dns="$(grep network.wan.resolv_dns /var/state/network | cut -d "=" -f 2)" + else + network_find_wan wan + network_get_dnsserver dns $wan + fi +} + iw=$(check_internet) if [ "$iw" == 0 ]; then - # check if we have a seperate routing table for our tests. - # If yes, move defaultroute to normal table and delete table gw-check + # Internet available again, restore default route and remove ip rules if [ -n "$defroutegwcheck" ]; then ip r a $defroutegwcheck ip r d $defroutegwcheck t gw-check - ip ru del fwmark 0x2 lookup gw-check for host in $testserver; do - iptables -t mangle -D OUTPUT -d $host -p tcp --dport 80 -j MARK --set-mark 0x2 + ips="$(resolve $host)" + for ip in $ips; do + [ -n "$(ip ru s | grep "to $ip lookup gw-check")" ] && ip rule del to $ip table gw-check + done + done + get_dnsservers + for d in $dns; do + [ -n "$(ip ru s | grep "to $d lookup gw-check")" ] && ip rule del to $d table gw-check done - logger -t gw-check "Internet is available again, restoring default route ( $defroutegwcheck)" + logger -p err -t gw-check "Internet is available again, default route restored ( $defroutegwcheck)" fi else - # Check failed. If we have a defaultroute with metric=0 and it is already in table gw-check then do nothing. - # If there is a defaultroute with metric=0 then remove it from the main routing table and add to table gw-check. + # Check failed. Move default route to table gw-check and setup ip rules. if [ -z "$(ip ru s | grep gw-check)" -a -n "$defroutemain" ]; then - ip rule add fwmark 0x2 lookup gw-check - for host in $testserver; do - iptables -t mangle -I OUTPUT -d $host -p tcp --dport 80 -j MARK --set-mark 0x2 - done ip r a $defroutemain table gw-check ip r d $defroutemain - logger -t gw-check "Internet is not available, deactivating the default route ( $defroutemain)" + logger -p err -t gw-check "Internet is not available, default route deactivated ( $defroutemain)" fi + for host in $testserver; do + ips="$(resolve $host)" + for ip in $ips; do + [ -z "$(ip ru s | grep "to $ip lookup gw-check")" ] && ip rule add to $ip table gw-check + done + done + get_dnsservers + for d in $dns; do + [ -z "$(ip ru s | grep "to $d lookup gw-check")" ] && ip rule add to $d table gw-check + done + logger -p err -t gw-check "Check your internet connection!" fi