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.

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