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.

656 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.12"
  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 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}" >/dev/null 2>&1
  276. rc=${?}
  277. f_log "info" "captive portal login '${login_command:0:40}' for '${cp_domain}' has been executed with rc '${rc}'"
  278. if [ "${rc}" -eq 0 ]
  279. then
  280. result="$(f_net)"
  281. fi
  282. fi
  283. fi
  284. trm_connection="${result:-"-"}/${trm_ifquality}"
  285. f_jsnup
  286. break
  287. fi
  288. elif [ -n "${trm_connection}" ]
  289. then
  290. sta_essid="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].*.ssid')"
  291. sta_bssid="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].*.bssid')"
  292. if [ "${trm_ifquality}" -lt "${trm_minquality}" ]
  293. then
  294. unset trm_connection
  295. trm_ifstatus="${status}"
  296. f_log "info" "uplink '${sta_essid:-"-"}/${sta_bssid:-"-"}' is out of range (${trm_ifquality}/${trm_minquality})"
  297. elif [ "${trm_netcheck}" -eq 1 ] && [ "${result}" = "net nok" ]
  298. then
  299. unset trm_connection
  300. trm_ifstatus="${status}"
  301. f_log "info" "uplink '${sta_essid:-"-"}/${sta_bssid:-"-"}' has no internet (${result})"
  302. fi
  303. f_jsnup
  304. break
  305. elif [ "${mode}" = "initial" ]
  306. then
  307. f_jsnup
  308. break
  309. fi
  310. elif [ -n "${trm_connection}" ]
  311. then
  312. unset trm_connection
  313. trm_ifstatus="${status}"
  314. f_jsnup
  315. break
  316. elif [ "${mode}" = "initial" ]
  317. then
  318. f_jsnup
  319. break
  320. fi
  321. fi
  322. fi
  323. wait_time=$((wait_time+1))
  324. sleep 1
  325. done
  326. 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}"
  327. }
  328. # update runtime information
  329. #
  330. f_jsnup()
  331. {
  332. 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}"
  333. dev_status="$(ubus -S call network.wireless status 2>/dev/null)"
  334. if [ -n "${dev_status}" ]
  335. then
  336. config="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].section')"
  337. if [ -n "${config}" ]
  338. then
  339. sta_iface="$(uci_get "wireless" "${config}" "network")"
  340. sta_radio="$(uci_get "wireless" "${config}" "device")"
  341. sta_essid="$(uci_get "wireless" "${config}" "ssid")"
  342. sta_bssid="$(uci_get "wireless" "${config}" "bssid")"
  343. fi
  344. fi
  345. json_get_var last_date "last_rundate"
  346. json_get_var last_station "station_id"
  347. if [ "${status}" = "true" ]
  348. then
  349. status="connected (${trm_connection:-"-"})"
  350. json_get_var last_status "travelmate_status"
  351. if [ "${last_status}" = "running / not connected" ] || [ "${last_station}" != "${sta_radio:-"-"}/${sta_essid:-"-"}/${sta_bssid:-"-"}" ]
  352. then
  353. last_date="$(/bin/date "+%Y.%m.%d-%H:%M:%S")"
  354. fi
  355. elif [ "${status}" = "error" ]
  356. then
  357. unset trm_connection
  358. status="program error"
  359. else
  360. unset trm_connection
  361. status="running / not connected"
  362. fi
  363. if [ -z "${last_date}" ]
  364. then
  365. last_date="$(/bin/date "+%Y.%m.%d-%H:%M:%S")"
  366. fi
  367. json_get_var faulty_list "faulty_stations"
  368. if [ -n "${faulty_list}" ] && [ "${trm_listexpiry}" -gt 0 ]
  369. then
  370. d1="$(/bin/date -d "${last_date}" "+%s")"
  371. d2="$(/bin/date "+%s")"
  372. d3=$(((d2 - d1)/60))
  373. if [ "${d3}" -ge "${trm_listexpiry}" ]
  374. then
  375. faulty_list=""
  376. fi
  377. fi
  378. if [ -n "${faulty_station}" ]
  379. then
  380. if [ -z "$(printf "%s" "${faulty_list}" | grep -Fo "${faulty_station}")" ]
  381. then
  382. faulty_list="$(f_trim "${faulty_list} ${faulty_station}")"
  383. last_date="$(/bin/date "+%Y.%m.%d-%H:%M:%S")"
  384. fi
  385. fi
  386. json_add_string "travelmate_status" "${status}"
  387. json_add_string "travelmate_version" "${trm_ver}"
  388. json_add_string "station_id" "${sta_radio:-"-"}/${sta_essid:-"-"}/${sta_bssid:-"-"}"
  389. json_add_string "station_interface" "${sta_iface:-"-"}"
  390. json_add_string "faulty_stations" "${faulty_list}"
  391. json_add_string "last_rundate" "${last_date}"
  392. json_add_string "system" "${trm_sysver}"
  393. json_dump > "${trm_rtfile}"
  394. 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}"
  395. }
  396. # write to syslog
  397. #
  398. f_log()
  399. {
  400. local IFS class="${1}" log_msg="${2}"
  401. if [ -n "${log_msg}" ] && { [ "${class}" != "debug" ] || [ "${trm_debug}" -eq 1 ]; }
  402. then
  403. logger -p "${class}" -t "travelmate-${trm_ver}[${$}]" "${log_msg}"
  404. if [ "${class}" = "err" ]
  405. then
  406. trm_ifstatus="error"
  407. f_jsnup
  408. logger -p "${class}" -t "travelmate-${trm_ver}[${$}]" "Please check 'https://github.com/openwrt/packages/blob/master/net/travelmate/files/README.md' (${trm_sysver})"
  409. exit 1
  410. fi
  411. fi
  412. }
  413. # main function for connection handling
  414. #
  415. f_main()
  416. {
  417. local IFS cnt dev config spec scan_list scan_essid scan_bssid scan_open scan_quality uci_essid cfg_essid faulty_list
  418. local station_id sta sta_essid sta_bssid sta_radio sta_iface active_essid active_bssid active_radio
  419. f_check "initial" "false" "true"
  420. f_log "debug" "f_main ::: status: ${trm_ifstatus}, proactive: ${trm_proactive}"
  421. if [ "${trm_ifstatus}" != "true" ] || [ "${trm_proactive}" -eq 1 ]
  422. then
  423. config_load wireless
  424. config_foreach f_prep wifi-iface ${trm_proactive}
  425. if [ "${trm_ifstatus}" = "true" ] && [ -n "${trm_active_sta}" ] && [ "${trm_proactive}" -eq 1 ]
  426. then
  427. json_get_var station_id "station_id"
  428. active_radio="${station_id%%/*}"
  429. active_essid="${station_id%/*}"
  430. active_essid="${active_essid#*/}"
  431. active_bssid="${station_id##*/}"
  432. f_check "dev" "true"
  433. f_log "debug" "f_main ::: active_radio: ${active_radio}, active_essid: \"${active_essid}\", active_bssid: ${active_bssid:-"-"}"
  434. else
  435. uci_commit "wireless"
  436. f_check "dev"
  437. fi
  438. json_get_var faulty_list "faulty_stations"
  439. f_log "debug" "f_main ::: iwinfo: ${trm_iwinfo:-"-"}, dev_list: ${trm_devlist:-"-"}, sta_list: ${trm_stalist:0:${trm_scanbuffer}}, faulty_list: ${faulty_list:-"-"}"
  440. # radio loop
  441. #
  442. for dev in ${trm_devlist}
  443. do
  444. if [ -z "$(printf "%s" "${trm_stalist}" | grep -o "\\-${dev}")" ]
  445. then
  446. f_log "debug" "f_main ::: no station on '${dev}' - continue"
  447. continue
  448. fi
  449. # station loop
  450. #
  451. for sta in ${trm_stalist}
  452. do
  453. config="${sta%%-*}"
  454. sta_radio="${sta##*-}"
  455. sta_essid="$(uci_get "wireless" "${config}" "ssid")"
  456. sta_bssid="$(uci_get "wireless" "${config}" "bssid")"
  457. sta_iface="$(uci_get "wireless" "${config}" "network")"
  458. json_get_var faulty_list "faulty_stations"
  459. if [ -n "$(printf "%s" "${faulty_list}" | grep -Fo "${sta_radio}/${sta_essid}/${sta_bssid}")" ]
  460. then
  461. f_log "debug" "f_main ::: faulty station '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' - continue"
  462. continue
  463. fi
  464. if [ "${dev}" = "${active_radio}" ] && [ "${sta_essid}" = "${active_essid}" ] && [ "${sta_bssid:-"-"}" = "${active_bssid}" ]
  465. then
  466. f_log "debug" "f_main ::: active station prioritized '${active_radio}/${active_essid}/${active_bssid:-"-"}' - break"
  467. break 2
  468. fi
  469. f_log "debug" "f_main ::: sta_radio: ${sta_radio}, sta_essid: \"${sta_essid}\", sta_bssid: ${sta_bssid:-"-"}"
  470. if [ -z "${scan_list}" ]
  471. then
  472. scan_list="$("${trm_iwinfo}" "${dev}" scan 2>/dev/null | \
  473. awk 'BEGIN{FS="[[:space:]]"}/Address:/{var1=$NF}/ESSID:/{var2="";for(i=12;i<=NF;i++)if(var2==""){var2=$i}else{var2=var2" "$i};
  474. 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}' | \
  475. sort -rn | awk -v buf="${trm_scanbuffer}" 'BEGIN{ORS=","}{print substr($0,1,buf)}')"
  476. f_log "debug" "f_main ::: scan_buffer: ${trm_scanbuffer}, scan_list: ${scan_list}"
  477. if [ -z "${scan_list}" ]
  478. then
  479. f_log "debug" "f_main ::: no scan results on '${dev}' - continue"
  480. continue 2
  481. fi
  482. fi
  483. # scan loop
  484. #
  485. IFS=","
  486. for spec in ${scan_list}
  487. do
  488. if [ -z "${scan_quality}" ]
  489. then
  490. scan_quality="${spec}"
  491. elif [ -z "${scan_bssid}" ]
  492. then
  493. scan_bssid="${spec}"
  494. elif [ -z "${scan_essid}" ]
  495. then
  496. scan_essid="${spec}"
  497. elif [ -z "${scan_open}" ]
  498. then
  499. scan_open="${spec}"
  500. fi
  501. if [ -n "${scan_quality}" ] && [ -n "${scan_bssid}" ] && [ -n "${scan_essid}" ] && [ -n "${scan_open}" ]
  502. then
  503. if [ "${scan_quality}" -ge "${trm_minquality}" ]
  504. then
  505. if { { [ "${scan_essid}" = "\"${sta_essid//,/.}\"" ] && { [ -z "${sta_bssid}" ] || [ "${scan_bssid}" = "${sta_bssid}" ]; } } || \
  506. { [ "${scan_bssid}" = "${sta_bssid}" ] && [ "${scan_essid}" = "unknown" ]; } } && [ "${dev}" = "${sta_radio}" ]
  507. then
  508. f_log "debug" "f_main ::: scan_quality: ${scan_quality}, scan_essid: ${scan_essid}, scan_bssid: ${scan_bssid:-"-"}, scan_open: ${scan_open}"
  509. if [ "${dev}" = "${active_radio}" ]
  510. then
  511. uci_set "wireless" "${trm_active_sta}" "disabled" "1"
  512. uci_commit "wireless"
  513. f_log "debug" "f_main ::: active uplink connection '${active_radio}/${active_essid}/${active_bssid:-"-"}' terminated"
  514. unset trm_connection active_radio active_essid active_bssid
  515. fi
  516. # retry loop
  517. #
  518. cnt=1
  519. while [ "${cnt}" -le "${trm_maxretry}" ]
  520. do
  521. uci_set "wireless" "${config}" "disabled" "0"
  522. f_check "sta"
  523. if [ "${trm_ifstatus}" = "true" ]
  524. then
  525. unset IFS scan_list
  526. uci_commit "wireless"
  527. f_log "info" "connected to uplink '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' (${cnt}/${trm_maxretry}, ${trm_sysver})"
  528. return 0
  529. else
  530. uci -q revert "wireless"
  531. f_check "rev"
  532. if [ "${cnt}" -eq "${trm_maxretry}" ]
  533. then
  534. faulty_station="${sta_radio}/${sta_essid}/${sta_bssid:-"-"}"
  535. f_jsnup "${faulty_station}"
  536. f_log "info" "uplink disabled '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' (${cnt}/${trm_maxretry}, ${trm_sysver})"
  537. break 2
  538. else
  539. f_jsnup
  540. f_log "info" "can't connect to uplink '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' (${cnt}/${trm_maxretry}, ${trm_sysver})"
  541. fi
  542. fi
  543. cnt=$((cnt+1))
  544. sleep $((trm_maxwait/6))
  545. done
  546. elif [ "${trm_autoadd}" -eq 1 ] && [ "${scan_open}" = "+" ] && [ "${scan_essid}" != "unknown" ]
  547. then
  548. cfg_essid="${scan_essid#*\"}"
  549. cfg_essid="${cfg_essid%\"*}"
  550. uci_essid="${cfg_essid//[^[:alnum:]_]/_}"
  551. if [ -z "$(uci_get "wireless" "trm_${uci_essid}")" ]
  552. then
  553. uci_add "wireless" "wifi-iface" "trm_${uci_essid}"
  554. uci_set "wireless" "trm_${uci_essid}" "mode" "sta"
  555. uci_set "wireless" "trm_${uci_essid}" "network" "${trm_iface}"
  556. uci_set "wireless" "trm_${uci_essid}" "device" "${sta_radio}"
  557. uci_set "wireless" "trm_${uci_essid}" "ssid" "${cfg_essid}"
  558. uci_set "wireless" "trm_${uci_essid}" "encryption" "none"
  559. uci_set "wireless" "trm_${uci_essid}" "disabled" "1"
  560. uci_commit "wireless"
  561. f_log "info" "open uplink '${sta_radio}/${cfg_essid}' added to wireless config"
  562. fi
  563. fi
  564. unset scan_quality scan_bssid scan_essid scan_open
  565. continue
  566. else
  567. unset scan_quality scan_bssid scan_essid scan_open
  568. continue
  569. fi
  570. fi
  571. done
  572. unset IFS scan_quality scan_bssid scan_essid scan_open
  573. done
  574. unset scan_list
  575. done
  576. fi
  577. }
  578. # source required system libraries
  579. #
  580. if [ -r "/lib/functions.sh" ] && [ -r "/usr/share/libubox/jshn.sh" ]
  581. then
  582. . "/lib/functions.sh"
  583. . "/usr/share/libubox/jshn.sh"
  584. else
  585. f_log "err" "system libraries not found"
  586. fi
  587. # control travelmate actions
  588. #
  589. f_envload
  590. while true
  591. do
  592. if [ -z "${trm_action}" ]
  593. then
  594. rc=0
  595. while true
  596. do
  597. if [ "${rc}" -eq 0 ]
  598. then
  599. f_check "initial"
  600. fi
  601. sleep ${trm_timeout} 0
  602. rc=${?}
  603. if [ "${rc}" -ne 0 ]
  604. then
  605. f_check "initial"
  606. fi
  607. if [ "${rc}" -eq 0 ] || { [ "${rc}" -ne 0 ] && [ "${trm_ifstatus}" = "false" ]; }
  608. then
  609. break
  610. fi
  611. done
  612. elif [ "${trm_action}" = "stop" ]
  613. then
  614. > "${trm_rtfile}"
  615. f_log "info" "travelmate instance stopped ::: action: ${trm_action}, pid: $(cat ${trm_pidfile} 2>/dev/null)"
  616. exit 0
  617. else
  618. f_log "info" "travelmate instance started ::: action: ${trm_action}, pid: ${$}"
  619. unset trm_action
  620. fi
  621. json_cleanup
  622. f_envload
  623. f_main
  624. done