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.

1047 lines
32 KiB

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