Merge pull request #4853 from StevenHessing/noddos
[feed/packages.git] / net / mwan3 / files / usr / libexec / rpcd / mwan3
1 #!/bin/sh
2
3 . /lib/functions.sh
4 . /lib/functions/network.sh
5 . /usr/share/libubox/jshn.sh
6
7 MWAN3TRACK_STATUS_DIR="/var/run/mwan3track"
8
9 IPS="ipset"
10 IPT4="iptables -t mangle -w"
11 IPT6="ip6tables -t mangle -w"
12
13 report_connected_v4() {
14 local address
15
16 if [ -n "$($IPT4 -S mwan3_connected 2> /dev/null)" ]; then
17 for address in $($IPS list mwan3_connected_v4 | tail -n +8); do
18 json_add_string "" "${address}"
19 done
20 fi
21 }
22
23 report_connected_v6() {
24 local address
25
26 if [ -n "$($IPT6 -S mwan3_connected 2> /dev/null)" ]; then
27 for address in $($IPS list mwan3_connected_v6 | tail -n +8); do
28 json_add_string "" "${address}"
29 done
30 fi
31 }
32
33 get_mwan3_status() {
34 local iface="${1}"
35 local iface_select="${2}"
36 local running="0"
37 local age=0
38 local pid device time_p time_n
39
40 network_get_device device $1
41
42 if [ "${iface}" = "${iface_select}" ] || [ "${iface_select}" = "" ]; then
43 pid="$(pgrep -f "mwan3track $iface $device")"
44 if [ "${pid}" != "" ]; then
45 running="1"
46 fi
47
48 time_p="$(cat "$MWAN3TRACK_STATUS_DIR/${iface}/TIME")"
49 [ -z "${time_p}" ] || {
50 time_n="$(date +'%s')"
51 let age=time_n-time_p
52 }
53
54 json_add_object "${iface}"
55 json_add_int age "$age"
56 json_add_int "score" "$(cat "$MWAN3TRACK_STATUS_DIR/${iface}/SCORE")"
57 json_add_int "lost" "$(cat "$MWAN3TRACK_STATUS_DIR/${iface}/LOST")"
58 json_add_int "turn" "$(cat "$MWAN3TRACK_STATUS_DIR/${iface}/TURN")"
59 json_add_string "status" "$(cat "$MWAN3TRACK_STATUS_DIR/${iface}/STATUS")"
60 json_add_boolean "running" "${running}"
61 json_add_array "track_ip"
62 for file in $MWAN3TRACK_STATUS_DIR/${iface}/*; do
63 track="${file#*/TRACK_}"
64 if [ "${track}" != "${file}" ]; then
65 json_add_object
66 json_add_string ip "${track}"
67 json_add_string status "$(cat "${file}")"
68 json_close_object
69 fi
70 done
71 json_close_array
72 json_close_object
73 fi
74 }
75
76 case "$1" in
77 list)
78 json_init
79 json_add_object "status"
80 json_add_string "section" "x"
81 json_add_string "interface" "x"
82 json_close_object
83 json_dump
84 ;;
85 call)
86 case "$2" in
87 status)
88 local section iface
89 read input;
90 json_load "$input"
91 json_get_var section section
92 json_get_var iface interface
93
94 config_load mwan3
95 json_init
96 case "$section" in
97 interfaces)
98 json_add_object interfaces
99 config_foreach get_mwan3_status interface "${iface}"
100 json_close_object
101 ;;
102 connected)
103 json_add_object connected
104 json_add_array ipv4
105 report_connected_v4
106 json_close_array
107 json_add_array ipv6
108 report_connected_v6
109 json_close_array
110 json_close_object
111 ;;
112 *)
113 # interfaces
114 json_add_object interfaces
115 config_foreach get_mwan3_status interface
116 json_close_object
117 # connected
118 json_add_object connected
119 json_add_array ipv4
120 report_connected_v4
121 json_close_array
122 json_add_array ipv6
123 report_connected_v6
124 json_close_array
125 json_close_object
126 ;;
127 esac
128 json_dump
129 ;;
130 esac
131 ;;
132 esac