travelmate: release 2.1.1
[feed/packages.git] / net / travelmate / files / travelmate.sh
1 #!/bin/sh
2 # travelmate, a wlan connection manager for travel router
3 # Copyright (c) 2016-2023 Dirk Brenken (dev@brenken.org)
4 # This is free software, licensed under the GNU General Public License v3.
5
6 # set (s)hellcheck exceptions
7 # shellcheck disable=all
8
9 export LC_ALL=C
10 export PATH="/usr/sbin:/usr/bin:/sbin:/bin"
11
12 trm_ver="2.1.1"
13 trm_enabled="0"
14 trm_debug="0"
15 trm_iface=""
16 trm_captive="1"
17 trm_proactive="1"
18 trm_vpn="1"
19 trm_netcheck="0"
20 trm_autoadd="0"
21 trm_randomize="0"
22 trm_mail="0"
23 trm_mailpgm="/etc/travelmate/travelmate.mail"
24 trm_vpnpgm="/etc/travelmate/travelmate.vpn"
25 trm_minquality="35"
26 trm_maxretry="3"
27 trm_maxwait="30"
28 trm_maxautoadd="5"
29 trm_maxscan="10"
30 trm_timeout="60"
31 trm_radio=""
32 trm_connection=""
33 trm_wpaflags=""
34 trm_ovpninfolist=""
35 trm_vpnifacelist=""
36 trm_vpninfolist=""
37 trm_stdvpnservice=""
38 trm_stdvpniface=""
39 trm_rtfile="/tmp/trm_runtime.json"
40 trm_wifi="$(command -v wifi)"
41 trm_fetch="$(command -v curl)"
42 trm_iwinfo="$(command -v iwinfo)"
43 trm_logger="$(command -v logger)"
44 trm_wpa="$(command -v wpa_supplicant)"
45 trm_captiveurl="http://detectportal.firefox.com"
46 trm_useragent="Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/118.0"
47 trm_ntpfile="/var/state/travelmate.ntp"
48 trm_vpnfile="/var/state/travelmate.vpn"
49 trm_mailfile="/var/state/travelmate.mail"
50 trm_refreshfile="/var/state/travelmate.refresh"
51 trm_pidfile="/var/run/travelmate.pid"
52 trm_action="${1:-"start"}"
53
54 # load travelmate environment
55 #
56 f_env() {
57 local check wpa_checks result
58
59 if [ "${trm_action}" = "stop" ]; then
60 return
61 fi
62
63 unset trm_stalist trm_radiolist trm_uplinklist trm_vpnifacelist trm_uplinkcfg trm_activesta trm_opensta
64
65 trm_sysver="$(ubus -S call system board 2>/dev/null | jsonfilter -q -e '@.model' -e '@.release.description' |
66 awk 'BEGIN{RS="";FS="\n"}{printf "%s, %s",$1,$2}')"
67
68 config_cb() {
69 local name="${1}" type="${2}"
70
71 if [ "${name}" = "travelmate" ] && [ "${type}" = "global" ]; then
72 option_cb() {
73 local option="${1}" value="${2}"
74 eval "${option}=\"${value}\""
75 }
76 list_cb() {
77 local option="${1}" value="${2}"
78 if [ "${option}" = "trm_vpnifacelist" ]; then
79 eval "trm_vpnifacelist=\"$(printf "%s" "${trm_vpnifacelist}") ${value}\""
80 fi
81 }
82 elif [ "${name}" = "uplink" ]; then
83 if [ "$(uci_get "travelmate.${type}.opensta")" = "1" ]; then
84 eval "trm_opensta=\"$((${trm_opensta:-0} + 1))\""
85 fi
86 else
87 option_cb() {
88 return 0
89 }
90 fi
91 }
92 config_load travelmate
93
94 if [ "${trm_enabled}" != "1" ]; then
95 f_log "info" "travelmate is currently disabled, please set 'trm_enabled' to '1' to use this service"
96 /etc/init.d/travelmate stop
97 elif [ -z "${trm_iface}" ]; then
98 f_log "info" "travelmate is currently not configured, please use the 'Interface Setup' in LuCI or the 'setup' option in CLI"
99 /etc/init.d/travelmate stop
100 elif ! ubus -t "${trm_maxwait}" wait_for network.wireless network.interface."${trm_iface}" >/dev/null 2>&1; then
101 f_log "info" "travelmate interface '${trm_iface}' does not appear on ubus, please check your network setup"
102 /etc/init.d/travelmate stop
103 fi
104
105 if [ -z "${trm_wpaflags}" ]; then
106 wpa_checks="sae owe eap suiteb192"
107 for check in ${wpa_checks}; do
108 if [ -x "${trm_wpa}" ]; then
109 if "${trm_wpa}" -v"${check}" >/dev/null 2>&1; then
110 result="$(f_trim "${result} ${check}: $(f_char 1)")"
111 else
112 result="$(f_trim "${result} ${check}: $(f_char 0)")"
113 fi
114 fi
115 done
116 trm_wpaflags="$(printf "%s" "${result}" | awk '{printf "%s %s, %s %s, %s %s, %s %s",$1,$2,$3,$4,$5,$6,$7,$8}')"
117 fi
118
119 config_load wireless
120 config_foreach f_setdev "wifi-device"
121 if [ -n "$(uci -q changes "wireless")" ]; then
122 uci_commit "wireless"
123 f_wifi
124 fi
125
126 json_load_file "${trm_rtfile}" >/dev/null 2>&1
127 if ! json_select data >/dev/null 2>&1; then
128 : >"${trm_rtfile}"
129 json_init
130 json_add_object "data"
131 fi
132
133 if [ "${trm_vpn}" = "1" ] && [ -z "${trm_vpninfolist}" ]; then
134 config_load network
135 config_foreach f_getvpn "interface"
136 fi
137 f_log "debug" "f_env ::: auto_sta: ${trm_opensta:-"-"}, wpa_flags: ${trm_wpaflags}, sys_ver: ${trm_sysver}"
138 }
139
140 # trim helper function
141 #
142 f_trim() {
143 local trim="${1}"
144
145 trim="${trim#"${trim%%[![:space:]]*}"}"
146 trim="${trim%"${trim##*[![:space:]]}"}"
147 printf "%s" "${trim}"
148 }
149
150 # status helper function
151 #
152 f_char() {
153 local result input="${1}"
154
155 if [ "${input}" = "1" ]; then
156 result="✔"
157 else
158 result="✘"
159 fi
160 printf "%s" "${result}"
161 }
162
163 # wifi helper function
164 #
165 f_wifi() {
166 local status radio radio_up timeout="0"
167
168 "${trm_wifi}" reload
169 for radio in ${trm_radiolist}; do
170 while true; do
171 if [ "${timeout}" -ge "${trm_maxwait}" ]; then
172 break 2
173 fi
174 status="$("${trm_wifi}" status 2>/dev/null)"
175 if [ "$(printf "%s" "${status}" | jsonfilter -q -l1 -e "@.${radio}.up")" != "true" ] ||
176 [ "$(printf "%s" "${status}" | jsonfilter -q -l1 -e "@.${radio}.pending")" != "false" ]; then
177 if [ "${radio}" != "${radio_up}" ]; then
178 "${trm_wifi}" up "${radio}"
179 radio_up="${radio}"
180 fi
181 timeout="$((timeout + 1))"
182 sleep 1
183 else
184 continue 2
185 fi
186 done
187 done
188 if [ "${timeout}" -lt "${trm_maxwait}" ]; then
189 sleep "$((trm_maxwait / 6))"
190 timeout="$((timeout + (trm_maxwait / 6)))"
191 fi
192 f_log "debug" "f_wifi ::: radio_list: ${trm_radiolist}, radio: ${radio}, timeout: ${timeout}"
193 }
194
195 # vpn helper function
196 #
197 f_vpn() {
198 local rc result info iface vpn vpn_service vpn_iface vpn_instance vpn_status vpn_action="${1}"
199
200 if [ "${trm_vpn}" = "1" ] && [ -n "${trm_vpninfolist}" ]; then
201 vpn="$(f_getval "vpn")"
202 vpn_service="$(f_getval "vpnservice")"
203 vpn_iface="$(f_getval "vpniface")"
204
205 if [ ! -f "${trm_vpnfile}" ] || { [ -f "${trm_vpnfile}" ] && [ "${vpn_action}" = "enable" ]; }; then
206 for info in ${trm_vpninfolist}; do
207 iface="${info%%&&*}"
208 [ "${iface}" = "${info}" ] && vpn_instance="" || vpn_instance="${info##*&&}"
209 vpn_status="$(ifstatus "${iface}" | jsonfilter -q -l1 -e '@.up')"
210 if [ "${vpn_status}" = "true" ]; then
211 ifdown "${iface}"
212 if [ -x "/etc/init.d/openvpn" ] && [ -n "${vpn_instance}" ] && /etc/init.d/openvpn running "${vpn_instance}"; then
213 /etc/init.d/openvpn stop "${vpn_instance}"
214 fi
215 f_log "info" "take down vpn interface '${iface}/${vpn_instance:-"-"}' (initial)"
216 fi
217 done
218 rm -f "${trm_vpnfile}"
219 elif [ "${vpn}" = "1" ] && [ -n "${vpn_iface}" ] && [ "${vpn_action}" = "enable_keep" ]; then
220 for info in ${trm_vpninfolist}; do
221 iface="${info%%&&*}"
222 [ "${iface}" = "${info}" ] && vpn_instance="" || vpn_instance="${info##*&&}"
223 vpn_status="$(ifstatus "${iface}" | jsonfilter -q -l1 -e '@.up')"
224 if [ "${vpn_status}" = "true" ] && [ "${iface}" != "${vpn_iface}" ]; then
225 ifdown "${iface}"
226 if [ -x "/etc/init.d/openvpn" ] && [ -n "${vpn_instance}" ] && /etc/init.d/openvpn running "${vpn_instance}"; then
227 /etc/init.d/openvpn stop "${vpn_instance}"
228 fi
229 f_log "info" "take down vpn interface '${iface}/${vpn_instance:-"-"}' (switch)"
230 rm -f "${trm_vpnfile}"
231 break
232 fi
233 done
234 fi
235 if [ -x "${trm_vpnpgm}" ] && [ -n "${vpn_service}" ] && [ -n "${vpn_iface}" ]; then
236 if { [ "${vpn_action}" = "disable" ] && [ -f "${trm_vpnfile}" ]; } ||
237 { [ -s "${trm_ntpfile}" ] && { [ "${vpn}" = "1" ] && [ "${vpn_action%_*}" = "enable" ] && [ ! -f "${trm_vpnfile}" ]; } ||
238 { [ "${vpn}" != "1" ] && [ "${vpn_action%_*}" = "enable" ] && [ -f "${trm_vpnfile}" ]; }; }; then
239 result="$(f_net)"
240 if [ "${result}" = "net ok" ] || [ "${vpn_action}" = "disable" ]; then
241 for info in ${trm_vpninfolist}; do
242 iface="${info%%&&*}"
243 if [ "${iface}" = "${vpn_iface}" ]; then
244 [ "${iface}" = "${info}" ] && vpn_instance="" || vpn_instance="${info##*&&}"
245 break
246 fi
247 done
248 f_log "debug" "f_vpn ::: vpn: ${vpn:-"0"}, action: ${vpn_action}, service: ${vpn_service}, iface: ${vpn_iface}, instance: ${vpn_instance}"
249 "${trm_vpnpgm}" "${vpn:-"0"}" "${vpn_action}" "${vpn_service}" "${vpn_iface}" "${vpn_instance}" >/dev/null 2>&1
250 rc="${?}"
251 fi
252 fi
253 [ -n "${rc}" ] && f_jsnup
254 fi
255 fi
256 f_log "debug" "f_vpn ::: vpn: ${trm_vpn:-"-"}, enabled: ${vpn:-"-"}, action: ${vpn_action}, service: ${vpn_service:-"-"}, iface: ${vpn_iface:-"-"}, instance: ${vpn_instance:-"-"}, infolist: ${trm_vpninfolist:-"-"}, result: ${result}, rc: ${rc:-"-"}"
257 }
258
259 # mac helper function
260 #
261 f_mac() {
262 local result ifname macaddr action="${1}" section="${2}"
263
264 if [ "${action}" = "set" ]; then
265 macaddr="$(f_getval "macaddr")"
266 if [ -n "${macaddr}" ]; then
267 result="${macaddr}"
268 uci_set "wireless" "${section}" "macaddr" "${result}"
269 elif [ "${trm_randomize}" = "1" ]; then
270 result="$(hexdump -n6 -ve '/1 "%.02X "' /dev/random 2>/dev/null |
271 awk -v local="2,6,A,E" -v seed="$(date +%s)" 'BEGIN{srand(seed)}NR==1{split(local,b,",");
272 seed=int(rand()*4+1);printf "%s%s:%s:%s:%s:%s:%s",substr($1,0,1),b[seed],$2,$3,$4,$5,$6}')"
273 uci_set "wireless" "${section}" "macaddr" "${result}"
274 else
275 uci_remove "wireless" "${section}" "macaddr" 2>/dev/null
276 ifname="$(ubus -S call network.wireless status 2>/dev/null | jsonfilter -q -l1 -e '@.*.interfaces[@.config.mode="sta"].ifname')"
277 result="$(${trm_iwinfo} "${ifname}" info 2>/dev/null | awk '/Access Point:/{printf "%s",$3}')"
278 fi
279 elif [ "${action}" = "get" ]; then
280 result="$(uci_get "wireless" "${section}" "macaddr")"
281 if [ -z "${result}" ]; then
282 ifname="$(ubus -S call network.wireless status 2>/dev/null | jsonfilter -q -l1 -e '@.*.interfaces[@.config.mode="sta"].ifname')"
283 result="$(${trm_iwinfo} "${ifname}" info 2>/dev/null | awk '/Access Point:/{printf "%s",$3}')"
284 fi
285 fi
286 printf "%s" "${result}"
287 f_log "debug" "f_mac ::: action: ${action:-"-"}, section: ${section:-"-"}, macaddr: ${macaddr:-"-"}, result: ${result:-"-"}"
288 }
289
290 # set connection information
291 #
292 f_ctrack() {
293 local expiry action="${1}"
294
295 if [ -n "${trm_uplinkcfg}" ]; then
296 case "${action}" in
297 "start")
298 uci_remove "travelmate" "${trm_uplinkcfg}" "con_start" 2>/dev/null
299 uci_remove "travelmate" "${trm_uplinkcfg}" "con_end" 2>/dev/null
300 if [ -s "${trm_ntpfile}" ]; then
301 uci_set "travelmate" "${trm_uplinkcfg}" "con_start" "$(date "+%Y.%m.%d-%H:%M:%S")"
302 fi
303 ;;
304 "refresh")
305 if [ -s "${trm_ntpfile}" ] && [ -z "$(uci_get "travelmate" "${trm_uplinkcfg}" "con_start")" ]; then
306 uci_set "travelmate" "${trm_uplinkcfg}" "con_start" "$(date "+%Y.%m.%d-%H:%M:%S")"
307 fi
308 ;;
309 "end")
310 if [ -s "${trm_ntpfile}" ]; then
311 uci_set "travelmate" "${trm_uplinkcfg}" "con_end" "$(date "+%Y.%m.%d-%H:%M:%S")"
312 fi
313 ;;
314 "start_expiry")
315 if [ -s "${trm_ntpfile}" ]; then
316 expiry="$(uci_get "travelmate" "${trm_uplinkcfg}" "con_start_expiry")"
317 uci_set "travelmate" "${trm_uplinkcfg}" "enabled" "0"
318 uci_set "travelmate" "${trm_uplinkcfg}" "con_end" "$(date "+%Y.%m.%d-%H:%M:%S")"
319 f_log "info" "uplink '${radio}/${essid}/${bssid:-"-"}' expired after ${expiry} minutes"
320 fi
321 ;;
322 "end_expiry")
323 if [ -s "${trm_ntpfile}" ]; then
324 expiry="$(uci_get "travelmate" "${trm_uplinkcfg}" "con_end_expiry")"
325 uci_set "travelmate" "${trm_uplinkcfg}" "enabled" "1"
326 uci_remove "travelmate" "${trm_uplinkcfg}" "con_start" 2>/dev/null
327 uci_remove "travelmate" "${trm_uplinkcfg}" "con_end" 2>/dev/null
328 f_log "info" "uplink '${radio}/${essid}/${bssid:-"-"}' re-enabled after ${expiry} minutes"
329 fi
330 ;;
331 "disabled")
332 uci_set "travelmate" "${trm_uplinkcfg}" "enabled" "0"
333 if [ -s "${trm_ntpfile}" ]; then
334 uci_set "travelmate" "${trm_uplinkcfg}" "con_end" "$(date "+%Y.%m.%d-%H:%M:%S")"
335 fi
336 ;;
337 esac
338 if [ -n "$(uci -q changes "travelmate")" ]; then
339 uci_commit "travelmate"
340 if [ ! -f "${trm_refreshfile}" ]; then
341 printf "%s" "cfg_reload" >"${trm_refreshfile}"
342 fi
343 fi
344 fi
345 f_log "debug" "f_ctrack ::: action: ${action:-"-"}, uplink_config: ${trm_uplinkcfg:-"-"}"
346 }
347
348 # get openvpn information
349 #
350 f_getovpn() {
351 local file instance device
352
353 for file in /etc/openvpn/*.conf /etc/openvpn/*.ovpn; do
354 if [ -f "${file}" ]; then
355 instance="${file##*/}"
356 instance="${instance%.conf}"
357 instance="${instance%.ovpn}"
358 device="$(awk '/^[[:space:]]*dev /{print $2}' "${file}")"
359 [ "${device}" = "tun" ] && device="tun0"
360 [ "${device}" = "tap" ] && device="tap0"
361 if [ -n "${device}" ] && [ -n "${instance}" ] && ! printf "%s" "${trm_ovpninfolist}" | grep -q "${device}"; then
362 trm_ovpninfolist="${trm_ovpninfolist} ${device}&&${instance}"
363 fi
364 fi
365 done
366
367 uci_config() {
368 local device section="${1}"
369
370 device="$(uci_get "openvpn" "${section}" "dev")"
371 [ "${device}" = "tun" ] && device="tun0"
372 [ "${device}" = "tap" ] && device="tap0"
373 if [ -n "${device}" ] && ! printf "%s" "${trm_ovpninfolist}" | grep -q "${device}"; then
374 trm_ovpninfolist="${trm_ovpninfolist} ${device}&&${section}"
375 fi
376 }
377 if [ -f "/etc/config/openvpn" ]; then
378 config_load openvpn
379 config_foreach uci_config "openvpn"
380 fi
381 f_log "debug" "f_getovpn ::: ovpninfolist: ${trm_ovpninfolist:-"-"}"
382 }
383
384 # get logical vpn network interfaces
385 #
386 f_getvpn() {
387 local info proto device iface="${1}"
388
389 proto="$(uci_get "network" "${iface}" "proto")"
390 device="$(uci_get "network" "${iface}" "device")"
391 if [ "${proto}" = "wireguard" ]; then
392 if { [ -z "${trm_vpnifacelist}" ] || printf "%s" "${trm_vpnifacelist}" | grep -q "${iface}"; }; then
393 if ! printf "%s" "${trm_vpninfolist}" | grep -q "${iface}"; then
394 trm_vpninfolist="$(f_trim "${trm_vpninfolist} ${iface}")"
395 fi
396 fi
397 elif [ "${proto}" = "none" ] && [ -n "${device}" ]; then
398 if [ -z "${trm_ovpninfolist}" ]; then
399 f_getovpn
400 fi
401 if { [ -z "${trm_vpnifacelist}" ] || printf "%s" "${trm_vpnifacelist}" | grep -q "${iface}"; }; then
402 for info in ${trm_ovpninfolist}; do
403 if [ "${info%%&&*}" = "${device}" ]; then
404 if ! printf "%s" "${trm_vpninfolist}" | grep -q "${iface}"; then
405 trm_vpninfolist="$(f_trim "${trm_vpninfolist} ${iface}&&${info##*&&}")"
406 break
407 fi
408 fi
409 done
410 fi
411 fi
412 f_log "debug" "f_getvpn ::: iface: ${iface:-"-"}, proto: ${proto:-"-"}, device: ${device:-"-"}, ifacelist: ${trm_vpnifacelist:-"-"}, infolist: ${trm_vpninfolist:-"-"}"
413 }
414
415 # get wan gateway addresses
416 #
417 f_getgw() {
418 local result wan4_if wan4_gw wan6_if wan6_gw
419
420 network_flush_cache
421 network_find_wan wan4_if
422 network_find_wan6 wan6_if
423 network_get_gateway wan4_gw "${wan4_if}"
424 network_get_gateway6 wan6_gw "${wan6_if}"
425 if [ -n "${wan4_gw}" ] || [ -n "${wan6_gw}" ]; then
426 result="${wan4_gw} ${wan6_gw}"
427 fi
428 printf "%s" "${result}"
429 f_log "debug" "f_getgw ::: wan4_gw: ${wan4_gw:-"-"}, wan6_gw: ${wan6_gw:-"-"}, result: ${result:-"-"}"
430 }
431
432 # get uplink config section
433 #
434 f_getcfg() {
435 local t_radio t_essid t_bssid radio="${1}" essid="${2}" bssid="${3}" cnt="0"
436
437 while uci_get "travelmate" "@uplink[${cnt}]" >/dev/null 2>&1; do
438 t_radio="$(uci_get "travelmate" "@uplink[${cnt}]" "device")"
439 t_essid="$(uci_get "travelmate" "@uplink[${cnt}]" "ssid")"
440 t_bssid="$(uci_get "travelmate" "@uplink[${cnt}]" "bssid")"
441 if [ -n "${radio}" ] && [ -n "${essid}" ] &&
442 [ "${t_radio}" = "${radio}" ] && [ "${t_essid}" = "${essid}" ] && [ "${t_bssid}" = "${bssid}" ]; then
443 trm_uplinkcfg="@uplink[${cnt}]"
444 break
445 fi
446 cnt="$((cnt + 1))"
447 done
448 f_log "debug" "f_getcfg ::: status: ${status}, section: ${section}, uplink_config: ${trm_uplinkcfg:-"-"}"
449 }
450
451 # get travelmate option value in 'uplink' sections
452 #
453 f_getval() {
454 local result t_option="${1}"
455
456 if [ -n "${trm_uplinkcfg}" ]; then
457 result="$(uci_get "travelmate" "${trm_uplinkcfg}" "${t_option}")"
458 printf "%s" "${result}"
459 fi
460 f_log "debug" "f_getval ::: option: ${t_option:-"-"}, result: ${result:-"-"}, uplink_config: ${trm_uplinkcfg:-"-"}"
461 }
462
463 # set 'wifi-device' sections
464 #
465 f_setdev() {
466 local disabled radio="${1}"
467
468 disabled="$(uci_get "wireless" "${radio}" "disabled")"
469 if [ "${disabled}" = "1" ]; then
470 uci_set wireless "${radio}" "disabled" "0"
471 fi
472 if [ -n "${trm_radio}" ] && [ -z "${trm_radiolist}" ]; then
473 trm_radiolist="${trm_radio}"
474 elif [ -z "${trm_radio}" ] && ! printf "%s" "${trm_radiolist}" | grep -q "${radio}"; then
475 trm_radiolist="$(f_trim "${trm_radiolist} ${radio}")"
476 fi
477 f_log "debug" "f_setdev ::: radio: ${radio:-"-"}, radio_list(cnf/cur): ${trm_radio:-"-"}/${trm_radiolist:-"-"}, disabled: ${disabled:-"-"}"
478 }
479
480 # set 'wifi-iface' sections
481 #
482 f_setif() {
483 local mode radio essid bssid enabled disabled con_start con_end con_start_expiry con_end_expiry section="${1}" proactive="${2}"
484
485 mode="$(uci_get "wireless" "${section}" "mode")"
486 radio="$(uci_get "wireless" "${section}" "device")"
487 essid="$(uci_get "wireless" "${section}" "ssid")"
488 bssid="$(uci_get "wireless" "${section}" "bssid")"
489 disabled="$(uci_get "wireless" "${section}" "disabled")"
490
491 f_getcfg "${radio}" "${essid}" "${bssid}"
492
493 enabled="$(f_getval "enabled")"
494 con_start="$(f_getval "con_start")"
495 con_end="$(f_getval "con_end")"
496 con_start_expiry="$(f_getval "con_start_expiry")"
497 con_end_expiry="$(f_getval "con_end_expiry")"
498
499 if [ "${enabled}" = "0" ] && [ -n "${con_end}" ] && [ -n "${con_end_expiry}" ] && [ "${con_end_expiry}" != "0" ]; then
500 d1="$(date -d "${con_end}" "+%s")"
501 d2="$(date "+%s")"
502 d3="$(((d2 - d1) / 60))"
503 if [ "${d3}" -ge "${con_end_expiry}" ]; then
504 enabled="1"
505 f_ctrack "end_expiry"
506 fi
507 elif [ "${enabled}" = "1" ] && [ -n "${con_start}" ] && [ -n "${con_start_expiry}" ] && [ "${con_start_expiry}" != "0" ]; then
508 d1="$(date -d "${con_start}" "+%s")"
509 d2="$(date "+%s")"
510 d3="$((d1 + (con_start_expiry * 60)))"
511 if [ "${d2}" -gt "${d3}" ]; then
512 enabled="0"
513 f_ctrack "start_expiry"
514 fi
515 fi
516
517 if [ "${mode}" = "sta" ]; then
518 if [ "${enabled}" = "0" ] || { { [ -z "${disabled}" ] || [ "${disabled}" = "0" ]; } &&
519 { [ "${proactive}" = "0" ] || [ "${trm_ifstatus}" != "true" ]; }; }; then
520 uci_set "wireless" "${section}" "disabled" "1"
521 elif [ "${enabled}" = "1" ] && [ "${disabled}" = "0" ] && [ "${trm_ifstatus}" = "true" ] && [ "${proactive}" = "1" ]; then
522 if [ -z "${trm_activesta}" ]; then
523 trm_activesta="${section}"
524 else
525 uci_set "wireless" "${section}" "disabled" "1"
526 fi
527 fi
528 if [ "${enabled}" = "1" ]; then
529 trm_stalist="$(f_trim "${trm_stalist} ${section}-${radio}")"
530 fi
531 fi
532 f_log "debug" "f_setif ::: enabled: ${enabled}, section: ${section}, active_sta: ${trm_activesta:-"-"}, uplink_config: ${trm_uplinkcfg:-"-"}"
533 }
534
535 # add open uplinks
536 #
537 f_addsta() {
538 local wifi_cfg trm_cfg new_uplink="1" offset="1" radio="${1}" essid="${2}"
539
540 if [ "${trm_maxautoadd}" = "0" ] || [ "${trm_opensta:-0}" -lt "${trm_maxautoadd}" ]; then
541 config_cb() {
542 local type="${1}" name="${2}"
543
544 if [ "${type}" = "wifi-iface" ]; then
545 if [ "$(uci_get "wireless.${name}.ssid")" = "${essid}" ] &&
546 [ "$(uci_get "wireless.${name}.device")" = "${radio}" ]; then
547 new_uplink="0"
548 return 0
549 fi
550 offset="$((offset + 1))"
551 fi
552 }
553 config_load wireless
554 else
555 new_uplink="0"
556 fi
557
558 if [ "${new_uplink}" = "1" ]; then
559 wifi_cfg="trm_uplink$((offset + 1))"
560 while [ -n "$(uci_get "wireless.${wifi_cfg}")" ]; do
561 offset="$((offset + 1))"
562 wifi_cfg="trm_uplink${offset}"
563 done
564 uci -q batch <<-EOC
565 set wireless."${wifi_cfg}"="wifi-iface"
566 set wireless."${wifi_cfg}".mode="sta"
567 set wireless."${wifi_cfg}".network="${trm_iface}"
568 set wireless."${wifi_cfg}".device="${radio}"
569 set wireless."${wifi_cfg}".ssid="${essid}"
570 set wireless."${wifi_cfg}".encryption="none"
571 set wireless."${wifi_cfg}".disabled="1"
572 EOC
573 trm_cfg="$(uci -q add travelmate uplink)"
574 uci -q batch <<-EOC
575 set travelmate."${trm_cfg}".device="${radio}"
576 set travelmate."${trm_cfg}".ssid="${essid}"
577 set travelmate."${trm_cfg}".opensta="1"
578 set travelmate."${trm_cfg}".con_start_expiry="0"
579 set travelmate."${trm_cfg}".con_end_expiry="0"
580 set travelmate."${trm_cfg}".enabled="1"
581 EOC
582 if [ -n "${trm_stdvpnservice}" ] && [ -n "${trm_stdvpniface}" ]; then
583 uci -q batch <<-EOC
584 set travelmate."${trm_cfg}".vpnservice="${trm_stdvpnservice}"
585 set travelmate."${trm_cfg}".vpniface="${trm_stdvpniface}"
586 set travelmate."${trm_cfg}".vpn="1"
587 EOC
588 fi
589 trm_opensta="$((trm_opensta + 1))"
590 uci_commit "travelmate"
591 uci_commit "wireless"
592 f_wifi
593 if [ ! -f "${trm_refreshfile}" ]; then
594 printf "%s" "ui_reload" >"${trm_refreshfile}"
595 fi
596 f_log "info" "open uplink '${radio}/${essid}' added to wireless config"
597 printf "%s" "${wifi_cfg}-${radio}"
598 fi
599 f_log "debug" "f_addsta ::: radio: ${radio:-"-"}, essid: ${essid}, opensta/maxautoadd: ${trm_opensta:-"-"}/${trm_maxautoadd:-"-"}, new_uplink: ${new_uplink}, offset: ${offset}"
600 }
601
602 # check net status
603 #
604 f_net() {
605 local err_msg raw json_raw html_raw html_cp js_cp json_ec json_rc json_cp json_ed result="net nok"
606
607 raw="$(${trm_fetch} --user-agent "${trm_useragent}" --referer "http://www.example.com" --header "Cache-Control: no-cache, no-store, must-revalidate, max-age=0" --write-out "%{json}" --silent --max-time $((trm_maxwait / 6)) "${trm_captiveurl}")"
608 json_raw="${raw#*\{}"
609 html_raw="${raw%%\{*}"
610 if [ -n "${json_raw}" ]; then
611 json_ec="$(printf "%s" "{${json_raw}" | jsonfilter -q -l1 -e '@.exitcode')"
612 json_rc="$(printf "%s" "{${json_raw}" | jsonfilter -q -l1 -e '@.response_code')"
613 json_cp="$(printf "%s" "{${json_raw}" | jsonfilter -q -l1 -e '@.redirect_url' | awk 'BEGIN{FS="/"}{printf "%s",tolower($3)}')"
614 if [ "${json_ec}" = "0" ]; then
615 if [ -n "${json_cp}" ]; then
616 result="net cp '${json_cp}'"
617 else
618 if [ "${json_rc}" = "200" ] || [ "${json_rc}" = "204" ]; then
619 html_cp="$(printf "%s" "${html_raw}" | awk 'match(tolower($0),/^.*<meta[ \t]+http-equiv=['\''"]*refresh.*[ \t;]url=/){print substr(tolower($0),RLENGTH+1)}' | awk 'BEGIN{FS="[:/]"}{printf "%s",$4;exit}')"
620 js_cp="$(printf "%s" "${html_raw}" | awk 'match(tolower($0),/^.*location\.href=['\''"]*/){print substr(tolower($0),RLENGTH+1)}' | awk 'BEGIN{FS="[:/]"}{printf "%s",$4;exit}')"
621 if [ -n "${html_cp}" ]; then
622 result="net cp '${html_cp}'"
623 elif [ -n "${js_cp}" ]; then
624 result="net cp '${js_cp}'"
625 else
626 result="net ok"
627 fi
628 fi
629 fi
630 else
631 err_msg="$(printf "%s" "{${json_raw}" | jsonfilter -q -l1 -e '@.errormsg')"
632 json_ed="$(printf "%s" "{${err_msg}" | awk '/([[:alnum:]_-]{1,63}\.)+[[:alpha:]]+$/{printf "%s",tolower($NF)}')"
633 if [ "${json_ec}" = "6" ]; then
634 if [ -n "${json_ed}" ] && [ "${json_ed}" != "${trm_captiveurl#http*://*}" ]; then
635 result="net cp '${json_ed}'"
636 fi
637 elif [ "${json_ec}" = "28" ]; then
638 if [ -n "$(f_getgw)" ]; then
639 result="net ok"
640 fi
641 fi
642 fi
643 fi
644 printf "%s" "${result}"
645 f_log "debug" "f_net ::: fetch: ${trm_fetch}, timeout: $((trm_maxwait / 6)), cp (json/html/js): ${json_cp:-"-"}/${html_cp:-"-"}/${js_cp:-"-"}, result: ${result}, error (rc/msg): ${json_ec}/${err_msg:-"-"}, url: ${trm_captiveurl}, user_agent: ${trm_useragent}"
646 }
647
648 # check interface status
649 #
650 f_check() {
651 local ifname radio dev_status result login_script login_script_args cp_domain wait_time="0" enabled="1" mode="${1}" status="${2}" sta_radio="${3}" sta_essid="${4}" sta_bssid="${5}"
652
653 if [ "${mode}" = "initial" ] || [ "${mode}" = "dev" ]; then
654 json_get_var station_id "station_id"
655 sta_radio="${station_id%%/*}"
656 sta_essid="${station_id%/*}"
657 sta_essid="${sta_essid#*/}"
658 sta_bssid="${station_id##*/}"
659 sta_bssid="${sta_bssid//-/}"
660 fi
661 f_getcfg "${sta_radio}" "${sta_essid}" "${sta_bssid}"
662
663 if [ "${mode}" != "rev" ] && [ -n "${sta_radio}" ] && [ "${sta_radio}" != "-" ] && [ -n "${sta_essid}" ] && [ "${sta_essid}" != "-" ]; then
664 enabled="$(f_getval "enabled")"
665 fi
666 if { [ "${mode}" != "initial" ] && [ "${mode}" != "dev" ] && [ "${status}" = "false" ]; } ||
667 { [ "${mode}" = "dev" ] && { [ "${status}" = "false" ] || { [ "${trm_ifstatus}" != "${status}" ] && [ "${enabled}" = "0" ]; }; }; }; then
668 f_wifi
669 fi
670 while [ "${wait_time}" -le "${trm_maxwait}" ]; do
671 [ "${wait_time}" -gt "0" ] && sleep 1
672 wait_time="$((wait_time + 1))"
673 dev_status="$(ubus -S call network.wireless status 2>/dev/null)"
674 if [ -n "${dev_status}" ]; then
675 if [ "${mode}" = "dev" ]; then
676 if [ "${trm_ifstatus}" != "${status}" ]; then
677 trm_ifstatus="${status}"
678 f_jsnup
679 fi
680 if [ "${status}" = "false" ]; then
681 sleep "$((trm_maxwait / 5))"
682 fi
683 break
684 elif [ "${mode}" = "rev" ]; then
685 unset trm_connection
686 trm_ifstatus="${status}"
687 break
688 else
689 ifname="$(printf "%s" "${dev_status}" | jsonfilter -q -l1 -e '@.*.interfaces[@.config.mode="sta"].ifname')"
690 if [ -n "${ifname}" ] && [ "${enabled}" = "1" ]; then
691 trm_ifquality="$(${trm_iwinfo} "${ifname}" info 2>/dev/null | awk -F '[ ]' '/Link Quality: [0-9]+\/[0-9]+/{split($NF,var0,"/");printf "%i\n",(var0[1]*100/var0[2])}')"
692 if [ -z "${trm_ifquality}" ]; then
693 continue
694 elif [ "${trm_ifquality}" -ge "${trm_minquality}" ]; then
695 trm_ifstatus="$(ubus -S call network.interface dump 2>/dev/null | jsonfilter -q -l1 -e "@.interface[@.device=\"${ifname}\"].up")"
696 if [ "${trm_ifstatus}" = "true" ]; then
697 result="$(f_net)"
698 if [ "${trm_captive}" = "1" ]; then
699 while true; do
700 cp_domain="$(printf "%s" "${result}" | awk -F '['\''| ]' '/^net cp/{printf "%s",$4}')"
701 if [ -x "/etc/init.d/dnsmasq" ] && [ -f "/etc/config/dhcp" ] &&
702 [ -n "${cp_domain}" ] && ! uci_get "dhcp" "@dnsmasq[0]" "rebind_domain" | grep -q "${cp_domain}"; then
703 uci_add_list "dhcp" "@dnsmasq[0]" "rebind_domain" "${cp_domain}"
704 uci_commit "dhcp"
705 /etc/init.d/dnsmasq reload
706 f_log "info" "captive portal domain '${cp_domain}' added to to dhcp rebind whitelist"
707 else
708 break
709 fi
710 result="$(f_net)"
711 done
712 if [ -n "${cp_domain}" ]; then
713 trm_connection="${result:-"-"}/${trm_ifquality}"
714 f_jsnup
715 login_script="$(f_getval "script")"
716 if [ -x "${login_script}" ]; then
717 login_script_args="$(f_getval "script_args")"
718 "${login_script}" ${login_script_args} >/dev/null 2>&1
719 rc="${?}"
720 if [ "${rc}" = "255" ]; then
721 f_log "info" "captive portal login script for '${cp_domain}' failed with rc '${rc}'"
722 unset trm_connection
723 trm_ifstatus="${status}"
724 f_jsnup
725 break
726 else
727 f_log "info" "captive portal login script for '${cp_domain}' has been finished with rc '${rc}'"
728 if [ "${rc}" = "0" ]; then
729 result="$(f_net)"
730 fi
731 fi
732 fi
733 fi
734 fi
735 if [ "${result}" = "net nok" ]; then
736 f_vpn "disable"
737 if [ "${trm_netcheck}" = "1" ]; then
738 f_log "info" "uplink has no internet"
739 trm_ifstatus="${status}"
740 f_jsnup
741 break
742 fi
743 fi
744 trm_connection="${result:-"-"}/${trm_ifquality}"
745 f_jsnup
746 break
747 fi
748 elif [ -n "${trm_connection}" ] && { [ "${trm_netcheck}" = "1" ] || [ "${mode}" = "initial" ]; }; then
749 f_log "info" "uplink is out of range (${trm_ifquality}/${trm_minquality})"
750 f_vpn "disable"
751 unset trm_connection
752 trm_ifstatus="${status}"
753 f_ctrack "end"
754 f_jsnup
755 break
756 elif [ "${mode}" = "initial" ] || [ "${mode}" = "sta" ]; then
757 unset trm_connection
758 trm_ifstatus="${status}"
759 f_jsnup
760 break
761 fi
762 elif [ -n "${trm_connection}" ]; then
763 f_vpn "disable"
764 unset trm_connection
765 trm_ifstatus="${status}"
766 f_jsnup
767 break
768 elif [ "${mode}" = "initial" ]; then
769 trm_ifstatus="${status}"
770 f_jsnup
771 break
772 fi
773 fi
774 fi
775 if [ "${mode}" = "initial" ]; then
776 trm_ifstatus="${status}"
777 f_jsnup
778 break
779 fi
780 done
781 f_log "debug" "f_check ::: mode: ${mode}, name: ${ifname:-"-"}, status: ${trm_ifstatus}, enabled: ${enabled}, connection: ${trm_connection:-"-"}, wait: ${wait_time}, max_wait: ${trm_maxwait}, min_quality: ${trm_minquality}, captive: ${trm_captive}, netcheck: ${trm_netcheck}"
782 }
783
784 # update runtime information
785 #
786 f_jsnup() {
787 local vpn vpn_iface section last_date sta_iface sta_radio sta_essid sta_bssid sta_mac dev_status status="${trm_ifstatus}" ntp_done="0" vpn_done="0" mail_done="0"
788
789 if [ "${status}" = "true" ]; then
790 status="connected (${trm_connection:-"-"})"
791 dev_status="$(ubus -S call network.wireless status 2>/dev/null)"
792 section="$(printf "%s" "${dev_status}" | jsonfilter -q -l1 -e '@.*.interfaces[@.config.mode="sta"].section')"
793 if [ -n "${section}" ]; then
794 sta_iface="$(uci_get "wireless" "${section}" "network")"
795 sta_radio="$(uci_get "wireless" "${section}" "device")"
796 sta_essid="$(uci_get "wireless" "${section}" "ssid")"
797 sta_bssid="$(uci_get "wireless" "${section}" "bssid")"
798 sta_mac="$(f_mac "get" "${section}")"
799 f_getcfg "${sta_radio}" "${sta_essid}" "${sta_bssid}"
800 fi
801 json_get_var last_date "last_run"
802
803 vpn="$(f_getval "vpn")"
804 if [ "${trm_vpn}" = "1" ] && [ -n "${trm_vpninfolist}" ] && [ "${vpn}" = "1" ] && [ -f "${trm_vpnfile}" ]; then
805 vpn_iface="$(f_getval "vpniface")"
806 vpn_done="1"
807 fi
808 elif [ "${status}" = "error" ]; then
809 unset trm_connection
810 status="program error"
811 else
812 unset trm_connection
813 status="running (not connected)"
814 fi
815 if [ -z "${last_date}" ]; then
816 last_date="$(date "+%Y.%m.%d-%H:%M:%S")"
817 fi
818 if [ -s "${trm_ntpfile}" ]; then
819 ntp_done="1"
820 fi
821 if [ "${trm_mail}" = "1" ] && [ -f "${trm_mailfile}" ]; then
822 mail_done="1"
823 fi
824 json_add_string "travelmate_status" "${status}"
825 json_add_string "travelmate_version" "${trm_ver}"
826 json_add_string "station_id" "${sta_radio:-"-"}/${sta_essid:-"-"}/${sta_bssid:-"-"}"
827 json_add_string "station_mac" "${sta_mac:-"-"}"
828 json_add_string "station_interfaces" "${sta_iface:-"-"}, ${vpn_iface:-"-"}"
829 json_add_string "wpa_flags" "${trm_wpaflags:-"-"}"
830 json_add_string "run_flags" "captive: $(f_char ${trm_captive}), proactive: $(f_char ${trm_proactive}), netcheck: $(f_char ${trm_netcheck}), autoadd: $(f_char ${trm_autoadd}), randomize: $(f_char ${trm_randomize})"
831 json_add_string "ext_hooks" "ntp: $(f_char ${ntp_done}), vpn: $(f_char ${vpn_done}), mail: $(f_char ${mail_done})"
832 json_add_string "last_run" "${last_date}"
833 json_add_string "system" "${trm_sysver}"
834 json_dump >"${trm_rtfile}"
835
836 if [ "${status%% (net ok/*}" = "connected" ] && [ "${trm_mail}" = "1" ] && [ -x "${trm_mailpgm}" ] && [ "${ntp_done}" = "1" ] && [ "${mail_done}" = "0" ]; then
837 if [ "${trm_vpn}" != "1" ] || [ "${vpn}" != "1" ] || [ -z "${trm_vpninfolist}" ] || [ "${vpn_done}" = "1" ]; then
838 : >"${trm_mailfile}"
839 "${trm_mailpgm}" >/dev/null 2>&1
840 fi
841 fi
842 f_log "debug" "f_jsnup ::: section: ${section:-"-"}, status: ${status:-"-"}, sta_iface: ${sta_iface:-"-"}, sta_radio: ${sta_radio:-"-"}, sta_essid: ${sta_essid:-"-"}, sta_bssid: ${sta_bssid:-"-"}, ntp: ${ntp_done}, vpn: ${vpn:-"0"}/${vpn_done}, mail: ${trm_mail}/${mail_done}"
843 }
844
845 # write to syslog
846 #
847 f_log() {
848 local class="${1}" log_msg="${2}"
849
850 if [ -n "${log_msg}" ] && { [ "${class}" != "debug" ] || [ "${trm_debug}" = "1" ]; }; then
851 if [ -x "${trm_logger}" ]; then
852 "${trm_logger}" -p "${class}" -t "trm-${trm_ver}[${$}]" "${log_msg}"
853 else
854 printf "%s %s %s\n" "${class}" "trm-${trm_ver}[${$}]" "${log_msg}"
855 fi
856 if [ "${class}" = "err" ]; then
857 trm_ifstatus="error"
858 f_jsnup
859 : >"${trm_pidfile}"
860 exit 1
861 fi
862 fi
863 }
864
865 # main function for connection handling
866 #
867 f_main() {
868 local radio cnt retrycnt scan_dev scan_list scan_essid scan_bssid scan_open scan_quality
869 local station_id section sta sta_essid sta_bssid sta_radio sta_mac config_essid config_bssid config_radio
870
871 f_check "initial" "false"
872 f_log "debug" "f_main-1 ::: status: ${trm_ifstatus}, proactive: ${trm_proactive}"
873 if [ "${trm_ifstatus}" != "true" ] || [ "${trm_proactive}" = "1" ]; then
874 config_load wireless
875 config_foreach f_setif wifi-iface "${trm_proactive}"
876 if [ "${trm_ifstatus}" = "true" ] && [ -n "${trm_activesta}" ] && [ "${trm_proactive}" = "1" ]; then
877 json_get_var station_id "station_id"
878 config_radio="${station_id%%/*}"
879 config_essid="${station_id%/*}"
880 config_essid="${config_essid#*/}"
881 config_bssid="${station_id##*/}"
882 config_bssid="${config_bssid//-/}"
883 f_check "dev" "true"
884 f_log "debug" "f_main-2 ::: config_radio: ${config_radio}, config_essid: \"${config_essid}\", config_bssid: ${config_bssid:-"-"}"
885 else
886 uci_commit "wireless"
887 f_check "dev" "false"
888 fi
889 f_log "debug" "f_main-3 ::: radio_list: ${trm_radiolist:-"-"}, sta_list: ${trm_stalist:-"-"}"
890
891 # radio loop
892 #
893 for radio in ${trm_radiolist}; do
894 if ! printf "%s" "${trm_stalist}" | grep -q "\\-${radio}"; then
895 if [ "${trm_autoadd}" = "0" ]; then
896 f_log "info" "no enabled station on radio '${radio}'"
897 continue
898 fi
899 fi
900 scan_list=""
901
902 # station loop
903 #
904 for sta in ${trm_stalist:-"${radio}"}; do
905 if [ "${sta}" != "${radio}" ]; then
906 section="${sta%%-*}"
907 sta_radio="$(uci_get "wireless" "${section}" "device")"
908 sta_essid="$(uci_get "wireless" "${section}" "ssid")"
909 sta_bssid="$(uci_get "wireless" "${section}" "bssid")"
910 sta_mac="$(f_mac "get" "${section}")"
911 if [ -z "${sta_radio}" ] || [ -z "${sta_essid}" ]; then
912 f_log "info" "invalid wireless section '${section}'"
913 continue
914 fi
915 if [ -n "${trm_connection}" ] && [ "${radio}" = "${config_radio}" ] && [ "${sta_radio}" = "${config_radio}" ] &&
916 [ "${sta_essid}" = "${config_essid}" ] && [ "${sta_bssid}" = "${config_bssid}" ]; then
917 f_ctrack "refresh"
918 f_vpn "enable_keep"
919 f_log "debug" "f_main-4 ::: config_radio: ${config_radio}, config_essid: ${config_essid}, config_bssid: ${config_bssid:-"-"}"
920 return 0
921 fi
922 f_log "debug" "f_main-5 ::: sta_radio: ${sta_radio}, sta_essid: \"${sta_essid}\", sta_bssid: ${sta_bssid:-"-"}"
923 fi
924 if [ -z "${scan_list}" ]; then
925 scan_dev="$(ubus -S call network.wireless status 2>/dev/null | jsonfilter -q -l1 -e "@.${radio}.interfaces[0].ifname")"
926 scan_list="$("${trm_iwinfo}" "${scan_dev:-${radio}}" scan 2>/dev/null |
927 awk 'BEGIN{FS="[[:space:]]"}/Address:/{var1=$NF}/ESSID:/{var2="";for(i=12;i<=NF;i++)if(var2==""){var2=$i}else{var2=var2" "$i}}
928 /Quality:/{split($NF,var0,"/")}/Encryption:/{if($NF=="none"){var3="+"}else{var3="-"};
929 printf "%i %s %s %s\n",(var0[1]*100/var0[2]),var3,var1,var2}' | sort -rn | head -qn "${trm_maxscan}")"
930 f_log "debug" "f_main-6 ::: radio: ${radio}, scan_device: ${scan_dev}, scan_max: ${trm_maxscan}"
931 if [ -z "${scan_list}" ]; then
932 f_log "info" "no scan results on '${radio}'"
933 continue 2
934 fi
935 fi
936
937 # scan loop
938 #
939 while read -r scan_quality scan_open scan_bssid scan_essid; do
940 if [ -n "${scan_quality}" ] && [ -n "${scan_open}" ] && [ -n "${scan_bssid}" ] && [ -n "${scan_essid}" ]; then
941 f_log "debug" "f_main-7 ::: radio(sta/scan): ${sta_radio}/${radio}, essid(sta/scan): \"${sta_essid}\"/${scan_essid}, bssid(sta/scan): ${sta_bssid}/${scan_bssid}, quality(min/scan): ${trm_minquality}/${scan_quality}, open: ${scan_open}"
942 if [ "${scan_quality}" -ge "${trm_minquality}" ]; then
943 if [ "${trm_autoadd}" = "1" ] && [ "${scan_open}" = "+" ] && [ "${scan_essid}" != "unknown" ]; then
944 open_essid="${scan_essid%?}"
945 open_essid="${open_essid:1}"
946 result="$(f_addsta "${radio}" "${open_essid}")"
947 if [ -n "${result}" ]; then
948 section="${result%%-*}"
949 sta_radio="$(uci_get "wireless" "${section}" "device")"
950 sta_essid="$(uci_get "wireless" "${section}" "ssid")"
951 sta_bssid=""
952 sta_mac=""
953 fi
954 fi
955 if { { [ "${scan_essid}" = "\"${sta_essid}\"" ] && { [ -z "${sta_bssid}" ] || [ "${scan_bssid}" = "${sta_bssid}" ]; }; } ||
956 { [ "${scan_bssid}" = "${sta_bssid}" ] && [ "${scan_essid}" = "unknown" ]; }; } && [ "${radio}" = "${sta_radio}" ]; then
957 if [ -n "${config_radio}" ]; then
958 f_vpn "disable"
959 uci_set "wireless" "${trm_activesta}" "disabled" "1"
960 uci_commit "wireless"
961 f_check "rev" "false"
962 f_ctrack "end"
963 f_log "info" "uplink connection terminated '${config_radio}/${config_essid}/${config_bssid:-"-"}'"
964 unset config_radio config_essid config_bssid
965 fi
966
967 # retry loop
968 #
969 retrycnt="1"
970 f_getcfg "${sta_radio}" "${sta_essid}" "${sta_bssid}"
971 while [ "${retrycnt}" -le "${trm_maxretry}" ]; do
972 sta_mac="$(f_mac "set" "${section}")"
973 uci_set "wireless" "${section}" "disabled" "0"
974 f_check "sta" "false" "${sta_radio}" "${sta_essid}" "${sta_bssid}"
975 if [ "${trm_ifstatus}" = "true" ]; then
976 rm -f "${trm_mailfile}"
977 uci_commit "wireless"
978 f_ctrack "start"
979 f_log "info" "connected to uplink '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' with mac '${sta_mac:-"-"}' (${retrycnt}/${trm_maxretry})"
980 f_vpn "enable"
981 return 0
982 else
983 uci -q revert "wireless"
984 f_check "rev" "false"
985 if [ "${retrycnt}" = "${trm_maxretry}" ]; then
986 f_ctrack "disabled"
987 f_log "info" "uplink has been disabled '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' (${retrycnt}/${trm_maxretry})"
988 break 2
989 else
990 f_jsnup
991 f_log "info" "can't connect to uplink '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' (${retrycnt}/${trm_maxretry})"
992 fi
993 fi
994 retrycnt="$((retrycnt + 1))"
995 sleep "$((trm_maxwait / 6))"
996 done
997 fi
998 fi
999 fi
1000 done <<-EOV
1001 ${scan_list}
1002 EOV
1003 done
1004 done
1005 fi
1006 }
1007
1008 # source required system libraries
1009 #
1010 if [ -r "/lib/functions.sh" ] && [ -r "/lib/functions/network.sh" ] && [ -r "/usr/share/libubox/jshn.sh" ]; then
1011 . "/lib/functions.sh"
1012 . "/lib/functions/network.sh"
1013 . "/usr/share/libubox/jshn.sh"
1014 else
1015 f_log "err" "system libraries not found"
1016 fi
1017
1018 # force ntp restart/sync
1019 #
1020 if [ -f "/etc/init.d/sysntpd" ] && [ ! -s "${trm_ntpfile}" ]; then
1021 /etc/init.d/sysntpd restart >/dev/null 2>&1
1022 f_log "debug" "ntp time sync requested"
1023 fi
1024
1025 # control travelmate actions
1026 #
1027 while true; do
1028 if [ "${trm_action}" = "stop" ]; then
1029 if [ -s "${trm_pidfile}" ]; then
1030 f_log "info" "travelmate instance stopped ::: action: ${trm_action}, pid: $(cat ${trm_pidfile} 2>/dev/null)"
1031 : >"${trm_rtfile}"
1032 : >"${trm_pidfile}"
1033 fi
1034 break
1035 elif [ -n "${trm_action}" ]; then
1036 f_log "info" "travelmate instance started ::: action: ${trm_action}, pid: ${$}"
1037 f_env
1038 f_main
1039 unset trm_action
1040 fi
1041 while true; do
1042 sleep "${trm_timeout}" 0
1043 rc="${?}"
1044 if [ "${rc}" != "0" ]; then
1045 if [ -z "$(f_getgw)" ]; then
1046 rc="0"
1047 fi
1048 fi
1049 if [ "${rc}" = "0" ]; then
1050 break
1051 fi
1052 done
1053 json_cleanup
1054 f_env
1055 f_main
1056 done