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.

1043 lines
38 KiB

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