1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
|
#!/bin/sh
# travelmate, a wlan connection manager for travel router
# written by Dirk Brenken (dev@brenken.org)
# This is free software, licensed under the GNU General Public License v3.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# (s)hellcheck exceptions
# shellcheck disable=1091 disable=2039 disable=2143 disable=2181 disable=2188
# set initial defaults
#
LC_ALL=C
PATH="/usr/sbin:/usr/bin:/sbin:/bin"
trm_ver="1.5.1"
trm_enabled=0
trm_debug=0
trm_iface="trm_wwan"
trm_captive=1
trm_proactive=1
trm_netcheck=0
trm_autoadd=0
trm_captiveurl="http://captive.apple.com"
trm_scanbuffer=1024
trm_minquality=35
trm_maxretry=5
trm_maxwait=30
trm_timeout=60
trm_listexpiry=0
trm_radio=""
trm_connection=""
trm_rtfile="/tmp/trm_runtime.json"
trm_fetch="$(command -v uclient-fetch)"
trm_iwinfo="$(command -v iwinfo)"
trm_wpa="$(command -v wpa_supplicant)"
trm_action="${1:-"start"}"
trm_pidfile="/var/run/travelmate.pid"
# trim leading and trailing whitespace characters
#
f_trim()
{
local IFS trim="${1}"
trim="${trim#"${trim%%[![:space:]]*}"}"
trim="${trim%"${trim##*[![:space:]]}"}"
printf '%s' "${trim}"
}
# load travelmate environment
#
f_envload()
{
local IFS check wpa_checks
# (re-)initialize global list variables
#
unset trm_devlist trm_stalist trm_radiolist trm_active_sta
# get system information
#
trm_sysver="$(ubus -S call system board 2>/dev/null | jsonfilter -e '@.model' -e '@.release.description' | \
awk 'BEGIN{ORS=", "}{print $0}' | awk '{print substr($0,1,length($0)-2)}')"
# get wpa_supplicant capabilities
#
wpa_checks="eap sae owe"
for check in ${wpa_checks}
do
if [ -x "${trm_wpa}" ]
then
eval "trm_${check}check=\"$("${trm_wpa}" -v${check} >/dev/null 2>&1; printf "%u" "${?}")\""
else
eval "trm_${check}check=\"1\""
fi
done
# load config and check 'enabled' option
#
config_cb()
{
local name="${1}" type="${2}"
if [ "${name}" = "travelmate" ] && [ "${type}" = "global" ]
then
option_cb()
{
local option="${1}" value="${2}"
eval "${option}=\"${value}\""
}
else
option_cb()
{
return 0
}
fi
}
config_load travelmate
if [ "${trm_enabled}" -ne 1 ]
then
f_log "info" "travelmate is currently disabled, please set 'trm_enabled' to '1' to use this service"
exit 0
fi
# validate input ranges
#
if [ "${trm_minquality}" -lt 20 ] || [ "${trm_minquality}" -gt 80 ]
then
trm_minquality=35
fi
if [ "${trm_listexpiry}" -lt 0 ] || [ "${trm_listexpiry}" -gt 300 ]
then
trm_listexpiry=0
fi
if [ "${trm_maxretry}" -lt 1 ] || [ "${trm_maxretry}" -gt 10 ]
then
trm_maxretry=5
fi
if [ "${trm_maxwait}" -lt 20 ] || [ "${trm_maxwait}" -gt 40 ] || [ "${trm_maxwait}" -ge "${trm_timeout}" ]
then
trm_maxwait=30
fi
if [ "${trm_timeout}" -lt 30 ] || [ "${trm_timeout}" -gt 300 ] || [ "${trm_timeout}" -le "${trm_maxwait}" ]
then
trm_timeout=60
fi
# load json runtime file
#
json_load_file "${trm_rtfile}" >/dev/null 2>&1
json_select data >/dev/null 2>&1
if [ "${?}" -ne 0 ]
then
> "${trm_rtfile}"
json_init
json_add_object "data"
fi
}
# prepare the 'wifi-device' sections
#
f_prepdev()
{
local IFS disabled config="${1}"
disabled="$(uci_get "wireless" "${config}" "disabled")"
if [ "${disabled}" = "1" ]
then
uci_set wireless "${config}" disabled 0
fi
f_log "debug" "f_prepdev ::: config: ${config}, disabled: ${disabled}"
}
# prepare the 'wifi-iface' sections
#
f_prepif()
{
local IFS mode network radio encryption eaptype disabled config="${1}" proactive="${2}"
mode="$(uci_get "wireless" "${config}" "mode")"
network="$(uci_get "wireless" "${config}" "network")"
radio="$(uci_get "wireless" "${config}" "device")"
encryption="$(uci_get "wireless" "${config}" "encryption")"
eaptype="$(uci_get "wireless" "${config}" "eap_type")"
disabled="$(uci_get "wireless" "${config}" "disabled")"
if [ -n "${config}" ] && [ -n "${radio}" ] && [ -n "${mode}" ] && [ -n "${network}" ]
then
if [ -z "${trm_radio}" ] && [ -z "$(printf "%s" "${trm_radiolist}" | grep -Fo "${radio}")" ]
then
trm_radiolist="$(f_trim "${trm_radiolist} ${radio}")"
elif [ -n "${trm_radio}" ] && [ -z "${trm_radiolist}" ]
then
trm_radiolist="$(f_trim "$(printf "%s" "${trm_radio}" | \
awk '{while(match(tolower($0),/radio[0-9]/)){ORS=" ";print substr(tolower($0),RSTART,RLENGTH);$0=substr($0,RSTART+RLENGTH)}}')")"
fi
if [ "${mode}" = "sta" ] && [ "${network}" = "${trm_iface}" ]
then
if { [ -z "${disabled}" ] || [ "${disabled}" = "0" ]; } && { [ "${proactive}" -eq 0 ] || [ "${trm_ifstatus}" != "true" ]; }
then
uci_set wireless "${config}" disabled 1
elif [ "${disabled}" = "0" ] && [ "${trm_ifstatus}" = "true" ] && [ "${proactive}" -eq 1 ]
then
if [ -z "${trm_active_sta}" ]
then
trm_active_sta="${config}"
else
uci_set wireless "${config}" disabled 1
fi
fi
if [ -z "${eaptype}" ] || { [ -n "${eaptype}" ] && [ "${trm_eapcheck}" -eq 0 ]; }
then
if { [ "${encryption%-*}" != "sae" ] && [ "${encryption%-*}" != "wpa3" ] && [ "${encryption}" != "owe" ]; } || \
{ { [ "${encryption%-*}" = "sae" ] || [ "${encryption%-*}" = "wpa3" ]; } && [ "${trm_saecheck}" -eq 0 ]; } || \
{ [ "${encryption}" = "owe" ] && [ "${trm_owecheck}" -eq 0 ]; }
then
trm_stalist="$(f_trim "${trm_stalist} ${config}-${radio}")"
fi
fi
fi
fi
f_log "debug" "f_prepif ::: config: ${config}, mode: ${mode}, network: ${network}, radio: ${radio}, trm_radio: ${trm_radio:-"-"}, trm_active_sta: ${trm_active_sta:-"-"}, proactive: ${proactive}, trm_eapcheck: ${trm_eapcheck:-"-"}, trm_saecheck: ${trm_saecheck:-"-"}, trm_owecheck: ${trm_owecheck:-"-"}, disabled: ${disabled}"
}
# check net status
#
f_net()
{
local IFS result
result="$(${trm_fetch} --timeout=$((trm_maxwait/6)) "${trm_captiveurl}" -O /dev/null 2>&1 | \
awk '/^Failed to redirect|^Redirected/{printf "%s" "net cp \047"$NF"\047";exit}/^Download completed/{printf "%s" "net ok";exit}/^Failed|Connection error/{printf "%s" "net nok";exit}')"
printf "%s" "${result}"
f_log "debug" "f_net ::: fetch: ${trm_fetch}, timeout: $((trm_maxwait/6)), url: ${trm_captiveurl}, result: ${result}"
}
# check interface status
#
f_check()
{
local IFS ifname radio dev_status config sta_essid sta_bssid result uci_essid uci_bssid login_command login_command_args wait_time mode="${1}" status="${2:-"false"}" cp_domain="${3:-"false"}"
if [ "${mode}" != "initial" ] && [ "${status}" = "false" ]
then
ubus call network reload
wait_time=$((trm_maxwait/6))
sleep "${wait_time}"
fi
wait_time=1
while [ "${wait_time}" -le "${trm_maxwait}" ]
do
dev_status="$(ubus -S call network.wireless status 2>/dev/null)"
if [ -n "${dev_status}" ]
then
if [ "${mode}" = "dev" ]
then
if [ "${trm_ifstatus}" != "${status}" ]
then
trm_ifstatus="${status}"
f_jsnup
fi
for radio in ${trm_radiolist}
do
result="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e "@.${radio}.up")"
if [ "${result}" = "true" ] && [ -z "$(printf "%s" "${trm_devlist}" | grep -Fo "${radio}")" ]
then
trm_devlist="$(f_trim "${trm_devlist} ${radio}")"
fi
done
if [ "${trm_devlist}" = "${trm_radiolist}" ] || [ "${wait_time}" -eq "${trm_maxwait}" ]
then
ifname="${trm_devlist}"
break
else
unset trm_devlist
fi
elif [ "${mode}" = "rev" ]
then
break
else
ifname="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].ifname')"
if [ -n "${ifname}" ]
then
trm_ifquality="$(${trm_iwinfo} "${ifname}" info 2>/dev/null | awk -F "[ ]" '/Link Quality:/{split($NF,var0,"/");printf "%i\n",(var0[1]*100/var0[2])}')"
if [ "${mode}" = "initial" ] && [ "${trm_captive}" -eq 1 ]
then
result="$(f_net)"
if [ "${cp_domain}" = "true" ]
then
cp_domain="$(printf "%s" "${result}" | awk -F "[\\'| ]" '/^net cp/{printf "%s" $4}')"
uci_essid="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].config.ssid')"
uci_essid="${uci_essid//[^[:alnum:]_]/_}"
uci_bssid="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].config.bssid')"
uci_bssid="${uci_bssid//[^[:alnum:]_]/_}"
fi
fi
if [ "${trm_ifquality}" -ge "${trm_minquality}" ] && [ "${result}" != "net nok" ]
then
trm_ifstatus="$(ubus -S call network.interface dump 2>/dev/null | jsonfilter -l1 -e "@.interface[@.device=\"${ifname}\"].up")"
if [ "${trm_ifstatus}" = "true" ]
then
if [ "${mode}" = "sta" ] && [ "${trm_captive}" -eq 1 ]
then
while true
do
result="$(f_net)"
cp_domain="$(printf "%s" "${result}" | awk -F "[\\'| ]" '/^net cp/{printf "%s" $4}')"
uci_essid="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].config.ssid')"
uci_essid="${uci_essid//[^[:alnum:]_]/_}"
uci_bssid="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].config.bssid')"
uci_bssid="${uci_bssid//[^[:alnum:]_]/_}"
if [ "${trm_netcheck}" -eq 1 ] && [ "${result}" = "net nok" ]
then
trm_ifstatus="${status}"
f_jsnup
break 2
fi
if [ -z "${cp_domain}" ] || [ -n "$(uci_get "dhcp" "@dnsmasq[0]" "rebind_domain" | grep -Fo "${cp_domain}")" ]
then
break
fi
uci -q add_list dhcp.@dnsmasq[0].rebind_domain="${cp_domain}"
f_log "info" "captive portal domain '${cp_domain}' added to to dhcp rebind whitelist"
if [ -z "$(uci_get "travelmate" "${trm_radio}_${uci_essid}_${uci_bssid}")" ]
then
uci_add travelmate "login" "${trm_radio}_${uci_essid}_${uci_bssid}"
uci_set travelmate "${trm_radio}_${uci_essid}_${uci_bssid}" "command" "none"
f_log "info" "captive portal login section '${trm_radio}_${uci_essid}_${uci_bssid}' added to travelmate config section"
fi
done
if [ -n "$(uci -q changes "dhcp")" ]
then
uci_commit "dhcp"
/etc/init.d/dnsmasq reload
fi
if [ -n "$(uci -q changes "travelmate")" ]
then
uci_commit "travelmate"
fi
fi
if [ -n "${cp_domain}" ] && [ "${cp_domain}" != "false" ] && [ -n "${uci_essid}" ] && [ "${trm_captive}" -eq 1 ]
then
trm_connection="${result:-"-"}/${trm_ifquality}"
f_jsnup
login_command="$(uci_get "travelmate" "${trm_radio}_${uci_essid}_${uci_bssid}" "command")"
if [ -x "${login_command}" ]
then
login_command_args="$(uci_get "travelmate" "${trm_radio}_${uci_essid}_${uci_bssid}" "command_args")"
"${login_command}" ${login_command_args} >/dev/null 2>&1
rc=${?}
f_log "info" "captive portal login '${login_command:0:40} ${login_command_args:0:20}' for '${cp_domain}' has been executed with rc '${rc}'"
if [ "${rc}" -eq 0 ]
then
result="$(f_net)"
fi
fi
fi
trm_connection="${result:-"-"}/${trm_ifquality}"
f_jsnup
break
fi
elif [ -n "${trm_connection}" ]
then
sta_essid="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].*.ssid')"
sta_bssid="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].*.bssid')"
if [ "${trm_ifquality}" -lt "${trm_minquality}" ]
then
unset trm_connection
trm_ifstatus="${status}"
f_log "info" "uplink '${sta_essid:-"-"}/${sta_bssid:-"-"}' is out of range (${trm_ifquality}/${trm_minquality})"
elif [ "${trm_netcheck}" -eq 1 ] && [ "${result}" = "net nok" ]
then
unset trm_connection
trm_ifstatus="${status}"
f_log "info" "uplink '${sta_essid:-"-"}/${sta_bssid:-"-"}' has no internet (${result})"
fi
f_jsnup
break
elif [ "${mode}" = "initial" ]
then
f_jsnup
break
fi
elif [ -n "${trm_connection}" ]
then
unset trm_connection
trm_ifstatus="${status}"
f_jsnup
break
elif [ "${mode}" = "initial" ]
then
f_jsnup
break
fi
fi
fi
wait_time=$((wait_time+1))
sleep 1
done
f_log "debug" "f_check ::: mode: ${mode}, name: ${ifname:-"-"}, status: ${trm_ifstatus}, connection: ${trm_connection:-"-"}, wait: ${wait_time}, max_wait: ${trm_maxwait}, min_quality: ${trm_minquality}, captive: ${trm_captive}, netcheck: ${trm_netcheck}"
}
# update runtime information
#
f_jsnup()
{
local IFS config d1 d2 d3 last_date last_station sta_iface sta_radio sta_essid sta_bssid last_status dev_status wpa_status status="${trm_ifstatus}" faulty_list faulty_station="${1}"
dev_status="$(ubus -S call network.wireless status 2>/dev/null)"
if [ -n "${dev_status}" ]
then
config="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].section')"
if [ -n "${config}" ]
then
sta_iface="$(uci_get "wireless" "${config}" "network")"
sta_radio="$(uci_get "wireless" "${config}" "device")"
sta_essid="$(uci_get "wireless" "${config}" "ssid")"
sta_bssid="$(uci_get "wireless" "${config}" "bssid")"
fi
fi
json_get_var last_date "last_rundate"
json_get_var last_station "station_id"
if [ "${status}" = "true" ]
then
status="connected (${trm_connection:-"-"})"
json_get_var last_status "travelmate_status"
if [ "${last_status}" = "running / not connected" ] || [ "${last_station}" != "${sta_radio:-"-"}/${sta_essid:-"-"}/${sta_bssid:-"-"}" ]
then
last_date="$(date "+%Y.%m.%d-%H:%M:%S")"
fi
elif [ "${status}" = "error" ]
then
unset trm_connection
status="program error"
else
unset trm_connection
status="running / not connected"
fi
if [ -z "${last_date}" ]
then
last_date="$(date "+%Y.%m.%d-%H:%M:%S")"
fi
json_get_var faulty_list "faulty_stations"
if [ -n "${faulty_list}" ] && [ "${trm_listexpiry}" -gt 0 ]
then
d1="$(date -d "${last_date}" "+%s")"
d2="$(date "+%s")"
d3=$(((d2 - d1)/60))
if [ "${d3}" -ge "${trm_listexpiry}" ]
then
faulty_list=""
fi
fi
if [ -n "${faulty_station}" ]
then
if [ -z "$(printf "%s" "${faulty_list}" | grep -Fo "${faulty_station}")" ]
then
faulty_list="$(f_trim "${faulty_list} ${faulty_station}")"
last_date="$(date "+%Y.%m.%d-%H:%M:%S")"
fi
fi
if [ "${trm_eapcheck}" -eq 0 ]
then
wpa_status="EAP"
else
wpa_status="-"
fi
if [ "${trm_saecheck}" -eq 0 ]
then
wpa_status="${wpa_status}/SAE"
else
wpa_status="${wpa_status}/-"
fi
if [ "${trm_owecheck}" -eq 0 ]
then
wpa_status="${wpa_status}/OWE"
else
wpa_status="${wpa_status}/-"
fi
json_add_string "travelmate_status" "${status}"
json_add_string "travelmate_version" "${trm_ver}"
json_add_string "station_id" "${sta_radio:-"-"}/${sta_essid:-"-"}/${sta_bssid:-"-"}"
json_add_string "station_interface" "${sta_iface:-"-"}"
json_add_string "faulty_stations" "${faulty_list}"
json_add_string "wpa_capabilities" "${wpa_status:-"-"}"
json_add_string "last_rundate" "${last_date}"
json_add_string "system" "${trm_sysver}"
json_dump > "${trm_rtfile}"
f_log "debug" "f_jsnup ::: config: ${config:-"-"}, status: ${status:-"-"}, sta_iface: ${sta_iface:-"-"}, sta_radio: ${sta_radio:-"-"}, sta_essid: ${sta_essid:-"-"}, sta_bssid: ${sta_bssid:-"-"}, faulty_list: ${faulty_list:-"-"}, list_expiry: ${trm_listexpiry}"
}
# write to syslog
#
f_log()
{
local IFS class="${1}" log_msg="${2}"
if [ -n "${log_msg}" ] && { [ "${class}" != "debug" ] || [ "${trm_debug}" -eq 1 ]; }
then
logger -p "${class}" -t "travelmate-${trm_ver}[${$}]" "${log_msg}"
if [ "${class}" = "err" ]
then
trm_ifstatus="error"
f_jsnup
logger -p "${class}" -t "travelmate-${trm_ver}[${$}]" "Please check 'https://github.com/openwrt/packages/blob/master/net/travelmate/files/README.md' (${trm_sysver})"
exit 1
fi
fi
}
# main function for connection handling
#
f_main()
{
local IFS cnt dev config spec scan_list scan_essid scan_bssid scan_open scan_quality uci_essid cfg_essid faulty_list
local station_id sta sta_essid sta_bssid sta_radio sta_iface active_essid active_bssid active_radio
config_load wireless
config_foreach f_prepdev wifi-device
if [ -n "$(uci -q changes "wireless")" ]
then
uci_commit "wireless"
ubus call network reload
sleep $((trm_maxwait/6))
fi
f_check "initial" "false" "true"
f_log "debug" "f_main ::: status: ${trm_ifstatus}, proactive: ${trm_proactive}"
if [ "${trm_ifstatus}" != "true" ] || [ "${trm_proactive}" -eq 1 ]
then
config_foreach f_prepif wifi-iface ${trm_proactive}
if [ "${trm_ifstatus}" = "true" ] && [ -n "${trm_active_sta}" ] && [ "${trm_proactive}" -eq 1 ]
then
json_get_var station_id "station_id"
active_radio="${station_id%%/*}"
active_essid="${station_id%/*}"
active_essid="${active_essid#*/}"
active_bssid="${station_id##*/}"
f_check "dev" "true"
f_log "debug" "f_main ::: active_radio: ${active_radio}, active_essid: \"${active_essid}\", active_bssid: ${active_bssid:-"-"}"
else
uci_commit "wireless"
f_check "dev"
fi
json_get_var faulty_list "faulty_stations"
f_log "debug" "f_main ::: iwinfo: ${trm_iwinfo:-"-"}, dev_list: ${trm_devlist:-"-"}, sta_list: ${trm_stalist:0:${trm_scanbuffer}}, faulty_list: ${faulty_list:-"-"}"
# radio loop
#
for dev in ${trm_devlist}
do
if [ -z "$(printf "%s" "${trm_stalist}" | grep -o "\\-${dev}")" ]
then
f_log "debug" "f_main ::: no station on '${dev}' - continue"
continue
fi
# station loop
#
for sta in ${trm_stalist}
do
config="${sta%%-*}"
sta_radio="${sta##*-}"
sta_essid="$(uci_get "wireless" "${config}" "ssid")"
sta_bssid="$(uci_get "wireless" "${config}" "bssid")"
sta_iface="$(uci_get "wireless" "${config}" "network")"
json_get_var faulty_list "faulty_stations"
if [ -n "$(printf "%s" "${faulty_list}" | grep -Fo "${sta_radio}/${sta_essid}/${sta_bssid}")" ]
then
f_log "debug" "f_main ::: faulty station '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' - continue"
continue
fi
if [ "${dev}" = "${active_radio}" ] && [ "${sta_essid}" = "${active_essid}" ] && [ "${sta_bssid:-"-"}" = "${active_bssid}" ]
then
f_log "debug" "f_main ::: active station prioritized '${active_radio}/${active_essid}/${active_bssid:-"-"}' - break"
break 2
fi
f_log "debug" "f_main ::: sta_radio: ${sta_radio}, sta_essid: \"${sta_essid}\", sta_bssid: ${sta_bssid:-"-"}"
if [ -z "${scan_list}" ]
then
scan_list="$("${trm_iwinfo}" "${dev}" scan 2>/dev/null | \
awk 'BEGIN{FS="[[:space:]]"}/Address:/{var1=$NF}/ESSID:/{var2="";for(i=12;i<=NF;i++)if(var2==""){var2=$i}else{var2=var2" "$i};
gsub(/,/,".",var2)}/Quality:/{split($NF,var0,"/")}/Encryption:/{if($NF=="none"){var3="+"}else{var3="-"};printf "%i,%s,%s,%s\n",(var0[1]*100/var0[2]),var1,var2,var3}' | \
sort -rn | awk -v buf="${trm_scanbuffer}" 'BEGIN{ORS=","}{print substr($0,1,buf)}')"
f_log "debug" "f_main ::: scan_radio: ${dev}, scan_buffer: ${trm_scanbuffer}, scan_list: ${scan_list}"
if [ -z "${scan_list}" ]
then
f_log "debug" "f_main ::: no scan results on '${dev}' - continue"
continue 2
fi
fi
# scan loop
#
IFS=","
for spec in ${scan_list}
do
if [ -z "${scan_quality}" ]
then
scan_quality="${spec}"
elif [ -z "${scan_bssid}" ]
then
scan_bssid="${spec}"
elif [ -z "${scan_essid}" ]
then
scan_essid="${spec}"
elif [ -z "${scan_open}" ]
then
scan_open="${spec}"
fi
if [ -n "${scan_quality}" ] && [ -n "${scan_bssid}" ] && [ -n "${scan_essid}" ] && [ -n "${scan_open}" ]
then
if [ "${scan_quality}" -ge "${trm_minquality}" ]
then
if { { [ "${scan_essid}" = "\"${sta_essid//,/.}\"" ] && { [ -z "${sta_bssid}" ] || [ "${scan_bssid}" = "${sta_bssid}" ]; } } || \
{ [ "${scan_bssid}" = "${sta_bssid}" ] && [ "${scan_essid}" = "unknown" ]; } } && [ "${dev}" = "${sta_radio}" ]
then
f_log "debug" "f_main ::: scan_quality: ${scan_quality}, scan_essid: ${scan_essid}, scan_bssid: ${scan_bssid:-"-"}, scan_open: ${scan_open}"
if [ -n "${active_radio}" ]
then
uci_set "wireless" "${trm_active_sta}" "disabled" "1"
uci_commit "wireless"
f_log "debug" "f_main ::: active uplink connection '${active_radio}/${active_essid}/${active_bssid:-"-"}' terminated"
unset trm_connection active_radio active_essid active_bssid
fi
# retry loop
#
cnt=1
while [ "${cnt}" -le "${trm_maxretry}" ]
do
uci_set "wireless" "${config}" "disabled" "0"
trm_radio="${sta_radio}"
f_check "sta"
if [ "${trm_ifstatus}" = "true" ]
then
unset IFS scan_list
uci_commit "wireless"
f_log "info" "connected to uplink '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' (${cnt}/${trm_maxretry}, ${trm_sysver})"
return 0
else
uci -q revert "wireless"
f_check "rev"
if [ "${cnt}" -eq "${trm_maxretry}" ]
then
faulty_station="${sta_radio}/${sta_essid}/${sta_bssid:-"-"}"
f_jsnup "${faulty_station}"
f_log "info" "uplink disabled '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' (${cnt}/${trm_maxretry}, ${trm_sysver})"
break 2
else
f_jsnup
f_log "info" "can't connect to uplink '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' (${cnt}/${trm_maxretry}, ${trm_sysver})"
fi
fi
cnt=$((cnt+1))
sleep $((trm_maxwait/6))
done
elif [ "${trm_autoadd}" -eq 1 ] && [ "${scan_open}" = "+" ] && [ "${scan_essid}" != "unknown" ]
then
cfg_essid="${scan_essid#*\"}"
cfg_essid="${cfg_essid%\"*}"
uci_essid="${cfg_essid//[^[:alnum:]_]/_}"
if [ -z "$(uci_get "wireless" "trm_${uci_essid}")" ]
then
uci_add "wireless" "wifi-iface" "trm_${uci_essid}"
uci_set "wireless" "trm_${uci_essid}" "mode" "sta"
uci_set "wireless" "trm_${uci_essid}" "network" "${trm_iface}"
uci_set "wireless" "trm_${uci_essid}" "device" "${sta_radio}"
uci_set "wireless" "trm_${uci_essid}" "ssid" "${cfg_essid}"
uci_set "wireless" "trm_${uci_essid}" "encryption" "none"
uci_set "wireless" "trm_${uci_essid}" "disabled" "1"
uci_commit "wireless"
f_log "info" "open uplink '${sta_radio}/${cfg_essid}' added to wireless config"
fi
fi
unset scan_quality scan_bssid scan_essid scan_open
continue
else
unset scan_quality scan_bssid scan_essid scan_open
continue
fi
fi
done
unset IFS scan_quality scan_bssid scan_essid scan_open
done
unset scan_list
done
fi
}
# source required system libraries
#
if [ -r "/lib/functions.sh" ] && [ -r "/usr/share/libubox/jshn.sh" ]
then
. "/lib/functions.sh"
. "/usr/share/libubox/jshn.sh"
else
f_log "err" "system libraries not found"
fi
# control travelmate actions
#
f_envload
while true
do
if [ -z "${trm_action}" ]
then
rc=0
while true
do
if [ "${rc}" -eq 0 ]
then
f_check "initial"
fi
sleep ${trm_timeout} 0
rc=${?}
if [ "${rc}" -ne 0 ]
then
f_check "initial"
fi
if [ "${rc}" -eq 0 ] || { [ "${rc}" -ne 0 ] && [ "${trm_ifstatus}" = "false" ]; }
then
break
fi
done
elif [ "${trm_action}" = "stop" ]
then
> "${trm_rtfile}"
f_log "info" "travelmate instance stopped ::: action: ${trm_action}, pid: $(cat ${trm_pidfile} 2>/dev/null)"
exit 0
else
f_log "info" "travelmate instance started ::: action: ${trm_action}, pid: ${$}"
unset trm_action
fi
json_cleanup
f_envload
f_main
done
|