a622a4fc4437aa0d0a141b50bf4696bcfdfae2fb
[project/luci.git] / contrib / package / freifunk-gwcheck / files / usr / sbin / ff_olsr_test_gw.sh
1 #!/bin/sh
2
3 #check if dyngw_plain is installed and enabled, else exit
4 dyngwplainlib=`uci show olsrd |grep dyn_gw_plain |awk {' FS="."; print $1"."$2 '}`
5 if [ -n "$dyngwplainlib" ]; then
6 if [ "$(uci -q get $dyngwplainlib.ignore)" == 1 ]; then
7 exit 1
8 fi
9 else
10 echo "dyngw_plain not found in olsrd config, exit"
11 exit 1
12 fi
13
14
15 # check if we have a defaultroute with metric=0 in one of these tables: main table and gw-check table.
16 # If not exit here.
17 defroutemain="$(ip r s |grep default |grep -v metric)"
18 defroutegwcheck="$(ip r s t gw-check |grep default |grep -v metric)"
19 if [ -z "$defroutegwcheck" -a -z "$defroutemain" ]; then
20 exit 1
21 fi
22
23 # get and shuffle list of testservers
24 testserver="$(uci -q get freifunk-gwcheck.hosts.host)"
25 [ -z "$testserver" ] && echo "No testservers found, exit" && exit
26
27 testserver="$(for t in $testserver; do echo $t; done | awk 'BEGIN {
28 srand();
29 }
30 {
31 l[NR] = $0;
32 }
33
34 END {
35 for (i = 1; i <= NR; i++) {
36 n = int(rand() * (NR - i + 1)) + i;
37 print l[n];
38 l[n] = l[i];
39 }
40 }')"
41
42 check_internet() {
43 for t in $testserver; do
44 local test
45 test=$(wget -q http://$t/conntest.html -O -| grep "Internet_works")
46 if [ "$test" == "Internet_works" ]; then
47 echo 0
48 break
49 else
50 logger -t gw-check "Could not get test file from http://$t/conntest.html"
51 fi
52 done
53 }
54
55 iw=$(check_internet)
56
57 if [ "$iw" == 0 ]; then
58 # check if we have a seperate routing table for our tests.
59 # If yes, move defaultroute to normal table and delete table gw-check
60 if [ -n "$defroutegwcheck" ]; then
61 ip r a $defroutegwcheck
62 ip r d $defroutegwcheck t gw-check
63 ip ru del fwmark 0x2 lookup gw-check
64 for host in $testserver; do
65 iptables -t mangle -D OUTPUT -d $host -p tcp --dport 80 -j MARK --set-mark 0x2
66 done
67 logger -t gw-check "Internet is available again, restoring default route ( $defroutegwcheck)"
68 fi
69
70 else
71 # Check failed. If we have a defaultroute with metric=0 and it is already in table gw-check then do nothing.
72 # If there is a defaultroute with metric=0 then remove it from the main routing table and add to table gw-check.
73 if [ -z "$(ip ru s | grep gw-check)" -a -n "$defroutemain" ]; then
74 ip rule add fwmark 0x2 lookup gw-check
75 for host in $testserver; do
76 iptables -t mangle -I OUTPUT -d $host -p tcp --dport 80 -j MARK --set-mark 0x2
77 done
78 ip r a $defroutemain table gw-check
79 ip r d $defroutemain
80 logger -t gw-check "Internet is not available, deactivating the default route ( $defroutemain)"
81 fi
82 fi