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.

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