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.
6 # set (s)hellcheck exceptions
7 # shellcheck disable=all
10 export PATH
="/usr/sbin:/usr/bin:/sbin:/bin"
23 trm_mailpgm
="/etc/travelmate/travelmate.mail"
24 trm_vpnpgm
="/etc/travelmate/travelmate.vpn"
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"}"
54 # load travelmate environment
57 local check wpa_checks result
59 if [ "${trm_action}" = "stop" ]; then
63 unset trm_stalist trm_radiolist trm_uplinklist trm_vpnifacelist trm_uplinkcfg trm_activesta trm_opensta
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}')"
69 local name
="${1}" type="${2}"
71 if [ "${name}" = "travelmate" ] && [ "${type}" = "global" ]; then
73 local option
="${1}" value
="${2}"
74 eval "${option}=\"${value}\""
77 local option
="${1}" value
="${2}"
78 if [ "${option}" = "trm_vpnifacelist" ]; then
79 eval "trm_vpnifacelist=\"$(printf "%s
" "${trm_vpnifacelist}") ${value}\""
82 elif [ "${name}" = "uplink" ]; then
83 if [ "$(uci_get "travelmate.
${type}.opensta
")" = "1" ]; then
84 eval "trm_opensta=\"$((${trm_opensta:-0} + 1))\""
92 config_load travelmate
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
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)")"
112 result
="$(f_trim "${result} ${check}: $
(f_char
0)")"
116 trm_wpaflags
="$(printf "%s
" "${result}" | awk '{printf "%s
%s
, %s
%s
, %s
%s
, %s
%s
",$1,$2,$3,$4,$5,$6,$7,$8}')"
120 config_foreach f_setdev
"wifi-device"
121 if [ -n "$(uci -q changes "wireless
")" ]; then
122 uci_commit
"wireless"
126 json_load_file
"${trm_rtfile}" >/dev
/null
2>&1
127 if ! json_select data
>/dev
/null
2>&1; then
130 json_add_object
"data"
133 if [ "${trm_vpn}" = "1" ] && [ -z "${trm_vpninfolist}" ]; then
135 config_foreach f_getvpn
"interface"
137 f_log
"debug" "f_env ::: auto_sta: ${trm_opensta:-"-"}, wpa_flags: ${trm_wpaflags}, sys_ver: ${trm_sysver}"
140 # trim helper function
145 trim
="${trim#"${trim%%[![:space:]]*}"}"
146 trim
="${trim%"${trim##*[![:space:]]}"}"
147 printf "%s" "${trim}"
150 # status helper function
153 local result input
="${1}"
155 if [ "${input}" = "1" ]; then
160 printf "%s" "${result}"
163 # wifi helper function
166 local status radio radio_up timeout
="0"
169 for radio
in ${trm_radiolist}; do
171 if [ "${timeout}" -ge "${trm_maxwait}" ]; then
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}"
181 timeout
="$((timeout + 1))"
188 if [ "${timeout}" -lt "${trm_maxwait}" ]; then
189 sleep "$((trm_maxwait / 6))"
190 timeout
="$((timeout + (trm_maxwait / 6)))"
192 f_log
"debug" "f_wifi ::: radio_list: ${trm_radiolist}, radio: ${radio}, timeout: ${timeout}"
195 # vpn helper function
198 local rc result info iface vpn vpn_service vpn_iface vpn_instance vpn_status vpn_action
="${1}"
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
")"
205 if [ ! -f "${trm_vpnfile}" ] || { [ -f "${trm_vpnfile}" ] && [ "${vpn_action}" = "enable" ]; }; then
206 for info in ${trm_vpninfolist}; do
208 [ "${iface}" = "${info}" ] && vpn_instance="" || vpn_instance="${info##*&&}"
209 vpn_status="$
(ifstatus
"${iface}" | jsonfilter
-q -l1 -e '@.up')"
210 if [ "${vpn_status}" = "true
" ]; then
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}"
215 f_log "info
" "take down vpn interface
'${iface}/${vpn_instance:-"-"}' (initial
)"
218 rm -f "${trm_vpnfile}"
219 elif [ "${vpn}" = "1" ] && [ -n "${vpn_iface}" ] && [ "${vpn_action}" = "enable_keep
" ]; then
220 for info in ${trm_vpninfolist}; do
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
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}"
229 f_log "info
" "take down vpn interface
'${iface}/${vpn_instance:-"-"}' (switch
)"
230 rm -f "${trm_vpnfile}"
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
240 if [ "${result}" = "net ok" ] ||
[ "${vpn_action}" = "disable" ]; then
241 for info
in ${trm_vpninfolist}; do
243 if [ "${iface}" = "${vpn_iface}" ]; then
244 [ "${iface}" = "${info}" ] && vpn_instance="" || vpn_instance="${info##*&&}"
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
253 [ -n "${rc}" ] && f_jsnup
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:-"-"}"
259 # mac helper function
262 local result ifname macaddr action
="${1}" section
="${2}"
264 if [ "${action}" = "set" ]; then
265 macaddr
="$(f_getval "macaddr
")"
266 if [ -n "${macaddr}" ]; then
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}"
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}')"
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}')"
286 printf "%s" "${result}"
287 f_log
"debug" "f_mac ::: action: ${action:-"-"}, section: ${section:-"-"}, macaddr: ${macaddr:-"-"}, result: ${result:-"-"}"
290 # set connection information
293 local expiry action
="${1}"
295 if [ -n "${trm_uplinkcfg}" ]; then
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
")"
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
")"
310 if [ -s "${trm_ntpfile}" ]; then
311 uci_set
"travelmate" "${trm_uplinkcfg}" "con_end" "$(date "+%Y.
%m.
%d-
%H
:%M
:%S
")"
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"
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"
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
")"
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}"
345 f_log
"debug" "f_ctrack ::: action: ${action:-"-"}, uplink_config: ${trm_uplinkcfg:-"-"}"
348 # get openvpn information
351 local file instance device
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}"
368 local device section
="${1}"
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}"
377 if [ -f "/etc/config/openvpn" ]; then
379 config_foreach uci_config
"openvpn"
381 f_log
"debug" "f_getovpn ::: ovpninfolist: ${trm_ovpninfolist:-"-"}"
384 # get logical vpn network interfaces
387 local info proto device iface
="${1}"
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}")"
397 elif [ "${proto}" = "none
" ] && [ -n "${device}" ]; then
398 if [ -z "${trm_ovpninfolist}" ]; then
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##*&&}")"
412 f_log
"debug" "f_getvpn ::: iface: ${iface:-"-"}, proto: ${proto:-"-"}, device: ${device:-"-"}, ifacelist: ${trm_vpnifacelist:-"-"}, infolist: ${trm_vpninfolist:-"-"}"
415 # get wan gateway addresses
418 local result wan4_if wan4_gw wan6_if wan6_gw
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}"
428 printf "%s" "${result}"
429 f_log
"debug" "f_getgw ::: wan4_gw: ${wan4_gw:-"-"}, wan6_gw: ${wan6_gw:-"-"}, result: ${result:-"-"}"
432 # get uplink config section
435 local t_radio t_essid t_bssid radio
="${1}" essid="${2}" bssid="${3}" cnt
="0"
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}]"
448 f_log
"debug" "f_getcfg ::: status: ${status}, section: ${section}, uplink_config: ${trm_uplinkcfg:-"-"}"
451 # get travelmate option value in 'uplink' sections
454 local result t_option
="${1}"
456 if [ -n "${trm_uplinkcfg}" ]; then
457 result
="$(uci_get "travelmate
" "${trm_uplinkcfg}" "${t_option}")"
458 printf "%s" "${result}"
460 f_log
"debug" "f_getval ::: option: ${t_option:-"-"}, result: ${result:-"-"}, uplink_config: ${trm_uplinkcfg:-"-"}"
463 # set 'wifi-device' sections
466 local disabled radio
="${1}"
468 disabled
="$(uci_get "wireless
" "${radio}" "disabled
")"
469 if [ "${disabled}" = "1" ]; then
470 uci_set wireless
"${radio}" "disabled" "0"
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}")"
477 f_log
"debug" "f_setdev ::: radio: ${radio:-"-"}, radio_list(cnf/cur): ${trm_radio:-"-"}/${trm_radiolist:-"-"}, disabled: ${disabled:-"-"}"
480 # set 'wifi-iface' sections
483 local mode radio essid bssid enabled disabled con_start con_end con_start_expiry con_end_expiry section
="${1}" proactive
="${2}"
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
")"
491 f_getcfg
"${radio}" "${essid}" "${bssid}"
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
")"
499 if [ "${enabled}" = "0" ] && [ -n "${con_end}" ] && [ -n "${con_end_expiry}" ] && [ "${con_end_expiry}" != "0" ]; then
500 d1
="$(date -d "${con_end}" "+%s
")"
502 d3
="$(((d2 - d1) / 60))"
503 if [ "${d3}" -ge "${con_end_expiry}" ]; then
505 f_ctrack
"end_expiry"
507 elif [ "${enabled}" = "1" ] && [ -n "${con_start}" ] && [ -n "${con_start_expiry}" ] && [ "${con_start_expiry}" != "0" ]; then
508 d1
="$(date -d "${con_start}" "+%s
")"
510 d3
="$((d1 + (con_start_expiry * 60)))"
511 if [ "${d2}" -gt "${d3}" ]; then
513 f_ctrack
"start_expiry"
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}"
525 uci_set
"wireless" "${section}" "disabled" "1"
528 if [ "${enabled}" = "1" ]; then
529 trm_stalist
="$(f_trim "${trm_stalist} ${section}-${radio}")"
532 f_log
"debug" "f_setif ::: enabled: ${enabled}, section: ${section}, active_sta: ${trm_activesta:-"-"}, uplink_config: ${trm_uplinkcfg:-"-"}"
538 local wifi_cfg trm_cfg new_uplink
="1" offset
="1" radio
="${1}" essid
="${2}"
540 if [ "${trm_maxautoadd}" = "0" ] || [ "${trm_opensta:-0}" -lt "${trm_maxautoadd}" ]; then
542 local type="${1}" name
="${2}"
544 if [ "${type}" = "wifi-iface" ]; then
545 if [ "$(uci_get "wireless.
${name}.ssid
")" = "${essid}" ] &&
546 [ "$(uci_get "wireless.
${name}.device
")" = "${radio}" ]; then
550 offset
="$((offset + 1))"
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}"
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"
573 trm_cfg
="$(uci -q add travelmate uplink)"
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"
582 if [ -n "${trm_stdvpnservice}" ] && [ -n "${trm_stdvpniface}" ]; then
584 set travelmate."${trm_cfg}".vpnservice="${trm_stdvpnservice}"
585 set travelmate."${trm_cfg}".vpniface="${trm_stdvpniface}"
586 set travelmate."${trm_cfg}".vpn="1"
589 trm_opensta
="$((trm_opensta + 1))"
590 uci_commit
"travelmate"
591 uci_commit
"wireless"
593 if [ ! -f "${trm_refreshfile}" ]; then
594 printf "%s" "ui_reload" >"${trm_refreshfile}"
596 f_log
"info" "open uplink '${radio}/${essid}' added to wireless config"
597 printf "%s" "${wifi_cfg}-${radio}"
599 f_log
"debug" "f_addsta ::: radio: ${radio:-"-"}, essid: ${essid}, opensta/maxautoadd: ${trm_opensta:-"-"}/${trm_maxautoadd:-"-"}, new_uplink: ${new_uplink}, offset: ${offset}"
605 local err_msg raw json_raw html_raw html_cp js_cp json_ec json_rc json_cp json_ed result
="net nok"
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}'"
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}'"
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}'"
637 elif [ "${json_ec}" = "28" ]; then
638 if [ -n "$
(f_getgw
)" ]; then
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}"
648 # check interface status
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}"
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//-/}"
661 f_getcfg "${sta_radio}" "${sta_essid}" "${sta_bssid}"
663 if [ "${mode}" != "rev" ] && [ -n "${sta_radio}" ] && [ "${sta_radio}" != "-" ] && [ -n "${sta_essid}" ] && [ "${sta_essid}" != "-" ]; then
664 enabled="$
(f_getval
"enabled")"
666 if { [ "${mode}" != "initial" ] && [ "${mode}" != "dev" ] && [ "${status}" = "false" ]; } ||
667 { [ "${mode}" = "dev" ] && { [ "${status}" = "false" ] || { [ "${trm_ifstatus}" != "${status}" ] && [ "${enabled}" = "0" ]; }; }; }; then
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}"
680 if [ "${status}" = "false
" ]; then
681 sleep "$
((trm_maxwait
/ 5))"
684 elif [ "${mode}" = "rev" ]; then
686 trm_ifstatus="${status}"
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
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
698 if [ "${trm_captive}" = "1" ]; then
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}"
705 /etc/init.d/dnsmasq reload
706 f_log "info" "captive portal domain '${cp_domain}' added to to dhcp rebind whitelist"
712 if [ -n "${cp_domain}" ]; then
713 trm_connection="${result:-"-"}/${trm_ifquality}"
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
720 if [ "${rc}" = "255" ]; then
721 f_log "info" "captive portal login script for '${cp_domain}' failed with rc '${rc}'"
723 trm_ifstatus="${status}"
727 f_log "info" "captive portal login script for '${cp_domain}' has been finished with rc '${rc}'"
728 if [ "${rc}" = "0" ]; then
735 if [ "${result}" = "net nok" ]; then
737 if [ "${trm_netcheck}" = "1" ]; then
738 f_log "info" "uplink has no internet"
739 trm_ifstatus="${status}"
744 trm_connection="${result:-"-"}/${trm_ifquality}"
748 elif [ -n "${trm_connection}" ] && { [ "${trm_netcheck}" = "1" ] || [ "${mode}" = "initial" ]; }; then
749 f_log "info" "uplink is out of range (${trm_ifquality}/${trm_minquality})"
752 trm_ifstatus="${status}"
756 elif [ "${mode}" = "initial" ] || [ "${mode}" = "sta" ]; then
758 trm_ifstatus="${status}"
762 elif [ -n "${trm_connection}" ]; then
765 trm_ifstatus="${status}"
768 elif [ "${mode}" = "initial" ]; then
769 trm_ifstatus="${status}"
775 if [ "${mode}" = "initial" ]; then
776 trm_ifstatus="${status}"
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}"
784 # update runtime information
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"
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}"
801 json_get_var last_date "last_run"
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")"
808 elif [ "${status}" = "error" ]; then
810 status="program error"
813 status="running (not connected)"
815 if [ -z "${last_date}" ]; then
816 last_date="$(date "+%Y.%m.%d-%H:%M:%S")"
818 if [ -s "${trm_ntpfile}" ]; then
821 if [ "${trm_mail}" = "1" ] && [ -f "${trm_mailfile}" ]; then
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}"
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
839 "${trm_mailpgm}" >/dev/null 2>&1
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}"
848 local class="${1}" log_msg="${2}"
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}"
854 printf "%s %s %s\n" "${class}" "trm-${trm_ver}[${$}]" "${log_msg}"
856 if [ "${class}" = "err" ]; then
865 # main function for connection handling
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
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
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//-/}"
884 f_log "debug" "f_main-2 ::: config_radio: ${config_radio}, config_essid: \"${config_essid}\", config_bssid: ${config_bssid:-"-"}"
886 uci_commit "wireless"
887 f_check "dev" "false"
889 f_log "debug" "f_main-3 ::: radio_list: ${trm_radiolist:-"-"}, sta_list: ${trm_stalist:-"-"}"
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}'"
904 for sta in ${trm_stalist:-"${radio}"}; do
905 if [ "${sta}" != "${radio}" ]; then
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}'"
915 if [ -n "${trm_connection}" ] && [ "${radio}" = "${config_radio}" ] && [ "${sta_radio}" = "${config_radio}" ] &&
916 [ "${sta_essid}" = "${config_essid}" ] && [ "${sta_bssid}" = "${config_bssid}" ]; then
919 f_log "debug" "f_main-4 ::: config_radio: ${config_radio}, config_essid: ${config_essid}, config_bssid: ${config_bssid:-"-"}"
922 f_log "debug" "f_main-5 ::: sta_radio: ${sta_radio}, sta_essid: \"${sta_essid}\", sta_bssid: ${sta_bssid:-"-"}"
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}'"
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")"
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
959 uci_set "wireless" "${trm_activesta}" "disabled" "1"
960 uci_commit "wireless"
961 f_check "rev" "false"
963 f_log "info" "uplink connection terminated '${config_radio}/${config_essid}/${config_bssid:-"-"}'"
964 unset config_radio config_essid config_bssid
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"
979 f_log "info" "connected to uplink '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' with mac '${sta_mac:-"-"}' (${retrycnt}/${trm_maxretry})"
983 uci -q revert "wireless
"
984 f_check "rev" "false
"
985 if [ "${retrycnt}" = "${trm_maxretry}" ]; then
987 f_log "info
" "uplink has been disabled
'${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' (${retrycnt}/${trm_maxretry})"
991 f_log "info" "can't connect to uplink
'${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' (${retrycnt}/${trm_maxretry})"
994 retrycnt="$((retrycnt + 1))"
995 sleep "$((trm_maxwait / 6))"
1008 # source required system libraries
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"
1015 f_log "err" "system libraries not found"
1018 # force ntp restart/sync
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"
1025 # control travelmate actions
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)"
1035 elif [ -n "${trm_action}" ]; then
1036 f_log "info" "travelmate instance started ::: action: ${trm_action}, pid: ${$}"
1042 sleep "${trm_timeout}" 0
1044 if [ "${rc}" != "0" ]; then
1045 if [ -z "$(f_getgw)" ]; then
1049 if [ "${rc}" = "0" ]; then