luci-app-bmx6: Fix corner case in bmx6-info?tunnels
[feed/routing.git] / luci-app-bmx6 / bmx6 / www / cgi-bin / bmx6-info
1 #!/bin/sh
2 # This script gives information about bmx6
3 # Can be executed from a linux shell: ./bmx6-info -s links
4 # Or from web interfae (with cgi enabled): http://host/cgi-bin/bmx6-info?links
5 # Special methods are tagged with '$', like $myself or $neighbours: http://host/cgi-bin/bmx6-info?$myself
6 # When '$' is not used, raw bmx6 information from the filesystem is returned (/var/runb/bmx6/json/)
7
8 BMX6_DIR="$(uci get bmx6.general.runtimeDir 2>/dev/null)" || BMX6_DIR="/var/run/bmx6/json"
9
10 #Checking if shell mode or cgi-bin mode
11 if [ "$1" == "-s" ]; then
12 QUERY="$2"
13 else
14 QUERY="${QUERY_STRING%%=*}"
15 QUERY="${QUERY%%\?*}"
16 QUERY="${QUERY%%\&*}"
17 echo "Content-type: application/json"
18 echo ""
19 fi
20
21 # workaround to support old format starting with '$'
22 QUERY="$(echo "$QUERY" | sed s/'\$'//)"
23
24 check_path() {
25 [ -d "$1" ] && path=$(cd $1; pwd)
26 [ -f "$1" ] && path=$(cd $1/..; pwd)
27 [ $(echo "$path" | grep -c "^$BMX6_DIR") -ne 1 ] && exit 1
28 }
29
30 print_query() {
31 # If the query is a directory
32 [ -d "$BMX6_DIR/$1" ] &&
33 {
34 # If /all has not been specified
35 [ -z "$QALL" ] &&
36 {
37 total=$(ls $BMX6_DIR/$1 | wc -w)
38 i=1
39 echo -n "{ \"$1\": [ "
40 for f in $(ls $BMX6_DIR/$1); do
41 echo -n "{ \"name\": \"$f\" }"
42 [ $i -lt $total ] && echo -n ','
43 i=$(( $i + 1 ))
44 done
45 echo -n " ] }"
46
47 # If /all has been specified, printing all the files together
48 } || {
49 comma=""
50 echo -n "[ "
51 for entry in "$BMX6_DIR/$1/"*; do
52 [ -f "$entry" ] &&
53 {
54 ${comma:+echo -n "$comma"}
55 tr -d '\n' < "$entry"
56 comma=","
57 }
58 done
59 echo -n " ]"
60 }
61 }
62 # If the query is a file, just printing the file
63 [ -f "$BMX6_DIR/$1" ] && cat "$BMX6_DIR/$1";
64 }
65
66 if [ "${QUERY##*/}" == "all" ]; then
67 QUERY="${QUERY%/all}"
68 QALL=1
69 fi
70
71 if [ "$QUERY" == 'myself' ]; then
72 hostname="$(cat /proc/sys/kernel/hostname)"
73 ip6="$(bmx6 -c show=status | grep ^BMX | awk '{print $5}')"
74 ip4="$(bmx6 -c show=status | grep ^BMX | awk '{print $6}')"
75 cidr6=$(lua -l luci.ip -e "ip=luci.ip.new(\"$ip6\"); print(ip:network():string()..'/'..ip:prefix())")
76 cidr4=$(lua -l luci.ip -e "ip=luci.ip.new(\"$ip4\"); print(ip:network():string()..'/'..ip:prefix())")
77 echo -n "{\"myself\":{\"hostname\":\"$hostname\",\"ip6\":\"$ip6\",\"ip4\":\"$ip4\",\"net6\":\"$cidr6\",\"net4\":\"$cidr4\"}}"
78 exit 0
79 fi
80
81 if [ "$QUERY" == 'info' ]; then
82 echo -n '{ "info": [ '
83 print_query status
84 echo -n ","
85 print_query interfaces
86 echo -n "] }"
87 exit 0
88 fi
89
90 if [ "$QUERY" == 'neighbours' ]; then
91 QALL=1
92 echo -n '{ "neighbours": [ '
93 echo -n '{ "originators": '
94 print_query originators
95 echo -n '}, '
96 echo -n '{ "descriptions": '
97 print_query descriptions
98 echo -n "} ] }"
99 exit 0
100 fi
101
102 if [ "$QUERY" == 'tunnels' ]; then
103 tunnels=$(bmx6 -c --jshow tunnels /r=0)
104 if [ -z $tunnels ]; then
105 echo '{ "tunnels" : [] }'
106 else
107 echo $tunnels
108 fi
109 exit 0
110 fi
111
112 if [ "$QUERY" == "" ]; then
113 echo -n '{ "queries": ['
114 echo -n '{ "name": "myself", "info": "basic network information of self node" },'
115 echo -n '{ "name": "info", "info": "full network and device information of self node" },'
116 echo -n '{ "name": "tunnels", "info": "accnouncements (tunnels) published by the mesh network" },'
117 echo -n '{ "name": "neighbours", "info": "list of all my neighbours and their information" },'
118 echo -n '{ "name": "/", "info": "raw bmx6 json API" }]}'
119 exit 0
120 fi
121
122 check_path "$BMX6_DIR/$QUERY"
123 print_query $QUERY
124
125 #ls -1F "$BMX6_DIR"
126 exit 0