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.

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