e0a6b497716386dd2d3e8a81084f7f2c1a15bbc0
[feed/packages.git] / net / travelmate / files / travelmate.sh
1 #!/bin/sh
2 # travelmate, a wlan connection manager for travel router
3 # Copyright (c) 2016-2021 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=1091,2086,3040,3043,3057,3060
8
9 export LC_ALL=C
10 export PATH="/usr/sbin:/usr/bin:/sbin:/bin"
11 set -o pipefail
12
13 trm_ver="2.0.7"
14 trm_enabled="0"
15 trm_debug="0"
16 trm_iface=""
17 trm_captive="1"
18 trm_proactive="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_opensta="0"
32 trm_radio=""
33 trm_connection=""
34 trm_wpaflags=""
35 trm_uplinkcfg=""
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"}"
50
51 # load travelmate environment
52 #
53 f_env() {
54 local check wpa_checks result
55
56 if [ "${trm_action}" = "stop" ]; then
57 return
58 fi
59
60 unset trm_stalist trm_radiolist trm_uplinklist trm_uplinkcfg trm_wpaflags trm_activesta trm_opensta
61
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}')"
64
65 config_cb() {
66 local name="${1}" type="${2}"
67
68 if [ "${name}" = "travelmate" ] && [ "${type}" = "global" ]; then
69 option_cb() {
70 local option="${1}" value="${2}"
71 eval "${option}=\"${value}\""
72 }
73 elif [ "${name}" = "uplink" ]; then
74 if [ "$(uci_get "travelmate.${type}.opensta")" = "1" ]; then
75 eval "trm_opensta=\"$((${trm_opensta:-0} + 1))\""
76 fi
77 else
78 option_cb() {
79 return 0
80 }
81 fi
82 }
83 config_load travelmate
84
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
94 fi
95
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)")"
101 else
102 result="$(f_trim "${result} ${check}: $(f_char 0)")"
103 fi
104 fi
105 done
106 trm_wpaflags="$(printf "%s" "${result}" | awk '{printf "%s %s, %s %s, %s %s, %s %s",$1,$2,$3,$4,$5,$6,$7,$8}')"
107
108 config_load wireless
109 config_foreach f_setdev "wifi-device"
110 if [ -n "$(uci -q changes "wireless")" ]; then
111 uci_commit "wireless"
112 f_wifi
113 fi
114
115 json_load_file "${trm_rtfile}" >/dev/null 2>&1
116
117 if ! json_select data >/dev/null 2>&1; then
118 : >"${trm_rtfile}"
119 json_init
120 json_add_object "data"
121 fi
122 f_log "debug" "f_env ::: auto_sta: ${trm_opensta:-"-"}, wpa_flags: ${trm_wpaflags}, sys_ver: ${trm_sysver}"
123 }
124
125 # trim helper function
126 #
127 f_trim() {
128 local trim="${1}"
129
130 trim="${trim#"${trim%%[![:space:]]*}"}"
131 trim="${trim%"${trim##*[![:space:]]}"}"
132 printf "%s" "${trim}"
133 }
134
135 # status helper function
136 #
137 f_char() {
138 local result input="${1}"
139
140 if [ "${input}" = "1" ]; then
141 result="✔"
142 else
143 result="✘"
144 fi
145 printf "%s" "${result}"
146 }
147
148 # wifi helper function
149 #
150 f_wifi() {
151 local status radio radio_up timeout="0"
152
153 "${trm_wifi}" reload
154 for radio in ${trm_radiolist}; do
155 while true; do
156 if [ "${timeout}" -ge "${trm_maxwait}" ]; then
157 break 2
158 fi
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}"
164 radio_up="${radio}"
165 fi
166 timeout="$((timeout + 1))"
167 sleep 1
168 else
169 continue 2
170 fi
171 done
172 done
173 if [ "${timeout}" -lt "${trm_maxwait}" ]; then
174 sleep "$((trm_maxwait / 6))"
175 timeout="$((timeout + (trm_maxwait / 6)))"
176 fi
177 f_log "debug" "f_wifi ::: radio_list: ${trm_radiolist}, radio: ${radio}, timeout: ${timeout}"
178 }
179
180 # vpn helper function
181 #
182 f_vpn() {
183 local rc vpn vpn_service vpn_iface vpn_action="${1}"
184
185 vpn="$(f_getval "vpn")"
186 vpn_service="$(f_getval "vpnservice")"
187 vpn_iface="$(f_getval "vpniface")"
188 [ -z "${vpn_action}" ] && { [ "${vpn}" = "1" ] && vpn_action="enable" || vpn_action="disable"; }
189
190 if [ -x "${trm_vpnpgm}" ] && [ -n "${vpn_service}" ] && [ -n "${vpn_iface}" ] && [ -f "${trm_ntpfile}" ]; then
191 if { [ "${vpn_action}" = "disable" ] && [ -f "${trm_vpnfile}" ]; } ||
192 { [ "${vpn}" = "1" ] && [ "${vpn_action}" = "enable" ] && [ ! -f "${trm_vpnfile}" ]; } ||
193 { [ "${vpn}" != "1" ] && [ "${vpn_action}" = "enable" ] && [ -f "${trm_vpnfile}" ]; }; then
194 "${trm_vpnpgm}" "${vpn}" "${vpn_action}" "${vpn_service}" "${vpn_iface}" >/dev/null 2>&1
195 rc="${?}"
196 fi
197 if [ "${vpn}" = "1" ] && [ "${vpn_action}" = "enable" ] && [ "${rc}" = "0" ]; then
198 : >"${trm_vpnfile}"
199 elif { [ "${vpn}" != "1" ] || [ "${vpn_action}" = "disable" ]; } && [ -f "${trm_vpnfile}" ]; then
200 rm -f "${trm_vpnfile}"
201 fi
202 [ -n "${rc}" ] && f_jsnup
203 fi
204 f_log "debug" "f_vpn ::: enabled: ${vpn:-"-"}, action: ${vpn_action}, service: ${vpn_service:-"-"}, iface: ${vpn_iface:-"-"}, rc: ${rc:-"-"}, program: ${trm_vpnpgm}"
205 }
206
207 # mac helper function
208 #
209 f_mac() {
210 local result ifname macaddr action="${1}" section="${2}"
211
212 if [ "${action}" = "set" ]; then
213 macaddr="$(f_getval "macaddr")"
214 if [ -n "${macaddr}" ]; then
215 result="${macaddr}"
216 uci_set "wireless" "${section}" "macaddr" "${result}"
217 elif [ "${trm_randomize}" = "1" ]; then
218 result="$(hexdump -n6 -ve '/1 "%.02X "' /dev/random 2>/dev/null |
219 awk -v local="2,6,A,E" -v seed="$(date +%s)" 'BEGIN{srand(seed)}NR==1{split(local,b,",");
220 seed=int(rand()*4+1);printf "%s%s:%s:%s:%s:%s:%s",substr($1,0,1),b[seed],$2,$3,$4,$5,$6}')"
221 uci_set "wireless" "${section}" "macaddr" "${result}"
222 else
223 uci_remove "wireless" "${section}" "macaddr" 2>/dev/null
224 ifname="$(ubus -S call network.wireless status 2>/dev/null | jsonfilter -q -l1 -e '@.*.interfaces[@.config.mode="sta"].ifname')"
225 result="$(${trm_iwinfo} "${ifname}" info 2>/dev/null | awk '/Access Point:/{printf "%s",$3}')"
226 fi
227 elif [ "${action}" = "get" ]; then
228 result="$(uci_get "wireless" "${section}" "macaddr")"
229 if [ -z "${result}" ]; then
230 ifname="$(ubus -S call network.wireless status 2>/dev/null | jsonfilter -q -l1 -e '@.*.interfaces[@.config.mode="sta"].ifname')"
231 result="$(${trm_iwinfo} "${ifname}" info 2>/dev/null | awk '/Access Point:/{printf "%s",$3}')"
232 fi
233 fi
234 printf "%s" "${result}"
235 f_log "debug" "f_mac ::: action: ${action:-"-"}, section: ${section:-"-"}, macaddr: ${macaddr:-"-"}, result: ${result:-"-"}"
236 }
237
238 # set connection information
239 #
240 f_ctrack() {
241 local expiry action="${1}"
242
243 if [ -n "${trm_uplinkcfg}" ]; then
244 case "${action}" in
245 "start")
246 uci_remove "travelmate" "${trm_uplinkcfg}" "con_start" 2>/dev/null
247 uci_remove "travelmate" "${trm_uplinkcfg}" "con_end" 2>/dev/null
248 if [ -f "${trm_ntpfile}" ]; then
249 uci_set "travelmate" "${trm_uplinkcfg}" "con_start" "$(date "+%Y.%m.%d-%H:%M:%S")"
250 fi
251 ;;
252 "refresh")
253 if [ -f "${trm_ntpfile}" ] && [ -z "$(uci_get "travelmate" "${trm_uplinkcfg}" "con_start")" ]; then
254 uci_set "travelmate" "${trm_uplinkcfg}" "con_start" "$(date "+%Y.%m.%d-%H:%M:%S")"
255 fi
256 ;;
257 "end")
258 if [ -f "${trm_ntpfile}" ]; then
259 uci_set "travelmate" "${trm_uplinkcfg}" "con_end" "$(date "+%Y.%m.%d-%H:%M:%S")"
260 fi
261 ;;
262 "start_expiry")
263 if [ -f "${trm_ntpfile}" ]; then
264 expiry="$(uci_get "travelmate" "${trm_uplinkcfg}" "con_start_expiry")"
265 uci_set "travelmate" "${trm_uplinkcfg}" "enabled" "0"
266 uci_set "travelmate" "${trm_uplinkcfg}" "con_end" "$(date "+%Y.%m.%d-%H:%M:%S")"
267 f_log "info" "uplink '${radio}/${essid}/${bssid:-"-"}' expired after ${expiry} minutes"
268 fi
269 ;;
270 "end_expiry")
271 if [ -f "${trm_ntpfile}" ]; then
272 expiry="$(uci_get "travelmate" "${trm_uplinkcfg}" "con_end_expiry")"
273 uci_set "travelmate" "${trm_uplinkcfg}" "enabled" "1"
274 uci_remove "travelmate" "${trm_uplinkcfg}" "con_start" 2>/dev/null
275 uci_remove "travelmate" "${trm_uplinkcfg}" "con_end" 2>/dev/null
276 f_log "info" "uplink '${radio}/${essid}/${bssid:-"-"}' re-enabled after ${expiry} minutes"
277 fi
278 ;;
279 "disabled")
280 uci_set "travelmate" "${trm_uplinkcfg}" "enabled" "0"
281 if [ -f "${trm_ntpfile}" ]; then
282 uci_set "travelmate" "${trm_uplinkcfg}" "con_end" "$(date "+%Y.%m.%d-%H:%M:%S")"
283 fi
284 ;;
285 esac
286 if [ -n "$(uci -q changes "travelmate")" ]; then
287 uci_commit "travelmate"
288 if [ ! -f "${trm_refreshfile}" ]; then
289 printf "%s" "cfg_reload" >"${trm_refreshfile}"
290 fi
291 fi
292 fi
293 f_log "debug" "f_ctrack ::: action: ${action:-"-"}, uplink_config: ${trm_uplinkcfg:-"-"}"
294 }
295
296 # get wan gateway addresses
297 #
298 f_getgw() {
299 local result wan4_if wan4_gw wan6_if wan6_gw
300
301 network_flush_cache
302 network_find_wan wan4_if
303 network_find_wan6 wan6_if
304 network_get_gateway wan4_gw "${wan4_if}"
305 network_get_gateway6 wan6_gw "${wan6_if}"
306 if [ -n "${wan4_gw}" ] || [ -n "${wan6_gw}" ]; then
307 result="${wan4_gw} ${wan6_gw}"
308 fi
309 printf "%s" "${result}"
310 f_log "debug" "f_getgw ::: wan4_gw: ${wan4_gw:-"-"}, wan6_gw: ${wan6_gw:-"-"}, result: ${result:-"-"}"
311 }
312
313 # get uplink config section
314 #
315 f_getcfg() {
316 local t_radio t_essid t_bssid radio="${1}" essid="${2}" bssid="${3}" cnt="0"
317
318 while uci_get "travelmate" "@uplink[${cnt}]" >/dev/null 2>&1; do
319 t_radio="$(uci_get "travelmate" "@uplink[${cnt}]" "device")"
320 t_essid="$(uci_get "travelmate" "@uplink[${cnt}]" "ssid")"
321 t_bssid="$(uci_get "travelmate" "@uplink[${cnt}]" "bssid")"
322 if [ -n "${radio}" ] && [ -n "${essid}" ] &&
323 [ "${t_radio}" = "${radio}" ] && [ "${t_essid}" = "${essid}" ] && [ "${t_bssid}" = "${bssid}" ]; then
324 trm_uplinkcfg="@uplink[${cnt}]"
325 break
326 fi
327 cnt="$((cnt + 1))"
328 done
329 f_log "debug" "f_getcfg ::: status: ${status}, section: ${section}, uplink_config: ${trm_uplinkcfg:-"-"}"
330 }
331
332 # get travelmate option value in 'uplink' sections
333 #
334 f_getval() {
335 local result t_option="${1}"
336
337 if [ -n "${trm_uplinkcfg}" ]; then
338 result="$(uci_get "travelmate" "${trm_uplinkcfg}" "${t_option}")"
339 printf "%s" "${result}"
340 fi
341 f_log "debug" "f_getval ::: option: ${t_option:-"-"}, result: ${result:-"-"}, uplink_config: ${trm_uplinkcfg:-"-"}"
342 }
343
344 # set 'wifi-device' sections
345 #
346 f_setdev() {
347 local disabled radio="${1}"
348
349 disabled="$(uci_get "wireless" "${radio}" "disabled")"
350 if [ "${disabled}" = "1" ]; then
351 uci_set wireless "${radio}" "disabled" "0"
352 fi
353 if [ -n "${trm_radio}" ] && [ -z "${trm_radiolist}" ]; then
354 trm_radiolist="${trm_radio}"
355 elif [ -z "${trm_radio}" ] && ! printf "%s" "${trm_radiolist}" | grep -q "${radio}"; then
356 trm_radiolist="$(f_trim "${trm_radiolist} ${radio}")"
357 fi
358 f_log "debug" "f_setdev ::: radio: ${radio:-"-"}, radio_list(cnf/cur): ${trm_radio:-"-"}/${trm_radiolist:-"-"}, disabled: ${disabled:-"-"}"
359 }
360
361 # set 'wifi-iface' sections
362 #
363 f_setif() {
364 local mode radio essid bssid enabled disabled con_start con_end con_start_expiry con_end_expiry section="${1}" proactive="${2}"
365
366 mode="$(uci_get "wireless" "${section}" "mode")"
367 radio="$(uci_get "wireless" "${section}" "device")"
368 essid="$(uci_get "wireless" "${section}" "ssid")"
369 bssid="$(uci_get "wireless" "${section}" "bssid")"
370 disabled="$(uci_get "wireless" "${section}" "disabled")"
371
372 f_getcfg "${radio}" "${essid}" "${bssid}"
373
374 enabled="$(f_getval "enabled")"
375 con_start="$(f_getval "con_start")"
376 con_end="$(f_getval "con_end")"
377 con_start_expiry="$(f_getval "con_start_expiry")"
378 con_end_expiry="$(f_getval "con_end_expiry")"
379
380 if [ "${enabled}" = "0" ] && [ -n "${con_end}" ] && [ -n "${con_end_expiry}" ] && [ "${con_end_expiry}" != "0" ]; then
381 d1="$(date -d "${con_end}" "+%s")"
382 d2="$(date "+%s")"
383 d3="$(((d2 - d1) / 60))"
384 if [ "${d3}" -ge "${con_end_expiry}" ]; then
385 enabled="1"
386 f_ctrack "end_expiry"
387 fi
388 elif [ "${enabled}" = "1" ] && [ -n "${con_start}" ] && [ -n "${con_start_expiry}" ] && [ "${con_start_expiry}" != "0" ]; then
389 d1="$(date -d "${con_start}" "+%s")"
390 d2="$(date "+%s")"
391 d3="$((d1 + (con_start_expiry * 60)))"
392 if [ "${d2}" -gt "${d3}" ]; then
393 enabled="0"
394 f_ctrack "start_expiry"
395 fi
396 fi
397
398 if [ "${mode}" = "sta" ]; then
399 if [ "${enabled}" = "0" ] || { { [ -z "${disabled}" ] || [ "${disabled}" = "0" ]; } &&
400 { [ "${proactive}" = "0" ] || [ "${trm_ifstatus}" != "true" ]; }; }; then
401 uci_set "wireless" "${section}" "disabled" "1"
402 elif [ "${enabled}" = "1" ] && [ "${disabled}" = "0" ] && [ "${trm_ifstatus}" = "true" ] && [ "${proactive}" = "1" ]; then
403 if [ -z "${trm_activesta}" ]; then
404 trm_activesta="${section}"
405 else
406 uci_set "wireless" "${section}" "disabled" "1"
407 fi
408 fi
409 if [ "${enabled}" = "1" ]; then
410 trm_stalist="$(f_trim "${trm_stalist} ${section}-${radio}")"
411 fi
412 fi
413 f_log "debug" "f_setif ::: enabled: ${enabled}, section: ${section}, active_sta: ${trm_activesta:-"-"}, uplink_config: ${trm_uplinkcfg:-"-"}"
414 }
415
416 # add open uplinks
417 #
418 f_addsta() {
419 local uci_cfg new_uplink="1" offset="1" radio="${1}" essid="${2}"
420
421 if [ "${trm_maxautoadd}" = "0" ] || [ "${trm_opensta:-0}" -lt "${trm_maxautoadd}" ]; then
422 config_cb() {
423 local type="${1}" name="${2}"
424
425 if [ "${type}" = "wifi-iface" ]; then
426 if [ "$(uci_get "wireless.${name}.ssid")" = "${essid}" ] &&
427 [ "$(uci_get "wireless.${name}.device")" = "${radio}" ]; then
428 new_uplink="0"
429 return 0
430 fi
431 offset="$((offset + 1))"
432 fi
433 }
434 config_load wireless
435 else
436 new_uplink="0"
437 fi
438
439 if [ "${new_uplink}" = "1" ]; then
440 uci_cfg="trm_uplink$((offset + 1))"
441 while [ -n "$(uci_get "wireless.${uci_cfg}")" ]; do
442 offset="$((offset + 1))"
443 uci_cfg="trm_uplink${offset}"
444 done
445 uci -q batch <<-EOC
446 set wireless."${uci_cfg}"="wifi-iface"
447 set wireless."${uci_cfg}".mode="sta"
448 set wireless."${uci_cfg}".network="${trm_iface}"
449 set wireless."${uci_cfg}".device="${radio}"
450 set wireless."${uci_cfg}".ssid="${essid}"
451 set wireless."${uci_cfg}".encryption="none"
452 set wireless."${uci_cfg}".disabled="1"
453 EOC
454 uci_cfg="$(uci -q add travelmate uplink)"
455 uci -q batch <<-EOC
456 set travelmate."${uci_cfg}".device="${radio}"
457 set travelmate."${uci_cfg}".ssid="${essid}"
458 set travelmate."${uci_cfg}".opensta="1"
459 set travelmate."${uci_cfg}".con_start_expiry="0"
460 set travelmate."${uci_cfg}".con_end_expiry="0"
461 set travelmate."${uci_cfg}".enabled="1"
462 EOC
463 if [ -n "$(uci -q changes "travelmate")" ] || [ -n "$(uci -q changes "wireless")" ]; then
464 trm_opensta="$((trm_opensta + 1))"
465 uci_commit "travelmate"
466 uci_commit "wireless"
467 f_wifi
468 if [ ! -f "${trm_refreshfile}" ]; then
469 printf "%s" "ui_reload" >"${trm_refreshfile}"
470 fi
471 f_log "info" "open uplink '${radio}/${essid}' added to wireless config"
472 fi
473 fi
474 f_log "debug" "f_addsta ::: radio: ${radio:-"-"}, essid: ${essid}, opensta/maxautoadd: ${trm_opensta:-"-"}/${trm_maxautoadd:-"-"}, new_uplink: ${new_uplink}, offset: ${offset}"
475 }
476
477 # check net status
478 #
479 f_net() {
480 local err_msg raw html_raw html_cp json_raw json_ec json_rc json_cp json_ed result="net nok"
481
482 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}")"
483 json_raw="${raw#*\{}"
484 html_raw="${raw%%\{*}"
485 if [ -n "${json_raw}" ]; then
486 json_ec="$(printf "%s" "{${json_raw}" | jsonfilter -q -l1 -e '@.exitcode')"
487 json_rc="$(printf "%s" "{${json_raw}" | jsonfilter -q -l1 -e '@.response_code')"
488 json_cp="$(printf "%s" "{${json_raw}" | jsonfilter -q -l1 -e '@.redirect_url' | awk 'BEGIN{FS="/"}{printf "%s",tolower($3)}')"
489 if [ "${json_ec}" = "0" ]; then
490 if [ -n "${json_cp}" ]; then
491 result="net cp '${json_cp}'"
492 else
493 if [ "${json_rc}" = "200" ] || [ "${json_rc}" = "204" ]; then
494 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}')"
495 if [ -n "${html_cp}" ]; then
496 result="net cp '${html_cp}'"
497 else
498 result="net ok"
499 fi
500 fi
501 fi
502 else
503 err_msg="$(printf "%s" "{${json_raw}" | jsonfilter -q -l1 -e '@.errormsg')"
504 json_ed="$(printf "%s" "{${err_msg}" | awk '/([[:alnum:]_-]{1,63}\.)+[[:alpha:]]+$/{printf "%s",tolower($NF)}')"
505 if [ "${json_ec}" = "6" ]; then
506 if [ -n "${json_ed}" ] && [ "${json_ed}" != "${trm_captiveurl#http*://*}" ]; then
507 result="net cp '${json_ed}'"
508 fi
509 elif [ "${json_ec}" = "28" ]; then
510 if [ -n "$(f_getgw)" ]; then
511 result="net ok"
512 fi
513 fi
514 fi
515 fi
516 printf "%s" "${result}"
517 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}"
518 }
519
520 # check interface status
521 #
522 f_check() {
523 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
525 if [ "${mode}" = "initial" ] || [ "${mode}" = "dev" ]; then
526 json_get_var station_id "station_id"
527 sta_radio="${station_id%%/*}"
528 sta_essid="${station_id%/*}"
529 sta_essid="${sta_essid#*/}"
530 sta_bssid="${station_id##*/}"
531 sta_bssid="${sta_bssid//-/}"
532 fi
533 f_getcfg "${sta_radio}" "${sta_essid}" "${sta_bssid}"
534
535 if [ "${mode}" != "rev" ] && [ -n "${sta_radio}" ] && [ "${sta_radio}" != "-" ] && [ -n "${sta_essid}" ] && [ "${sta_essid}" != "-" ]; then
536 enabled="$(f_getval "enabled")"
537 fi
538 if { [ "${mode}" != "initial" ] && [ "${mode}" != "dev" ] && [ "${status}" = "false" ]; } ||
539 { [ "${mode}" = "dev" ] && { [ "${status}" = "false" ] || { [ "${trm_ifstatus}" != "${status}" ] && [ "${enabled}" = "0" ]; }; }; }; then
540 f_wifi
541 fi
542 while [ "${wait_time}" -le "${trm_maxwait}" ]; do
543 dev_status="$(ubus -S call network.wireless status 2>/dev/null)"
544 if [ -n "${dev_status}" ]; then
545 if [ "${mode}" = "dev" ]; then
546 if [ "${trm_ifstatus}" != "${status}" ]; then
547 trm_ifstatus="${status}"
548 f_jsnup
549 fi
550 if [ "${status}" = "false" ]; then
551 sleep "$((trm_maxwait / 5))"
552 fi
553 break
554 elif [ "${mode}" = "rev" ]; then
555 unset trm_connection
556 trm_ifstatus="${status}"
557 break
558 else
559 ifname="$(printf "%s" "${dev_status}" | jsonfilter -q -l1 -e '@.*.interfaces[@.config.mode="sta"].ifname')"
560 if [ -n "${ifname}" ] && [ "${enabled}" = "1" ]; then
561 trm_ifquality="$(${trm_iwinfo} "${ifname}" info 2>/dev/null | awk -F '[ ]' '/Link Quality:/{split($NF,var0,"/");printf "%i\n",(var0[1]*100/var0[2])}')"
562 if [ "${trm_ifquality}" -ge "${trm_minquality}" ]; then
563 trm_ifstatus="$(ubus -S call network.interface dump 2>/dev/null | jsonfilter -q -l1 -e "@.interface[@.device=\"${ifname}\"].up")"
564 if [ "${trm_ifstatus}" = "true" ]; then
565 result="$(f_net)"
566 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}"
571 uci_commit "dhcp"
572 /etc/init.d/dnsmasq reload
573 f_log "info" "captive portal domain '${cp_domain}' added to to dhcp rebind whitelist"
574 fi
575 if [ -n "${cp_domain}" ] && [ "${trm_captive}" = "1" ]; then
576 trm_connection="${result:-"-"}/${trm_ifquality}"
577 f_jsnup
578 login_script="$(f_getval "script")"
579 if [ -x "${login_script}" ]; then
580 login_script_args="$(f_getval "script_args")"
581 "${login_script}" ${login_script_args} >/dev/null 2>&1
582 rc="${?}"
583 if [ "${rc}" = "255" ]; then
584 f_log "info" "captive portal login script for '${cp_domain}' failed with rc '${rc}'"
585 unset trm_connection
586 trm_ifstatus="${status}"
587 f_jsnup
588 break
589 else
590 f_log "info" "captive portal login script for '${cp_domain}' has been finished with rc '${rc}'"
591 if [ "${rc}" = "0" ]; then
592 result="$(f_net)"
593 fi
594 fi
595 fi
596 fi
597 fi
598 if [ "${trm_netcheck}" = "1" ] && [ "${result}" = "net nok" ]; then
599 f_log "info" "uplink has no internet"
600 f_vpn "disable"
601 trm_ifstatus="${status}"
602 f_jsnup
603 break
604 fi
605 trm_connection="${result:-"-"}/${trm_ifquality}"
606 f_jsnup
607 break
608 fi
609 elif [ -n "${trm_connection}" ] && { [ "${trm_netcheck}" = "1" ] || [ "${mode}" = "initial" ]; }; then
610 f_log "info" "uplink is out of range (${trm_ifquality}/${trm_minquality})"
611 f_vpn "disable"
612 unset trm_connection
613 trm_ifstatus="${status}"
614 f_ctrack "end"
615 f_jsnup
616 break
617 elif [ "${mode}" = "initial" ] || [ "${mode}" = "sta" ]; then
618 unset trm_connection
619 trm_ifstatus="${status}"
620 f_jsnup
621 break
622 fi
623 elif [ -n "${trm_connection}" ]; then
624 f_vpn "disable"
625 unset trm_connection
626 trm_ifstatus="${status}"
627 f_jsnup
628 break
629 elif [ "${mode}" = "initial" ]; then
630 trm_ifstatus="${status}"
631 f_jsnup
632 break
633 fi
634 fi
635 fi
636 if [ "${mode}" = "initial" ]; then
637 trm_ifstatus="${status}"
638 f_jsnup
639 break
640 fi
641 wait_time="$((wait_time + 1))"
642 sleep 1
643 done
644 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}"
645 }
646
647 # update runtime information
648 #
649 f_jsnup() {
650 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"
651
652 if [ "${status}" = "true" ]; then
653 status="connected (${trm_connection:-"-"})"
654 dev_status="$(ubus -S call network.wireless status 2>/dev/null)"
655 section="$(printf "%s" "${dev_status}" | jsonfilter -q -l1 -e '@.*.interfaces[@.config.mode="sta"].section')"
656 if [ -n "${section}" ]; then
657 sta_iface="$(uci_get "wireless" "${section}" "network")"
658 sta_radio="$(uci_get "wireless" "${section}" "device")"
659 sta_essid="$(uci_get "wireless" "${section}" "ssid")"
660 sta_bssid="$(uci_get "wireless" "${section}" "bssid")"
661 sta_mac="$(f_mac "get" "${section}")"
662 f_getcfg "${sta_radio}" "${sta_essid}" "${sta_bssid}"
663 vpn="$(f_getval "vpn")"
664 fi
665 json_get_var last_date "last_run"
666 json_get_var last_station "station_id"
667 json_get_var last_status "travelmate_status"
668
669 if { [ -f "${trm_ntpfile}" ] && [ ! -s "${trm_ntpfile}" ]; } || [ "${last_status}" = "running (not connected)" ] ||
670 { [ -n "${last_station}" ] && [ "${last_station}" != "${sta_radio:-"-"}/${sta_essid:-"-"}/${sta_bssid:-"-"}" ]; }; then
671 last_date="$(date "+%Y.%m.%d-%H:%M:%S")"
672 if [ -f "${trm_ntpfile}" ] && [ ! -s "${trm_ntpfile}" ]; then
673 printf "%s" "${last_date}" >"${trm_ntpfile}"
674 fi
675 fi
676 elif [ "${status}" = "error" ]; then
677 unset trm_connection
678 status="program error"
679 else
680 unset trm_connection
681 status="running (not connected)"
682 fi
683 if [ -z "${last_date}" ]; then
684 last_date="$(date "+%Y.%m.%d-%H:%M:%S")"
685 fi
686 if [ -s "${trm_ntpfile}" ]; then
687 ntp_done="1"
688 fi
689 if [ "${vpn}" = "1" ] && [ -f "${trm_vpnfile}" ]; then
690 vpn_done="1"
691 fi
692 if [ "${trm_mail}" = "1" ] && [ -f "${trm_mailfile}" ]; then
693 mail_done="1"
694 fi
695 json_add_string "travelmate_status" "${status}"
696 json_add_string "travelmate_version" "${trm_ver}"
697 json_add_string "station_id" "${sta_radio:-"-"}/${sta_essid:-"-"}/${sta_bssid:-"-"}"
698 json_add_string "station_mac" "${sta_mac:-"-"}"
699 json_add_string "station_interface" "${sta_iface:-"-"}"
700 json_add_string "wpa_flags" "${trm_wpaflags:-"-"}"
701 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})"
702 json_add_string "ext_hooks" "ntp: $(f_char ${ntp_done}), vpn: $(f_char ${vpn_done}), mail: $(f_char ${mail_done})"
703 json_add_string "last_run" "${last_date}"
704 json_add_string "system" "${trm_sysver}"
705 json_dump >"${trm_rtfile}"
706
707 if [ "${status%% (net ok/*}" = "connected" ] && [ "${trm_mail}" = "1" ] && [ -x "${trm_mailpgm}" ] && [ "${ntp_done}" = "1" ] && [ "${mail_done}" = "0" ]; then
708 if [ "${vpn}" != "1" ] || [ "${vpn_done}" = "1" ]; then
709 : >"${trm_mailfile}"
710 "${trm_mailpgm}" >/dev/null 2>&1
711 fi
712 fi
713 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}"
714 }
715
716 # write to syslog
717 #
718 f_log() {
719 local class="${1}" log_msg="${2}"
720
721 if [ -n "${log_msg}" ] && { [ "${class}" != "debug" ] || [ "${trm_debug}" = "1" ]; }; then
722 if [ -x "${trm_logger}" ]; then
723 "${trm_logger}" -p "${class}" -t "trm-${trm_ver}[${$}]" "${log_msg}"
724 else
725 printf "%s %s %s\n" "${class}" "trm-${trm_ver}[${$}]" "${log_msg}"
726 fi
727 if [ "${class}" = "err" ]; then
728 trm_ifstatus="error"
729 f_jsnup
730 : >"${trm_pidfile}"
731 exit 1
732 fi
733 fi
734 }
735
736 # main function for connection handling
737 #
738 f_main() {
739 local radio cnt retrycnt scan_dev scan_list scan_essid scan_bssid scan_open scan_quality
740 local station_id section sta sta_essid sta_bssid sta_radio sta_mac config_essid config_bssid config_radio
741
742 f_check "initial" "false"
743 f_log "debug" "f_main-1 ::: status: ${trm_ifstatus}, proactive: ${trm_proactive}"
744 if [ "${trm_ifstatus}" != "true" ] || [ "${trm_proactive}" = "1" ]; then
745 config_load wireless
746 config_foreach f_setif wifi-iface "${trm_proactive}"
747 if [ "${trm_ifstatus}" = "true" ] && [ -n "${trm_activesta}" ] && [ "${trm_proactive}" = "1" ]; then
748 json_get_var station_id "station_id"
749 config_radio="${station_id%%/*}"
750 config_essid="${station_id%/*}"
751 config_essid="${config_essid#*/}"
752 config_bssid="${station_id##*/}"
753 config_bssid="${config_bssid//-/}"
754 f_check "dev" "true"
755 f_log "debug" "f_main-2 ::: config_radio: ${config_radio}, config_essid: \"${config_essid}\", config_bssid: ${config_bssid:-"-"}"
756 else
757 uci_commit "wireless"
758 f_check "dev" "false"
759 fi
760 f_log "debug" "f_main-3 ::: radio_list: ${trm_radiolist:-"-"}, sta_list: ${trm_stalist:-"-"}"
761
762 # radio loop
763 #
764 for radio in ${trm_radiolist}; do
765 if ! printf "%s" "${trm_stalist}" | grep -q "\\-${radio}"; then
766 if [ "${trm_autoadd}" = "0" ]; then
767 f_log "info" "no enabled station on radio '${radio}'"
768 continue
769 fi
770 fi
771 scan_list=""
772
773 # station loop
774 #
775 for sta in ${trm_stalist:-"${radio}"}; do
776 if [ "${sta}" != "${radio}" ]; then
777 section="${sta%%-*}"
778 sta_radio="$(uci_get "wireless" "${section}" "device")"
779 sta_essid="$(uci_get "wireless" "${section}" "ssid")"
780 sta_bssid="$(uci_get "wireless" "${section}" "bssid")"
781 sta_mac="$(f_mac "get" "${section}")"
782 if [ -z "${sta_radio}" ] || [ -z "${sta_essid}" ]; then
783 f_log "info" "invalid wireless section '${section}'"
784 continue
785 fi
786 if [ -n "${trm_connection}" ] && [ "${radio}" = "${config_radio}" ] && [ "${sta_radio}" = "${config_radio}" ] &&
787 [ "${sta_essid}" = "${config_essid}" ] && [ "${sta_bssid}" = "${config_bssid}" ]; then
788 f_ctrack "refresh"
789 f_log "info" "uplink still in range '${config_radio}/${config_essid}/${config_bssid:-"-"}' with mac '${sta_mac:-"-"}'"
790 f_vpn
791 return 0
792 fi
793 f_log "debug" "f_main-4 ::: sta_radio: ${sta_radio}, sta_essid: \"${sta_essid}\", sta_bssid: ${sta_bssid:-"-"}"
794 fi
795 if [ -z "${scan_list}" ]; then
796 scan_dev="$(ubus -S call network.wireless status 2>/dev/null | jsonfilter -q -l1 -e "@.${radio}.interfaces[0].ifname")"
797 scan_list="$("${trm_iwinfo}" "${scan_dev:-${radio}}" scan 2>/dev/null |
798 awk 'BEGIN{FS="[[:space:]]"}/Address:/{var1=$NF}/ESSID:/{var2="";for(i=12;i<=NF;i++)if(var2==""){var2=$i}else{var2=var2" "$i}}
799 /Quality:/{split($NF,var0,"/")}/Encryption:/{if($NF=="none"){var3="+"}else{var3="-"};
800 printf "%i %s %s %s\n",(var0[1]*100/var0[2]),var3,var1,var2}' | sort -rn | head -qn "${trm_maxscan}")"
801 f_log "debug" "f_main-5 ::: radio: ${radio}, scan_device: ${scan_dev}, scan_max: ${trm_maxscan}"
802 if [ -z "${scan_list}" ]; then
803 f_log "info" "no scan results on '${radio}'"
804 continue 2
805 fi
806 fi
807
808 # scan loop
809 #
810 while read -r scan_quality scan_open scan_bssid scan_essid; do
811 if [ -n "${scan_quality}" ] && [ -n "${scan_open}" ] && [ -n "${scan_bssid}" ] && [ -n "${scan_essid}" ]; then
812 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}"
813 if [ "${scan_quality}" -ge "${trm_minquality}" ]; then
814 if { { [ "${scan_essid}" = "\"${sta_essid}\"" ] && { [ -z "${sta_bssid}" ] || [ "${scan_bssid}" = "${sta_bssid}" ]; }; } ||
815 { [ "${scan_bssid}" = "${sta_bssid}" ] && [ "${scan_essid}" = "unknown" ]; }; } && [ "${radio}" = "${sta_radio}" ]; then
816 if [ -n "${config_radio}" ]; then
817 f_vpn "disable"
818 uci_set "wireless" "${trm_activesta}" "disabled" "1"
819 uci_commit "wireless"
820 f_check "rev" "false"
821 f_ctrack "end"
822 f_log "info" "uplink connection terminated '${config_radio}/${config_essid}/${config_bssid:-"-"}'"
823 unset config_radio config_essid config_bssid
824 fi
825
826 # retry loop
827 #
828 retrycnt="1"
829 f_getcfg "${sta_radio}" "${sta_essid}" "${sta_bssid}"
830 while [ "${retrycnt}" -le "${trm_maxretry}" ]; do
831 sta_mac="$(f_mac "set" "${section}")"
832 uci_set "wireless" "${section}" "disabled" "0"
833 f_check "sta" "false" "${sta_radio}" "${sta_essid}" "${sta_bssid}"
834 if [ "${trm_ifstatus}" = "true" ]; then
835 rm -f "${trm_mailfile}"
836 uci_commit "wireless"
837 f_ctrack "start"
838 f_log "info" "connected to uplink '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' with mac '${sta_mac:-"-"}' (${retrycnt}/${trm_maxretry})"
839 f_vpn "enable"
840 return 0
841 else
842 uci -q revert "wireless"
843 f_check "rev" "false"
844 if [ "${retrycnt}" = "${trm_maxretry}" ]; then
845 f_ctrack "disabled"
846 f_log "info" "uplink has been disabled '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' (${retrycnt}/${trm_maxretry})"
847 break 2
848 else
849 f_jsnup
850 f_log "info" "can't connect to uplink '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' (${retrycnt}/${trm_maxretry})"
851 fi
852 fi
853 retrycnt="$((retrycnt + 1))"
854 sleep "$((trm_maxwait / 6))"
855 done
856 elif [ "${trm_autoadd}" = "1" ] && [ "${scan_open}" = "+" ] && [ "${scan_essid}" != "unknown" ]; then
857 scan_essid="${scan_essid%?}"
858 scan_essid="${scan_essid:1}"
859 f_addsta "${radio}" "${scan_essid}"
860 fi
861 fi
862 fi
863 done <<-EOV
864 ${scan_list}
865 EOV
866 done
867 done
868 fi
869 }
870
871 # source required system libraries
872 #
873 if [ -r "/lib/functions.sh" ] && [ -r "/lib/functions/network.sh" ] && [ -r "/usr/share/libubox/jshn.sh" ]; then
874 . "/lib/functions.sh"
875 . "/lib/functions/network.sh"
876 . "/usr/share/libubox/jshn.sh"
877 else
878 f_log "err" "system libraries not found"
879 fi
880
881 # control travelmate actions
882 #
883 while true; do
884 if [ "${trm_action}" = "stop" ]; then
885 if [ -s "${trm_pidfile}" ]; then
886 f_log "info" "travelmate instance stopped ::: action: ${trm_action}, pid: $(cat ${trm_pidfile} 2>/dev/null)"
887 : >"${trm_rtfile}"
888 : >"${trm_pidfile}"
889 fi
890 break
891 elif [ -n "${trm_action}" ]; then
892 f_log "info" "travelmate instance started ::: action: ${trm_action}, pid: ${$}"
893 f_env
894 f_main
895 unset trm_action
896 fi
897 while true; do
898 sleep "${trm_timeout}" 0
899 rc="${?}"
900 if [ "${rc}" != "0" ]; then
901 if [ -z "$(f_getgw)" ]; then
902 rc="0"
903 fi
904 fi
905 if [ "${rc}" = "0" ]; then
906 break
907 fi
908 done
909 json_cleanup
910 f_env
911 f_main
912 done