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.

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