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.

417 lines
13 KiB

  1. #!/bin/sh
  2. # travelmate, a wlan connection manager for travel router
  3. # written by Dirk Brenken (dev@brenken.org)
  4. # This is free software, licensed under the GNU General Public License v3.
  5. # You should have received a copy of the GNU General Public License
  6. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  7. # set initial defaults
  8. #
  9. LC_ALL=C
  10. PATH="/usr/sbin:/usr/bin:/sbin:/bin"
  11. trm_ver="1.2.3"
  12. trm_sysver="unknown"
  13. trm_enabled=0
  14. trm_debug=0
  15. trm_captive=1
  16. trm_captiveurl="http://captive.apple.com"
  17. trm_minquality=35
  18. trm_maxretry=3
  19. trm_maxwait=30
  20. trm_timeout=60
  21. trm_radio=""
  22. trm_connection=""
  23. trm_rtfile="/tmp/trm_runtime.json"
  24. trm_fetch="$(command -v uclient-fetch)"
  25. trm_iwinfo="$(command -v iwinfo)"
  26. trm_wpa="$(command -v wpa_supplicant)"
  27. trm_action="${1:-"start"}"
  28. trm_pidfile="/var/run/travelmate.pid"
  29. # load travelmate environment
  30. #
  31. f_envload()
  32. {
  33. local sys_call sys_desc sys_model sys_ver
  34. # get system information
  35. #
  36. sys_call="$(ubus -S call system board 2>/dev/null)"
  37. if [ -n "${sys_call}" ]
  38. then
  39. sys_desc="$(printf '%s' "${sys_call}" | jsonfilter -e '@.release.description')"
  40. sys_model="$(printf '%s' "${sys_call}" | jsonfilter -e '@.model')"
  41. sys_ver="$(cat /etc/turris-version 2>/dev/null)"
  42. if [ -n "${sys_ver}" ]
  43. then
  44. sys_desc="${sys_desc}/${sys_ver}"
  45. fi
  46. trm_sysver="${sys_model}, ${sys_desc}"
  47. fi
  48. # (re-)initialize global list variables
  49. #
  50. unset trm_devlist trm_stalist trm_radiolist
  51. # load config and check 'enabled' option
  52. #
  53. option_cb()
  54. {
  55. local option="${1}"
  56. local value="${2}"
  57. eval "${option}=\"${value}\""
  58. }
  59. config_load travelmate
  60. if [ ${trm_enabled} -ne 1 ]
  61. then
  62. f_log "info" "travelmate is currently disabled, please set 'trm_enabled' to '1' to use this service"
  63. exit 0
  64. fi
  65. # validate input ranges
  66. #
  67. if [ ${trm_minquality} -lt 20 ] || [ ${trm_minquality} -gt 80 ]
  68. then
  69. trm_minquality=35
  70. fi
  71. if [ ${trm_maxretry} -lt 1 ] || [ ${trm_maxretry} -gt 10 ]
  72. then
  73. trm_maxretry=3
  74. fi
  75. if [ ${trm_maxwait} -lt 20 ] || [ ${trm_maxwait} -gt 40 ] || [ ${trm_maxwait} -ge ${trm_timeout} ]
  76. then
  77. trm_maxwait=30
  78. fi
  79. if [ ${trm_timeout} -lt 30 ] || [ ${trm_timeout} -gt 300 ] || [ ${trm_timeout} -le ${trm_maxwait} ]
  80. then
  81. trm_timeout=60
  82. fi
  83. }
  84. # gather radio information & bring down all STA interfaces
  85. #
  86. f_prep()
  87. {
  88. local eap_rc=0 config="${1}"
  89. local mode="$(uci_get wireless "${config}" mode)"
  90. local network="$(uci_get wireless "${config}" network)"
  91. local radio="$(uci_get wireless "${config}" device)"
  92. local disabled="$(uci_get wireless "${config}" disabled)"
  93. local eaptype="$(uci_get wireless "${config}" eap_type)"
  94. if ([ -z "${trm_radio}" ] || [ "${trm_radio}" = "${radio}" ]) && \
  95. [ -z "$(printf "%s" "${trm_radiolist}" | grep -Fo " ${radio}")" ]
  96. then
  97. trm_radiolist="${trm_radiolist} ${radio}"
  98. fi
  99. if [ "${mode}" = "sta" ] && [ "${network}" = "${trm_iface}" ]
  100. then
  101. if [ -z "${disabled}" ] || [ "${disabled}" = "0" ]
  102. then
  103. uci_set wireless "${config}" disabled 1
  104. fi
  105. if [ -n "${eaptype}" ]
  106. then
  107. eap_rc="$("${trm_wpa}" -veap >/dev/null 2>&1; printf "%u" ${?})"
  108. fi
  109. if [ -z "${eaptype}" ] || [ ${eap_rc} -eq 0 ]
  110. then
  111. trm_stalist="${trm_stalist} ${config}_${radio}"
  112. fi
  113. fi
  114. f_log "debug" "f_prep ::: config: ${config}, mode: ${mode}, network: ${network}, eap_rc: ${eap_rc}, radio: ${radio}, trm_radio: ${trm_radio:-"-"}, disabled: ${disabled}"
  115. }
  116. # check interface status
  117. #
  118. f_check()
  119. {
  120. local IFS ifname radio dev_status config sta_essid sta_bssid result wait=1 mode="${1}" status="${2:-"false"}"
  121. trm_ifquality=0
  122. trm_ifstatus="false"
  123. if [ "${mode}" != "initial" ]
  124. then
  125. ubus call network reload
  126. fi
  127. while [ ${wait} -le ${trm_maxwait} ]
  128. do
  129. dev_status="$(ubus -S call network.wireless status 2>/dev/null)"
  130. if [ -n "${dev_status}" ]
  131. then
  132. if [ "${mode}" = "dev" ]
  133. then
  134. if [ "${trm_ifstatus}" != "${status}" ]
  135. then
  136. trm_ifstatus="${status}"
  137. f_jsnup
  138. fi
  139. for radio in ${trm_radiolist}
  140. do
  141. result="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e "@.${radio}.up")"
  142. if [ "${result}" = "true" ] && [ -z "$(printf "%s" "${trm_devlist}" | grep -Fo " ${radio}")" ]
  143. then
  144. trm_devlist="${trm_devlist} ${radio}"
  145. fi
  146. done
  147. if [ "${trm_devlist}" = "${trm_radiolist}" ] || [ ${wait} -eq ${trm_maxwait} ]
  148. then
  149. ifname="${trm_devlist}"
  150. break
  151. else
  152. unset trm_devlist
  153. fi
  154. elif [ "${mode}" = "rev" ]
  155. then
  156. wait=$(( ${trm_maxwait} / 3 ))
  157. sleep ${wait}
  158. break
  159. else
  160. ifname="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].ifname')"
  161. if [ -n "${ifname}" ]
  162. then
  163. trm_ifquality="$(${trm_iwinfo} ${ifname} info 2>/dev/null | awk -F "[\/| ]" '/Link Quality:/{printf "%i\n", (100 / $NF * $(NF-1)) }')"
  164. if [ ${trm_ifquality} -ge ${trm_minquality} ]
  165. then
  166. trm_ifstatus="$(ubus -S call network.interface dump 2>/dev/null | jsonfilter -l1 -e "@.interface[@.device=\"${ifname}\"].up")"
  167. elif [ "${mode}" = "initial" ] && [ ${trm_ifquality} -lt ${trm_minquality} ]
  168. then
  169. trm_ifstatus="${status}"
  170. sta_essid="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].*.ssid')"
  171. sta_bssid="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].*.bssid')"
  172. f_log "info" "uplink '${sta_essid:-"-"}/${sta_bssid:-"-"}' is out of range (${trm_ifquality}/${trm_minquality}), uplink disconnected (${trm_sysver})"
  173. fi
  174. fi
  175. fi
  176. if [ "${mode}" = "initial" ] || [ "${trm_ifstatus}" = "true" ]
  177. then
  178. if ([ "${trm_ifstatus}" != "true" ] && [ "${trm_ifstatus}" != "${status}" ]) || [ ${trm_ifquality} -lt ${trm_minquality} ]
  179. then
  180. f_jsnup
  181. fi
  182. if [ "${mode}" = "initial" ] && [ "${trm_captive}" -eq 1 ] && [ "${trm_ifstatus}" = "true" ]
  183. then
  184. result="$(${trm_fetch} --timeout=$(( ${trm_maxwait} / 3 )) --spider "${trm_captiveurl}" 2>&1 | awk '/^Redirected/{printf "%s" "net cp \047"$NF"\047";exit}/^Download completed/{printf "%s" "net ok";exit}/^Failed|^Connection error/{printf "%s" "net nok";exit}')"
  185. if [ -n "${result}" ] && ([ -z "${trm_connection}" ] || [ "${result}" != "${trm_connection%/*}" ])
  186. then
  187. trm_connection="${result}/${trm_ifquality}"
  188. f_jsnup
  189. fi
  190. fi
  191. break
  192. fi
  193. fi
  194. wait=$(( wait + 1 ))
  195. sleep 1
  196. done
  197. f_log "debug" "f_check::: mode: ${mode}, name: ${ifname:-"-"}, status: ${trm_ifstatus}, quality: ${trm_ifquality}, connection: ${trm_connection:-"-"}, wait: ${wait}, max_wait: ${trm_maxwait}, min_quality: ${trm_minquality}, captive: ${trm_captive}"
  198. }
  199. # update runtime information
  200. #
  201. f_jsnup()
  202. {
  203. local config sta_iface sta_radio sta_essid sta_bssid dev_status status="${trm_ifstatus}" faulty_list faulty_station="${1}"
  204. if [ "${status}" = "true" ]
  205. then
  206. status="connected (${trm_connection:-"-"})"
  207. else
  208. unset trm_connection
  209. status="running / not connected"
  210. fi
  211. dev_status="$(ubus -S call network.wireless status 2>/dev/null)"
  212. if [ -n "${dev_status}" ]
  213. then
  214. config="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].section')"
  215. if [ -n "${config}" ]
  216. then
  217. sta_iface="$(uci_get wireless "${config}" network)"
  218. sta_radio="$(uci_get wireless "${config}" device)"
  219. sta_essid="$(uci_get wireless "${config}" ssid)"
  220. sta_bssid="$(uci_get wireless "${config}" bssid)"
  221. fi
  222. fi
  223. json_get_var faulty_list "faulty_stations"
  224. if [ -n "${faulty_station}" ]
  225. then
  226. if [ -z "$(printf "%s" "${faulty_list}" | grep -Fo "${faulty_station}")" ]
  227. then
  228. faulty_list="${faulty_list} ${faulty_station}"
  229. fi
  230. fi
  231. json_add_string "travelmate_status" "${status}"
  232. json_add_string "travelmate_version" "${trm_ver}"
  233. json_add_string "station_id" "${sta_radio:-"-"}/${sta_essid:-"-"}/${sta_bssid:-"-"}"
  234. json_add_string "station_interface" "${sta_iface:-"-"}"
  235. json_add_string "faulty_stations" "${faulty_list}"
  236. json_add_string "last_rundate" "$(/bin/date "+%d.%m.%Y %H:%M:%S")"
  237. json_add_string "system" "${trm_sysver}"
  238. json_dump > "${trm_rtfile}"
  239. f_log "debug" "f_jsnup::: config: ${config:-"-"}, status: ${status:-"-"}, sta_iface: ${sta_iface:-"-"}, sta_radio: ${sta_radio:-"-"}, sta_essid: ${sta_essid:-"-"}, sta_bssid: ${sta_bssid:-"-"}, faulty_list: ${faulty_list:-"-"}"
  240. }
  241. # write to syslog
  242. #
  243. f_log()
  244. {
  245. local class="${1}"
  246. local log_msg="${2}"
  247. if [ -n "${log_msg}" ] && ([ "${class}" != "debug" ] || [ ${trm_debug} -eq 1 ])
  248. then
  249. logger -p "${class}" -t "travelmate-${trm_ver}[${$}]" "${log_msg}"
  250. if [ "${class}" = "err" ]
  251. then
  252. trm_ifstatus="error"
  253. f_jsnup
  254. logger -p "${class}" -t "travelmate-${trm_ver}[${$}]" "Please check 'https://github.com/openwrt/packages/blob/master/net/travelmate/files/README.md' (${trm_sysver})"
  255. exit 1
  256. fi
  257. fi
  258. }
  259. # main function for connection handling
  260. #
  261. f_main()
  262. {
  263. local IFS cnt dev config scan scan_list scan_essid scan_bssid scan_quality sta sta_essid sta_bssid sta_radio sta_iface faulty_list
  264. f_check "initial"
  265. if [ "${trm_ifstatus}" != "true" ]
  266. then
  267. config_load wireless
  268. config_foreach f_prep wifi-iface
  269. uci_commit wireless
  270. f_check "dev" "running / not connected"
  271. json_get_var faulty_list "faulty_stations"
  272. f_log "debug" "f_main ::: iwinfo: ${trm_iwinfo}, dev_list: ${trm_devlist}, sta_list: ${trm_stalist:0:800}, faulty_list: ${faulty_list:-"-"}"
  273. for dev in ${trm_devlist}
  274. do
  275. if [ -z "$(printf "%s" "${trm_stalist}" | grep -Fo "_${dev}")" ]
  276. then
  277. continue
  278. fi
  279. cnt=1
  280. while [ ${cnt} -le ${trm_maxretry} ]
  281. do
  282. scan_list="$(${trm_iwinfo} "${dev}" scan 2>/dev/null | awk 'BEGIN{FS="[/ ]"}/Address:/{var1=$NF}/ESSID:/{var2="";for(i=12;i<=NF;i++)if(var2==""){var2=$i}else{var2=var2" "$i}}/Quality:/{printf "%i,%s,%s\n",(100/$NF*$(NF-1)),var1,var2}' | sort -rn | awk '{ORS=",";print $0}')"
  283. f_log "debug" "f_main ::: dev: ${dev}, scan_list: ${scan_list:0:800}, cnt: ${cnt}, max_cnt: ${trm_maxretry}"
  284. if [ -n "${scan_list}" ]
  285. then
  286. for sta in ${trm_stalist}
  287. do
  288. config="${sta%%_*}"
  289. sta_radio="${sta##*_}"
  290. sta_essid="$(uci_get wireless "${config}" ssid)"
  291. sta_bssid="$(uci_get wireless "${config}" bssid)"
  292. sta_iface="$(uci_get wireless "${config}" network)"
  293. json_get_var faulty_list "faulty_stations"
  294. if [ -n "$(printf "%s" "${faulty_list}" | grep -Fo "${sta_radio}/${sta_essid}/${sta_bssid}")" ]
  295. then
  296. continue
  297. fi
  298. IFS=","
  299. for scan in ${scan_list}
  300. do
  301. if [ -z "${scan_quality}" ]
  302. then
  303. scan_quality="${scan}"
  304. elif [ -z "${scan_bssid}" ]
  305. then
  306. scan_bssid="${scan}"
  307. elif [ -z "${scan_essid}" ]
  308. then
  309. scan_essid="${scan}"
  310. fi
  311. if [ -n "${scan_quality}" ] && [ -n "${scan_bssid}" ] && [ -n "${scan_essid}" ]
  312. then
  313. if [ ${scan_quality} -ge ${trm_minquality} ]
  314. then
  315. if (([ "${scan_essid}" = "\"${sta_essid}\"" ] && ([ -z "${sta_bssid}" ] || [ "${scan_bssid}" = "${sta_bssid}" ])) || \
  316. ([ "${scan_bssid}" = "${sta_bssid}" ] && [ "${scan_essid}" = "unknown" ])) && [ "${dev}" = "${sta_radio}" ]
  317. then
  318. f_log "debug" "f_main ::: scan_quality: ${scan_quality}, scan_bssid: ${scan_bssid}, scan_essid: ${scan_essid}"
  319. uci_set wireless "${config}" disabled 0
  320. f_check "sta"
  321. if [ "${trm_ifstatus}" = "true" ]
  322. then
  323. uci_commit wireless
  324. f_check "initial"
  325. f_log "info" "connected to uplink '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' (${trm_sysver})"
  326. return 0
  327. elif [ ${cnt} -eq ${trm_maxretry} ]
  328. then
  329. uci_set wireless "${config}" disabled 1
  330. uci_commit wireless
  331. faulty_station="${sta_radio}/${sta_essid}/${sta_bssid:-"-"}"
  332. f_jsnup "${faulty_station}"
  333. f_log "info" "can't connect to uplink '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}', uplink disabled (${trm_sysver})"
  334. f_check "rev"
  335. break
  336. else
  337. uci -q revert wireless
  338. f_jsnup
  339. f_log "info" "can't connect to uplink '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' (${trm_sysver})"
  340. f_check "rev"
  341. break
  342. fi
  343. fi
  344. fi
  345. unset scan_quality scan_bssid scan_essid
  346. fi
  347. done
  348. unset IFS scan_quality scan_bssid scan_essid
  349. done
  350. fi
  351. cnt=$(( cnt + 1 ))
  352. sleep $(( ${trm_maxwait} / 6 ))
  353. done
  354. done
  355. fi
  356. }
  357. # source required system libraries
  358. #
  359. if [ -r "/lib/functions.sh" ] && [ -r "/usr/share/libubox/jshn.sh" ]
  360. then
  361. . "/lib/functions.sh"
  362. . "/usr/share/libubox/jshn.sh"
  363. else
  364. f_log "err" "system libraries not found"
  365. fi
  366. # initialize json runtime file
  367. #
  368. json_load_file "${trm_rtfile}" >/dev/null 2>&1
  369. json_select data >/dev/null 2>&1
  370. if [ ${?} -ne 0 ]
  371. then
  372. > "${trm_rtfile}"
  373. json_init
  374. json_add_object "data"
  375. fi
  376. # control travelmate actions
  377. #
  378. while true
  379. do
  380. if [ -z "${trm_action}" ]
  381. then
  382. sleep ${trm_timeout}
  383. elif [ "${trm_action}" = "stop" ]
  384. then
  385. > "${trm_rtfile}"
  386. f_log "info" "travelmate instance stopped ::: action: ${trm_action}, pid: $(cat ${trm_pidfile} 2>/dev/null)"
  387. exit 0
  388. else
  389. f_log "info" "travelmate instance started ::: action: ${trm_action}, pid: ${$}"
  390. unset trm_action
  391. fi
  392. f_envload
  393. f_main
  394. done