contrib/package: Add freifunk-gwcheck to replace the gateway-check in modules/freifunk
authorManuel Munz <freifunk@somakoma.de>
Fri, 23 Sep 2011 19:23:18 +0000 (19:23 +0000)
committerManuel Munz <freifunk@somakoma.de>
Fri, 23 Sep 2011 19:23:18 +0000 (19:23 +0000)
contrib/package/freifunk-gwcheck/Makefile [new file with mode: 0644]
contrib/package/freifunk-gwcheck/files/etc/config/freifunk-gwcheck [new file with mode: 0644]
contrib/package/freifunk-gwcheck/files/usr/sbin/ff_olsr_test_gw.sh [new file with mode: 0755]
contrib/package/freifunk-gwcheck/ipkg/postinst [new file with mode: 0644]
contrib/package/freifunk-gwcheck/ipkg/postrm [new file with mode: 0644]
contrib/package/freifunk-gwcheck/root/etc/uci-defaults/freifunk-gwcheck [new file with mode: 0644]

diff --git a/contrib/package/freifunk-gwcheck/Makefile b/contrib/package/freifunk-gwcheck/Makefile
new file mode 100644 (file)
index 0000000..4922c23
--- /dev/null
@@ -0,0 +1,39 @@
+# Copyright (C) 2011 Manuel Munz <freifunk at somakoma de>
+# This is free software, licensed under the Apache 2.0 license.
+
+include $(TOPDIR)/rules.mk
+
+PKG_NAME:=freifunk-gwcheck
+PKG_RELEASE:=1
+
+PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME)
+
+include $(INCLUDE_DIR)/package.mk
+
+define Package/freifunk-gwcheck
+  SECTION:=luci
+  CATEGORY:=LuCI
+  SUBMENU:=Freifunk
+  TITLE:=Freifunk gateway check script
+  DEPENDS:=+firewall +ip +iptables-mod-ipopt
+endef
+
+define Package/freifunk-gwcheck/description
+  This script periodically checks if internet is available via your own gateway. If it detects that it is broken, then the defaultroute is removed from the main table and temporarilly placed in table gw-check until your internet works again. Config file is /etc/config/freifunk-gwcheck.
+endef
+
+define Build/Prepare
+       mkdir -p $(PKG_BUILD_DIR)
+endef
+
+define Build/Configure
+endef
+
+define Build/Compile
+endef
+
+define Package/freifunk-gwcheck/install
+       $(CP) ./files/* $(1)/
+endef
+
+$(eval $(call BuildPackage,freifunk-gwcheck))
diff --git a/contrib/package/freifunk-gwcheck/files/etc/config/freifunk-gwcheck b/contrib/package/freifunk-gwcheck/files/etc/config/freifunk-gwcheck
new file mode 100644 (file)
index 0000000..382747e
--- /dev/null
@@ -0,0 +1,5 @@
+config 'gwcheck' 'hosts'
+       list 'host' 'augsburg.freifunk.net'
+       list 'host' 'vpn4.leipzig.freifunk.net'
+       list 'host' 'luci.subsignal.org'
+       list 'host' 'conntest.jpod.cc'
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
new file mode 100755 (executable)
index 0000000..5d34d9a
--- /dev/null
@@ -0,0 +1,82 @@
+#!/bin/sh
+
+#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)" == 0 ]; then
+               exit 1
+       fi
+else
+       echo "dyngw_plain not found in olsrd config, exit"
+       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.
+defroutemain="$(ip r s |grep default |grep -v metric)"
+defroutegw-check="$(ip r s t gw-check |grep default |grep -v metric)"
+if [ -z "$defroutegw-check" -a -z "$defroutemain" ]; then
+       exit 1
+fi
+
+# get and shuffle list of testservers
+testserver="$(uci -q get freifunk-gwcheck.hosts.host)"
+[ -z "$testserver" ] && echo "No testservers found, exit" && exit 
+
+testserver="$(for t in $testserver; do echo $t; done | awk 'BEGIN {
+       srand();
+}
+{
+       l[NR] = $0;
+}
+
+END {
+       for (i = 1; i <= NR; i++) {
+               n = int(rand() * (NR - i + 1)) + i;
+               print l[n];
+               l[n] = l[i];
+       }
+}')"
+
+check_internet() {
+       for t in $testserver; do
+               local test
+               test=$(wget -q http://$t/conntest.html -O -| grep "Internet_works")
+               if [ "$test" == "Internet_works" ]; then
+                       echo 0
+                       break
+               else
+                       logger -t gw-check "Could not get test file from http://$t/conntest.html"
+               fi
+       done
+}
+
+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
+       if [ -n "$defroutegw-check" ]; then
+               ip r a $defroutegw-check
+               ip r d $defroutegw-check 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
+               done
+               logger -t gw-check "Internet is available again, restoring default route ( $defroutegw-check)"
+       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.
+       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)"
+       fi
+fi
diff --git a/contrib/package/freifunk-gwcheck/ipkg/postinst b/contrib/package/freifunk-gwcheck/ipkg/postinst
new file mode 100644 (file)
index 0000000..1c5308f
--- /dev/null
@@ -0,0 +1,4 @@
+#!/bin/sh
+[ -n "${IPKG_INSTROOT}" ] || {
+       ( . /etc/uci-defaults/freifunk-gwcheck ) && rm -f /etc/uci-defaults/freifunk-gwcheck
+}
diff --git a/contrib/package/freifunk-gwcheck/ipkg/postrm b/contrib/package/freifunk-gwcheck/ipkg/postrm
new file mode 100644 (file)
index 0000000..18e4a07
--- /dev/null
@@ -0,0 +1,5 @@
+#!/bin/sh
+[ -n "${IPKG_INSTROOT}" ] || {
+       sed -i '/gw-check/d' /etc/iproute2/rt_tables
+       sed -i '/ff_olsr_test_gw.sh/d' /etc/crontabs/root
+}
diff --git a/contrib/package/freifunk-gwcheck/root/etc/uci-defaults/freifunk-gwcheck b/contrib/package/freifunk-gwcheck/root/etc/uci-defaults/freifunk-gwcheck
new file mode 100644 (file)
index 0000000..e7fc988
--- /dev/null
@@ -0,0 +1,5 @@
+#!/bin/sh
+tables="/etc/iproute2/rt_tables"
+[ -z "`grep "gw-check" $tables`" ] && echo "200 gw-check" >> $tables
+[ -z "`grep "ff_olsr_test_gw.sh" /etc/crontabs/root`" ] && echo "* * * * *     /usr/sbin/ff_olsr_test_gw.sh" >> /etc/crontabs/root
+/etc/init.d/cron restart