travelmate: release 2.1.2-1
[feed/packages.git] / net / travelmate / files / travelmate.init
1 #!/bin/sh /etc/rc.common
2 # Copyright (c) 2016-2024 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=all
7
8 START=25
9 USE_PROCD=1
10
11 extra_command "scan" "[<radio>|<ifname>] Scan for available nearby uplinks"
12 extra_command "assoc" "[<radio>|<ifname>] Get MAC adresses of associated wlan stations"
13 extra_command "setup" "[<iface>] [<zone>] [<metric>] Setup the travelmate uplink interface, by default 'trm_wwan' with firewall zone 'wan' and metric '100'"
14
15 trm_init="/etc/init.d/travelmate"
16 trm_script="/usr/bin/travelmate.sh"
17 trm_pidfile="/var/run/travelmate.pid"
18 trm_scanfile="/var/run/travelmate.scan"
19
20 boot() {
21 if [ -s "${trm_pidfile}" ]; then
22 : >"${trm_pidfile}"
23 fi
24 rc_procd start_service
25 }
26
27 start_service() {
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 local ppid pid timeout
44
45 timeout="$(uci_get travelmate global trm_timeout)"
46
47 if [ -s "${trm_pidfile}" ]; then
48 ppid="$(cat "${trm_pidfile}" 2>/dev/null)"
49 if [ -n "${ppid}" ]; then
50 pid="$(pgrep -xnf "sleep ${timeout:-60} 0" -P ${ppid} 2>/dev/null)"
51 if [ -n "${pid}" ]; then
52 kill -INT ${pid} 2>/dev/null
53 fi
54 fi
55 fi
56 }
57
58 stop_service() {
59 rc_procd "${trm_script}" stop
60 }
61
62 status_service() {
63 local key keylist value rtfile
64
65 rtfile="$(uci_get travelmate global trm_rtfile "/tmp/trm_runtime.json")"
66 json_load_file "${rtfile}" >/dev/null 2>&1
67 if json_select data >/dev/null 2>&1; then
68 printf "%s\n" "::: travelmate runtime information"
69 json_get_keys keylist
70 for key in ${keylist}; do
71 json_get_var value "${key}"
72 printf " + %-18s : %s\n" "${key}" "${value}"
73 done
74 else
75 printf "%s\n" "::: no travelmate runtime information available"
76 fi
77 }
78
79 scan() {
80 local result radio="${1}"
81
82 : > "${trm_scanfile}"
83 if [ -z "${radio}" ]; then
84 radio="$(ubus -S call network.wireless status 2>/dev/null | jsonfilter -q -l1 -e '@[@.up=true].interfaces[0].ifname')"
85 fi
86 result="$(iwinfo "${radio}" scan 2>/dev/null |
87 awk 'BEGIN{FS="[[:space:]]"}/Address:/{var1=$NF}/ESSID:/{var2="";
88 for(i=12;i<=NF;i++)if(var2==""){var2=$i}else{var2=var2" "$i}}/Channel:/{var3=$NF}/Quality:/{split($NF,var0,"/")}/Encryption:/{var4="";
89 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}' |
90 sort -rn)"
91 printf "::: %s\n:::\n" "Available nearby uplinks on '${radio}'"
92 if [ -n "${result}" ]; then
93 printf "%s\n" "${result}" > "${trm_scanfile}"
94 printf "%-15s%-10s%-35s%-20s%s\n" " Strength" "Channel" "ESSID" "BSSID" "Encryption"
95 printf "%s\n" " --------------------------------------------------------------------------------------"
96 printf "%s\n" "${result}"
97 else
98 printf "%s\n" "::: Empty resultset"
99 fi
100 }
101
102 setup() {
103 local iface cnt=0 input="${1:-"trm_wwan"}" zone="${2:-"wan"}" metric="${3:-"100"}"
104
105 iface="$(uci_get travelmate global trm_iface)"
106 input="${input//[+*~%&\$@\"\' ]/}"
107 zone="${zone//[+*~%&\$@\"\' ]/}"
108 metric="${metric//[^0-9]/}"
109
110 if [ -n "${iface}" ] && [ "${iface}" = "${input}" ]; then
111 printf "%s\n" "The uplink interface '${input}' has been already configured"
112 elif [ -n "${input}" ]; then
113 if [ -n "${iface}" ]; then
114 uci -q batch <<-EOC
115 del network."${iface}"
116 del network."${iface}6"
117 EOC
118 fi
119 uci -q batch <<-EOC
120 set travelmate.global.trm_enabled="1"
121 set travelmate.global.trm_iface="${input}"
122 set network."${input}"="interface"
123 set network."${input}".proto="dhcp"
124 set network."${input}".metric="${metric}"
125 set network."${input}6"=interface
126 set network."${input}6".device="@${input}"
127 set network."${input}6".proto="dhcpv6"
128 commit travelmate
129 commit network
130 EOC
131
132 while [ -n "$(uci -q get firewall.@zone["${cnt}"].name)" ]; do
133 if [ "$(uci -q get firewall.@zone["${cnt}"].name)" = "${zone}" ]; then
134 if [ -n "${iface}" ]; then
135 uci -q batch <<-EOC
136 del_list firewall.@zone["${cnt}"].network="${iface}"
137 del_list firewall.@zone["${cnt}"].network="${iface}6"
138 EOC
139 fi
140 uci -q batch <<-EOC
141 add_list firewall.@zone["${cnt}"].network="${input}"
142 add_list firewall.@zone["${cnt}"].network="${input}6"
143 commit firewall
144 EOC
145 break
146 fi
147 cnt=$((cnt + 1))
148 done
149
150 if [ -n "${iface}" ]; then
151 cnt=0
152 while [ -n "$(uci -q get wireless.@wifi-iface["${cnt}"].network)" ]; do
153 if [ "$(uci -q get wireless.@wifi-iface["${cnt}"].network)" = "${iface}" ]; then
154 uci -q set wireless.@wifi-iface["${cnt}"].network="${input}"
155 fi
156 cnt=$((cnt + 1))
157 done
158 uci -q commit wireless
159 fi
160 /etc/init.d/network reload >/dev/null 2>&1
161 /etc/init.d/firewall reload >/dev/null 2>&1
162 "${trm_init}" restart
163 fi
164 }
165
166 assoc() {
167 local result radio="${1}"
168
169 if [ -z "${radio}" ]; then
170 radio="$(ubus -S call network.wireless status 2>/dev/null | jsonfilter -q -l1 -e '@[@.*.*.config.mode="ap"].interfaces[0].ifname')"
171 fi
172 result="$(iwinfo "${radio}" assoc 2>/dev/null | awk '/^[A-Z0-9:]+/{printf " %s\n",$1}')"
173 printf "%s\n" "::: Associated wlan stations on '${radio}'"
174 printf "%s\n" ":::"
175 if [ -n "${result}" ]; then
176 printf "%s\n" " MAC addresses"
177 printf "%s\n" " -----------------"
178 printf "%s\n" "${result}"
179 else
180 printf "%s\n" "::: Empty resultset"
181 fi
182 }
183
184 service_triggers() {
185 local iface delay
186
187 iface="$(uci_get travelmate global trm_iface)"
188 delay="$(uci_get travelmate global trm_triggerdelay "2")"
189 PROCD_RELOAD_DELAY=$((delay * 1000))
190
191 if [ -n "${iface}" ]; then
192 procd_add_interface_trigger "interface.*.down" "${iface}" "${trm_init}" reload
193 fi
194 procd_add_raw_trigger "interface.*.up" "${PROCD_RELOAD_DELAY}" "${trm_init}" start
195 procd_add_config_trigger "config.change" "travelmate" "${trm_init}" restart
196 }