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.

884 lines
31 KiB

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