You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

657 lines
22 KiB

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