luci-app-bmx: Redesign Makefile. Split JS libraries. Clean package
[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 check_path() {
22 [ -d "$1" ] && path=$(cd $1; pwd)
23 [ -f "$1" ] && path=$(cd $1/..; pwd)
24 [ $(echo "$path" | grep -c "^$BMX6_DIR") -ne 1 ] && exit 1
25 }
26
27 print_query() {
28 # If the query is a directory
29 [ -d "$BMX6_DIR/$1" ] &&
30 {
31 # If /all has not been specified
32 [ -z "$QALL" ] &&
33 {
34 total=$(ls $BMX6_DIR/$1 | wc -w)
35 i=1
36 echo -n "{ \"$1\": [ "
37 for f in $(ls $BMX6_DIR/$1); do
38 echo -n "{ \"name\": \"$f\" }"
39 [ $i -lt $total ] && echo -n ','
40 i=$(( $i + 1 ))
41 done
42 echo -n " ] }"
43
44 # If /all has been specified, printing all the files together
45 } || {
46 comma=""
47 echo -n "[ "
48 for entry in "$BMX6_DIR/$1/"*; do
49 [ -f "$entry" ] &&
50 {
51 ${comma:+echo -n "$comma"}
52 tr -d '\n' < "$entry"
53 comma=","
54 }
55 done
56 echo -n " ]"
57 }
58 }
59 # If the query is a file, just printing the file
60 [ -f "$BMX6_DIR/$1" ] && cat "$BMX6_DIR/$1";
61 }
62
63 if [ "${QUERY##*/}" == "all" ]; then
64 QUERY="${QUERY%/all}"
65 QALL=1
66 fi
67
68 if [ "$QUERY" == '$myself' ]; then
69 hostname="$(cat /proc/sys/kernel/hostname)"
70 ip6="$(bmx6 -c show=status | grep ^BMX | awk '{print $5}')"
71 ip4="$(bmx6 -c show=status | grep ^BMX | awk '{print $6}')"
72 cidr6=$(lua -l luci.ip -e "ip=luci.ip.new(\"$ip6\"); print(ip:network():string()..'/'..ip:prefix())")
73 cidr4=$(lua -l luci.ip -e "ip=luci.ip.new(\"$ip4\"); print(ip:network():string()..'/'..ip:prefix())")
74 echo -n "{\"myself\":{\"hostname\":\"$hostname\",\"ip6\":\"$ip6\",\"ip4\":\"$ip4\",\"net6\":\"$cidr6\",\"net4\":\"$cidr4\"}}"
75 exit 0
76 fi
77
78 if [ "$QUERY" == '$info' ]; then
79 echo -n '{ "info": [ '
80 print_query status
81 echo -n ","
82 print_query interfaces
83 echo -n "] }"
84 exit 0
85 fi
86
87 if [ "$QUERY" == '$neighbours' ]; then
88 QALL=1
89 echo -n '{ "neighbours": [ '
90 echo -n '{ "originators": '
91 print_query originators
92 echo -n '}, '
93 echo -n '{ "descriptions": '
94 print_query descriptions
95 echo -n "} ] }"
96 exit 0
97 fi
98
99 if [ "$QUERY" == '$tunnels' ]; then
100 bmx6 -c --jshow tunnels /r=0
101 exit 0
102 fi
103
104 if [ "$QUERY" == "" ]; then
105 echo -n '{ "queries": ['
106 echo -n '{ "name": "$myself", "info": "basic network information of self node" },'
107 echo -n '{ "name": "$info", "info": "full network and device information of self node" },'
108 echo -n '{ "name": "$tunnels", "info": "accnouncements (tunnels) published by the mesh network" },'
109 echo -n '{ "name": "$neighbours", "info": "list of all my neighbours and their information" },'
110 echo -n '{ "name": "/", "info": "raw bmx6 json API" }]}'
111 exit 0
112 fi
113
114 check_path "$BMX6_DIR/$QUERY"
115 print_query $QUERY
116
117 #ls -1F "$BMX6_DIR"
118 exit 0