Merge pull request #16163 from stintel/openvswitch
[feed/packages.git] / net / travelmate / files / travelmate.init
1 #!/bin/sh /etc/rc.common
2 # Copyright (c) 2016-2021 Dirk Brenken (dev@brenken.org)
3 # This is free software, licensed under the GNU General Public License v3.
4
5 # set (s)hellcheck exceptions
6 # shellcheck disable=2034,2086,2154,3043,3060
7
8 START=25
9 USE_PROCD=1
10
11 extra_command "scan" "<radio> Scan for available nearby uplinks"
12 extra_command "setup" "[<iface>] [<zone>] [<metric>] Setup the travelmate uplink interface, by default 'trm_wwan' with firewall zone 'wan' and metric '100'"
13
14 trm_init="/etc/init.d/travelmate"
15 trm_script="/usr/bin/travelmate.sh"
16 trm_pidfile="/var/run/travelmate.pid"
17
18 boot()
19 {
20 if [ -s "${trm_pidfile}" ]; then
21 : >"${trm_pidfile}"
22 fi
23 rc_procd start_service
24 }
25
26 start_service()
27 {
28 if "${trm_init}" enabled; then
29 if [ "${action}" = "boot" ]; then
30 return 0
31 fi
32 procd_open_instance "travelmate"
33 procd_set_param command "${trm_script}" "${@}"
34 procd_set_param pidfile "${trm_pidfile}"
35 procd_set_param nice "$(uci_get travelmate global trm_nice "0")"
36 procd_set_param stdout 1
37 procd_set_param stderr 1
38 procd_close_instance
39 fi
40 }
41
42 reload_service()
43 {
44 local ppid pid timeout
45
46 timeout="$(uci_get travelmate global trm_timeout)"
47
48 if [ -s "${trm_pidfile}" ]; then
49 ppid="$(cat "${trm_pidfile}" 2>/dev/null)"
50 if [ -n "${ppid}" ]; then
51 pid="$(pgrep -xnf "sleep ${timeout:-60} 0" -P ${ppid} 2>/dev/null)"
52 if [ -n "${pid}" ]; then
53 kill -INT ${pid} 2>/dev/null
54 fi
55 fi
56 fi
57 }
58
59 stop_service()
60 {
61 rc_procd "${trm_script}" stop
62 }
63
64 status_service()
65 {
66 local key keylist value rtfile
67
68 rtfile="$(uci_get travelmate global trm_rtfile "/tmp/trm_runtime.json")"
69 json_load_file "${rtfile}" >/dev/null 2>&1
70 if json_select data >/dev/null 2>&1; then
71 printf "%s\n" "::: travelmate runtime information"
72 json_get_keys keylist
73 for key in ${keylist}; do
74 json_get_var value "${key}"
75 printf " + %-18s : %s\n" "${key}" "${value}"
76 done
77 else
78 printf "%s\n" "::: no travelmate runtime information available"
79 fi
80 }
81
82 scan()
83 {
84 local result scan_dev radio="${1:-"radio0"}"
85
86 scan_dev="$(ubus -S call network.wireless status 2>/dev/null | jsonfilter -q -l1 -e "@.${radio}.interfaces[0].ifname")"
87 result="$(iwinfo "${scan_dev:-${radio}}" scan 2>/dev/null |
88 awk 'BEGIN{FS="[[:space:]]"}/Address:/{var1=$NF}/ESSID:/{var2="";
89 for(i=12;i<=NF;i++)if(var2==""){var2=$i}else{var2=var2" "$i}}/Channel:/{var3=$NF}/Quality:/{split($NF,var0,"/")}/Encryption:/{var4="";
90 for(j=12;j<=NF;j++)if(var4==""){var4=$j}else{var4=var4" "$j};printf " %-11i%-10s%-35s%-20s%s\n",(var0[1]*100/var0[2]),var3,var2,var1,var4}' |
91 sort -rn)"
92 printf '%s\n' "::: Available nearby uplinks on '${scan_dev:-${radio}}'"
93 printf '%s\n' ":::"
94 if [ -n "${result}" ]; then
95 printf '%-15s%-10s%-35s%-20s%s\n' " Strength" "Channel" "ESSID" "BSSID" "Encryption"
96 printf '%s\n' " --------------------------------------------------------------------------------------"
97 printf '%s\n' "${result}"
98 else
99 printf '%s\n' "::: No scan results"
100 fi
101 }
102
103 setup()
104 {
105 local iface cnt=0 input="${1:-"trm_wwan"}" zone="${2:-"wan"}" metric="${3:-"100"}"
106
107 iface="$(uci_get travelmate global trm_iface)"
108 input="${input//[+*~%&\$@\"\' ]/}"
109 zone="${zone//[+*~%&\$@\"\' ]/}"
110 metric="${metric//[^0-9]/}"
111
112 if [ -n "${iface}" ] && [ "${iface}" = "${input}" ]; then
113 printf "%s\n" "The uplink interface '${input}' has been already configured"
114 elif [ -n "${input}" ]; then
115 if [ -n "${iface}" ]; then
116 uci -q batch <<-EOC
117 del network."${iface}"
118 del network."${iface}6"
119 EOC
120 fi
121 uci -q batch <<-EOC
122 set travelmate.global.trm_enabled="1"
123 set travelmate.global.trm_iface="${input}"
124 set network."${input}"="interface"
125 set network."${input}".proto="dhcp"
126 set network."${input}".metric="${metric}"
127 set network."${input}6"=interface
128 set network."${input}6".device="@${input}"
129 set network."${input}6".proto="dhcpv6"
130 commit travelmate
131 commit network
132 EOC
133
134 while [ -n "$(uci -q get firewall.@zone["${cnt}"].name)" ]; do
135 if [ "$(uci -q get firewall.@zone["${cnt}"].name)" = "${zone}" ]; then
136 if [ -n "${iface}" ]; then
137 uci -q batch <<-EOC
138 del_list firewall.@zone["${cnt}"].network="${iface}"
139 del_list firewall.@zone["${cnt}"].network="${iface}6"
140 EOC
141 fi
142 uci -q batch <<-EOC
143 add_list firewall.@zone["${cnt}"].network="${input}"
144 add_list firewall.@zone["${cnt}"].network="${input}6"
145 commit firewall
146 EOC
147 break
148 fi
149 cnt=$((cnt + 1))
150 done
151
152 if [ -n "${iface}" ]; then
153 cnt=0
154 while [ -n "$(uci -q get wireless.@wifi-iface["${cnt}"].network)" ]; do
155 if [ "$(uci -q get wireless.@wifi-iface["${cnt}"].network)" = "${iface}" ]; then
156 uci -q set wireless.@wifi-iface["${cnt}"].network="${input}"
157 fi
158 cnt=$((cnt + 1))
159 done
160 uci -q commit wireless
161 fi
162 /etc/init.d/network reload >/dev/null 2>&1
163 /etc/init.d/firewall reload >/dev/null 2>&1
164 "${trm_init}" restart
165 fi
166 }
167
168 service_triggers()
169 {
170 local iface delay
171
172 iface="$(uci_get travelmate global trm_iface)"
173 delay="$(uci_get travelmate global trm_triggerdelay "2")"
174 PROCD_RELOAD_DELAY=$((delay * 1000))
175
176 if [ -n "${iface}" ]; then
177 procd_add_interface_trigger "interface.*.down" "${iface}" "${trm_init}" reload
178 fi
179 procd_add_raw_trigger "interface.*.up" "${PROCD_RELOAD_DELAY}" "${trm_init}" start
180 procd_add_config_trigger "config.change" "travelmate" "${trm_init}" restart
181 }