2 # travelmate, a wlan connection manager for travel router
3 # Copyright (c) 2016-2022 Dirk Brenken (dev@brenken.org)
4 # This is free software, licensed under the GNU General Public License v3.
6 # set (s)hellcheck exceptions
7 # shellcheck disable=1091,2086,3040,3043,3057,3060
10 export PATH
="/usr/sbin:/usr/bin:/sbin:/bin"
23 trm_mailpgm
="/etc/travelmate/travelmate.mail"
24 trm_vpnpgm
="/etc/travelmate/travelmate.vpn"
36 trm_rtfile
="/tmp/trm_runtime.json"
37 trm_wifi
="$(command -v wifi)"
38 trm_fetch
="$(command -v curl)"
39 trm_iwinfo
="$(command -v iwinfo)"
40 trm_logger
="$(command -v logger)"
41 trm_wpa
="$(command -v wpa_supplicant)"
42 trm_captiveurl
="http://detectportal.firefox.com"
43 trm_useragent
="Mozilla/5.0 (Linux x86_64; rv:90.0) Gecko/20100101 Firefox/90.0"
44 trm_ntpfile
="/var/state/travelmate.ntp"
45 trm_vpnfile
="/var/state/travelmate.vpn"
46 trm_mailfile
="/var/state/travelmate.mail"
47 trm_refreshfile
="/var/state/travelmate.refresh"
48 trm_pidfile
="/var/run/travelmate.pid"
49 trm_action
="${1:-"start"}"
51 # load travelmate environment
54 local check wpa_checks result
56 if [ "${trm_action}" = "stop" ]; then
60 unset trm_stalist trm_radiolist trm_uplinklist trm_uplinkcfg trm_wpaflags trm_activesta trm_opensta
62 trm_sysver
="$(ubus -S call system board 2>/dev/null | jsonfilter -q -e '@.model' -e '@.release.description' |
63 awk 'BEGIN{RS="";FS="\n"}{printf "%s
, %s
",$1,$2}')"
66 local name
="${1}" type="${2}"
68 if [ "${name}" = "travelmate" ] && [ "${type}" = "global" ]; then
70 local option
="${1}" value
="${2}"
71 eval "${option}=\"${value}\""
73 elif [ "${name}" = "uplink" ]; then
74 if [ "$(uci_get "travelmate.
${type}.opensta
")" = "1" ]; then
75 eval "trm_opensta=\"$((${trm_opensta:-0} + 1))\""
83 config_load travelmate
85 if [ "${trm_enabled}" != "1" ]; then
86 f_log
"info" "travelmate is currently disabled, please set 'trm_enabled' to '1' to use this service"
87 /etc
/init.d
/travelmate stop
88 elif [ -z "${trm_iface}" ]; then
89 f_log
"info" "travelmate is currently not configured, please use the 'Interface Setup' in LuCI or the 'setup' option in CLI"
90 /etc
/init.d
/travelmate stop
91 elif ! ubus
-t "${trm_maxwait}" wait_for network.wireless network.interface.
"${trm_iface}" >/dev
/null
2>&1; then
92 f_log
"info" "travelmate interface '${trm_iface}' does not appear on ubus, please check your network setup"
93 /etc
/init.d
/travelmate stop
96 wpa_checks
="sae owe eap suiteb192"
97 for check
in ${wpa_checks}; do
98 if [ -x "${trm_wpa}" ]; then
99 if "${trm_wpa}" -v"${check}" >/dev
/null
2>&1; then
100 result
="$(f_trim "${result} ${check}: $
(f_char
1)")"
102 result
="$(f_trim "${result} ${check}: $
(f_char
0)")"
106 trm_wpaflags
="$(printf "%s
" "${result}" | awk '{printf "%s
%s
, %s
%s
, %s
%s
, %s
%s
",$1,$2,$3,$4,$5,$6,$7,$8}')"
109 config_foreach f_setdev
"wifi-device"
110 if [ -n "$(uci -q changes "wireless
")" ]; then
111 uci_commit
"wireless"
115 json_load_file
"${trm_rtfile}" >/dev
/null
2>&1
117 if ! json_select data
>/dev
/null
2>&1; then
120 json_add_object
"data"
122 f_log
"debug" "f_env ::: auto_sta: ${trm_opensta:-"-"}, wpa_flags: ${trm_wpaflags}, sys_ver: ${trm_sysver}"
125 # trim helper function
130 trim
="${trim#"${trim%%[![:space:]]*}"}"
131 trim
="${trim%"${trim##*[![:space:]]}"}"
132 printf "%s" "${trim}"
135 # status helper function
138 local result input
="${1}"
140 if [ "${input}" = "1" ]; then
145 printf "%s" "${result}"
148 # wifi helper function
151 local status radio radio_up timeout
="0"
154 for radio
in ${trm_radiolist}; do
156 if [ "${timeout}" -ge "${trm_maxwait}" ]; then
159 status
="$("${trm_wifi}" status 2>/dev/null)"
160 if [ "$(printf "%s
" "${status}" | jsonfilter -q -l1 -e "@.
${radio}.up
")" != "true" ] ||
161 [ "$(printf "%s
" "${status}" | jsonfilter -q -l1 -e "@.
${radio}.pending
")" != "false" ]; then
162 if [ "${radio}" != "${radio_up}" ]; then
163 "${trm_wifi}" up
"${radio}"
166 timeout
="$((timeout + 1))"
173 if [ "${timeout}" -lt "${trm_maxwait}" ]; then
174 sleep "$((trm_maxwait / 6))"
175 timeout
="$((timeout + (trm_maxwait / 6)))"
177 f_log
"debug" "f_wifi ::: radio_list: ${trm_radiolist}, radio: ${radio}, timeout: ${timeout}"
180 # vpn helper function
183 local rc vpn vpn_service vpn_iface vpn_action
="${1}"
185 vpn
="$(f_getval "vpn
")"
186 vpn_service
="$(f_getval "vpnservice
")"
187 vpn_iface
="$(f_getval "vpniface
")"
189 if [ -x "${trm_vpnpgm}" ] && [ -n "${vpn}" ] && [ -n "${vpn_service}" ] && [ -n "${vpn_iface}" ] && [ -f "${trm_ntpfile}" ]; then
190 if { [ "${vpn_action}" = "disable" ] && [ -f "${trm_vpnfile}" ]; } ||
191 { [ "${vpn}" = "1" ] && [ "${vpn_action}" = "enable" ] && [ ! -f "${trm_vpnfile}" ]; } ||
192 { [ "${vpn}" != "1" ] && [ "${vpn_action}" = "enable" ] && [ -f "${trm_vpnfile}" ]; }; then
193 "${trm_vpnpgm}" "${vpn}" "${vpn_action}" "${vpn_service}" "${vpn_iface}" >/dev/null 2>&1
196 if [ "${vpn}" = "1" ] && [ "${vpn_action}" = "enable" ] && [ "${rc}" = "0" ]; then
198 elif { [ "${vpn}" != "1" ] || [ "${vpn_action}" = "disable" ]; } && [ -f "${trm_vpnfile}" ]; then
199 rm -f "${trm_vpnfile}"
201 [ -n "${rc}" ] && f_jsnup
203 f_log
"debug" "f_vpn ::: enabled: ${vpn:-"-"}, action: ${vpn_action}, service: ${vpn_service:-"-"}, iface: ${vpn_iface:-"-"}, rc: ${rc:-"-"}, program: ${trm_vpnpgm}"
206 # mac helper function
209 local result ifname macaddr action
="${1}" section
="${2}"
211 if [ "${action}" = "set" ]; then
212 macaddr
="$(f_getval "macaddr
")"
213 if [ -n "${macaddr}" ]; then
215 uci_set
"wireless" "${section}" "macaddr" "${result}"
216 elif [ "${trm_randomize}" = "1" ]; then
217 result
="$(hexdump -n6 -ve '/1 "%.02X
"' /dev/random 2>/dev/null |
218 awk -v local="2,6,A
,E
" -v seed="$
(date +%s
)" 'BEGIN{srand(seed)}NR==1{split(local,b,",");
219 seed=int(rand()*4+1);printf "%s
%s
:%s
:%s
:%s
:%s
:%s
",substr($1,0,1),b[seed],$2,$3,$4,$5,$6}')"
220 uci_set
"wireless" "${section}" "macaddr" "${result}"
222 uci_remove
"wireless" "${section}" "macaddr" 2>/dev
/null
223 ifname
="$(ubus -S call network.wireless status 2>/dev/null | jsonfilter -q -l1 -e '@.*.interfaces[@.config.mode="sta
"].ifname')"
224 result
="$(${trm_iwinfo} "${ifname}" info 2>/dev/null | awk '/Access Point:/{printf "%s",$3}')"
226 elif [ "${action}" = "get" ]; then
227 result
="$(uci_get "wireless
" "${section}" "macaddr
")"
228 if [ -z "${result}" ]; then
229 ifname
="$(ubus -S call network.wireless status 2>/dev/null | jsonfilter -q -l1 -e '@.*.interfaces[@.config.mode="sta
"].ifname')"
230 result
="$(${trm_iwinfo} "${ifname}" info 2>/dev/null | awk '/Access Point:/{printf "%s",$3}')"
233 printf "%s" "${result}"
234 f_log
"debug" "f_mac ::: action: ${action:-"-"}, section: ${section:-"-"}, macaddr: ${macaddr:-"-"}, result: ${result:-"-"}"
237 # set connection information
240 local expiry action
="${1}"
242 if [ -n "${trm_uplinkcfg}" ]; then
245 uci_remove
"travelmate" "${trm_uplinkcfg}" "con_start" 2>/dev
/null
246 uci_remove
"travelmate" "${trm_uplinkcfg}" "con_end" 2>/dev
/null
247 if [ -f "${trm_ntpfile}" ]; then
248 uci_set
"travelmate" "${trm_uplinkcfg}" "con_start" "$(date "+%Y.
%m.
%d-
%H
:%M
:%S
")"
252 if [ -f "${trm_ntpfile}" ] && [ -z "$(uci_get "travelmate
" "${trm_uplinkcfg}" "con_start
")" ]; then
253 uci_set
"travelmate" "${trm_uplinkcfg}" "con_start" "$(date "+%Y.
%m.
%d-
%H
:%M
:%S
")"
257 if [ -f "${trm_ntpfile}" ]; then
258 uci_set
"travelmate" "${trm_uplinkcfg}" "con_end" "$(date "+%Y.
%m.
%d-
%H
:%M
:%S
")"
262 if [ -f "${trm_ntpfile}" ]; then
263 expiry
="$(uci_get "travelmate
" "${trm_uplinkcfg}" "con_start_expiry
")"
264 uci_set
"travelmate" "${trm_uplinkcfg}" "enabled" "0"
265 uci_set
"travelmate" "${trm_uplinkcfg}" "con_end" "$(date "+%Y.
%m.
%d-
%H
:%M
:%S
")"
266 f_log
"info" "uplink '${radio}/${essid}/${bssid:-"-"}' expired after ${expiry} minutes"
270 if [ -f "${trm_ntpfile}" ]; then
271 expiry
="$(uci_get "travelmate
" "${trm_uplinkcfg}" "con_end_expiry
")"
272 uci_set
"travelmate" "${trm_uplinkcfg}" "enabled" "1"
273 uci_remove
"travelmate" "${trm_uplinkcfg}" "con_start" 2>/dev
/null
274 uci_remove
"travelmate" "${trm_uplinkcfg}" "con_end" 2>/dev
/null
275 f_log
"info" "uplink '${radio}/${essid}/${bssid:-"-"}' re-enabled after ${expiry} minutes"
279 uci_set
"travelmate" "${trm_uplinkcfg}" "enabled" "0"
280 if [ -f "${trm_ntpfile}" ]; then
281 uci_set
"travelmate" "${trm_uplinkcfg}" "con_end" "$(date "+%Y.
%m.
%d-
%H
:%M
:%S
")"
285 if [ -n "$(uci -q changes "travelmate
")" ]; then
286 uci_commit
"travelmate"
287 if [ ! -f "${trm_refreshfile}" ]; then
288 printf "%s" "cfg_reload" >"${trm_refreshfile}"
292 f_log
"debug" "f_ctrack ::: action: ${action:-"-"}, uplink_config: ${trm_uplinkcfg:-"-"}"
295 # get wan gateway addresses
298 local result wan4_if wan4_gw wan6_if wan6_gw
301 network_find_wan wan4_if
302 network_find_wan6 wan6_if
303 network_get_gateway wan4_gw
"${wan4_if}"
304 network_get_gateway6 wan6_gw
"${wan6_if}"
305 if [ -n "${wan4_gw}" ] ||
[ -n "${wan6_gw}" ]; then
306 result
="${wan4_gw} ${wan6_gw}"
308 printf "%s" "${result}"
309 f_log
"debug" "f_getgw ::: wan4_gw: ${wan4_gw:-"-"}, wan6_gw: ${wan6_gw:-"-"}, result: ${result:-"-"}"
312 # get uplink config section
315 local t_radio t_essid t_bssid radio
="${1}" essid="${2}" bssid="${3}" cnt
="0"
317 while uci_get
"travelmate" "@uplink[${cnt}]" >/dev
/null
2>&1; do
318 t_radio
="$(uci_get "travelmate
" "@uplink
[${cnt}]" "device
")"
319 t_essid
="$(uci_get "travelmate
" "@uplink
[${cnt}]" "ssid
")"
320 t_bssid
="$(uci_get "travelmate
" "@uplink
[${cnt}]" "bssid
")"
321 if [ -n "${radio}" ] && [ -n "${essid}" ] &&
322 [ "${t_radio}" = "${radio}" ] && [ "${t_essid}" = "${essid}" ] && [ "${t_bssid}" = "${bssid}" ]; then
323 trm_uplinkcfg
="@uplink[${cnt}]"
328 f_log
"debug" "f_getcfg ::: status: ${status}, section: ${section}, uplink_config: ${trm_uplinkcfg:-"-"}"
331 # get travelmate option value in 'uplink' sections
334 local result t_option
="${1}"
336 if [ -n "${trm_uplinkcfg}" ]; then
337 result
="$(uci_get "travelmate
" "${trm_uplinkcfg}" "${t_option}")"
338 printf "%s" "${result}"
340 f_log
"debug" "f_getval ::: option: ${t_option:-"-"}, result: ${result:-"-"}, uplink_config: ${trm_uplinkcfg:-"-"}"
343 # set 'wifi-device' sections
346 local disabled radio
="${1}"
348 disabled
="$(uci_get "wireless
" "${radio}" "disabled
")"
349 if [ "${disabled}" = "1" ]; then
350 uci_set wireless
"${radio}" "disabled" "0"
352 if [ -n "${trm_radio}" ] && [ -z "${trm_radiolist}" ]; then
353 trm_radiolist
="${trm_radio}"
354 elif [ -z "${trm_radio}" ] && ! printf "%s" "${trm_radiolist}" | grep -q "${radio}"; then
355 trm_radiolist
="$(f_trim "${trm_radiolist} ${radio}")"
357 f_log
"debug" "f_setdev ::: radio: ${radio:-"-"}, radio_list(cnf/cur): ${trm_radio:-"-"}/${trm_radiolist:-"-"}, disabled: ${disabled:-"-"}"
360 # set 'wifi-iface' sections
363 local mode radio essid bssid enabled disabled con_start con_end con_start_expiry con_end_expiry section
="${1}" proactive
="${2}"
365 mode
="$(uci_get "wireless
" "${section}" "mode
")"
366 radio
="$(uci_get "wireless
" "${section}" "device
")"
367 essid
="$(uci_get "wireless
" "${section}" "ssid
")"
368 bssid
="$(uci_get "wireless
" "${section}" "bssid
")"
369 disabled
="$(uci_get "wireless
" "${section}" "disabled
")"
371 f_getcfg
"${radio}" "${essid}" "${bssid}"
373 enabled
="$(f_getval "enabled
")"
374 con_start
="$(f_getval "con_start
")"
375 con_end
="$(f_getval "con_end
")"
376 con_start_expiry
="$(f_getval "con_start_expiry
")"
377 con_end_expiry
="$(f_getval "con_end_expiry
")"
379 if [ "${enabled}" = "0" ] && [ -n "${con_end}" ] && [ -n "${con_end_expiry}" ] && [ "${con_end_expiry}" != "0" ]; then
380 d1
="$(date -d "${con_end}" "+%s
")"
382 d3
="$(((d2 - d1) / 60))"
383 if [ "${d3}" -ge "${con_end_expiry}" ]; then
385 f_ctrack
"end_expiry"
387 elif [ "${enabled}" = "1" ] && [ -n "${con_start}" ] && [ -n "${con_start_expiry}" ] && [ "${con_start_expiry}" != "0" ]; then
388 d1
="$(date -d "${con_start}" "+%s
")"
390 d3
="$((d1 + (con_start_expiry * 60)))"
391 if [ "${d2}" -gt "${d3}" ]; then
393 f_ctrack
"start_expiry"
397 if [ "${mode}" = "sta" ]; then
398 if [ "${enabled}" = "0" ] || { { [ -z "${disabled}" ] || [ "${disabled}" = "0" ]; } &&
399 { [ "${proactive}" = "0" ] || [ "${trm_ifstatus}" != "true" ]; }; }; then
400 uci_set
"wireless" "${section}" "disabled" "1"
401 elif [ "${enabled}" = "1" ] && [ "${disabled}" = "0" ] && [ "${trm_ifstatus}" = "true" ] && [ "${proactive}" = "1" ]; then
402 if [ -z "${trm_activesta}" ]; then
403 trm_activesta
="${section}"
405 uci_set
"wireless" "${section}" "disabled" "1"
408 if [ "${enabled}" = "1" ]; then
409 trm_stalist
="$(f_trim "${trm_stalist} ${section}-${radio}")"
412 f_log
"debug" "f_setif ::: enabled: ${enabled}, section: ${section}, active_sta: ${trm_activesta:-"-"}, uplink_config: ${trm_uplinkcfg:-"-"}"
418 local uci_cfg new_uplink
="1" offset
="1" radio
="${1}" essid
="${2}"
420 if [ "${trm_maxautoadd}" = "0" ] || [ "${trm_opensta:-0}" -lt "${trm_maxautoadd}" ]; then
422 local type="${1}" name
="${2}"
424 if [ "${type}" = "wifi-iface" ]; then
425 if [ "$(uci_get "wireless.
${name}.ssid
")" = "${essid}" ] &&
426 [ "$(uci_get "wireless.
${name}.device
")" = "${radio}" ]; then
430 offset
="$((offset + 1))"
438 if [ "${new_uplink}" = "1" ]; then
439 uci_cfg
="trm_uplink$((offset + 1))"
440 while [ -n "$(uci_get "wireless.
${uci_cfg}")" ]; do
441 offset
="$((offset + 1))"
442 uci_cfg
="trm_uplink${offset}"
445 set wireless."${uci_cfg}"="wifi-iface"
446 set wireless."${uci_cfg}".mode="sta"
447 set wireless."${uci_cfg}".network="${trm_iface}"
448 set wireless."${uci_cfg}".device="${radio}"
449 set wireless."${uci_cfg}".ssid="${essid}"
450 set wireless."${uci_cfg}".encryption="none"
451 set wireless."${uci_cfg}".disabled="1"
453 uci_cfg
="$(uci -q add travelmate uplink)"
455 set travelmate."${uci_cfg}".device="${radio}"
456 set travelmate."${uci_cfg}".ssid="${essid}"
457 set travelmate."${uci_cfg}".opensta="1"
458 set travelmate."${uci_cfg}".con_start_expiry="0"
459 set travelmate."${uci_cfg}".con_end_expiry="0"
460 set travelmate."${uci_cfg}".enabled="1"
462 if [ -n "$(uci -q changes "travelmate
")" ] ||
[ -n "$(uci -q changes "wireless
")" ]; then
463 trm_opensta
="$((trm_opensta + 1))"
464 uci_commit
"travelmate"
465 uci_commit
"wireless"
467 if [ ! -f "${trm_refreshfile}" ]; then
468 printf "%s" "ui_reload" >"${trm_refreshfile}"
470 f_log
"info" "open uplink '${radio}/${essid}' added to wireless config"
473 f_log
"debug" "f_addsta ::: radio: ${radio:-"-"}, essid: ${essid}, opensta/maxautoadd: ${trm_opensta:-"-"}/${trm_maxautoadd:-"-"}, new_uplink: ${new_uplink}, offset: ${offset}"
479 local err_msg raw html_raw html_cp json_raw json_ec json_rc json_cp json_ed result
="net nok"
481 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}")"
482 json_raw="${raw#*\{}"
483 html_raw="${raw%%\{*}"
484 if [ -n "${json_raw}" ]; then
485 json_ec="$
(printf "%s" "{${json_raw}" | jsonfilter
-q -l1 -e '@.exitcode')"
486 json_rc="$
(printf "%s" "{${json_raw}" | jsonfilter
-q -l1 -e '@.response_code')"
487 json_cp="$
(printf "%s" "{${json_raw}" | jsonfilter
-q -l1 -e '@.redirect_url' |
awk 'BEGIN{FS="/"}{printf "%s",tolower($3)}')"
488 if [ "${json_ec}" = "0" ]; then
489 if [ -n "${json_cp}" ]; then
490 result="net
cp '${json_cp}'"
492 if [ "${json_rc}" = "200" ] || [ "${json_rc}" = "204" ]; then
493 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}')"
494 if [ -n "${html_cp}" ]; then
495 result="net
cp '${html_cp}'"
502 err_msg="$
(printf "%s" "{${json_raw}" | jsonfilter
-q -l1 -e '@.errormsg')"
503 json_ed="$
(printf "%s" "{${err_msg}" |
awk '/([[:alnum:]_-]{1,63}\.)+[[:alpha:]]+$/{printf "%s",tolower($NF)}')"
504 if [ "${json_ec}" = "6" ]; then
505 if [ -n "${json_ed}" ] && [ "${json_ed}" != "${trm_captiveurl#http*://*}" ]; then
506 result="net
cp '${json_ed}'"
508 elif [ "${json_ec}" = "28" ]; then
509 if [ -n "$
(f_getgw
)" ]; then
515 printf "%s
" "${result}"
516 f_log "debug
" "f_net
::: fetch
: ${trm_fetch}, timeout: $((trm_maxwait / 6)), cp (json/html): ${json_cp:-"-"}/${html_cp:-"-"}, result: ${result}, error (rc/msg): ${json_ec}/${err_msg:-"-"}, url: ${trm_captiveurl}, user_agent: ${trm_useragent}"
519 # check interface status
522 local ifname radio dev_status result login_script login_script_args cp_domain wait_time="1" enabled="1" mode="${1}" status="${2}" sta_radio="${3}" sta_essid="${4}" sta_bssid="${5}"
524 if [ "${mode}" = "initial
" ] || [ "${mode}" = "dev
" ]; then
525 json_get_var station_id "station_id
"
526 sta_radio="${station_id%%/*}"
527 sta_essid="${station_id%/*}"
528 sta_essid="${sta_essid#*/}"
529 sta_bssid="${station_id##*/}"
530 sta_bssid="${sta_bssid//-/}"
532 f_getcfg "${sta_radio}" "${sta_essid}" "${sta_bssid}"
534 if [ "${mode}" != "rev" ] && [ -n "${sta_radio}" ] && [ "${sta_radio}" != "-" ] && [ -n "${sta_essid}" ] && [ "${sta_essid}" != "-" ]; then
535 enabled="$
(f_getval
"enabled")"
537 if { [ "${mode}" != "initial" ] && [ "${mode}" != "dev" ] && [ "${status}" = "false" ]; } ||
538 { [ "${mode}" = "dev" ] && { [ "${status}" = "false" ] || { [ "${trm_ifstatus}" != "${status}" ] && [ "${enabled}" = "0" ]; }; }; }; then
541 while [ "${wait_time}" -le "${trm_maxwait}" ]; do
542 dev_status="$
(ubus
-S call network.wireless status
2>/dev
/null
)"
543 if [ -n "${dev_status}" ]; then
544 if [ "${mode}" = "dev
" ]; then
545 if [ "${trm_ifstatus}" != "${status}" ]; then
546 trm_ifstatus="${status}"
549 if [ "${status}" = "false
" ]; then
550 sleep "$
((trm_maxwait
/ 5))"
553 elif [ "${mode}" = "rev" ]; then
555 trm_ifstatus="${status}"
558 ifname="$
(printf "%s" "${dev_status}" | jsonfilter
-q -l1 -e '@.*.interfaces[@.config.mode="sta"].ifname')"
559 if [ -n "${ifname}" ] && [ "${enabled}" = "1" ]; then
560 trm_ifquality="$
(${trm_iwinfo} "${ifname}" info 2>/dev/null | awk -F '[ ]' '/Link Quality:/{split($NF,var0,"/");printf "%i\n",(var0[1]*100/var0[2])}')"
561 if [ "${trm_ifquality}" -ge "${trm_minquality}" ]; then
562 trm_ifstatus="$(ubus -S call network.interface dump 2>/dev/null | jsonfilter -q -l1 -e "@.interface[@.device=\"${ifname}\"].up")"
563 if [ "${trm_ifstatus}" = "true" ]; then
565 if [ "${trm_captive}" = "1" ]; then
567 cp_domain="$(printf "%s" "${result}" | awk -F '['\''|
]' '/^net cp
/{printf "%s",$4}')"
568 if [ -x "/etc/init.d/dnsmasq" ] && [ -f "/etc/config/dhcp" ] &&
569 [ -n "${cp_domain}" ] && ! uci_get "dhcp" "@dnsmasq[0]" "rebind_domain" | grep -q "${cp_domain}"; then
570 uci_add_list "dhcp" "@dnsmasq[0]" "rebind_domain" "${cp_domain}"
572 /etc/init.d/dnsmasq reload
573 f_log "info" "captive portal domain '${cp_domain}' added to to dhcp rebind whitelist"
579 if [ -n "${cp_domain}" ]; then
580 trm_connection="${result:-"-"}/${trm_ifquality}"
582 login_script="$(f_getval "script")"
583 if [ -x "${login_script}" ]; then
584 login_script_args="$(f_getval "script_args")"
585 "${login_script}" ${login_script_args} >/dev/null 2>&1
587 if [ "${rc}" = "255" ]; then
588 f_log "info" "captive portal login script for '${cp_domain}' failed with rc '${rc}'"
590 trm_ifstatus="${status}"
594 f_log "info" "captive portal login script for '${cp_domain}' has been finished with rc '${rc}'"
595 if [ "${rc}" = "0" ]; then
602 if [ "${trm_netcheck}" = "1" ] && [ "${result}" = "net nok" ]; then
603 f_log "info" "uplink has no internet"
605 trm_ifstatus="${status}"
609 trm_connection="${result:-"-"}/${trm_ifquality}"
613 elif [ -n "${trm_connection}" ] && { [ "${trm_netcheck}" = "1" ] || [ "${mode}" = "initial" ]; }; then
614 f_log "info" "uplink is out of range (${trm_ifquality}/${trm_minquality})"
617 trm_ifstatus="${status}"
621 elif [ "${mode}" = "initial" ] || [ "${mode}" = "sta" ]; then
623 trm_ifstatus="${status}"
627 elif [ -n "${trm_connection}" ]; then
630 trm_ifstatus="${status}"
633 elif [ "${mode}" = "initial" ]; then
634 trm_ifstatus="${status}"
640 if [ "${mode}" = "initial" ]; then
641 trm_ifstatus="${status}"
645 wait_time="$((wait_time + 1))"
648 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}"
651 # update runtime information
654 local vpn section last_date last_station sta_iface sta_radio sta_essid sta_bssid sta_mac dev_status last_status status="${trm_ifstatus}" ntp_done="0" vpn_done="0" mail_done="0"
656 if [ "${status}" = "true" ]; then
657 status="connected (${trm_connection:-"-"})"
658 dev_status="$(ubus -S call network.wireless status 2>/dev/null)"
659 section="$(printf "%s" "${dev_status}" | jsonfilter -q -l1 -e '@.
*.interfaces
[@.config.mode
="sta"].section
')"
660 if [ -n "${section}" ]; then
661 sta_iface="$(uci_get "wireless" "${section}" "network")"
662 sta_radio="$(uci_get "wireless" "${section}" "device")"
663 sta_essid="$(uci_get "wireless" "${section}" "ssid")"
664 sta_bssid="$(uci_get "wireless" "${section}" "bssid")"
665 sta_mac="$(f_mac "get" "${section}")"
666 f_getcfg "${sta_radio}" "${sta_essid}" "${sta_bssid}"
667 vpn="$(f_getval "vpn")"
669 json_get_var last_date "last_run"
670 json_get_var last_station "station_id"
671 json_get_var last_status "travelmate_status"
673 if { [ -f "${trm_ntpfile}" ] && [ ! -s "${trm_ntpfile}" ]; } || [ "${last_status}" = "running (not connected)" ] ||
674 { [ -n "${last_station}" ] && [ "${last_station}" != "${sta_radio:-"-"}/${sta_essid:-"-"}/${sta_bssid:-"-"}" ]; }; then
675 last_date="$(date "+%Y.%m.%d-%H:%M:%S")"
676 if [ -f "${trm_ntpfile}" ] && [ ! -s "${trm_ntpfile}" ]; then
677 printf "%s" "${last_date}" >"${trm_ntpfile}"
680 elif [ "${status}" = "error" ]; then
682 status="program error"
685 status="running (not connected)"
687 if [ -z "${last_date}" ]; then
688 last_date="$(date "+%Y.%m.%d-%H:%M:%S")"
690 if [ -s "${trm_ntpfile}" ]; then
693 if [ "${vpn}" = "1" ] && [ -f "${trm_vpnfile}" ]; then
696 if [ "${trm_mail}" = "1" ] && [ -f "${trm_mailfile}" ]; then
699 json_add_string "travelmate_status" "${status}"
700 json_add_string "travelmate_version" "${trm_ver}"
701 json_add_string "station_id" "${sta_radio:-"-"}/${sta_essid:-"-"}/${sta_bssid:-"-"}"
702 json_add_string "station_mac" "${sta_mac:-"-"}"
703 json_add_string "station_interface" "${sta_iface:-"-"}"
704 json_add_string "wpa_flags" "${trm_wpaflags:-"-"}"
705 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})"
706 json_add_string "ext_hooks" "ntp: $(f_char ${ntp_done}), vpn: $(f_char ${vpn_done}), mail: $(f_char ${mail_done})"
707 json_add_string "last_run" "${last_date}"
708 json_add_string "system" "${trm_sysver}"
709 json_dump >"${trm_rtfile}"
711 if [ "${status%% (net ok/*}" = "connected" ] && [ "${trm_mail}" = "1" ] && [ -x "${trm_mailpgm}" ] && [ "${ntp_done}" = "1" ] && [ "${mail_done}" = "0" ]; then
712 if [ "${vpn}" != "1" ] || [ "${vpn_done}" = "1" ]; then
714 "${trm_mailpgm}" >/dev/null 2>&1
717 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}"
723 local class="${1}" log_msg="${2}"
725 if [ -n "${log_msg}" ] && { [ "${class}" != "debug" ] || [ "${trm_debug}" = "1" ]; }; then
726 if [ -x "${trm_logger}" ]; then
727 "${trm_logger}" -p "${class}" -t "trm-${trm_ver}[${$}]" "${log_msg}"
729 printf "%s %s %s\n" "${class}" "trm-${trm_ver}[${$}]" "${log_msg}"
731 if [ "${class}" = "err" ]; then
740 # main function for connection handling
743 local radio cnt retrycnt scan_dev scan_list scan_essid scan_bssid scan_open scan_quality
744 local station_id section sta sta_essid sta_bssid sta_radio sta_mac config_essid config_bssid config_radio
746 f_check "initial" "false"
747 f_log "debug" "f_main-1 ::: status: ${trm_ifstatus}, proactive: ${trm_proactive}"
748 if [ "${trm_ifstatus}" != "true" ] || [ "${trm_proactive}" = "1" ]; then
750 config_foreach f_setif wifi-iface "${trm_proactive}"
751 if [ "${trm_ifstatus}" = "true" ] && [ -n "${trm_activesta}" ] && [ "${trm_proactive}" = "1" ]; then
752 json_get_var station_id "station_id"
753 config_radio="${station_id%%/*}"
754 config_essid="${station_id%/*}"
755 config_essid="${config_essid#*/}"
756 config_bssid="${station_id##*/}"
757 config_bssid="${config_bssid//-/}"
759 f_log "debug" "f_main-2 ::: config_radio: ${config_radio}, config_essid: \"${config_essid}\", config_bssid: ${config_bssid:-"-"}"
761 uci_commit "wireless"
762 f_check "dev" "false"
764 f_log "debug" "f_main-3 ::: radio_list: ${trm_radiolist:-"-"}, sta_list: ${trm_stalist:-"-"}"
768 for radio in ${trm_radiolist}; do
769 if ! printf "%s" "${trm_stalist}" | grep -q "\\-${radio}"; then
770 if [ "${trm_autoadd}" = "0" ]; then
771 f_log "info" "no enabled station on radio '${radio}'"
779 for sta in ${trm_stalist:-"${radio}"}; do
780 if [ "${sta}" != "${radio}" ]; then
782 sta_radio="$(uci_get "wireless" "${section}" "device")"
783 sta_essid="$(uci_get "wireless" "${section}" "ssid")"
784 sta_bssid="$(uci_get "wireless" "${section}" "bssid")"
785 sta_mac="$(f_mac "get" "${section}")"
786 if [ -z "${sta_radio}" ] || [ -z "${sta_essid}" ]; then
787 f_log "info" "invalid wireless section '${section}'"
790 if [ -n "${trm_connection}" ] && [ "${radio}" = "${config_radio}" ] && [ "${sta_radio}" = "${config_radio}" ] &&
791 [ "${sta_essid}" = "${config_essid}" ] && [ "${sta_bssid}" = "${config_bssid}" ]; then
793 f_log "info" "uplink still in range '${config_radio}/${config_essid}/${config_bssid:-"-"}' with mac '${sta_mac:-"-"}'"
797 f_log "debug" "f_main-4 ::: sta_radio: ${sta_radio}, sta_essid: \"${sta_essid}\", sta_bssid: ${sta_bssid:-"-"}"
799 if [ -z "${scan_list}" ]; then
800 scan_dev="$(ubus -S call network.wireless status 2>/dev/null | jsonfilter -q -l1 -e "@.${radio}.interfaces[0].ifname")"
801 scan_list="$("${trm_iwinfo}" "${scan_dev:-${radio}}" scan 2>/dev/null |
802 awk 'BEGIN
{FS
="[[:space:]]"}/Address
:/{var1
=$NF}/ESSID
:/{var2
="";for(i
=12;i
<=NF
;i
++)if(var2
==""){var2
=$i}else{var2
=var2
" "$i}}
803 /Quality
:/{split($NF,var0
,"/")}/Encryption
:/{if($NF=="none"){var3
="+"}else{var3
="-"};
804 printf "%i %s %s %s\n",(var0
[1]*100/var0
[2]),var3
,var1
,var2
}' | sort -rn | head -qn "${trm_maxscan}")"
805 f_log "debug" "f_main-5 ::: radio: ${radio}, scan_device: ${scan_dev}, scan_max: ${trm_maxscan}"
806 if [ -z "${scan_list}" ]; then
807 f_log "info" "no scan results on '${radio}'"
814 while read -r scan_quality scan_open scan_bssid scan_essid; do
815 if [ -n "${scan_quality}" ] && [ -n "${scan_open}" ] && [ -n "${scan_bssid}" ] && [ -n "${scan_essid}" ]; then
816 f_log "debug" "f_main-6 ::: 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}"
817 if [ "${scan_quality}" -ge "${trm_minquality}" ]; then
818 if { { [ "${scan_essid}" = "\"${sta_essid}\"" ] && { [ -z "${sta_bssid}" ] || [ "${scan_bssid}" = "${sta_bssid}" ]; }; } ||
819 { [ "${scan_bssid}" = "${sta_bssid}" ] && [ "${scan_essid}" = "unknown" ]; }; } && [ "${radio}" = "${sta_radio}" ]; then
820 if [ -n "${config_radio}" ]; then
822 uci_set "wireless" "${trm_activesta}" "disabled" "1"
823 uci_commit "wireless"
824 f_check "rev" "false"
826 f_log "info" "uplink connection terminated '${config_radio}/${config_essid}/${config_bssid:-"-"}'"
827 unset config_radio config_essid config_bssid
833 f_getcfg "${sta_radio}" "${sta_essid}" "${sta_bssid}"
834 while [ "${retrycnt}" -le "${trm_maxretry}" ]; do
835 sta_mac="$(f_mac "set" "${section}")"
836 uci_set "wireless" "${section}" "disabled" "0"
837 f_check "sta" "false" "${sta_radio}" "${sta_essid}" "${sta_bssid}"
838 if [ "${trm_ifstatus}" = "true" ]; then
839 rm -f "${trm_mailfile}"
840 uci_commit "wireless"
842 f_log "info" "connected to uplink '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' with mac '${sta_mac:-"-"}' (${retrycnt}/${trm_maxretry})"
846 uci -q revert "wireless
"
847 f_check "rev" "false
"
848 if [ "${retrycnt}" = "${trm_maxretry}" ]; then
850 f_log "info
" "uplink has been disabled
'${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' (${retrycnt}/${trm_maxretry})"
854 f_log "info" "can't connect to uplink
'${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' (${retrycnt}/${trm_maxretry})"
857 retrycnt="$((retrycnt + 1))"
858 sleep "$((trm_maxwait / 6))"
860 elif [ "${trm_autoadd}" = "1" ] && [ "${scan_open}" = "+" ] && [ "${scan_essid}" != "unknown" ]; then
861 scan_essid="${scan_essid%?}"
862 scan_essid="${scan_essid:1}"
863 f_addsta "${radio}" "${scan_essid}"
875 # source required system libraries
877 if [ -r "/lib/functions.sh" ] && [ -r "/lib/functions/network.sh" ] && [ -r "/usr/share/libubox/jshn.sh" ]; then
878 . "/lib/functions.sh"
879 . "/lib/functions/network.sh"
880 . "/usr/share/libubox/jshn.sh"
882 f_log "err" "system libraries not found"
885 # control travelmate actions
888 if [ "${trm_action}" = "stop" ]; then
889 if [ -s "${trm_pidfile}" ]; then
890 f_log "info" "travelmate instance stopped ::: action: ${trm_action}, pid: $(cat ${trm_pidfile} 2>/dev/null)"
895 elif [ -n "${trm_action}" ]; then
896 f_log "info" "travelmate instance started ::: action: ${trm_action}, pid: ${$}"
902 sleep "${trm_timeout}" 0
904 if [ "${rc}" != "0" ]; then
905 if [ -z "$(f_getgw)" ]; then
909 if [ "${rc}" = "0" ]; then